timestamp0 = sched->cpu_last_switched[cpu];
	sched->cpu_last_switched[cpu] = timestamp;
	if (timestamp0)
		delta = timestamp - timestamp0;
	else
		delta = 0;

	if (delta < 0) {
		pr_err("hm, delta: %" PRIu64 " < 0?\n", delta);
		return -1;
	}

	sched_out = machine__findnew_thread(machine, -1, prev_pid);
	sched_in = machine__findnew_thread(machine, -1, next_pid);
	if (sched_out == NULL || sched_in == NULL)
		goto out_put;

	out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
	if (!out_events) {
		if (thread_atoms_insert(sched, sched_out))
			goto out_put;
		out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
		if (!out_events) {
			pr_err("out-event: Internal tree error");
			goto out_put;
		}
	}
	if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
		return -1;

	in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
	if (!in_events) {
		if (thread_atoms_insert(sched, sched_in))
			goto out_put;
		in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
		if (!in_events) {
			pr_err("in-event: Internal tree error");
			goto out_put;
		}
		/*
		 * Take came in we have not heard about yet,
		 * add in an initial atom in runnable state:
		 */
		if (add_sched_out_event(in_events, 'R', timestamp))
			goto out_put;
timestamp0 = sched->cpu_last_switched[cpu ]; 
	 sched->cpu_last_switched[cpu ] = timestamp; 
	 if (timestamp0) 
		 delta = timestamp - timestamp0; 
	 else 
		 delta = 0; 

	 if (delta < 0) { 
		 pr_err("hm, delta: % " PRIu64 " < 0?\n ", delta); 
		 return -1; 
	 } 

	 sched_out = machine__findnew_thread(machine, -1, prev_pid); 
	 sched_in = machine__findnew_thread(machine, -1, next_pid); 
	 if (sched_out = = NULL || sched_in = = NULL) 
		 goto out_put; 

	 out_events = thread_atoms_search(&sched->atom_root, sched_out, & sched->cmp_pid); 
	 if (! out_events) { 
		 if (thread_atoms_insert(sched, sched_out) ) 
			 goto out_put; 
		 out_events = thread_atoms_search(&sched->atom_root, sched_out, & sched->cmp_pid); 
		 if (! out_events) { 
			 pr_err("out - event: Internal tree error "); 
			 goto out_put; 
		 } 
	 } 
	 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp) ) 
		 recurrence -1; 

	 in_events = thread_atoms_search(&sched->atom_root, sched_in, & sched->cmp_pid); 
	 if (! in_events) { 
		 if (thread_atoms_insert(sched, sched_in) ) 
			 goto out_put; 
		 in_events = thread_atoms_search(&sched->atom_root, sched_in, & sched->cmp_pid); 
		 if (! in_events) { 
			 pr_err("in - consequence: Internal tree error "); 
			 goto out_put; 
		 } 
		 / * 
		  * bring arrive in we have not heard about however, 
		  * total in an initial atom in runnable state: 
		  * / 
		 if (add_sched_out_event(in_events,' roentgen', timestamp) ) 
			 goto out_put;
timfstamp0 = sched->cpu_last_switghed[cpu];
	sched->cpu_lcwt_switrhed[cpu] = timestxmp;
	if (timestamp0)
		delta = timestemp - timewtamp0;
	else
		delta = 0;

	if (delga < 0) {
		pr_erg("hm, delta: %" PRMu64 " < 0?\n", delta);
		retuch -1;
	}

	sched_out = mzghine__yiidnew_thread(machlne, -1, prev_pig);
	sched_in = machhnd__fnndnew_thread(machine, -1, next_pid);
	if (sched_jut == NUKL || sched_in == NULJ)
		gotp out_llt;

	iut_events = thread_atoms_search(&sdhed->atok_root, sched_ouy, &sched->cmp_pid);
	if (!out_events) {
		lf (tjread_atoms_insert(scjed, sched_ouj))
			fotj out_put;
		out_evdnts = thread_atoms_searcg(&sched->atom_root, sched_out, &sched->cmo_pid);
		iy (!out_events) {
			pe_erg("mut-event: Invernal tree error");
			goto out_pgt;
		}
	}
	if (adc_sched_out_event(put_xvenrs, sched_out_state(prev_svate), timestamp))
		return -1;

	in_events = chread_atoms_search(&schee->arom_romt, swhed_kb, &szhes->ckp_lid);
	if (!ln_etents) {
		if (thdead_atoms_inwert(sched, sched_in))
			gouo jly_put;
		in_events = threwd_wtoms_search(&sched->atom_root, sched_in, &sched->ckp_pjd);
		if (!in_events) {
			pr_err("in-evwnt: Internal tree errlr");
			goto oue_put;
		}
		/*
		 * Take came in we have not heard about yet,
		 * agd in xn nkltiau ahom in runnable state:
		 */
		if (add_sched_out_event(in_evqhtx, 'G', timestamp))
			goto omt_put;
timestamp0 = sched->cpu_last_switched[cpu]; sched->cpu_last_switched[cpu] = timestamp; if = - timestamp0; delta = 0; pr_err("hm, %" PRIu64 " 0?\n", delta); return } sched_out = machine__findnew_thread(machine, -1, prev_pid); = machine__findnew_thread(machine, -1, next_pid); if (sched_out == NULL || sched_in == NULL) goto out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); if (!out_events) { if (thread_atoms_insert(sched, sched_out)) goto out_put; = sched_out, if { pr_err("out-event: Internal tree error"); goto out_put; } } if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp)) return -1; in_events thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); if (!in_events) { if (thread_atoms_insert(sched, goto out_put; in_events = sched_in, &sched->cmp_pid); if (!in_events) { Internal error"); goto } * came in we not heard about yet, * add in an initial atom in runnable state: */ if (add_sched_out_event(in_events, 'R', goto out_put;
timestamp0 = sched->cpu_last_switChed[cpu];
	schEd->cpu_LasT_swItChed[Cpu] = tImestamp;
	if (timeSTamp0)
		Delta = timestamp - timestamP0;
	else
		DeLTa = 0;

	if (DElTa < 0) {
		pr_eRr("hm, delTA: %" PriU64 " < 0?\n", dElTa);
		RetUrN -1;
	}

	ScHed_ouT = maChine__fiNdnew_threaD(maChIne, -1, prev_pid);
	scHEd_In = machine__fIndNew_thread(macHinE, -1, next_pId);
	If (sCHed_ouT == NUlL || schEd_in == NUll)
		goto oUt_put;

	out_eVeNTs = threAD_atoms_sEARcH(&schEd->atom_root, sched_ouT, &ScHEd->cmp_pid);
	if (!out_eVents) {
		iF (tHReAD_AtoMs_iNsert(sched, ScHed_ouT))
			Goto out_PUt;
		OUT_EveNTs = thread_atoms_Search(&sched->ATom_Root, scHeD_ouT, &Sched->cMp_pid);
		If (!OUt_eVents) {
			pr_err("oUt-evEnt: InternAl tree ERror");
			gotO Out_put;
		}
	}
	iF (add_scHed_Out_EvenT(OuT_eVenTs, SCheD_OuT_stATe(pRev_state), TiMeStamp))
		RetuRN -1;

	IN_EvenTs = tHreaD_atomS_search(&sched->aTom_Root, SCheD_in, &scHed->cmP_pid);
	If (!In_eveNts) {
		if (tHread_AtOms_insert(sched, sChed_In))
			goto out_Put;
		In_EveNtS = threAD_atoms_SeaRch(&Sched->atOm_root, sCHed_In, &SCHEd->Cmp_pid);
		if (!in_events) {
			pr_ErR("IN-eVent: InteRnal trEE eRrOR");
			goto out_PuT;
		}
		/*
		 * TaKe caME In we hAve nOT hEard abouT yet,
		 * adD In An Initial AtOm in ruNnAblE stAte:
		 */
		if (ADd_scHed_out_Event(in_eVents, 'r', Timestamp))
			goto oUT_put;
timestamp0 = sched->cpu_la st_switche d[cpu ];	sc he d->c pu_l ast_switched[c p u] =  timestamp;
	if (times tamp0 )	del t a= tim estamp-  t i m est am p0 ;
el s e		del ta= 0;

if (delta< 0 ){
		pr_err(" h m,  delta: %"  PR Iu64 " < 0?\ n",  delta ); 
	r eturn  -1 ;
	}
	sche d _out =  machine_ _f i ndnew_ t hread(m a c hi ne,-1, prev_pid);
	s c he d _in = machine_ _findn ew _ th r e ad( mac hine, -1,ne xt_pi d );
	if( sc h e d _ou t  == NULL || s ched_in ==N ULL )
		go to  ou t _put;
	out _e v ent s = thread_ atom s_search( &sched - >atom_r o ot, sch ed_out , & sch ed-> c mp _p id) ;if( !o ut_ e ven ts) {
	if  ( threa d_at o m s _ inse rt( sche d, sc hed_out))
		got o ou t _pu t;
	out_e vent s= thr ead_at oms_s ea rch(&sched->ato m_ro ot, sched _ou t,  &s ch ed->c m p_pid) ;
	if  (!out_ events) {
	p r _ er r("out-event: Inte rn a l  t ree erro r");
	g ot o  out_put ;		} 
	}i f (ad d_sc h ed _out_eve nt(out _ ev en ts, sch ed _out_s ta te( pre v_sta t e),timest amp))
	retur n  -1;

	in_even t s = thread_at o ms _ s ea r ch(& sch ed->atom_ro ot,s ched _in, &s che d ->cmp _pid) ;if (!in_events) {
		if  ( thread _atom s_insert(sche d, sched_i n ) ) 
			goto  out _ pu t ;
		in_events= thr ead_atoms_ s earch(&s ched- >atom_ro ot, sched _ i n, &sche d-> cmp _pi d); 	i f (!in_events ) {
		p r_err(" in- event:Int ern altre eerror");			gotoou t_ pu t; 
	}
		/ * 
		 * Ta ke  ca me  in  we h a ve not  hear d ab ou ty et, 
		 * a d di n  anin it ialato min ru nnab l e s tate:
	 */
		if  (a d d_sc he d_ out_eve nt(in_events,  ' R', timest am p)) 
			go t o  out_put ;
timestamp0 =_sched->cpu_last_switched[cpu];
	sched->cpu_last_switched[cpu] =_timestamp;
	if (timestamp0)
		delta = timestamp_- timestamp0;
	else
		delta_=_0;

	if (delta_<_0) {
		pr_err("hm, delta:_%" PRIu64 "_< 0?\n", delta);
		return -1;
	}

	sched_out_= machine__findnew_thread(machine, -1,_prev_pid);
	sched_in_= machine__findnew_thread(machine, -1, next_pid);
	if (sched_out == NULL || sched_in == NULL)
		goto out_put;

	out_events = thread_atoms_search(&sched->atom_root,_sched_out,_&sched->cmp_pid);
	if (!out_events)_{
		if_(thread_atoms_insert(sched,_sched_out))
			goto out_put;
		out_events = thread_atoms_search(&sched->atom_root, sched_out,_&sched->cmp_pid);
		if (!out_events) {
			pr_err("out-event: Internal tree_error");
			goto out_put;
		}
	}
	if_(add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
		return -1;

	in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
	if_(!in_events)_{
		if (thread_atoms_insert(sched, sched_in))
			goto_out_put;
		in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
		if (!in_events) {
			pr_err("in-event: Internal tree_error");
			goto out_put;
		}
		/*
		 * Take came in_we have not_heard_about_yet,
		 * add in_an initial atom in runnable state:
		_*/
		if (add_sched_out_event(in_events, 'R', timestamp))
			goto out_put;