Module ConcurCC_correct:= (Concurrent_correctness CC_correct Args).
(*USed to be start_stack*)
Definition Clight_init_state (p: Clight.program):=
Clight.initial_state p.
Definition Asm_init_state (p: Asm.program):=
Asm.initial_state  p.
Notation valid Sem:=
(valid dryResources Sem OrdinalPool.OrdinalThreadPool).
Definition opt_init_mem_source (p : Clight.program):=
(Genv.init_mem (Ctypes.program_of_program p)).
Definition opt_init_mem_target {F V} (tp:AST.program F V ):=
(Genv.init_mem tp).
unfold concurrent_simulation_safety_preservation; intros.
(* destruct H0. simpl in H2. unfold init_mach in *. *)
pose proof (ConcurrentCompilerCorrectness Args.Asm_program H asm_genv_safety) as SIM.
unfold ConcurrentCompilerCorrectness_specification in SIM.
(*Construct the initial state*)
exploit HybridMachine_simulation.initial_setup.
eapply SIM.
match goal with
[H: machine_semantics.initial_machine ?SEM1 _ _ _ _ _ _ |-
machine_semantics.initial_machine ?SEM2 _ _ _ _ _ _] =>
replace SEM2 with SEM1 by (rewrite H0; reflexivity)
end.
eapply H1.
intro HH; destruct HH as (j&cd&t_mach_state&t_mem&t_mem'&r2&(INIT_mem & INIT)&?).
assert(INIT':= INIT).
destruct r2; try solve[inversion INIT'].
destruct INIT' as (c&?&?).
subst t_mach_state; simpl in *.
do 3 eexists; repeat split; eauto.
eapply INIT.
destruct H4 as (H21 & H22); subst.
clear INIT H21.
(* Now, we strip out the scheudle, until it starts with 1*)
eapply initial_csafe_all_schedule.
intros; eapply csafety_step; eauto.
eapply H3.
Qed.
Definition SemSource:= (ClightSemanticsForMachines.ClightSem
(Clight.globalenv Args.C_program)).
Definition SemTarget asm_genv_safety:= @X86Sem Args.Asm_program asm_genv_safety.
Definition SourceMachineSig:=
(@DryHybridMachineSig SemSource (@OrdinalPool.OrdinalThreadPool dryResources SemSource)).
Definition TargetMachineSig ags:=
(@DryHybridMachineSig (SemTarget ags) (@OrdinalPool.OrdinalThreadPool dryResources (SemTarget ags))).
Definition SourceThreadPool:= threadPool.OrdinalPool.OrdinalThreadPool(Sem:=SemSource).
Definition TargetThreadPool ags:= threadPool.OrdinalPool.OrdinalThreadPool(Sem:=(SemTarget ags)).
Notation resources:= HybridMachine.DryHybridMachine.dryResources.
Definition SourceHybridMachine:=
@HybridCoarseMachine.HybridCoarseMachine resources SemSource SourceThreadPool SourceMachineSig.
Definition TargetHybridMachine ags:=
@HybridCoarseMachine.HybridCoarseMachine resources (SemTarget ags) (TargetThreadPool ags) (TargetMachineSig ags).
Definition SourceSemantics:= (fun m => ConcurMachineSemantics(HybridMachine:=SourceHybridMachine) m).
Definition TargetSemantics ags:= (fun m => ConcurMachineSemantics(HybridMachine:=TargetHybridMachine ags) m).

Lemma clean_theorem_equivalence:
forall asm_genv_safety : Asm_core.safe_genv (@the_ge Args.Asm_program),
let SemSource:= (ClightSemanticsForMachines.ClightSem
(Clight.globalenv Args.C_program)) in
let SemTarget:= @X86Sem Args.Asm_program asm_genv_safety in
concurrent_simulation_safety_preservation
(Genv.init_mem (Ctypes.program_of_program Args.C_program))
(Genv.init_mem Args.Asm_program)
(SemSource:= SemSource)
(SourceThreadPool:= threadPool.OrdinalPool.OrdinalThreadPool(Sem:=SemSource))
(SourceMachineSig:= HybridMachine.DryHybridMachine.DryHybridMachineSig)
(SemTarget :=  SemTarget)
(TargetThreadPool:= threadPool.OrdinalPool.OrdinalThreadPool(Sem:=SemTarget))
(TargetMachineSig:= HybridMachine.DryHybridMachine.DryHybridMachineSig) ->
main_safety_preservation SourceSemantics (TargetSemantics asm_genv_safety).