(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Correctness proof for RTL generation. *)

Require Import Wellfounded Coqlib Maps AST Linking.
Require Import Integers Values Memory Events Smallstep Globalenvs.
Require Import Switch Registers Cminor Op CminorSel RTL.
Require Import RTLgen RTLgenspec.

(** * Correspondence between Cminor environments and RTL register sets *)

(** A compilation environment (mapping) is well-formed if
  the following properties hold:
- Two distinct Cminor local variables are mapped to distinct pseudo-registers.
- A Cminor local variable and a let-bound variable are mapped to
  distinct pseudo-registers.
*)

Record map_wf (m: mapping) : Prop :=
  mk_map_wf {
    map_wf_inj:
      (forall id1 id2 r,
         m.(map_vars)!id1 = Some r -> m.(map_vars)!id2 = Some r -> id1 = id2);
     map_wf_disj:
      (forall id r,
         m.(map_vars)!id = Some r -> In r m.(map_letvars) -> False)
  }.

Lemma init_mapping_wf:
  map_wf init_mapping.
Proof.
(* Goal: map_wf init_mapping *)
  unfold init_mapping; split; simpl.
(* Goal: forall (id1 id2 : positive) (r : reg) (_ : @eq (option reg) (@PTree.get reg id1 (PTree.empty reg)) (@Some reg r)) (_ : @eq (option reg) (@PTree.get reg id2 (PTree.empty reg)) (@Some reg r)), @eq positive id1 id2 *)
(* Goal: forall (id : positive) (r : reg) (_ : @eq (option reg) (@PTree.get reg id (PTree.empty reg)) (@Some reg r)) (_ : False), False *)
  intros until r. rewrite PTree.gempty. congruence.
(* Goal: forall (id : positive) (r : reg) (_ : @eq (option reg) (@PTree.get reg id (PTree.empty reg)) (@Some reg r)) (_ : False), False *)
  tauto.
Qed.

Lemma add_var_wf:
  forall s1 s2 map name r map' i,
  add_var map name s1 = OK (r,map') s2 i ->
  map_wf map -> map_valid map s1 -> map_wf map'.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. monadInv H.
(* Goal: map_wf (mkmapping (@PTree.set reg name r (map_vars map)) (map_letvars map)) *)
  apply mk_map_wf; simpl.
(* Goal: forall (id : positive) (r0 : reg) (_ : @eq (option reg) (@PTree.get reg id (@PTree.set reg name r (map_vars map))) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  intros until r0. repeat rewrite PTree.gsspec.
(* Goal: forall (_ : @eq (option reg) (if peq id1 name then @Some reg r else @PTree.get reg id1 (map_vars map)) (@Some reg r0)) (_ : @eq (option reg) (if peq id2 name then @Some reg r else @PTree.get reg id2 (map_vars map)) (@Some reg r0)), @eq positive id1 id2 *)
(* Goal: forall (id : positive) (r0 : reg) (_ : @eq (option reg) (@PTree.get reg id (@PTree.set reg name r (map_vars map))) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  destruct (peq id1 name); destruct (peq id2 name).
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  congruence.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H. elimtype False.
(* Goal: False *)
(* Goal: forall (_ : @eq (option reg) (@PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  apply valid_fresh_absurd with r0 s1.
(* Goal: reg_valid r0 s1 *)
(* Goal: reg_fresh r0 s1 *)
(* Goal: forall (_ : @eq (option reg) (@PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  apply H1. left; exists id2; auto.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  eauto with rtlg.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H2. elimtype False.
(* Goal: False *)
(* Goal: forall (_ : @eq (option reg) (@PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  apply valid_fresh_absurd with r0 s1.
(* Goal: reg_valid r0 s1 *)
(* Goal: reg_fresh r0 s1 *)
(* Goal: forall (_ : @eq (option reg) (@PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  apply H1. left; exists id1; auto.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  eauto with rtlg.
(* Goal: final_state R r *)
  inv H0. eauto.
(* Goal: forall (id : positive) (r0 : reg) (_ : @eq (option reg) (@PTree.get reg id (@PTree.set reg name r (map_vars map))) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  intros until r0. rewrite PTree.gsspec.
(* Goal: forall (_ : @eq (option reg) (if peq id name then @Some reg r else @PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  destruct (peq id name).
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H.
(* Goal: False *)
(* Goal: forall (_ : @eq (option reg) (@PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  apply valid_fresh_absurd with r0 s1.
(* Goal: reg_valid r0 s1 *)
(* Goal: reg_fresh r0 s1 *)
(* Goal: forall (_ : @eq (option reg) (@PTree.get reg id (map_vars map)) (@Some reg r0)) (_ : @In reg r0 (map_letvars map)), False *)
  apply H1. right; auto.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  eauto with rtlg.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  inv H0; eauto.
Qed.

Lemma add_vars_wf:
  forall names s1 s2 map map' rl i,
  add_vars map names s1 = OK (rl,map') s2 i ->
  map_wf map -> map_valid map s1 -> map_wf map'.
Proof.
(* Goal: and (match_env map2 (set_params vl (@cons ident a il)) (@nil val) (init_regs tvl rl)) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (init_regs tvl rl)) Vundef) *)
  induction names; simpl; intros; monadInv H.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit add_vars_valid; eauto. intros [A B].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply add_var_wf; eauto.
Qed.

Lemma add_letvar_wf:
  forall map r,
  map_wf map -> ~reg_in_map map r -> map_wf (add_letvar map r).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H. unfold add_letvar; constructor; simpl.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. elim H1; intro. subst r0. elim H0. left; exists id; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eauto.
Qed.

(** An RTL register environment matches a CminorSel local environment and
  let-environment if the value of every local or let-bound variable in
  the CminorSel environments is identical to the value of the
  corresponding pseudo-register in the RTL register environment. *)

Record match_env
      (map: mapping) (e: env) (le: letenv) (rs: regset) : Prop :=
  mk_match_env {
    me_vars:
      (forall id v,
         e!id = Some v -> exists r, map.(map_vars)!id = Some r /\ Val.lessdef v rs#r);
    me_letvars:
      Val.lessdef_list le rs##(map.(map_letvars))
  }.

Lemma match_env_find_var:
  forall map e le rs id v r,
  match_env map e le rs ->
  e!id = Some v ->
  map.(map_vars)!id = Some r ->
  Val.lessdef v rs#r.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. exploit me_vars; eauto. intros [r' [EQ' RS]].
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  replace r with r'. auto. congruence.
Qed.

Lemma match_env_find_letvar:
  forall map e le rs idx v r,
  match_env map e le rs ->
  List.nth_error le idx = Some v ->
  List.nth_error map.(map_letvars) idx = Some r ->
  Val.lessdef v rs#r.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. exploit me_letvars; eauto.
(* Goal: forall _ : Val.lessdef_list le (@List.map positive val (fun r : positive => Regmap.get val r rs) (map_letvars map)), Val.lessdef v (Regmap.get val r rs) *)
  clear H. revert le H0 H1. generalize (map_letvars map). clear map.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  induction idx; simpl; intros.
(* Goal: Val.lessdef v (Regmap.get val r rs) *)
(* Goal: Val.lessdef v (Regmap.get val r rs) *)
  inversion H; subst le; inversion H0. subst v1.
(* Goal: Val.lessdef v (Regmap.get val r rs) *)
(* Goal: Val.lessdef v (Regmap.get val r rs) *)
  destruct l; inversion H1. subst r0.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  inversion H2. subst v2. auto.
(* Goal: Val.lessdef v (Regmap.get val r rs) *)
  destruct l; destruct le; try discriminate.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply IHidx; eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  inversion H. auto.
Qed.

Lemma match_env_invariant:
  forall map e le rs rs',
  match_env map e le rs ->
  (forall r, (reg_in_map map r) -> rs'#r = rs#r) ->
  match_env map e le rs'.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inversion H. apply mk_match_env.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. exploit me_vars0; eauto. intros [r [A B]].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists r; split. auto. rewrite H0; auto. left; exists id; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  replace (rs'##(map_letvars map)) with (rs ## (map_letvars map)). auto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  apply list_map_exten. intros. apply H0. right; auto.
Qed.

(** Matching between environments is preserved when an unmapped register
  (not corresponding to any Cminor variable) is assigned in the RTL
  execution. *)

Lemma match_env_update_temp:
  forall map e le rs r v,
  match_env map e le rs ->
  ~(reg_in_map map r) ->
  match_env map e le (rs#r <- v).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. apply match_env_invariant with rs; auto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. case (Reg.eq r r0); intro.
(* Goal: @eq val (Regmap.get val r0 (Regmap.set val r v rs)) (Regmap.get val r0 rs) *)
(* Goal: @eq val (Regmap.get val r0 (Regmap.set val r v rs)) (Regmap.get val r0 rs) *)
  subst r0; contradiction.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply Regmap.gso; auto.
Qed.
Hint Resolve match_env_update_temp: rtlg.

(** Matching between environments is preserved by simultaneous
  assignment to a Cminor local variable (in the Cminor environments)
  and to the corresponding RTL pseudo-register (in the RTL register
  environment). *)

Lemma match_env_update_var:
  forall map e le rs id r v tv,
  Val.lessdef v tv ->
  map_wf map ->
  map.(map_vars)!id = Some r ->
  match_env map e le rs ->
  match_env map (PTree.set id v e) le (rs#r <- tv).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inversion H0. inversion H2. apply mk_match_env.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros id' v'. rewrite PTree.gsspec. destruct (peq id' id); intros.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  subst id'. inv H3. exists r; split. auto. rewrite PMap.gss. auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit me_vars0; eauto. intros [r' [A B]].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists r'; split. auto. rewrite PMap.gso; auto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  red; intros. subst r'. elim n. eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  erewrite list_map_exten. eauto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. symmetry. apply PMap.gso. red; intros. subst x. eauto.
Qed.

(** A variant of [match_env_update_var] where a variable is optionally
  assigned to, depending on the [dst] parameter. *)

Lemma match_env_update_dest:
  forall map e le rs dst r v tv,
  Val.lessdef v tv ->
  map_wf map ->
  reg_map_ok map r dst ->
  match_env map e le rs ->
  match_env map (set_optvar dst v e) le (rs#r <- tv).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H1; simpl.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply match_env_update_temp; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply match_env_update_var; eauto.
Qed.
Hint Resolve match_env_update_dest: rtlg.

(** A variant of [match_env_update_var] corresponding to the assignment
  of the result of a builtin. *)

Lemma match_env_update_res:
  forall map res v e le tres tv rs,
  Val.lessdef v tv ->
  map_wf map ->
  tr_builtin_res map res tres ->
  match_env map e le rs ->
  match_env map (set_builtin_res res v e) le (regmap_setres tres tv rs).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H1; simpl.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
- eapply match_env_update_var; eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
- auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
- eapply match_env_update_temp; eauto.
Qed.

(** Matching and [let]-bound variables. *)

Lemma match_env_bind_letvar:
  forall map e le rs r v,
  match_env map e le rs ->
  Val.lessdef v rs#r ->
  match_env (add_letvar map r) e (v :: le) rs.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H. unfold add_letvar. apply mk_match_env; simpl; auto.
Qed.

Lemma match_env_unbind_letvar:
  forall map e le rs r v,
  match_env (add_letvar map r) e (v :: le) rs ->
  match_env map e le rs.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  unfold add_letvar; intros. inv H. simpl in *.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor. auto. inversion me_letvars0. auto.
Qed.

(** Matching between initial environments. *)

Lemma match_env_empty:
  forall map,
  map.(map_letvars) = nil ->
  match_env map (PTree.empty val) nil (Regmap.init Vundef).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. apply mk_match_env.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. rewrite PTree.gempty in H0. discriminate.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) tm) r *)
  rewrite H. constructor.
Qed.

(** The assignment of function arguments to local variables (on the Cminor
  side) and pseudo-registers (on the RTL side) preserves matching
  between environments. *)

Lemma match_set_params_init_regs:
  forall il rl s1 map2 s2 vl tvl i,
  add_vars init_mapping il s1 = OK (rl, map2) s2 i ->
  Val.lessdef_list vl tvl ->
  match_env map2 (set_params vl il) nil (init_regs tvl rl)
  /\ (forall r, reg_fresh r s2 -> (init_regs tvl rl)#r = Vundef).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  induction il; intros.

(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  inv H. split. apply match_env_empty. auto. intros.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  simpl. apply Regmap.gi.

(* Goal: and (match_env map2 (set_params vl (@cons ident a il)) (@nil val) (init_regs tvl rl)) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (init_regs tvl rl)) Vundef) *)
  monadInv H. simpl.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit add_vars_valid; eauto. apply init_mapping_valid. intros [A B].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit add_var_valid; eauto. intros [A' B']. clear B'.
(* Goal: match_env map2 (@PTree.set val a Vundef (set_locals il e)) le rs *)
  monadInv EQ1.
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) match vl with | nil => @PTree.set val a Vundef (set_params (@nil val) il) | cons v1 vs => @PTree.set val a v1 (set_params vs il) end (@nil val) match tvl with | nil => Regmap.init val Vundef | cons v1 vs => Regmap.set val x1 v1 (init_regs vs x) end) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r match tvl with | nil => Regmap.init val Vundef | cons v1 vs => Regmap.set val x1 v1 (init_regs vs x) end) Vundef) *)
  destruct H0 as [ | v1 tv1 vs tvs].
  (* vl = nil *)
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) tm) r *)
  destruct (IHil _ _ _ _ nil nil _ EQ) as [ME UNDEF]. constructor. inv ME. split.
(* Goal: match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a Vundef (set_params (@nil val) il)) (@nil val) (Regmap.init val Vundef) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  replace (init_regs nil x) with (Regmap.init Vundef) in me_vars0, me_letvars0.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  constructor; simpl.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros id v. repeat rewrite PTree.gsspec. destruct (peq id a); intros.
(* Goal: final_state R r *)
  subst a. inv H. exists x1; split. auto. constructor.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eauto.
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  destruct x; reflexivity.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. apply Regmap.gi.
  (* vl = v1 :: vs *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  destruct (IHil _ _ _ _ _ _ _ EQ H0) as [ME UNDEF]. inv ME. split.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  constructor; simpl.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros id v. repeat rewrite PTree.gsspec. destruct (peq id a); intros.
(* Goal: final_state R r *)
  subst a. inv H. inv H1. exists x1; split. auto. rewrite Regmap.gss. constructor.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  inv H1. eexists; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit me_vars0; eauto. intros [r' [C D]].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists r'; split. auto. rewrite Regmap.gso. auto.
(* Goal: not (@eq positive r' x1) *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) (map_letvars x0)) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef *)
  apply valid_fresh_different with s.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply B. left; exists id; auto.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  eauto with rtlg.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  destruct (map_letvars x0). auto. simpl in me_letvars0. inversion me_letvars0.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. rewrite Regmap.gso. apply UNDEF.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  apply reg_fresh_decr with s2; eauto with rtlg.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply not_eq_sym. apply valid_fresh_different with s2; auto.
Qed.

Lemma match_set_locals:
  forall map1 s1,
  map_wf map1 ->
  forall il rl map2 s2 e le rs i,
  match_env map1 e le rs ->
  (forall r, reg_fresh r s1 -> rs#r = Vundef) ->
  add_vars map1 il s1 = OK (rl, map2) s2 i ->
  match_env map2 (set_locals il e) le rs.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  induction il; simpl in *; intros.

(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kseq (Sloop s) k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sloop s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kblock k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sblock s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (if ident_eq lbl l then @Some (prod stmt cont) (@pair stmt cont s k) else find_label lbl s k) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Slabel l s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  inv H2. auto.

(* Goal: match_env map2 (@PTree.set val a Vundef (set_locals il e)) le rs *)
  monadInv H2.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHil; eauto. intro.
(* Goal: match_env map2 (@PTree.set val a Vundef (set_locals il e)) le rs *)
  monadInv EQ1.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) tm) r *)
  constructor.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (@PTree.set val a Vundef (set_locals il e))) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (@PTree.set reg a x1 (map_vars x0))) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  intros id v. simpl. repeat rewrite PTree.gsspec.
(* Goal: forall _ : @eq (option val) (@Some val Vundef) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@Some reg x1) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  destruct (peq id a). subst a. intro.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists x1. split. auto. inv H3. constructor.
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  eauto with rtlg.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. eapply me_vars; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  simpl. eapply me_letvars; eauto.
Qed.

Lemma match_init_env_init_reg:
  forall params s0 rparams map1 s1 i1 vars rvars map2 s2 i2 vparams tvparams,
  add_vars init_mapping params s0 = OK (rparams, map1) s1 i1 ->
  add_vars map1 vars s1 = OK (rvars, map2) s2 i2 ->
  Val.lessdef_list vparams tvparams ->
  match_env map2 (set_locals vars (set_params vparams params))
            nil (init_regs tvparams rparams).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit match_set_params_init_regs; eauto. intros [A B].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply match_set_locals; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply add_vars_wf; eauto. apply init_mapping_wf.
(* Goal: map_valid init_mapping s0 *)
  apply init_mapping_valid.
Qed.

(** * The simulation argument *)

Require Import Errors.

Definition match_prog (p: CminorSel.program) (tp: RTL.program) :=
  match_program (fun cu f tf => transl_fundef f = Errors.OK tf) eq p tp.

Lemma transf_program_match:
  forall p tp, transl_program p = OK tp -> match_prog p tp.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. apply match_transform_partial_program; auto.
Qed.

Section CORRECTNESS.

Variable prog: CminorSel.program.
Variable tprog: RTL.program.
Hypothesis TRANSL: match_prog prog tprog.

Let ge : CminorSel.genv := Genv.globalenv prog.
Let tge : RTL.genv := Genv.globalenv tprog.

(** Relationship between the global environments for the original
  CminorSel program and the generated RTL program. *)

Lemma symbols_preserved:
  forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
Proof
  (Genv.find_symbol_transf_partial TRANSL).

Lemma function_ptr_translated:
  forall (b: block) (f: CminorSel.fundef),
  Genv.find_funct_ptr ge b = Some f ->
  exists tf,
  Genv.find_funct_ptr tge b = Some tf /\ transl_fundef f = OK tf.
Proof
  (Genv.find_funct_ptr_transf_partial TRANSL).

Lemma functions_translated:
  forall (v: val) (f: CminorSel.fundef),
  Genv.find_funct ge v = Some f ->
  exists tf,
  Genv.find_funct tge v = Some tf /\ transl_fundef f = OK tf.
Proof
  (Genv.find_funct_transf_partial TRANSL).

Lemma sig_transl_function:
  forall (f: CminorSel.fundef) (tf: RTL.fundef),
  transl_fundef f = OK tf ->
  RTL.funsig tf = CminorSel.funsig f.
Proof.
(* Goal: forall (f : CminorSel.fundef) (tf : fundef) (_ : @eq (res (AST.fundef function)) (transl_fundef f) (@OK fundef tf)), @eq signature (funsig tf) (CminorSel.funsig f) *)
  intros until tf. unfold transl_fundef, transf_partial_fundef.
(* Goal: forall _ : @eq (res (AST.fundef function)) (@OK (AST.fundef function) (@External function e)) (@OK fundef tf), @eq signature (funsig tf) (CminorSel.funsig (@External CminorSel.function e)) *)
  case f; intro.
(* Goal: forall _ : @eq (res (AST.fundef function)) (@bind function (AST.fundef function) (transl_function f0) (fun f' : function => @OK (AST.fundef function) (@Internal function f'))) (@OK fundef tf), @eq signature (funsig tf) (CminorSel.funsig (@Internal CminorSel.function f0)) *)
(* Goal: forall _ : @eq (res (AST.fundef function)) (@OK (AST.fundef function) (@External function e)) (@OK fundef tf), @eq signature (funsig tf) (CminorSel.funsig (@External CminorSel.function e)) *)
  unfold transl_function.
(* Goal: forall _ : @eq (res (AST.fundef function)) (@bind function (AST.fundef function) (let (ngoto, s0) := reserve_labels (fn_body f0) (@pair (PTree.t node) state (PTree.empty node) init_state) in match transl_fun f0 ngoto s0 with | RTLgen.Error msg => @Error function msg | RTLgen.OK (pair nentry rparams as p) s i => @OK function (mkfunction (CminorSel.fn_sig f0) rparams (fn_stackspace f0) (st_code s) nentry) end) (fun f' : function => @OK (AST.fundef function) (@Internal function f'))) (@OK fundef tf), @eq signature (funsig tf) (CminorSel.funsig (@Internal CminorSel.function f0)) *)
(* Goal: forall _ : @eq (res (AST.fundef function)) (@OK (AST.fundef function) (@External function e)) (@OK fundef tf), @eq signature (funsig tf) (CminorSel.funsig (@External CminorSel.function e)) *)
  destruct (reserve_labels (fn_body f0) (PTree.empty node, init_state)) as [ngoto s0].
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  case (transl_fun f0 ngoto s0); simpl; intros.
(* Goal: @eq signature (funsig tf) (CminorSel.fn_sig f0) *)
(* Goal: @eq signature (funsig tf) (CminorSel.fn_sig f0) *)
(* Goal: forall _ : @eq (res (AST.fundef function)) (@OK (AST.fundef function) (@External function e)) (@OK fundef tf), @eq signature (funsig tf) (CminorSel.funsig (@External CminorSel.function e)) *)
  discriminate.
(* Goal: @eq signature (funsig tf) (CminorSel.funsig (@External CminorSel.function e)) *)
  destruct p. simpl in H. inversion H. reflexivity.
(* Goal: @eq signature (funsig tf) (CminorSel.funsig (@External CminorSel.function e)) *)
  intro. inversion H. reflexivity.
Qed.

Lemma senv_preserved:
  Senv.equiv (Genv.to_senv ge) (Genv.to_senv tge).
Proof
  (Genv.senv_transf_partial TRANSL).

(** Correctness of the code generated by [add_move]. *)

Lemma tr_move_correct:
  forall r1 ns r2 nd cs f sp rs m,
  tr_move f.(fn_code) ns r1 nd r2 ->
  exists rs',
  star step tge (State cs f sp ns rs m) E0 (State cs f sp nd rs' m) /\
  rs'#r2 = rs#r1 /\
  (forall r, r <> r2 -> rs'#r = rs#r).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists rs; split. constructor. auto.
  exists (rs#r2 <- (rs#r1)); split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  apply star_one. eapply exec_Iop. eauto. auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. apply Regmap.gss. intros; apply Regmap.gso; auto.
Qed.

(** ** Semantic preservation for the translation of expressions *)

Section CORRECTNESS_EXPR.

Variable sp: val.
Variable e: env.
Variable m: mem.

(** The proof of semantic preservation for the translation of expressions
  is a simulation argument based on diagrams of the following form:
<<
                    I /\ P
    e, le, m, a ------------- State cs code sp ns rs tm
         ||                      |
         ||                      |*
         ||                      |
         \/                      v
    e, le, m, v ------------ State cs code sp nd rs' tm'
                    I /\ Q
>>
  where [tr_expr code map pr a ns nd rd] is assumed to hold.
  The left vertical arrow represents an evaluation of the expression [a].
  The right vertical arrow represents the execution of zero, one or
  several instructions in the generated RTL flow graph [code].

  The invariant [I] is the agreement between Cminor environments and
  RTL register environment, as captured by [match_envs].

  The precondition [P] includes the well-formedness of the compilation
  environment [mut].

  The postconditions [Q] state that in the final register environment
  [rs'], register [rd] contains value [v], and the registers in
  the set of preserved registers [pr] are unchanged, as are the registers
  in the codomain of [map].

  We formalize this simulation property by the following predicate
  parameterized by the CminorSel evaluation (left arrow).  *)

Definition transl_expr_prop
     (le: letenv) (a: expr) (v: val) : Prop :=
  forall tm cs f map pr ns nd rd rs dst
    (MWF: map_wf map)
    (TE: tr_expr f.(fn_code) map pr a ns nd rd dst)
    (ME: match_env map e le rs)
    (EXT: Mem.extends m tm),
  exists rs', exists tm',
     star step tge (State cs f sp ns rs tm) E0 (State cs f sp nd rs' tm')
  /\ match_env map (set_optvar dst v e) le rs'
  /\ Val.lessdef v rs'#rd
  /\ (forall r, In r pr -> rs'#r = rs#r)
  /\ Mem.extends m tm'.

Definition transl_exprlist_prop
     (le: letenv) (al: exprlist) (vl: list val) : Prop :=
  forall tm cs f map pr ns nd rl rs
    (MWF: map_wf map)
    (TE: tr_exprlist f.(fn_code) map pr al ns nd rl)
    (ME: match_env map e le rs)
    (EXT: Mem.extends m tm),
  exists rs', exists tm',
     star step tge (State cs f sp ns rs tm) E0 (State cs f sp nd rs' tm')
  /\ match_env map e le rs'
  /\ Val.lessdef_list vl rs'##rl
  /\ (forall r, In r pr -> rs'#r = rs#r)
  /\ Mem.extends m tm'.

Definition transl_condexpr_prop
     (le: letenv) (a: condexpr) (v: bool) : Prop :=
  forall tm cs f map pr ns ntrue nfalse rs
    (MWF: map_wf map)
    (TE: tr_condition f.(fn_code) map pr a ns ntrue nfalse)
    (ME: match_env map e le rs)
    (EXT: Mem.extends m tm),
  exists rs', exists tm',
     plus step tge (State cs f sp ns rs tm) E0 (State cs f sp (if v then ntrue else nfalse) rs' tm')
  /\ match_env map e le rs'
  /\ (forall r, In r pr -> rs'#r = rs#r)
  /\ Mem.extends m tm'.

(** The correctness of the translation is a huge induction over
  the CminorSel evaluation derivation for the source program.  To keep
  the proof manageable, we put each case of the proof in a separate
  lemma.  There is one lemma for each CminorSel evaluation rule.
  It takes as hypotheses the premises of the CminorSel evaluation rule,
  plus the induction hypotheses, that is, the [transl_expr_prop], etc,
  corresponding to the evaluations of sub-expressions or sub-statements. *)

Lemma transl_expr_Evar_correct:
  forall (le : letenv) (id : positive) (v: val),
  e ! id = Some v ->
  transl_expr_prop le (Evar id) v.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit match_env_find_var; eauto. intro EQ.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit tr_move_correct; eauto. intros [rs' [A [B C]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exists rs'; exists tm; split. eauto.
  destruct H2 as [[D E] | [D E]].
  (* optimized case *)
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  subst r dst. simpl.
  assert (forall r, rs'#r = rs#r).
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
    intros. destruct (Reg.eq r rd). subst r. auto. auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply match_env_invariant; eauto.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. congruence.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split; auto.
  (* general case *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split.
  apply match_env_invariant with (rs#rd <- (rs#r)).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply match_env_update_dest; auto.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  intros. rewrite Regmap.gsspec. destruct (peq r0 rd). congruence. auto.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. congruence.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. intros. apply C. intuition congruence.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_expr_Eop_correct:
  forall (le : letenv) (op : operation) (args : exprlist)
         (vargs : list val) (v : val),
  eval_exprlist ge sp e m le args vargs ->
  transl_exprlist_prop le args vargs ->
  eval_operation ge sp op vargs m = Some v ->
  transl_expr_prop le (Eop op args) v.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* normal case *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RR1 [RO1 EXT1]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct eval_operation_lessdef as [v' []]; eauto.
  exists (rs1#rd <- v'); exists tm1.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_right. eexact EX1.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Iop; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite (@eval_operation_preserved CminorSel.fundef _ _ _ ge tge). eauto.
  exact symbols_preserved. traceEq.
(* Match-env *)
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  split. eauto with rtlg.
(* Result reg *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. rewrite Regmap.gss. auto.
(* Other regs *)
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. intros. rewrite Regmap.gso. auto. intuition congruence.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_expr_Eload_correct:
  forall (le : letenv) (chunk : memory_chunk) (addr : Op.addressing)
         (args : exprlist) (vargs : list val) (vaddr v : val),
  eval_exprlist ge sp e m le args vargs ->
  transl_exprlist_prop le args vargs ->
  Op.eval_addressing ge sp addr vargs = Some vaddr ->
  Mem.loadv chunk m vaddr = Some v ->
  transl_expr_prop le (Eload chunk addr args) v.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RES1 [OTHER1 EXT1]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct eval_addressing_lessdef as [vaddr' []]; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.loadv_extends as [v' []]; eauto.
  exists (rs1#rd <- v'); exists tm1.
(* Exec *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply star_right. eexact EX1. eapply exec_Iload. eauto.
  instantiate (1 := vaddr'). rewrite <- H3.
  apply eval_addressing_preserved. exact symbols_preserved.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto. traceEq.
(* Match-env *)
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  split. eauto with rtlg.
(* Result *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. rewrite Regmap.gss. auto.
(* Other regs *)
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. intros. rewrite Regmap.gso. auto. intuition congruence.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_expr_Econdition_correct:
  forall (le : letenv) (a: condexpr) (ifso ifnot : expr)
         (va : bool) (v : val),
  eval_condexpr ge sp e m le a va ->
  transl_condexpr_prop le a va ->
  eval_expr ge sp e m le (if va then ifso else ifnot) v ->
  transl_expr_prop le (if va then ifso else ifnot) v ->
  transl_expr_prop le (Econdition a ifso ifnot) v.
Proof.
  intros; red; intros; inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [OTHER1 EXT1]]]]].
  assert (tr_expr f.(fn_code) map pr (if va then ifso else ifnot) (if va then ntrue else nfalse) nd rd dst).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
    destruct va; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H2; eauto. intros [rs2 [tm2 [EX2 [ME2 [RES2 [OTHER2 EXT2]]]]]].
  exists rs2; exists tm2.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_trans. apply plus_star. eexact EX1. eexact EX2. traceEq.
(* Match-env *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Result value *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Other regs *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  split. intros. transitivity (rs1#r); auto.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_expr_Elet_correct:
  forall (le : letenv) (a1 a2 : expr) (v1 v2 : val),
  eval_expr ge sp e m le a1 v1 ->
  transl_expr_prop le a1 v1 ->
  eval_expr ge sp e m (v1 :: le) a2 v2 ->
  transl_expr_prop (v1 :: le) a2 v2 ->
  transl_expr_prop le (Elet a1 a2) v2.
Proof.
  intros; red; intros; inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RES1 [OTHER1 EXT1]]]]]].
  assert (map_wf (add_letvar map r)).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    eapply add_letvar_wf; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H2; eauto. eapply match_env_bind_letvar; eauto.
  intros [rs2 [tm2 [EX2 [ME3 [RES2 [OTHER2 EXT2]]]]]].
  exists rs2; exists tm2.
(* Exec *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. eapply star_trans. eexact EX1. eexact EX2. auto.
(* Match-env *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply match_env_unbind_letvar; eauto.
(* Result *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Other regs *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  split. intros. transitivity (rs1#r0); auto.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_expr_Eletvar_correct:
  forall (le : list val) (n : nat) (v : val),
  nth_error le n = Some v ->
  transl_expr_prop le (Eletvar n) v.
Proof.
  intros; red; intros; inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit tr_move_correct; eauto. intros [rs1 [EX1 [RES1 OTHER1]]].
  exists rs1; exists tm.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eexact EX1.
(* Match-env *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split.
  destruct H2 as [[A B] | [A B]].
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  subst r dst; simpl.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply match_env_invariant with rs. auto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. destruct (Reg.eq r rd). subst r. auto. auto.
  apply match_env_invariant with (rs#rd <- (rs#r)).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply match_env_update_dest; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply match_env_find_letvar; eauto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. rewrite Regmap.gsspec. destruct (peq r0 rd); auto.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  congruence.
(* Result *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. rewrite RES1. eapply match_env_find_letvar; eauto.
(* Other regs *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  split. intros.
  destruct H2 as [[A B] | [A B]].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  destruct (Reg.eq r0 rd); subst; auto.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  apply OTHER1. intuition congruence.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Remark eval_builtin_args_trivial:
  forall (ge: RTL.genv) (rs: regset) sp m rl,
  eval_builtin_args ge (fun r => rs#r) sp m (List.map (@BA reg) rl) rs##rl.
Proof.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  induction rl; simpl.
- constructor.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
- constructor; auto. constructor.
Qed.

Lemma transl_expr_Ebuiltin_correct:
  forall le ef al vl v,
  eval_exprlist ge sp e m le al vl ->
  transl_exprlist_prop le al vl ->
  external_call ef ge vl m E0 v m ->
  transl_expr_prop le (Ebuiltin ef al) v.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RR1 [RO1 EXT1]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit external_call_mem_extends; eauto.
  intros [v' [tm2 [A [B [C [D E]]]]]].
  exists (rs1#rd <- v'); exists tm2.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_right. eexact EX1.
  change (rs1#rd <- v') with (regmap_setres (BR rd) v' rs1).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Ibuiltin; eauto.
  eapply eval_builtin_args_trivial.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: @eq signature (funsig (@External function e)) (CminorSel.funsig (@External CminorSel.function e)) *)
  reflexivity.
(* Match-env *)
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  split. eauto with rtlg.
(* Result reg *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. rewrite Regmap.gss. auto.
(* Other regs *)
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. intros. rewrite Regmap.gso. auto. intuition congruence.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_expr_Eexternal_correct:
  forall le id sg al b ef vl v,
  Genv.find_symbol ge id = Some b ->
  Genv.find_funct_ptr ge b = Some (External ef) ->
  ef_sig ef = sg ->
  eval_exprlist ge sp e m le al vl ->
  transl_exprlist_prop le al vl ->
  external_call ef ge vl m E0 v m ->
  transl_expr_prop le (Eexternal id sg al) v.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H3; eauto. intros [rs1 [tm1 [EX1 [ME1 [RR1 [RO1 EXT1]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit external_call_mem_extends; eauto.
  intros [v' [tm2 [A [B [C [D E]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit function_ptr_translated; eauto. simpl. intros [tf [P Q]]. inv Q.
  exists (rs1#rd <- v'); exists tm2.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_trans. eexact EX1.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply star_left. eapply exec_Icall; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  simpl. rewrite symbols_preserved. rewrite H. eauto. auto.
  eapply star_left. eapply exec_function_external.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply external_call_symbols_preserved; eauto. apply senv_preserved.
  apply star_one. apply exec_return.
(* Goal: @eq signature (funsig (@External function e)) (CminorSel.funsig (@External CminorSel.function e)) *)
  reflexivity. reflexivity. reflexivity.
(* Match-env *)
(* Goal: forall _ : @eq (option val) (@PTree.get val id (set_locals il e)) (@Some val v), @ex reg (fun r : reg => and (@eq (option reg) (@PTree.get reg id (map_vars x0)) (@Some reg r)) (Val.lessdef v (Regmap.get val r rs))) *)
(* Goal: Val.lessdef_list le (@map positive val (fun r : positive => Regmap.get val r rs) (map_letvars (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)))) *)
  split. eauto with rtlg.
(* Result reg *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. rewrite Regmap.gss. auto.
(* Other regs *)
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  split. intros. rewrite Regmap.gso. auto. intuition congruence.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_exprlist_Enil_correct:
  forall (le : letenv),
  transl_exprlist_prop le Enil nil.
Proof.
  intros; red; intros; inv TE.
(* Goal: @ex regset (fun rs' : regset => @ex Mem.mem (fun tm' : Mem.mem => and (@star genv RTL.state step tge (State cs f sp nd rs tm) E0 (State cs f sp nd rs' tm')) (and (match_env map e le rs') (and (Val.lessdef_list (@nil val) (@List.map positive val (fun r : positive => Regmap.get val r rs') (@nil reg))) (and (forall (r : reg) (_ : @In reg r pr), @eq val (Regmap.get val r rs') (Regmap.get val r rs)) (Mem.extends m tm')))))) *)
  exists rs; exists tm.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. apply star_refl.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) tm) r *)
  split. constructor.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_exprlist_Econs_correct:
  forall (le : letenv) (a1 : expr) (al : exprlist) (v1 : val)
         (vl : list val),
  eval_expr ge sp e m le a1 v1 ->
  transl_expr_prop le a1 v1 ->
  eval_exprlist ge sp e m le al vl ->
  transl_exprlist_prop le al vl ->
  transl_exprlist_prop le (Econs a1 al) (v1 :: vl).
Proof.
  intros; red; intros; inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RES1 [OTHER1 EXT1]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H2; eauto. intros [rs2 [tm2 [EX2 [ME2 [RES2 [OTHER2 EXT2]]]]]].
  exists rs2; exists tm2.
(* Exec *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. eapply star_trans. eexact EX1. eexact EX2. auto.
(* Match-env *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Results *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. simpl. constructor. rewrite OTHER2. auto.
(* Goal: forall (id : positive) (r : reg) (_ : @eq (option reg) (@PTree.get reg id (PTree.empty reg)) (@Some reg r)) (_ : False), False *)
  simpl; tauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
(* Other regs *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  split. intros. transitivity (rs1#r).
(* Goal: forall (id : positive) (r : reg) (_ : @eq (option reg) (@PTree.get reg id (PTree.empty reg)) (@Some reg r)) (_ : False), False *)
  apply OTHER2; auto. simpl; tauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply OTHER1; auto.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_condexpr_CEcond_correct:
  forall le cond al vl vb,
  eval_exprlist ge sp e m le al vl ->
  transl_exprlist_prop le al vl ->
  eval_condition cond vl m = Some vb ->
  transl_condexpr_prop le (CEcond cond al) vb.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RES1 [OTHER1 EXT1]]]]]].
  exists rs1; exists tm1.
(* Exec *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply plus_right. eexact EX1. eapply exec_Icond. eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply eval_condition_lessdef; eauto. auto. traceEq.
(* Match-env *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Other regs *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_condexpr_CEcondition_correct:
  forall le a b c va v,
  eval_condexpr ge sp e m le a va ->
  transl_condexpr_prop le a va ->
  eval_condexpr ge sp e m le (if va then b else c) v ->
  transl_condexpr_prop le (if va then b else c) v ->
  transl_condexpr_prop le (CEcondition a b c) v.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [OTHER1 EXT1]]]]].
  assert (tr_condition (fn_code f) map pr (if va then b else c) (if va then n2 else n3) ntrue nfalse).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
    destruct va; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H2; eauto. intros [rs2 [tm2 [EX2 [ME2 [OTHER2 EXT2]]]]].
  exists rs2; exists tm2.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply plus_trans. eexact EX1. eexact EX2. traceEq.
(* Match-env *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. assumption.
(* Other regs *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  split. intros. rewrite OTHER2; auto.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Lemma transl_condexpr_CElet_correct:
  forall le a b v1 v2,
  eval_expr ge sp e m le a v1 ->
  transl_expr_prop le a v1 ->
  eval_condexpr ge sp e m (v1 :: le) b v2 ->
  transl_condexpr_prop (v1 :: le) b v2 ->
  transl_condexpr_prop le (CElet a b) v2.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros; red; intros. inv TE.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H0; eauto. intros [rs1 [tm1 [EX1 [ME1 [RES1 [OTHER1 EXT1]]]]]].
  assert (map_wf (add_letvar map r)).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    eapply add_letvar_wf; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H2; eauto. eapply match_env_bind_letvar; eauto.
  intros [rs2 [tm2 [EX2 [ME3 [OTHER2 EXT2]]]]].
  exists rs2; exists tm2.
(* Exec *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_plus_trans. eexact EX1. eexact EX2. traceEq.
(* Match-env *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply match_env_unbind_letvar; eauto.
(* Other regs *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  split. intros. rewrite OTHER2; auto.
(* Mem *)
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

Theorem transl_expr_correct:
  forall le a v,
  eval_expr ge sp e m le a v ->
  transl_expr_prop le a v.
Proof
  (eval_expr_ind3 ge sp e m
     transl_expr_prop
     transl_exprlist_prop
     transl_condexpr_prop
     transl_expr_Evar_correct
     transl_expr_Eop_correct
     transl_expr_Eload_correct
     transl_expr_Econdition_correct
     transl_expr_Elet_correct
     transl_expr_Eletvar_correct
     transl_expr_Ebuiltin_correct
     transl_expr_Eexternal_correct
     transl_exprlist_Enil_correct
     transl_exprlist_Econs_correct
     transl_condexpr_CEcond_correct
     transl_condexpr_CEcondition_correct
     transl_condexpr_CElet_correct).

Theorem transl_exprlist_correct:
  forall le a v,
  eval_exprlist ge sp e m le a v ->
  transl_exprlist_prop le a v.
Proof
  (eval_exprlist_ind3 ge sp e m
     transl_expr_prop
     transl_exprlist_prop
     transl_condexpr_prop
     transl_expr_Evar_correct
     transl_expr_Eop_correct
     transl_expr_Eload_correct
     transl_expr_Econdition_correct
     transl_expr_Elet_correct
     transl_expr_Eletvar_correct
     transl_expr_Ebuiltin_correct
     transl_expr_Eexternal_correct
     transl_exprlist_Enil_correct
     transl_exprlist_Econs_correct
     transl_condexpr_CEcond_correct
     transl_condexpr_CEcondition_correct
     transl_condexpr_CElet_correct).

Theorem transl_condexpr_correct:
  forall le a v,
  eval_condexpr ge sp e m le a v ->
  transl_condexpr_prop le a v.
Proof
  (eval_condexpr_ind3 ge sp e m
     transl_expr_prop
     transl_exprlist_prop
     transl_condexpr_prop
     transl_expr_Evar_correct
     transl_expr_Eop_correct
     transl_expr_Eload_correct
     transl_expr_Econdition_correct
     transl_expr_Elet_correct
     transl_expr_Eletvar_correct
     transl_expr_Ebuiltin_correct
     transl_expr_Eexternal_correct
     transl_exprlist_Enil_correct
     transl_exprlist_Econs_correct
     transl_condexpr_CEcond_correct
     transl_condexpr_CEcondition_correct
     transl_condexpr_CElet_correct).

(** Exit expressions. *)

Definition transl_exitexpr_prop
     (le: letenv) (a: exitexpr) (x: nat) : Prop :=
  forall tm cs f map ns nexits rs
    (MWF: map_wf map)
    (TE: tr_exitexpr f.(fn_code) map a ns nexits)
    (ME: match_env map e le rs)
    (EXT: Mem.extends m tm),
  exists nd, exists rs', exists tm',
     star step tge (State cs f sp ns rs tm) E0 (State cs f sp nd rs' tm')
  /\ nth_error nexits x = Some nd
  /\ match_env map e le rs'
  /\ Mem.extends m tm'.

Theorem transl_exitexpr_correct:
  forall le a x,
  eval_exitexpr ge sp e m le a x ->
  transl_exitexpr_prop le a x.
Proof.
  induction 1; red; intros; inv TE.
- (* XEexit *)
  exists ns, rs, tm.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. apply star_refl.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
- (* XEjumptable *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit H3; eauto. intros (nd & A & B).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto. intros (rs1 & tm1 & EXEC1 & ME1 & RES1 & PRES1 & EXT1).
  exists nd, rs1, tm1.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply star_right. eexact EXEC1. eapply exec_Ijumptable; eauto. inv RES1; auto. traceEq.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
- (* XEcondition *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_condexpr_correct; eauto. intros (rs1 & tm1 & EXEC1 & ME1 & RES1 & EXT1).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHeval_exitexpr; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  instantiate (2 := if va then n2 else n3). destruct va; eauto.
  intros (nd & rs2 & tm2 & EXEC2 & EXIT2 & ME2 & EXT2).
  exists nd, rs2, tm2.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_trans. apply plus_star. eexact EXEC1. eexact EXEC2. traceEq.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
- (* XElet *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto. intros (rs1 & tm1 & EXEC1 & ME1 & RES1 & PRES1 & EXT1).
  assert (map_wf (add_letvar map r)).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    eapply add_letvar_wf; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHeval_exitexpr; eauto. eapply match_env_bind_letvar; eauto.
  intros (nd & rs2 & tm2 & EXEC2 & EXIT2 & ME2 & EXT2).
  exists nd, rs2, tm2.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  split. eapply star_trans. eexact EXEC1. eexact EXEC2. traceEq.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  split. eapply match_env_unbind_letvar; eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  auto.
Qed.

(** Builtin arguments. *)

Lemma eval_exprlist_append:
  forall le al1 vl1 al2 vl2,
  eval_exprlist ge sp e m le (exprlist_of_expr_list al1) vl1 ->
  eval_exprlist ge sp e m le (exprlist_of_expr_list al2) vl2 ->
  eval_exprlist ge sp e m le (exprlist_of_expr_list (al1 ++ al2)) (vl1 ++ vl2).
Proof.
(* Goal: forall (le : letenv) (al1 : list expr) (vl1 : list val) (al2 : list expr) (vl2 : list val) (_ : eval_exprlist ge sp e m le (exprlist_of_expr_list al1) vl1) (_ : eval_exprlist ge sp e m le (exprlist_of_expr_list al2) vl2), eval_exprlist ge sp e m le (exprlist_of_expr_list (@app expr al1 al2)) (@app val vl1 vl2) *)
  induction al1; simpl; intros vl1 al2 vl2 E1 E2; inv E1.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
- auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
- simpl. constructor; eauto.
Qed.

Lemma invert_eval_builtin_arg:
  forall a v,
  eval_builtin_arg ge sp e m a v ->
  exists vl,
     eval_exprlist ge sp e m nil (exprlist_of_expr_list (params_of_builtin_arg a)) vl
  /\ Events.eval_builtin_arg ge (fun v => v) sp m (fst (convert_builtin_arg a vl)) v
  /\ (forall vl', convert_builtin_arg a (vl ++ vl') = (fst (convert_builtin_arg a vl), vl')).
Proof.
(* Goal: forall (a : builtin_arg expr) (v : val) (_ : eval_builtin_arg ge sp e m a v), @ex (list val) (fun vl : list val => and (eval_exprlist ge sp e m (@nil val) (exprlist_of_expr_list (@params_of_builtin_arg expr a)) vl) (and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v0 : val => v0) sp m (@fst (builtin_arg val) (list val) (@convert_builtin_arg val a vl)) v) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (@convert_builtin_arg val a (@app val vl vl')) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (@convert_builtin_arg val a vl)) vl')))) *)
  induction 1; simpl; try (econstructor; intuition eauto with evalexpr barg; fail).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
- econstructor; split; eauto with evalexpr. split. constructor. auto. 
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
- econstructor; split; eauto with evalexpr. split. constructor. auto. 
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
- econstructor; split; eauto with evalexpr. split. repeat constructor. auto. 
- destruct IHeval_builtin_arg1 as (vl1 & A1 & B1 & C1).
(* Goal: @ex (list val) (fun vl : list val => and (eval_exprlist ge sp e m (@nil val) (exprlist_of_expr_list (@app expr (@params_of_builtin_arg expr a1) (@params_of_builtin_arg expr a2))) vl) (and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 vl in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 vl in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')))) *)
  destruct IHeval_builtin_arg2 as (vl2 & A2 & B2 & C2).
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a1 vl1) as [a1' rl1] eqn:E1; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a2 vl2) as [a2' rl2] eqn:E2; simpl in *.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  exists (vl1 ++ vl2); split.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply eval_exprlist_append; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split. rewrite C1, E2. constructor; auto. 
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. rewrite app_ass, !C1, C2, E2. auto.
Qed.

Lemma invert_eval_builtin_args:
  forall al vl,
  list_forall2 (eval_builtin_arg ge sp e m) al vl ->
  exists vl',
     eval_exprlist ge sp e m nil (exprlist_of_expr_list (params_of_builtin_args al)) vl'
  /\ Events.eval_builtin_args ge (fun v => v) sp m (convert_builtin_args al vl') vl.
Proof.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  induction 1; simpl.
- exists (@nil val); split; constructor.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
- exploit invert_eval_builtin_arg; eauto. intros (vl1 & A & B & C).
(* Goal: @ex (list val) (fun vl' : list val => and (eval_exprlist ge sp e m (@nil val) (exprlist_of_expr_list (@app expr (@params_of_builtin_arg expr a1) (@params_of_builtin_args expr al))) vl') (@eval_builtin_args val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (let (a1', rl1) := @convert_builtin_arg val a1 vl' in @cons (builtin_arg val) a1' (@convert_builtin_args val al rl1)) (@cons val b1 bl))) *)
  destruct IHlist_forall2 as (vl2 & D & E).
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  exists (vl1 ++ vl2); split.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply eval_exprlist_append; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  rewrite C; simpl. constructor; auto.
Qed.

Lemma transl_eval_builtin_arg:
  forall rs a vl rl v,
  Val.lessdef_list vl rs##rl ->
  Events.eval_builtin_arg ge (fun v => v) sp m (fst (convert_builtin_arg a vl)) v ->
  exists v',
     Events.eval_builtin_arg ge (fun r => rs#r) sp m (fst (convert_builtin_arg a rl)) v'
  /\ Val.lessdef v v'
  /\ Val.lessdef_list (snd (convert_builtin_arg a vl)) rs##(snd (convert_builtin_arg a rl)).
Proof.
  induction a; simpl; intros until v; intros LD EV;
  try (now (inv EV; econstructor; eauto with barg)).
- destruct rl; simpl in LD; inv LD; inv EV; simpl.
  econstructor; eauto with barg.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists (rs#p); intuition auto. constructor.
- destruct (convert_builtin_arg a1 vl) as [a1' vl1] eqn:CV1; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a2 vl1) as [a2' vl2] eqn:CV2; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a1 rl) as [a1'' rl1] eqn:CV3; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a2 rl1) as [a2'' rl2] eqn:CV4; simpl in *.
  inv EV.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHa1; eauto. rewrite CV1; simpl; eauto.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  rewrite CV1, CV3; simpl. intros (v1' & A1 & B1 & C1).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHa2. eexact C1. rewrite CV2; simpl; eauto.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  rewrite CV2, CV4; simpl. intros (v2' & A2 & B2 & C2).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists (Val.longofwords v1' v2'); split. constructor; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split; auto. apply Val.longofwords_lessdef; auto.
- destruct (convert_builtin_arg a1 vl) as [a1' vl1] eqn:CV1; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a2 vl1) as [a2' vl2] eqn:CV2; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a1 rl) as [a1'' rl1] eqn:CV3; simpl in *.
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a2 rl1) as [a2'' rl2] eqn:CV4; simpl in *.
  inv EV.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHa1; eauto. rewrite CV1; simpl; eauto.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  rewrite CV1, CV3; simpl. intros (v1' & A1 & B1 & C1).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHa2. eexact C1. rewrite CV2; simpl; eauto.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  rewrite CV2, CV4; simpl. intros (v2' & A2 & B2 & C2).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; split. constructor; eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  split; auto. destruct Archi.ptr64; auto using Val.add_lessdef, Val.addl_lessdef.
Qed.

Lemma transl_eval_builtin_args:
  forall rs al vl1 rl vl,
  Val.lessdef_list vl1 rs##rl ->
  Events.eval_builtin_args ge (fun v => v) sp m (convert_builtin_args al vl1) vl ->
  exists vl',
     Events.eval_builtin_args ge (fun r => rs#r) sp m (convert_builtin_args al rl) vl'
  /\ Val.lessdef_list vl vl'.
Proof.
  induction al; simpl; intros until vl; intros LD EV.
- inv EV. exists (@nil val); split; constructor.
- destruct (convert_builtin_arg a vl1) as [a1' vl2] eqn:CV1; simpl in *.
  inv EV.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_eval_builtin_arg. eauto. instantiate (2 := a). rewrite CV1; simpl; eauto.
(* Goal: @lex_ord nat nat lt lt (measure_state (CminorSel.State f1 s1 k1 sp1 e1 m1)) (measure_state (CminorSel.State f2 s2 k2 sp2 e2 m2)) *)
  rewrite CV1; simpl. intros (v1' & A1 & B1 & C1).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit IHal. eexact C1. eauto. intros (vl' & A2 & B2).
(* Goal: match_env map e le rs *)
  destruct (convert_builtin_arg a rl) as [a1'' rl2]; simpl in *.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists (v1' :: vl'); split; constructor; auto.
Qed.

End CORRECTNESS_EXPR.

(** ** Measure over CminorSel states *)

Local Open Scope nat_scope.

Fixpoint size_stmt (s: stmt) : nat :=
  match s with
  | Sskip => 0
  | Sseq s1 s2 => (size_stmt s1 + size_stmt s2 + 1)
  | Sifthenelse c s1 s2 => (size_stmt s1 + size_stmt s2 + 1)
  | Sloop s1 => (size_stmt s1 + 1)
  | Sblock s1 => (size_stmt s1 + 1)
  | Sexit n => 0
  | Slabel lbl s1 => (size_stmt s1 + 1)
  | _ => 1
  end.

Fixpoint size_cont (k: cont) : nat :=
  match k with
  | Kseq s k1 => (size_stmt s + size_cont k1 + 1)
  | Kblock k1 => (size_cont k1 + 1)
  | _ => 0%nat
  end.

Definition measure_state (S: CminorSel.state) :=
  match S with
  | CminorSel.State _ s k _ _ _ => (size_stmt s + size_cont k, size_stmt s)
  | _                           => (0, 0)
  end.

Definition lt_state (S1 S2: CminorSel.state) :=
  lex_ord lt lt (measure_state S1) (measure_state S2).

Lemma lt_state_intro:
  forall f1 s1 k1 sp1 e1 m1 f2 s2 k2 sp2 e2 m2,
  size_stmt s1 + size_cont k1 < size_stmt s2 + size_cont k2
  \/ (size_stmt s1 + size_cont k1 = size_stmt s2 + size_cont k2
      /\ size_stmt s1 < size_stmt s2) ->
  lt_state (CminorSel.State f1 s1 k1 sp1 e1 m1)
           (CminorSel.State f2 s2 k2 sp2 e2 m2).
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. unfold lt_state. simpl. destruct H as [A | [A B]].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  left. auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  rewrite A. right. auto.
Qed.

Ltac Lt_state :=
  apply lt_state_intro; simpl; try omega.

Lemma lt_state_wf:
  well_founded lt_state.
Proof.
(* Goal: @well_founded CminorSel.state lt_state *)
  unfold lt_state. apply wf_inverse_image with (f := measure_state).
(* Goal: @well_founded (prod nat nat) (@lex_ord nat nat lt lt) *)
  apply wf_lex_ord. apply lt_wf. apply lt_wf.
Qed.

(** ** Semantic preservation for the translation of statements *)

(** The simulation diagram for the translation of statements
  and functions is a "star" diagram of the form:
<<
           I                         I
     S1 ------- R1             S1 ------- R1
     |          |              |          |
   t |        + | t      or  t |        * | t    and |S2| < |S1|
     v          v              v          |
     S2 ------- R2             S2 ------- R2
           I                         I
>>
  where [I] is the [match_states] predicate defined below.  It includes:
- Agreement between the Cminor statement under consideration and
  the current program point in the RTL control-flow graph,
  as captured by the [tr_stmt] predicate.
- Agreement between the Cminor continuation and the RTL control-flow
  graph and call stack, as captured by the [tr_cont] predicate below.
- Agreement between Cminor environments and RTL register environments,
  as captured by [match_envs].

*)

Inductive tr_fun (tf: function) (map: mapping) (f: CminorSel.function)
                     (ngoto: labelmap) (nret: node) (rret: option reg) : Prop :=
  | tr_fun_intro: forall nentry r,
      rret = ret_reg f.(CminorSel.fn_sig) r ->
      tr_stmt tf.(fn_code) map f.(fn_body) nentry nret nil ngoto nret rret ->
      tf.(fn_stacksize) = f.(fn_stackspace) ->
      tr_fun tf map f ngoto nret rret.

Inductive tr_cont: RTL.code -> mapping ->
                   CminorSel.cont -> node -> list node -> labelmap -> node -> option reg ->
                   list RTL.stackframe -> Prop :=
  | tr_Kseq: forall c map s k nd nexits ngoto nret rret cs n,
      tr_stmt c map s nd n nexits ngoto nret rret ->
      tr_cont c map k n nexits ngoto nret rret cs ->
      tr_cont c map (Kseq s k) nd nexits ngoto nret rret cs
  | tr_Kblock: forall c map k nd nexits ngoto nret rret cs,
      tr_cont c map k nd nexits ngoto nret rret cs ->
      tr_cont c map (Kblock k) nd (nd :: nexits) ngoto nret rret cs
  | tr_Kstop: forall c map ngoto nret rret cs,
      c!nret = Some(Ireturn rret) ->
      match_stacks Kstop cs ->
      tr_cont c map Kstop nret nil ngoto nret rret cs
  | tr_Kcall: forall c map optid f sp e k ngoto nret rret cs,
      c!nret = Some(Ireturn rret) ->
      match_stacks (Kcall optid f sp e k) cs ->
      tr_cont c map (Kcall optid f sp e k) nret nil ngoto nret rret cs

with match_stacks: CminorSel.cont -> list RTL.stackframe -> Prop :=
  | match_stacks_stop:
      match_stacks Kstop nil
  | match_stacks_call: forall optid f sp e k r tf n rs cs map nexits ngoto nret rret,
      map_wf map ->
      tr_fun tf map f ngoto nret rret ->
      match_env map e nil rs ->
      reg_map_ok map r optid ->
      tr_cont tf.(fn_code) map k n nexits ngoto nret rret cs ->
      match_stacks (Kcall optid f sp e k) (Stackframe r tf sp n rs :: cs).

Inductive match_states: CminorSel.state -> RTL.state -> Prop :=
  | match_state:
      forall f s k sp e m tm cs tf ns rs map ncont nexits ngoto nret rret
        (MWF: map_wf map)
        (TS: tr_stmt tf.(fn_code) map s ns ncont nexits ngoto nret rret)
        (TF: tr_fun tf map f ngoto nret rret)
        (TK: tr_cont tf.(fn_code) map k ncont nexits ngoto nret rret cs)
        (ME: match_env map e nil rs)
        (MEXT: Mem.extends m tm),
      match_states (CminorSel.State f s k sp e m)
                   (RTL.State cs tf sp ns rs tm)
  | match_callstate:
      forall f args targs k m tm cs tf
        (TF: transl_fundef f = OK tf)
        (MS: match_stacks k cs)
        (LD: Val.lessdef_list args targs)
        (MEXT: Mem.extends m tm),
      match_states (CminorSel.Callstate f args k m)
                   (RTL.Callstate cs tf targs tm)
  | match_returnstate:
      forall v tv k m tm cs
        (MS: match_stacks k cs)
        (LD: Val.lessdef v tv)
        (MEXT: Mem.extends m tm),
      match_states (CminorSel.Returnstate v k m)
                   (RTL.Returnstate cs tv tm).

Lemma match_stacks_call_cont:
  forall c map k ncont nexits ngoto nret rret cs,
  tr_cont c map k ncont nexits ngoto nret rret cs ->
  match_stacks (call_cont k) cs /\ c!nret = Some(Ireturn rret).
Proof.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  induction 1; simpl; auto.
Qed.

Lemma tr_cont_call_cont:
  forall c map k ncont nexits ngoto nret rret cs,
  tr_cont c map k ncont nexits ngoto nret rret cs ->
  tr_cont c map (call_cont k) nret nil ngoto nret rret cs.
Proof.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  induction 1; simpl; auto; econstructor; eauto.
Qed.

Lemma tr_find_label:
  forall c map lbl n (ngoto: labelmap) nret rret s' k' cs,
  ngoto!lbl = Some n ->
  forall s k ns1 nd1 nexits1,
  find_label lbl s k = Some (s', k') ->
  tr_stmt c map s ns1 nd1 nexits1 ngoto nret rret ->
  tr_cont c map k nd1 nexits1 ngoto nret rret cs ->
  exists ns2, exists nd2, exists nexits2,
     c!n = Some(Inop ns2)
  /\ tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret
  /\ tr_cont c map k' nd2 nexits2 ngoto nret rret cs.
Proof.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  induction s; intros until nexits1; simpl; try congruence.
  (* seq *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  caseEq (find_label lbl s1 (Kseq s2 k)); intros.
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kseq (Sloop s) k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sloop s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kblock k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sblock s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (if ident_eq lbl l then @Some (prod stmt cont) (@pair stmt cont s k) else find_label lbl s k) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Slabel l s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  inv H1. inv H2. eapply IHs1; eauto. econstructor; eauto.
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kseq (Sloop s) k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sloop s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kblock k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sblock s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (if ident_eq lbl l then @Some (prod stmt cont) (@pair stmt cont s k) else find_label lbl s k) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Slabel l s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  inv H2. eapply IHs2; eauto.
  (* ifthenelse *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  caseEq (find_label lbl s1 k); intros.
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kseq (Sloop s) k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sloop s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kblock k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sblock s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (if ident_eq lbl l then @Some (prod stmt cont) (@pair stmt cont s k) else find_label lbl s k) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Slabel l s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  inv H1. inv H2. eapply IHs1; eauto.
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kseq (Sloop s) k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sloop s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (find_label lbl s (Kblock k)) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Sblock s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: forall (_ : @eq (option (prod stmt cont)) (if ident_eq lbl l then @Some (prod stmt cont) (@pair stmt cont s k) else find_label lbl s k) (@Some (prod stmt cont) (@pair stmt cont s' k'))) (_ : tr_stmt c map (Slabel l s) ns1 nd1 nexits1 ngoto nret rret) (_ : tr_cont c map k nd1 nexits1 ngoto nret rret cs), @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  inv H2. eapply IHs2; eauto.
  (* loop *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inversion H1; subst.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply IHs; eauto. econstructor; eauto. econstructor; eauto.
  (* block *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H1.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply IHs; eauto. econstructor; eauto.
  (* label *)
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  destruct (ident_eq lbl l); intros.
(* Goal: final_state R r *)
  inv H0. inv H1.
(* Goal: @eq positive n0 n *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
(* Goal: @ex node (fun ns2 : node => @ex node (fun nd2 : node => @ex (list node) (fun nexits2 : list node => and (@eq (option instruction) (@PTree.get instruction n c) (@Some instruction (Inop ns2))) (and (tr_stmt c map s' ns2 nd2 nexits2 ngoto nret rret) (tr_cont c map k' nd2 nexits2 ngoto nret rret cs))))) *)
  assert (n0 = n). change positive with node in H4. congruence. subst n0.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  exists ns1; exists nd1; exists nexits1; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  inv H1. eapply IHs; eauto.
Qed.

Theorem transl_step_correct:
  forall S1 t S2, CminorSel.step ge S1 t S2 ->
  forall R1, match_states S1 R1 ->
  exists R2,
  (plus RTL.step tge R1 t R2 \/ (star RTL.step tge R1 t R2 /\ lt_state S2 S1))
  /\ match_states S2 R2.
Proof.
  induction 1; intros R1 MSTATE; inv MSTATE.

  (* skip seq *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  inv TS. inv TK. econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.

  (* skip block *)
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  inv TS. inv TK. econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. constructor.

  (* skip return *)
  inv TS.
  assert ((fn_code tf)!ncont = Some(Ireturn rret)
          /\ match_stacks k cs).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
    inv TK; simpl in H; try contradiction; auto.
  destruct H1.
  assert (fn_stacksize tf = fn_stackspace f).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
    inv TF. auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.free_parallel_extends as [tm' []]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left; apply plus_one. eapply exec_Ireturn. eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite H3. eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor; auto.

  (* assign *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto.
  intros [rs' [tm' [A [B [C [D E]]]]]].
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  right; split. eauto. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. constructor.

  (* store *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exprlist_correct; eauto.
  intros [rs' [tm' [A [B [C [D E]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto.
  intros [rs'' [tm'' [F [G [J [K L]]]]]].
  assert (Val.lessdef_list vl rs''##rl).
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
    replace (rs'' ## rl) with (rs' ## rl). auto.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
    apply list_map_exten. intros. apply K. auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct eval_addressing_lessdef as [vaddr' []]; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.storev_extends as [tm''' []]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: @eq signature (funsig (@External function e)) (CminorSel.funsig (@External CminorSel.function e)) *)
  left; eapply plus_right. eapply star_trans. eexact A. eexact F. reflexivity.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Istore with (a := vaddr'). eauto.
  rewrite <- H4. apply eval_addressing_preserved. exact symbols_preserved.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eauto. traceEq.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. constructor.

  (* call *)
(* Goal: final_state R r *)
  inv TS; inv H.
  (* indirect *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto.
  intros [rs' [tm' [A [B [C [D X]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exprlist_correct; eauto.
  intros [rs'' [tm'' [E [F [G [J Y]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit functions_translated; eauto. intros [tf' [P Q]].
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: @eq signature (funsig (@External function e)) (CminorSel.funsig (@External CminorSel.function e)) *)
  left; eapply plus_right. eapply star_trans. eexact A. eexact E. reflexivity.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Icall; eauto. simpl. rewrite J. destruct C. eauto. discriminate P. simpl; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply sig_transl_function; auto.
  traceEq.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  constructor; auto. econstructor; eauto.
  (* direct *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exprlist_correct; eauto.
  intros [rs'' [tm'' [E [F [G [J Y]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit functions_translated; eauto. intros [tf' [P Q]].
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
  left; eapply plus_right. eexact E.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Icall; eauto. simpl. rewrite symbols_preserved. rewrite H4.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    rewrite Genv.find_funct_find_funct_ptr in P. eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply sig_transl_function; auto.
  traceEq.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  constructor; auto. econstructor; eauto.

  (* tailcall *)
(* Goal: final_state R r *)
  inv TS; inv H.
  (* indirect *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto.
  intros [rs' [tm' [A [B [C [D X]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exprlist_correct; eauto.
  intros [rs'' [tm'' [E [F [G [J Y]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit functions_translated; eauto. intros [tf' [P Q]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit match_stacks_call_cont; eauto. intros [U V].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  assert (fn_stacksize tf = fn_stackspace f). inv TF; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.free_parallel_extends as [tm''' []]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: @eq signature (funsig (@External function e)) (CminorSel.funsig (@External CminorSel.function e)) *)
  left; eapply plus_right. eapply star_trans. eexact A. eexact E. reflexivity.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Itailcall; eauto. simpl. rewrite J. destruct C. eauto. discriminate P. simpl; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply sig_transl_function; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite H; eauto.
  traceEq.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor; auto.
  (* direct *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exprlist_correct; eauto.
  intros [rs'' [tm'' [E [F [G [J Y]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit functions_translated; eauto. intros [tf' [P Q]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit match_stacks_call_cont; eauto. intros [U V].
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  assert (fn_stacksize tf = fn_stackspace f). inv TF; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.free_parallel_extends as [tm''' []]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
  left; eapply plus_right. eexact E.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Itailcall; eauto. simpl. rewrite symbols_preserved. rewrite H5.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite Genv.find_funct_find_funct_ptr in P. eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  apply sig_transl_function; auto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite H; eauto.
  traceEq.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor; auto.

  (* builtin *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit invert_eval_builtin_args; eauto. intros (vparams & P & Q).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exprlist_correct; eauto.
  intros [rs' [tm' [E [F [G [J K]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_eval_builtin_args; eauto.
  intros (vargs' & U & V).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit (@eval_builtin_args_lessdef _ ge (fun r => rs'#r) (fun r => rs'#r)); eauto.
  intros (vargs'' & X & Y).
  assert (Z: Val.lessdef_list vl vargs'') by (eapply Val.lessdef_list_trans; eauto).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct external_call_mem_extends as [tv [tm'' [A [B [C D]]]]]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
  left. eapply plus_right. eexact E.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply exec_Ibuiltin. eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply external_call_symbols_preserved. apply senv_preserved. eauto.
  traceEq.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. constructor.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply match_env_update_res; eauto.

  (* seq *)
  inv TS.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. econstructor; eauto.

  (* ifthenelse *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_condexpr_correct; eauto. intros [rs' [tm' [A [B [C D]]]]].
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: @lex_ord nat nat lt lt (@pair nat nat (Init.Nat.add (size_stmt s1) (size_cont k1)) (size_stmt s1)) (@pair nat nat (Init.Nat.add (size_stmt s2) (size_cont k2)) (size_stmt s2)) *)
(* Goal: @lex_ord nat nat lt lt (@pair nat nat (Init.Nat.add (size_stmt s1) (size_cont k1)) (size_stmt s1)) (@pair nat nat (Init.Nat.add (size_stmt s2) (size_cont k2)) (size_stmt s2)) *)
  left. eexact A.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  destruct b; econstructor; eauto.

  (* loop *)
  inversion TS; subst.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left. apply plus_one. eapply exec_Inop; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.

  (* block *)
  inv TS.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. econstructor; eauto.

  (* exit seq *)
  inv TS. inv TK.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. econstructor; eauto.

  (* exit block 0 *)
(* Goal: final_state R r *)
  inv TS. inv TK. simpl in H0. inv H0.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. econstructor; eauto.

  (* exit block n+1 *)
  inv TS. inv TK. simpl in H0.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. econstructor; eauto.

  (* switch *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_exitexpr_correct; eauto.
  intros (nd & rs' & tm' & A & B & C & D).
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. eexact A. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. constructor; auto.

  (* return none *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit match_stacks_call_cont; eauto. intros [U V].
  inversion TF.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.free_parallel_extends as [tm' []]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left; apply plus_one. eapply exec_Ireturn; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite H2; eauto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor; auto.

  (* return some *)
  inv TS.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit transl_expr_correct; eauto.
  intros [rs' [tm' [A [B [C [D E]]]]]].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit match_stacks_call_cont; eauto. intros [U V].
  inversion TF.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct Mem.free_parallel_extends as [tm'' []]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left; eapply plus_right. eexact A. eapply exec_Ireturn; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  rewrite H4; eauto. traceEq.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  simpl. constructor; auto.

  (* label *)
  inv TS.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  right; split. apply star_refl. Lt_state.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.

  (* goto *)
  inv TS. inversion TF; subst.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit tr_find_label; eauto. eapply tr_cont_call_cont; eauto.
  intros [ns2 [nd2 [nexits2 [A [B C]]]]].
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left; apply plus_one. eapply exec_Inop; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.

  (* internal call *)
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  monadInv TF. exploit transl_function_charact; eauto. intro TRF.
  inversion TRF. subst f0.
  pose (e := set_locals (fn_vars f) (set_params vargs (CminorSel.fn_params f))).
  pose (rs := init_regs targs rparams).
  assert (ME: match_env map2 e nil rs).
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    unfold rs, e. eapply match_init_env_init_reg; eauto.
  assert (MWF: map_wf map2).
(* Goal: map_valid init_mapping s0 *)
    assert (map_valid init_mapping s0) by apply init_mapping_valid.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    exploit (add_vars_valid (CminorSel.fn_params f)); eauto. intros [A B].
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
    eapply add_vars_wf; eauto. eapply add_vars_wf; eauto. apply init_mapping_wf.
  edestruct Mem.alloc_extends as [tm' []]; eauto; try apply Z.le_refl.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left; apply plus_one. eapply exec_function_internal; simpl; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  simpl. econstructor; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  inversion MS; subst; econstructor; eauto.

  (* external call *)
  monadInv TF.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  edestruct external_call_mem_extends as [tvres [tm' [A [B [C D]]]]]; eauto.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  left; apply plus_one. eapply exec_function_external; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor; auto.

  (* return *)
(* Goal: final_state (Returnstate cs tv tm) r *)
  inv MS.
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) tm) r *)
  left; apply plus_one; constructor.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor; eauto. constructor.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  eapply match_env_update_dest; eauto.
Qed.

Lemma transl_initial_states:
  forall S, CminorSel.initial_state prog S ->
  exists R, RTL.initial_state tprog R /\ match_states S R.
Proof.
  induction 1.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  exploit function_ptr_translated; eauto. intros [tf [A B]].
(* Goal: and (@Events.eval_builtin_arg val (@Genv.to_senv CminorSel.fundef unit ge) (fun v : val => v) sp m (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) (if Archi.ptr64 then Val.addl v1 v2 else Val.add v1 v2)) (forall vl' : list val, @eq (prod (builtin_arg val) (list val)) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val (@app val vl1 vl2) vl') in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2) (@pair (builtin_arg val) (list val) (@fst (builtin_arg val) (list val) (let (a1', rl1) := @convert_builtin_arg val a1 (@app val vl1 vl2) in let (a2', rl2) := @convert_builtin_arg val a2 rl1 in @pair (builtin_arg val) (list val) (@BA_addptr val a1' a2') rl2)) vl')) *)
  econstructor; split.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  econstructor. apply (Genv.init_mem_transf_partial TRANSL); eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  replace (prog_main tprog) with (prog_main prog). rewrite symbols_preserved; eauto.
(* Goal: Val.lessdef_list (@nil val) (@map positive val (fun r : positive => Regmap.get val r (Regmap.init val Vundef)) (map_letvars x0)) *)
(* Goal: @eq (prod val (PTree.t val)) (Regmap.init val Vundef) (init_regs (@nil val) x) *)
(* Goal: forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.init val Vundef)) Vundef *)
(* Goal: and (match_env (mkmapping (@PTree.set reg a x1 (map_vars x0)) (map_letvars x0)) (@PTree.set val a v1 (set_params vs il)) (@nil val) (Regmap.set val x1 tv1 (init_regs tvs x))) (forall (r : reg) (_ : reg_fresh r s2), @eq val (Regmap.get val r (Regmap.set val x1 tv1 (init_regs tvs x))) Vundef) *)
  symmetry; eapply match_program_main; eauto.
  eexact A.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  rewrite <- H2. apply sig_transl_function; auto.
(* Goal: lt (size_stmt s1) (size_stmt s2) *)
  constructor. auto. constructor.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) tm) r *)
  constructor. apply Mem.extends_refl.
Qed.

Lemma transl_final_states:
  forall S R r,
  match_states S R -> CminorSel.final_state S r -> RTL.final_state R r.
Proof.
(* Goal: forall (S : CminorSel.state) (R : RTL.state) (r : Int.int) (_ : match_states S R) (_ : CminorSel.final_state S r), final_state R r *)
  intros. inv H0. inv H. inv MS. inv LD. constructor.
Qed.

Theorem transf_program_correct:
  forward_simulation (CminorSel.semantics prog) (RTL.semantics tprog).
Proof.
  eapply forward_simulation_star_wf with (order := lt_state).
  apply senv_preserved.
  eexact transl_initial_states.
  eexact transl_final_states.
  apply lt_state_wf.
  exact transl_step_correct.
Qed.

End CORRECTNESS.
