(* *********************************************************************)
(*                                                                     *)
(*              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.     *)
(*                                                                     *)
(* *********************************************************************)

(** RTL function inlining: semantic preservation *)

Require Import Coqlib Wfsimpl Maps Errors Integers.
Require Import AST Linking Values Memory Globalenvs Events Smallstep.
Require Import Op Registers RTL.
Require Import Inlining Inliningspec.

Definition match_prog (prog tprog: program) :=
  match_program (fun cunit f tf => transf_fundef (funenv_program cunit) f = OK tf) eq prog tprog.

Lemma transf_program_match:
  forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply match_transform_partial_program_contextual; eauto.
Qed.

Section INLINING.

Variable prog: program.
Variable tprog: program.
Hypothesis TRANSF: match_prog prog tprog.
Let ge := Genv.globalenv prog.
Let tge := Genv.globalenv tprog.

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

Lemma senv_preserved:
  Senv.equiv ge tge.
Proof (Genv.senv_match TRANSF).

Lemma functions_translated:
  forall (v: val) (f: fundef),
  Genv.find_funct ge v = Some f ->
  exists cu f', Genv.find_funct tge v = Some f' /\ transf_fundef (funenv_program cu) f = OK f' /\ linkorder cu prog.
Proof (Genv.find_funct_match TRANSF).

Lemma function_ptr_translated:
  forall (b: block) (f: fundef),
  Genv.find_funct_ptr ge b = Some f ->
  exists cu f', Genv.find_funct_ptr tge b = Some f' /\ transf_fundef (funenv_program cu) f = OK f' /\ linkorder cu prog.
Proof (Genv.find_funct_ptr_match TRANSF).

Lemma sig_function_translated:
  forall cu f f', transf_fundef (funenv_program cu) f = OK f' -> funsig f' = funsig f.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct f; Errors.monadInv H.
(* Goal: @eq signature (funsig (@Internal function x)) (funsig (@Internal function f)) *)
(* Goal: @eq signature (funsig (@External function e)) (funsig (@External function e)) *)
  exploit transf_function_spec; eauto. intros SP; inv SP. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto.
Qed.

(** ** Properties of contexts and relocations *)

Remark sreg_below_diff:
  forall ctx r r', Plt r' ctx.(dreg) -> sreg ctx r <> r'.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. zify. unfold sreg; rewrite shiftpos_eq. xomega.
Qed.

Remark context_below_diff:
  forall ctx1 ctx2 r1 r2,
  context_below ctx1 ctx2 -> Ple r1 ctx1.(mreg) -> sreg ctx1 r1 <> sreg ctx2 r2.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. red in H. zify. unfold sreg; rewrite ! shiftpos_eq. xomega.
Qed.

Remark context_below_lt:
  forall ctx1 ctx2 r, context_below ctx1 ctx2 -> Ple r ctx1.(mreg) -> Plt (sreg ctx1 r) ctx2.(dreg).
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. red in H. unfold Plt; zify. unfold sreg; rewrite shiftpos_eq.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  xomega.
Qed.

(*
Remark context_below_le:
  forall ctx1 ctx2 r, context_below ctx1 ctx2 -> Ple r ctx1.(mreg) -> Ple (sreg ctx1 r) ctx2.(dreg).
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. red in H. unfold Ple; zify. unfold sreg; rewrite shiftpos_eq.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  xomega.
Qed.
*)

(** ** Agreement between register sets before and after inlining. *)

Definition agree_regs (F: meminj) (ctx: context) (rs rs': regset) :=
  (forall r, Ple r ctx.(mreg) -> Val.inject F rs#r rs'#(sreg ctx r))
/\(forall r, Plt ctx.(mreg) r -> rs#r = Vundef).

Definition val_reg_charact (F: meminj) (ctx: context) (rs': regset) (v: val) (r: reg) :=
  (Plt ctx.(mreg) r /\ v = Vundef) \/ (Ple r ctx.(mreg) /\ Val.inject F v rs'#(sreg ctx r)).

Remark Plt_Ple_dec:
(* Goal: @eq val (Regmap.get val (sreg ctx r) rs') (Regmap.get val r rs) *)
(* Goal: @ex program (fun cu : program => @ex fundef (fun fd' : fundef => and (@eq (option fundef) (@Genv.find_funct fundef unit tge (Regmap.get val (sreg ctx r) rs')) (@Some fundef fd')) (and (@eq (Errors.res fundef) (transf_fundef (funenv_program cu) fd) (@OK fundef fd')) (@linkorder program (@Linker_prog fundef unit (Linker_fundef function) Linker_unit) cu prog)))) *)
  forall p q, {Plt p q} + {Ple q p}.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct (plt p q). left; auto. right; xomega.
Qed.

Lemma agree_val_reg_gen:
  forall F ctx rs rs' r, agree_regs F ctx rs rs' -> val_reg_charact F ctx rs' rs#r r.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct H as [A B].
(* Goal: val_reg_charact F ctx rs' (Regmap.get val r rs) r *)
  destruct (Plt_Ple_dec (mreg ctx) r).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  left. rewrite B; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  right. auto.
Qed.

Lemma agree_val_regs_gen:
  forall F ctx rs rs' rl,
  agree_regs F ctx rs rs' -> list_forall2 (val_reg_charact F ctx rs') rs##rl rl.
Proof.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  induction rl; intros; constructor; auto. apply agree_val_reg_gen; auto.
Qed.

Lemma agree_val_reg:
  forall F ctx rs rs' r, agree_regs F ctx rs rs' -> Val.inject F rs#r rs'#(sreg ctx r).
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. exploit agree_val_reg_gen; eauto. instantiate (1 := r). intros [[A B] | [A B]].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite B; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto.
Qed.

Lemma agree_val_regs:
  forall F ctx rs rs' rl, agree_regs F ctx rs rs' -> Val.inject_list F rs##rl rs'##(sregs ctx rl).
Proof.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  induction rl; intros; simpl. constructor. constructor; auto. apply agree_val_reg; auto.
Qed.

Lemma agree_set_reg:
  forall F ctx rs rs' r v v',
  agree_regs F ctx rs rs' ->
  Val.inject F v v' ->
  Ple r ctx.(mreg) ->
  agree_regs F ctx (rs#r <- v) (rs'#(sreg ctx r) <- v').
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold agree_regs; intros. destruct H. split; intros.
(* Goal: Val.inject F (Regmap.get val r0 (Regmap.set val r Vundef rs)) (Regmap.get val (sreg ctx r0) (Regmap.set val (sreg ctx r) v' rs')) *)
(* Goal: @eq val (Regmap.get val r0 (Regmap.set val r Vundef rs)) Vundef *)
  repeat rewrite Regmap.gsspec.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct (peq r0 r). subst r0. rewrite peq_true. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite peq_false. auto. apply shiftpos_diff; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite Regmap.gso. auto. xomega.
Qed.

Lemma agree_set_reg_undef:
  forall F ctx rs rs' r v',
  agree_regs F ctx rs rs' ->
  agree_regs F ctx (rs#r <- Vundef) (rs'#(sreg ctx r) <- v').
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold agree_regs; intros. destruct H. split; intros.
(* Goal: Val.inject F (Regmap.get val r0 (Regmap.set val r Vundef rs)) (Regmap.get val (sreg ctx r0) (Regmap.set val (sreg ctx r) v' rs')) *)
(* Goal: @eq val (Regmap.get val r0 (Regmap.set val r Vundef rs)) Vundef *)
  repeat rewrite Regmap.gsspec.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct (peq r0 r). subst r0. rewrite peq_true. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite peq_false. auto. apply shiftpos_diff; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite Regmap.gsspec. destruct (peq r0 r); auto.
Qed.

Lemma agree_set_reg_undef':
  forall F ctx rs rs' r,
  agree_regs F ctx rs rs' ->
  agree_regs F ctx (rs#r <- Vundef) rs'.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold agree_regs; intros. destruct H. split; intros.
(* Goal: @eq val (Regmap.get val r0 (Regmap.set val r Vundef rs)) Vundef *)
  rewrite Regmap.gsspec.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct (peq r0 r). subst r0. auto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite Regmap.gsspec. destruct (peq r0 r); auto.
Qed.

Lemma agree_regs_invariant:
  forall F ctx rs rs1 rs2,
  agree_regs F ctx rs rs1 ->
  (forall r, Ple ctx.(dreg) r -> Plt r (ctx.(dreg) + ctx.(mreg)) -> rs2#r = rs1#r) ->
  agree_regs F ctx rs rs2.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold agree_regs; intros. destruct H. split; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite H0. auto.
  apply shiftpos_above.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  eapply Pos.lt_le_trans. apply shiftpos_below. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply H1; auto.
Qed.

Lemma agree_regs_incr:
  forall F ctx rs1 rs2 F',
  agree_regs F ctx rs1 rs2 ->
  inject_incr F F' ->
  agree_regs F' ctx rs1 rs2.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct H. split; intros. eauto. auto.
Qed.

Remark agree_regs_init:
  forall F ctx rs, agree_regs F ctx (Regmap.init Vundef) rs.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; split; intros. rewrite Regmap.gi; auto. rewrite Regmap.gi; auto.
Qed.

Lemma agree_regs_init_regs:
  forall F ctx rl vl vl',
  Val.inject_list F vl vl' ->
  (forall r, In r rl -> Ple r ctx.(mreg)) ->
  agree_regs F ctx (init_regs vl rl) (init_regs vl' (sregs ctx rl)).
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction rl; simpl; intros.
(* Goal: agree_regs F ctx (Regmap.init val Vundef) (Regmap.init val Vundef) *)
(* Goal: agree_regs F ctx (Regmap.set val a v (init_regs vl0 rl)) (Regmap.set val (sreg ctx a) v' (init_regs vl'0 (sregs ctx rl))) *)
  apply agree_regs_init.
(* Goal: agree_regs F ctx (Regmap.init val Vundef) (Regmap.init val Vundef) *)
(* Goal: agree_regs F ctx (Regmap.set val a v (init_regs vl0 rl)) (Regmap.set val (sreg ctx a) v' (init_regs vl'0 (sregs ctx rl))) *)
  inv H. apply agree_regs_init.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_set_reg; auto.
Qed.

(** ** Executing sequences of moves *)

Lemma tr_moves_init_regs:
  forall F stk f sp m ctx1 ctx2, context_below ctx1 ctx2 ->
  forall rdsts rsrcs vl pc1 pc2 rs1,
  tr_moves f.(fn_code) pc1 (sregs ctx1 rsrcs) (sregs ctx2 rdsts) pc2 ->
  (forall r, In r rdsts -> Ple r ctx2.(mreg)) ->
  list_forall2 (val_reg_charact F ctx1 rs1) vl rsrcs ->
  exists rs2,
    star step tge (State stk f sp pc1 rs1 m)
               E0 (State stk f sp pc2 rs2 m)
  /\ agree_regs F ctx2 (init_regs vl rdsts) rs2
  /\ forall r, Plt r ctx2.(dreg) -> rs2#r = rs1#r.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction rdsts; simpl; intros.
(* rdsts = nil *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  inv H0. exists rs1; split. apply star_refl. split. apply agree_regs_init. auto.
(* rdsts = a :: rdsts *)
(* Goal: final_state st2 r *)
  inv H2. inv H0.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exists rs1; split. apply star_refl. split. apply agree_regs_init. auto.
(* Goal: final_state st2 r *)
  simpl in H0. inv H0.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit IHrdsts; eauto. intros [rs2 [A [B C]]].
  exists (rs2#(sreg ctx2 a) <- (rs2#(sreg ctx1 b1))).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  split. eapply star_right. eauto. eapply exec_Iop; eauto. traceEq.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  split. destruct H3 as [[P Q] | [P Q]].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  subst a1. eapply agree_set_reg_undef; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply agree_set_reg; eauto. rewrite C; auto.  apply context_below_lt; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. rewrite Regmap.gso. auto. apply not_eq_sym. eapply sreg_below_diff; eauto.
  destruct H2; discriminate.
Qed.

(** ** Memory invariants *)

(** A stack location is private if it is not the image of a valid
   location and we have full rights on it. *)

Definition loc_private (F: meminj) (m m': mem) (sp: block) (ofs: Z) : Prop :=
  Mem.perm m' sp ofs Cur Freeable /\
  (forall b delta, F b = Some(sp, delta) -> ~Mem.perm m b (ofs - delta) Max Nonempty).

(** Likewise, for a range of locations. *)

Definition range_private (F: meminj) (m m': mem) (sp: block) (lo hi: Z) : Prop :=
  forall ofs, lo <= ofs < hi -> loc_private F m m' sp ofs.

Lemma range_private_invariant:
  forall F m m' sp lo hi F1 m1 m1',
  range_private F m m' sp lo hi ->
  (forall b delta ofs,
      F1 b = Some(sp, delta) ->
      Mem.perm m1 b ofs Max Nonempty ->
      lo <= ofs + delta < hi ->
      F b = Some(sp, delta) /\ Mem.perm m b ofs Max Nonempty) ->
  (forall ofs, Mem.perm m' sp ofs Cur Freeable -> Mem.perm m1' sp ofs Cur Freeable) ->
  range_private F1 m1 m1' sp lo hi.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. exploit H; eauto. intros [A B]. split; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. exploit H0; eauto. omega. intros [P Q].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eelim B; eauto.
Qed.

Lemma range_private_perms:
  forall F m m' sp lo hi,
  range_private F m m' sp lo hi ->
  Mem.range_perm m' sp lo hi Cur Freeable.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. eapply H; eauto.
Qed.

Lemma range_private_alloc_left:
  forall F m m' sp' base hi sz m1 sp F1,
  range_private F m m' sp' base hi ->
  Mem.alloc m 0 sz = (m1, sp) ->
  F1 sp = Some(sp', base) ->
  (forall b, b <> sp -> F1 b = F b) ->
  range_private F1 m1 m' sp' (base + Z.max sz 0) hi.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros.
(* Goal: forall _ : and (@eq (list stackframe) stk' (@nil stackframe)) (@eq (option (prod node reg)) (retinfo ctx) (@None (prod node reg))), final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  exploit (H ofs). generalize (Z.le_max_r sz 0). omega. intros [A B].
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  split; auto. intros; red; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.perm_alloc_inv; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  destruct (eq_block b sp); intros.
(* Goal: False *)
(* Goal: False *)
  subst b. rewrite H1 in H4; inv H4.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  rewrite Zmax_spec in H3. destruct (zlt 0 sz); omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite H2 in H4; auto. eelim B; eauto.
Qed.

Lemma range_private_free_left:
  forall F m m' sp base sz hi b m1,
  range_private F m m' sp (base + Z.max sz 0) hi ->
  Mem.free m b 0 sz = Some m1 ->
  F b = Some(sp, base) ->
  Mem.inject F m m' ->
  range_private F m1 m' sp base hi.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros.
  destruct (zlt ofs (base + Z.max sz 0)) as [z|z].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  red; split.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  replace ofs with ((ofs - base) + base) by omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.perm_inject; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_range_perm; eauto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  rewrite Zmax_spec in z. destruct (zlt 0 sz); omega.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. destruct (eq_block b b0).
(* Goal: @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b2 delta0)) *)
(* Goal: @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta0)) *)
(* Goal: forall (b1 b2 : block) (delta ofs : Z) (_ : @eq (option (prod block Z)) (F1 b1) (@Some (prod block Z) (@pair block Z b2 delta))) (_ : Plt b2 sp') (_ : Mem.perm m1 b1 ofs Max Nonempty), Mem.perm m b1 ofs Max Nonempty *)
(* Goal: match_stacks_inside F1 m1 m' (@cons stackframe (Stackframe res f (Vptr sp Ptrofs.zero) pc rs) stk) stk' f' ctx sp' rs' *)
  subst b0. rewrite H1 in H4; inv H4.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eelim Mem.perm_free_2; eauto. rewrite Zmax_spec in z. destruct (zlt 0 sz); omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.mi_no_overlap; eauto.
  apply Mem.perm_cur_max. apply Mem.perm_implies with Freeable; auto with mem.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_range_perm. eauto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  instantiate (1 := ofs - base). rewrite Zmax_spec in z. destruct (zlt 0 sz); omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.perm_free_3; eauto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  intros [A | A]. congruence. omega.

(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit (H ofs). omega. intros [A B]. split. auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. eelim B; eauto. eapply Mem.perm_free_3; eauto.
Qed.

Lemma range_private_extcall:
  forall F F' m1 m2 m1' m2' sp base hi,
  range_private F m1 m1' sp base hi ->
  (forall b ofs p,
     Mem.valid_block m1 b -> Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p) ->
  Mem.unchanged_on (loc_out_of_reach F m1) m1' m2' ->
  Mem.inject F m1 m1' ->
  inject_incr F F' ->
  inject_separated F F' m1 m1' ->
  Mem.valid_block m1' sp ->
  range_private F' m2 m2' sp base hi.
Proof.
(* Goal: forall (F F' : Val.meminj) (m1 m2 m1' m2' : Mem.mem) (sp : block) (base hi : Z) (_ : range_private F m1 m1' sp base hi) (_ : forall (b : block) (ofs : Z) (p : permission) (_ : Mem.valid_block m1 b) (_ : Mem.perm m2 b ofs Max p), Mem.perm m1 b ofs Max p) (_ : Mem.unchanged_on (loc_out_of_reach F m1) m1' m2') (_ : Mem.inject F m1 m1') (_ : inject_incr F F') (_ : inject_separated F F' m1 m1') (_ : Mem.valid_block m1' sp), range_private F' m2 m2' sp base hi *)
  intros until hi; intros RP PERM UNCH INJ INCR SEP VB.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. exploit RP; eauto. intros [A B].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  split. eapply Mem.perm_unchanged_on; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. red in SEP. destruct (F b) as [[sp1 delta1] |] eqn:?.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit INCR; eauto. intros EQ; rewrite H0 in EQ; inv EQ.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  red; intros; eelim B; eauto. eapply PERM; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  red. destruct (plt b (Mem.nextblock m1)); auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.mi_freeblocks; eauto. congruence.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit SEP; eauto. tauto.
Qed.

(** ** Relating global environments *)

Inductive match_globalenvs (F: meminj) (bound: block): Prop :=
  | mk_match_globalenvs
      (DOMAIN: forall b, Plt b bound -> F b = Some(b, 0))
      (IMAGE: forall b1 b2 delta, F b1 = Some(b2, delta) -> Plt b2 bound -> b1 = b2)
      (SYMBOLS: forall id b, Genv.find_symbol ge id = Some b -> Plt b bound)
      (FUNCTIONS: forall b fd, Genv.find_funct_ptr ge b = Some fd -> Plt b bound)
      (VARINFOS: forall b gv, Genv.find_var_info ge b = Some gv -> Plt b bound).

Lemma find_function_agree:
  forall ros rs fd F ctx rs' bound,
  find_function ge ros rs = Some fd ->
  agree_regs F ctx rs rs' ->
  match_globalenvs F bound ->
  exists cu fd',
  find_function tge (sros ctx ros) rs' = Some fd' /\ transf_fundef (funenv_program cu) fd = OK fd' /\ linkorder cu prog.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct ros as [r | id]; simpl in *.
- (* register *)
(* Goal: @ex program (fun cu : program => @ex fundef (fun fd' : fundef => and (@eq (option fundef) (@Genv.find_funct fundef unit tge (Regmap.get val (sreg ctx r) rs')) (@Some fundef fd')) (and (@eq (Errors.res fundef) (transf_fundef (funenv_program cu) fd) (@OK fundef fd')) (@linkorder program (@Linker_prog fundef unit (Linker_fundef function) Linker_unit) cu prog)))) *)
  assert (EQ: rs'#(sreg ctx r) = rs#r).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  { exploit Genv.find_funct_inv; eauto. intros [b EQ].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    assert (A: Val.inject F rs#r rs'#(sreg ctx r)). eapply agree_val_reg; eauto.
(* Goal: @eq val (Regmap.get val (sreg ctx r) rs') (Regmap.get val r rs) *)
    rewrite EQ in A; inv A.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    inv H1. rewrite DOMAIN in H5. inv H5. auto.
(* Goal: Plt b bound *)
    apply FUNCTIONS with fd.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite EQ in H; rewrite Genv.find_funct_find_funct_ptr in H. auto.
  }
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite EQ. eapply functions_translated; eauto.
- (* symbol *)
(* Goal: @ex program (fun cu : program => @ex fundef (fun fd' : fundef => and (@eq (option fundef) match @Genv.find_symbol fundef unit tge id with | Some b => @Genv.find_funct_ptr fundef unit tge b | None => @None fundef end (@Some fundef fd')) (and (@eq (Errors.res fundef) (transf_fundef (funenv_program cu) fd) (@OK fundef fd')) (@linkorder program (@Linker_prog fundef unit (Linker_fundef function) Linker_unit) cu prog)))) *)
  rewrite symbols_preserved. destruct (Genv.find_symbol ge id); try discriminate.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply function_ptr_translated; eauto.
Qed.

Lemma find_inlined_function:
  forall fenv id rs fd f,
  fenv_compat prog fenv ->
  find_function ge (inr id) rs = Some fd ->
  fenv!id = Some f ->
  fd = Internal f.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros.
(* Goal: @eq fundef fd (@Internal function f) *)
  apply H in H1. apply Genv.find_def_symbol in H1. destruct H1 as (b & A & B).
(* Goal: @eq fundef fd (@Internal function f) *)
  simpl in H0. unfold ge, fundef in H0. rewrite A in H0.
  rewrite <- Genv.find_funct_ptr_iff in B.
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  congruence.
Qed.

(** Translation of builtin arguments. *)

Lemma tr_builtin_arg:
  forall F bound ctx rs rs' sp sp' m m',
  match_globalenvs F bound ->
  agree_regs F ctx rs rs' ->
  F sp = Some(sp', ctx.(dstk)) ->
  Mem.inject F m m' ->
  forall a v,
  eval_builtin_arg ge (fun r => rs#r) (Vptr sp Ptrofs.zero) m a v ->
  exists v', eval_builtin_arg tge (fun r => rs'#r) (Vptr sp' Ptrofs.zero) m' (sbuiltinarg ctx a) v'
          /\ Val.inject F v v'.
Proof.
  intros until m'; intros MG AG SP MI. induction 1; simpl.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
- exists rs'#(sreg ctx x); split. constructor. eapply agree_val_reg; eauto.
- econstructor; eauto with barg.
- econstructor; eauto with barg.
- econstructor; eauto with barg.
- econstructor; eauto with barg.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
- exploit Mem.loadv_inject; eauto.
  instantiate (1 := Vptr sp' (Ptrofs.add ofs (Ptrofs.repr (dstk ctx)))).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  simpl. econstructor; eauto. rewrite Ptrofs.add_zero_l; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  intros (v' & A & B). exists v'; split; auto. constructor. simpl. rewrite Ptrofs.add_zero_l; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
- econstructor; split. constructor. simpl. econstructor; eauto. rewrite ! Ptrofs.add_zero_l; auto.
- assert (Val.inject F (Senv.symbol_address ge id ofs) (Senv.symbol_address tge id ofs)).
(* Goal: @eq val (Regmap.get val (sreg ctx r) rs') (Regmap.get val r rs) *)
(* Goal: @ex program (fun cu : program => @ex fundef (fun fd' : fundef => and (@eq (option fundef) (@Genv.find_funct fundef unit tge (Regmap.get val (sreg ctx r) rs')) (@Some fundef fd')) (and (@eq (Errors.res fundef) (transf_fundef (funenv_program cu) fd) (@OK fundef fd')) (@linkorder program (@Linker_prog fundef unit (Linker_fundef function) Linker_unit) cu prog)))) *)
  { unfold Senv.symbol_address; simpl; unfold Genv.symbol_address.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite symbols_preserved. destruct (Genv.find_symbol ge id) as [b|] eqn:FS; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    inv MG. econstructor. eauto. rewrite Ptrofs.add_zero; auto. }
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.loadv_inject; eauto. intros (v' & A & B).
  exists v'; eauto with barg.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) m') r *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
- econstructor; split. constructor.
  unfold Senv.symbol_address; simpl; unfold Genv.symbol_address.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite symbols_preserved. destruct (Genv.find_symbol ge id) as [b|] eqn:FS; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  inv MG. econstructor. eauto. rewrite Ptrofs.add_zero; auto.
- destruct IHeval_builtin_arg1 as (v1' & A1 & B1).
  destruct IHeval_builtin_arg2 as (v2' & A2 & B2).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; split. eauto with barg. apply Val.longofwords_inject; auto.
- destruct IHeval_builtin_arg1 as (v1' & A1 & B1).
  destruct IHeval_builtin_arg2 as (v2' & A2 & B2).
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  econstructor; split. eauto with barg.
  destruct Archi.ptr64; auto using Val.add_inject, Val.addl_inject.
Qed.

Lemma tr_builtin_args:
  forall F bound ctx rs rs' sp sp' m m',
  match_globalenvs F bound ->
  agree_regs F ctx rs rs' ->
  F sp = Some(sp', ctx.(dstk)) ->
  Mem.inject F m m' ->
  forall al vl,
  eval_builtin_args ge (fun r => rs#r) (Vptr sp Ptrofs.zero) m al vl ->
  exists vl', eval_builtin_args tge (fun r => rs'#r) (Vptr sp' Ptrofs.zero) m' (map (sbuiltinarg ctx) al) vl'
          /\ Val.inject_list F vl vl'.
Proof.
  induction 5; simpl.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) m') r *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
- exists (@nil val); split; constructor.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
- exploit tr_builtin_arg; eauto. intros (v1' & A & B).
(* Goal: @ex (list val) (fun vl' : list val => and (@eval_builtin_args positive (@Genv.to_senv fundef unit tge) (fun r : positive => Regmap.get val r rs') (Vptr sp' Ptrofs.zero) m' (@cons (builtin_arg reg) (sbuiltinarg ctx a1) (@map (builtin_arg reg) (builtin_arg reg) (sbuiltinarg ctx) al)) vl') (Val.inject_list F (@cons val b1 bl) vl')) *)
  destruct IHlist_forall2 as (vl' & C & D).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exists (v1' :: vl'); split; constructor; auto.
Qed.

(** ** Relating stacks *)

Inductive match_stacks (F: meminj) (m m': mem):
             list stackframe -> list stackframe -> block -> Prop :=
  | match_stacks_nil: forall bound1 bound
        (MG: match_globalenvs F bound1)
        (BELOW: Ple bound1 bound),
      match_stacks F m m' nil nil bound
  | match_stacks_cons: forall res f sp pc rs stk f' sp' rs' stk' bound fenv ctx
        (MS: match_stacks_inside F m m' stk stk' f' ctx sp' rs')
        (COMPAT: fenv_compat prog fenv)
        (FB: tr_funbody fenv f'.(fn_stacksize) ctx f f'.(fn_code))
        (AG: agree_regs F ctx rs rs')
        (SP: F sp = Some(sp', ctx.(dstk)))
        (PRIV: range_private F m m' sp' (ctx.(dstk) + ctx.(mstk)) f'.(fn_stacksize))
        (SSZ1: 0 <= f'.(fn_stacksize) < Ptrofs.max_unsigned)
        (SSZ2: forall ofs, Mem.perm m' sp' ofs Max Nonempty -> 0 <= ofs <= f'.(fn_stacksize))
        (RES: Ple res ctx.(mreg))
        (BELOW: Plt sp' bound),
      match_stacks F m m'
                   (Stackframe res f (Vptr sp Ptrofs.zero) pc rs :: stk)
                   (Stackframe (sreg ctx res) f' (Vptr sp' Ptrofs.zero) (spc ctx pc) rs' :: stk')
                   bound
  | match_stacks_untailcall: forall stk res f' sp' rpc rs' stk' bound ctx
        (MS: match_stacks_inside F m m' stk stk' f' ctx sp' rs')
        (PRIV: range_private F m m' sp' ctx.(dstk) f'.(fn_stacksize))
        (SSZ1: 0 <= f'.(fn_stacksize) < Ptrofs.max_unsigned)
        (SSZ2: forall ofs, Mem.perm m' sp' ofs Max Nonempty -> 0 <= ofs <= f'.(fn_stacksize))
        (RET: ctx.(retinfo) = Some (rpc, res))
        (BELOW: Plt sp' bound),
      match_stacks F m m'
                   stk
                   (Stackframe res f' (Vptr sp' Ptrofs.zero) rpc rs' :: stk')
                   bound

with match_stacks_inside (F: meminj) (m m': mem):
        list stackframe -> list stackframe -> function -> context -> block -> regset -> Prop :=
  | match_stacks_inside_base: forall stk stk' f' ctx sp' rs'
        (MS: match_stacks F m m' stk stk' sp')
        (RET: ctx.(retinfo) = None)
        (DSTK: ctx.(dstk) = 0),
      match_stacks_inside F m m' stk stk' f' ctx sp' rs'
  | match_stacks_inside_inlined: forall res f sp pc rs stk stk' f' fenv ctx sp' rs' ctx'
        (MS: match_stacks_inside F m m' stk stk' f' ctx' sp' rs')
        (COMPAT: fenv_compat prog fenv)
        (FB: tr_funbody fenv f'.(fn_stacksize) ctx' f f'.(fn_code))
        (AG: agree_regs F ctx' rs rs')
        (SP: F sp = Some(sp', ctx'.(dstk)))
        (PAD: range_private F m m' sp' (ctx'.(dstk) + ctx'.(mstk)) ctx.(dstk))
        (RES: Ple res ctx'.(mreg))
        (RET: ctx.(retinfo) = Some (spc ctx' pc, sreg ctx' res))
        (BELOW: context_below ctx' ctx)
        (SBELOW: context_stack_call ctx' ctx),
      match_stacks_inside F m m' (Stackframe res f (Vptr sp Ptrofs.zero) pc rs :: stk)
                                 stk' f' ctx sp' rs'.

(** Properties of match_stacks *)

Section MATCH_STACKS.

Variable F: meminj.
Variables m m': mem.

Lemma match_stacks_globalenvs:
  forall stk stk' bound,
  match_stacks F m m' stk stk' bound -> exists b, match_globalenvs F b
with match_stacks_inside_globalenvs:
  forall stk stk' f ctx sp rs',
  match_stacks_inside F m m' stk stk' f ctx sp rs' -> exists b, match_globalenvs F b.
Proof.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  induction 1; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  induction 1; eauto.
Qed.

Lemma match_globalenvs_preserves_globals:
  forall b, match_globalenvs F b -> meminj_preserves_globals ge F.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H. red. split. eauto. split. eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. symmetry. eapply IMAGE; eauto.
Qed.

Lemma match_stacks_inside_globals:
  forall stk stk' f ctx sp rs',
  match_stacks_inside F m m' stk stk' f ctx sp rs' -> meminj_preserves_globals ge F.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. exploit match_stacks_inside_globalenvs; eauto. intros [b A].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_globalenvs_preserves_globals; eauto.
Qed.

Lemma match_stacks_bound:
  forall stk stk' bound bound1,
  match_stacks F m m' stk stk' bound ->
  Ple bound bound1 ->
  match_stacks F m m' stk stk' bound1.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_nil with bound0. auto. eapply Ple_trans; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_cons; eauto. eapply Pos.lt_le_trans; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_untailcall; eauto. eapply Pos.lt_le_trans; eauto.
Qed.

Variable F1: meminj.
Variables m1 m1': mem.
Hypothesis INCR: inject_incr F F1.

Lemma match_stacks_invariant:
  forall stk stk' bound, match_stacks F m m' stk stk' bound ->
  forall (INJ: forall b1 b2 delta,
               F1 b1 = Some(b2, delta) -> Plt b2 bound -> F b1 = Some(b2, delta))
         (PERM1: forall b1 b2 delta ofs,
               F1 b1 = Some(b2, delta) -> Plt b2 bound ->
               Mem.perm m1 b1 ofs Max Nonempty -> Mem.perm m b1 ofs Max Nonempty)
         (PERM2: forall b ofs, Plt b bound ->
               Mem.perm m' b ofs Cur Freeable -> Mem.perm m1' b ofs Cur Freeable)
         (PERM3: forall b ofs k p, Plt b bound ->
               Mem.perm m1' b ofs k p -> Mem.perm m' b ofs k p),
  match_stacks F1 m1 m1' stk stk' bound

with match_stacks_inside_invariant:
  forall stk stk' f' ctx sp' rs1,
  match_stacks_inside F m m' stk stk' f' ctx sp' rs1 ->
  forall rs2
         (RS: forall r, Plt r ctx.(dreg) -> rs2#r = rs1#r)
         (INJ: forall b1 b2 delta,
               F1 b1 = Some(b2, delta) -> Ple b2 sp' -> F b1 = Some(b2, delta))
         (PERM1: forall b1 b2 delta ofs,
               F1 b1 = Some(b2, delta) -> Ple b2 sp' ->
               Mem.perm m1 b1 ofs Max Nonempty -> Mem.perm m b1 ofs Max Nonempty)
         (PERM2: forall b ofs, Ple b sp' ->
               Mem.perm m' b ofs Cur Freeable -> Mem.perm m1' b ofs Cur Freeable)
         (PERM3: forall b ofs k p, Ple b sp' ->
               Mem.perm m1' b ofs k p -> Mem.perm m' b ofs k p),
  match_stacks_inside F1 m1 m1' stk stk' f' ctx sp' rs2.

Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
  (* nil *)
  apply match_stacks_nil with (bound1 := bound1).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  inv MG. constructor; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. apply IMAGE with delta. eapply INJ; eauto. eapply Pos.lt_le_trans; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto.
  (* cons *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_cons with (fenv := fenv) (ctx := ctx); auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_invariant; eauto.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply INJ; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM1; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM2; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM3; eauto; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply agree_regs_incr; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply range_private_invariant; eauto.
  (* untailcall *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_untailcall with (ctx := ctx); auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_invariant; eauto.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply INJ; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM1; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM2; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM3; eauto; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply range_private_invariant; eauto.

(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
  (* base *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_base; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_invariant; eauto.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply INJ; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM1; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM2; eauto; xomega.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  intros; eapply PERM3; eauto; xomega.
  (* inlined *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_inside_inlined with (fenv := fenv) (ctx' := ctx'); auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply IHmatch_stacks_inside; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. apply RS. red in BELOW. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_regs_incr with F; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_regs_invariant with rs'; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. apply RS. red in BELOW. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply range_private_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. split. eapply INJ; eauto. xomega. eapply PERM1; eauto. xomega.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply PERM2; eauto. xomega.
Qed.

Lemma match_stacks_empty:
  forall stk stk' bound,
  match_stacks F m m' stk stk' bound -> stk = nil -> stk' = nil
with match_stacks_inside_empty:
  forall stk stk' f ctx sp rs,
  match_stacks_inside F m m' stk stk' f ctx sp rs -> stk = nil -> stk' = nil /\ ctx.(retinfo) = None.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto.
  discriminate.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_inside_empty; eauto. intros [A B]. congruence.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  split. eapply match_stacks_empty; eauto. auto.
  discriminate.
Qed.

End MATCH_STACKS.

(** Preservation by assignment to a register *)

Lemma match_stacks_inside_set_reg:
  forall F m m' stk stk' f' ctx sp' rs' r v,
  match_stacks_inside F m m' stk stk' f' ctx sp' rs' ->
  match_stacks_inside F m m' stk stk' f' ctx sp' (rs'#(sreg ctx r) <- v).
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply match_stacks_inside_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. apply Regmap.gso. zify. unfold sreg; rewrite shiftpos_eq. xomega.
Qed.

Lemma match_stacks_inside_set_res:
  forall F m m' stk stk' f' ctx sp' rs' res v,
  match_stacks_inside F m m' stk stk' f' ctx sp' rs' ->
  match_stacks_inside F m m' stk stk' f' ctx sp' (regmap_setres (sbuiltinres ctx res) v rs').
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct res; simpl; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_inside_set_reg; auto.
Qed.

(** Preservation by a memory store *)

Lemma match_stacks_inside_store:
  forall F m m' stk stk' f' ctx sp' rs' chunk b ofs v m1 chunk' b' ofs' v' m1',
  match_stacks_inside F m m' stk stk' f' ctx sp' rs' ->
  Mem.store chunk m b ofs v = Some m1 ->
  Mem.store chunk' m' b' ofs' v' = Some m1' ->
  match_stacks_inside F m1 m1' stk stk' f' ctx sp' rs'.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros.
(* Goal: match_stacks_inside F m1 m1' stk stk' f' ctx sp' rs' *)
  eapply match_stacks_inside_invariant; eauto with mem.
Qed.

(** Preservation by an allocation *)

Lemma match_stacks_inside_alloc_left:
  forall F m m' stk stk' f' ctx sp' rs',
  match_stacks_inside F m m' stk stk' f' ctx sp' rs' ->
  forall sz m1 b F1 delta,
  Mem.alloc m 0 sz = (m1, b) ->
  inject_incr F F1 ->
  F1 b = Some(sp', delta) ->
  (forall b1, b1 <> b -> F1 b1 = F b1) ->
  delta >= ctx.(dstk) ->
  match_stacks_inside F1 m1 m' stk stk' f' ctx sp' rs'.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
  (* base *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_base; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct (eq_block b1 b).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  subst b1. rewrite H1 in H4; inv H4. eelim Plt_strict; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite H2 in H4; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. exploit Mem.perm_alloc_inv; eauto. destruct (eq_block b1 b); intros; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  subst b1. rewrite H1 in H4. inv H4. eelim Plt_strict; eauto.
  (* inlined *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_inlined; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply IHmatch_stacks_inside; eauto. destruct SBELOW. omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply agree_regs_incr; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply range_private_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. exploit Mem.perm_alloc_inv; eauto. destruct (eq_block b0 b); intros.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  subst b0. rewrite H2 in H5; inv H5. elimtype False; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite H3 in H5; auto.
Qed.

(** Preservation by freeing *)

Lemma match_stacks_free_left:
  forall F m m' stk stk' sp b lo hi m1,
  match_stacks F m m' stk stk' sp ->
  Mem.free m b lo hi = Some m1 ->
  match_stacks F m1 m' stk stk' sp.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply match_stacks_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply Mem.perm_free_3; eauto.
Qed.

Lemma match_stacks_free_right:
  forall F m m' stk stk' sp lo hi m1',
  match_stacks F m m' stk stk' sp ->
  Mem.free m' sp lo hi = Some m1' ->
  match_stacks F m m1' stk stk' sp.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply match_stacks_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply Mem.perm_free_1; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply Mem.perm_free_3; eauto.
Qed.

Lemma min_alignment_sound:
  forall sz n, (min_alignment sz | n) -> Mem.inj_offset_aligned n sz.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. unfold min_alignment in H.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  assert (2 <= sz -> (2 | n)). intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 1). omegaContradiction.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 2). auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 4). apply Zdivides_trans with 4; auto. exists 2; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    apply Zdivides_trans with 8; auto. exists 4; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  assert (4 <= sz -> (4 | n)). intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 1). omegaContradiction.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 2). omegaContradiction.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 4). auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    apply Zdivides_trans with 8; auto. exists 2; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  assert (8 <= sz -> (8 | n)). intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 1). omegaContradiction.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 2). omegaContradiction.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (zle sz 4). omegaContradiction.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct chunk; simpl in *; auto.
(* Goal: Z.divide (Zpos xH) n *)
(* Goal: Z.divide (Zpos (xO (xO xH))) n *)
(* Goal: Z.divide (Zpos (xO (xO xH))) n *)
  apply Z.divide_1_l.
(* Goal: Z.divide (Zpos xH) n *)
(* Goal: Z.divide (Zpos (xO (xO xH))) n *)
(* Goal: Z.divide (Zpos (xO (xO xH))) n *)
  apply Z.divide_1_l.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  apply H2; omega.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  apply H2; omega.
Qed.

(** Preservation by external calls *)

Section EXTCALL.

Variables F1 F2: meminj.
Variables m1 m2 m1' m2': mem.
Hypothesis MAXPERM: forall b ofs p, Mem.valid_block m1 b -> Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p.
Hypothesis MAXPERM': forall b ofs p, Mem.valid_block m1' b -> Mem.perm m2' b ofs Max p -> Mem.perm m1' b ofs Max p.
Hypothesis UNCHANGED: Mem.unchanged_on (loc_out_of_reach F1 m1) m1' m2'.
Hypothesis INJ: Mem.inject F1 m1 m1'.
Hypothesis INCR: inject_incr F1 F2.
Hypothesis SEP: inject_separated F1 F2 m1 m1'.

Lemma match_stacks_extcall:
  forall stk stk' bound,
  match_stacks F1 m1 m1' stk stk' bound ->
  Ple bound (Mem.nextblock m1') ->
  match_stacks F2 m2 m2' stk stk' bound
with match_stacks_inside_extcall:
  forall stk stk' f' ctx sp' rs',
  match_stacks_inside F1 m1 m1' stk stk' f' ctx sp' rs' ->
  Plt sp' (Mem.nextblock m1') ->
  match_stacks_inside F2 m2 m2' stk stk' f' ctx sp' rs'.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_nil with bound1; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    inv MG. constructor; intros; eauto.
    destruct (F1 b1) as [[b2' delta']|] eqn:?.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    exploit INCR; eauto. intros EQ; rewrite H0 in EQ; inv EQ. eapply IMAGE; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    exploit SEP; eauto. intros [A B]. elim B. red. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_cons; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_extcall; eauto. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply agree_regs_incr; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply range_private_extcall; eauto. red; xomega.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. apply SSZ2; auto. apply MAXPERM'; auto. red; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_untailcall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_extcall; eauto. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply range_private_extcall; eauto. red; xomega.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. apply SSZ2; auto. apply MAXPERM'; auto. red; xomega.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_base; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_extcall; eauto. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_inlined; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply agree_regs_incr; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply range_private_extcall; eauto.
Qed.

End EXTCALL.

(** Change of context corresponding to an inlined tailcall *)

Lemma align_unchanged:
  forall n amount, amount > 0 -> (amount | n) -> align n amount = n.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct H0 as [p EQ]. subst n. unfold align. decEq.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  apply Zdiv_unique with (b := amount - 1). omega. omega.
Qed.

Lemma match_stacks_inside_inlined_tailcall:
  forall fenv F m m' stk stk' f' ctx sp' rs' ctx' f,
  match_stacks_inside F m m' stk stk' f' ctx sp' rs' ->
  context_below ctx ctx' ->
  context_stack_tailcall ctx f ctx' ->
  ctx'.(retinfo) = ctx.(retinfo) ->
  range_private F m m' sp' ctx.(dstk) f'.(fn_stacksize) ->
  tr_funbody fenv f'.(fn_stacksize) ctx' f f'.(fn_code) ->
  match_stacks_inside F m m' stk stk' f' ctx' sp' rs'.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H.
  (* base *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_base; eauto. congruence.
(* Goal: Z.le (dstk ctx) (dstk ctx') *)
(* Goal: match_stacks_inside F m m' (@cons stackframe (Stackframe res f0 (Vptr sp Ptrofs.zero) pc rs) stk0) stk' f' ctx' sp' rs' *)
  rewrite H1. rewrite DSTK. apply align_unchanged. apply min_alignment_pos. apply Z.divide_0_r.
  (* inlined *)
(* Goal: Z.le (dstk ctx) (dstk ctx') *)
(* Goal: match_stacks_inside F m m' (@cons stackframe (Stackframe res f0 (Vptr sp Ptrofs.zero) pc rs) stk0) stk' f' ctx' sp' rs' *)
  assert (dstk ctx <= dstk ctx'). rewrite H1. apply align_le. apply min_alignment_pos.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_inlined; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. destruct (zlt ofs (dstk ctx)). apply PAD; omega. apply H3. inv H4. xomega.
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  congruence.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  unfold context_below in *. xomega.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  unfold context_stack_call in *. omega.
Qed.

(** ** Relating states *)

Inductive match_states: RTL.state -> RTL.state -> Prop :=
  | match_regular_states: forall stk f sp pc rs m stk' f' sp' rs' m' F fenv ctx
        (MS: match_stacks_inside F m m' stk stk' f' ctx sp' rs')
        (COMPAT: fenv_compat prog fenv)
        (FB: tr_funbody fenv f'.(fn_stacksize) ctx f f'.(fn_code))
        (AG: agree_regs F ctx rs rs')
        (SP: F sp = Some(sp', ctx.(dstk)))
        (MINJ: Mem.inject F m m')
        (VB: Mem.valid_block m' sp')
        (PRIV: range_private F m m' sp' (ctx.(dstk) + ctx.(mstk)) f'.(fn_stacksize))
        (SSZ1: 0 <= f'.(fn_stacksize) < Ptrofs.max_unsigned)
        (SSZ2: forall ofs, Mem.perm m' sp' ofs Max Nonempty -> 0 <= ofs <= f'.(fn_stacksize)),
      match_states (State stk f (Vptr sp Ptrofs.zero) pc rs m)
                   (State stk' f' (Vptr sp' Ptrofs.zero) (spc ctx pc) rs' m')
  | match_call_states: forall stk fd args m stk' fd' args' m' cunit F
        (MS: match_stacks F m m' stk stk' (Mem.nextblock m'))
        (LINK: linkorder cunit prog)
        (FD: transf_fundef (funenv_program cunit) fd = OK fd')
        (VINJ: Val.inject_list F args args')
        (MINJ: Mem.inject F m m'),
      match_states (Callstate stk fd args m)
                   (Callstate stk' fd' args' m')
  | match_call_regular_states: forall stk f vargs m stk' f' sp' rs' m' F fenv ctx ctx' pc' pc1' rargs
        (MS: match_stacks_inside F m m' stk stk' f' ctx sp' rs')
        (COMPAT: fenv_compat prog fenv)
        (FB: tr_funbody fenv f'.(fn_stacksize) ctx f f'.(fn_code))
        (BELOW: context_below ctx' ctx)
        (NOP: f'.(fn_code)!pc' = Some(Inop pc1'))
        (MOVES: tr_moves f'.(fn_code) pc1' (sregs ctx' rargs) (sregs ctx f.(fn_params)) (spc ctx f.(fn_entrypoint)))
        (VINJ: list_forall2 (val_reg_charact F ctx' rs') vargs rargs)
        (MINJ: Mem.inject F m m')
        (VB: Mem.valid_block m' sp')
        (PRIV: range_private F m m' sp' ctx.(dstk) f'.(fn_stacksize))
        (SSZ1: 0 <= f'.(fn_stacksize) < Ptrofs.max_unsigned)
        (SSZ2: forall ofs, Mem.perm m' sp' ofs Max Nonempty -> 0 <= ofs <= f'.(fn_stacksize)),
      match_states (Callstate stk (Internal f) vargs m)
                   (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m')
  | match_return_states: forall stk v m stk' v' m' F
        (MS: match_stacks F m m' stk stk' (Mem.nextblock m'))
        (VINJ: Val.inject F v v')
        (MINJ: Mem.inject F m m'),
      match_states (Returnstate stk v m)
                   (Returnstate stk' v' m')
  | match_return_regular_states: forall stk v m stk' f' sp' rs' m' F ctx pc' or rinfo
        (MS: match_stacks_inside F m m' stk stk' f' ctx sp' rs')
        (RET: ctx.(retinfo) = Some rinfo)
        (AT: f'.(fn_code)!pc' = Some(inline_return ctx or rinfo))
        (VINJ: match or with None => v = Vundef | Some r => Val.inject F v rs'#(sreg ctx r) end)
        (MINJ: Mem.inject F m m')
        (VB: Mem.valid_block m' sp')
        (PRIV: range_private F m m' sp' ctx.(dstk) f'.(fn_stacksize))
        (SSZ1: 0 <= f'.(fn_stacksize) < Ptrofs.max_unsigned)
        (SSZ2: forall ofs, Mem.perm m' sp' ofs Max Nonempty -> 0 <= ofs <= f'.(fn_stacksize)),
      match_states (Returnstate stk v m)
                   (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m').

(** ** Forward simulation *)

Definition measure (S: RTL.state) : nat :=
  match S with
  | State _ _ _ _ _ _ => 1%nat
  | Callstate _ _ _ _ => 0%nat
  | Returnstate _ _ _ => 0%nat
  end.

Lemma tr_funbody_inv:
  forall fenv sz cts f c pc i,
  tr_funbody fenv sz cts f c -> f.(fn_code)!pc = Some i -> tr_instr fenv sz cts pc i c.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H. eauto.
Qed.

Theorem step_simulation:
  forall S1 t S2,
  step ge S1 t S2 ->
  forall S1' (MS: match_states S1 S1'),
  (exists S2', plus step tge S1' t S2' /\ match_states S2 S2')
  \/ (measure S2 < measure S1 /\ t = E0 /\ match_states S2 S1')%nat.
Proof.
  induction 1; intros; inv MS.

- (* nop *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Inop; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.

- (* op *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
  exploit eval_operation_inject.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_globals; eauto.
    eexact SP.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    instantiate (2 := rs##args). instantiate (1 := rs'##(sregs ctx args)). eapply agree_val_regs; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eexact MINJ. eauto.
  fold (sop ctx op). intros [v' [A B]].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Iop; eauto. erewrite eval_operation_preserved; eauto.
  exact symbols_preserved.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_inside_set_reg; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_set_reg; auto.

- (* load *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
  exploit eval_addressing_inject.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_globals; eauto.
    eexact SP.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    instantiate (2 := rs##args). instantiate (1 := rs'##(sregs ctx args)). eapply agree_val_regs; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eauto.
  fold (saddr ctx addr). intros [a' [P Q]].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.loadv_inject; eauto. intros [v' [U V]].
  assert (eval_addressing tge (Vptr sp' Ptrofs.zero) (saddr ctx addr) rs' ## (sregs ctx args) = Some a').
  rewrite <- P. apply eval_addressing_preserved. exact symbols_preserved.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Iload; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_inside_set_reg; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_set_reg; auto.

- (* store *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
  exploit eval_addressing_inject.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_globals; eauto.
    eexact SP.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    instantiate (2 := rs##args). instantiate (1 := rs'##(sregs ctx args)). eapply agree_val_regs; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eauto.
  fold saddr. intros [a' [P Q]].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.storev_mapped_inject; eauto. eapply agree_val_reg; eauto.
  intros [m1' [U V]].
  assert (eval_addressing tge (Vptr sp' Ptrofs.zero) (saddr ctx addr) rs' ## (sregs ctx args) = Some a').
    rewrite <- P. apply eval_addressing_preserved. exact symbols_preserved.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Istore; eauto.
  destruct a; simpl in H1; try discriminate.
  destruct a'; simpl in U; try discriminate.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_store; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.store_valid_block_1; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply range_private_invariant; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  intros; split; auto. eapply Mem.perm_store_2; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  intros; eapply Mem.perm_store_1; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply SSZ2. eapply Mem.perm_store_2; eauto.

- (* call *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_inside_globalenvs; eauto. intros [bound G].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit find_function_agree; eauto. intros (cu & fd' & A & B & C).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
+ (* not inlined *)
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Icall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply sig_function_translated; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_cons; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply agree_val_regs; eauto.
+ (* inlined *)
  assert (EQ: fd = Internal f0) by (eapply find_inlined_function; eauto).
  subst fd.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  right; split. simpl; omega. split. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_inlined; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. apply PRIV. inv H13. destruct H16. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_val_regs_gen; auto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  red; intros; apply PRIV. destruct H16. omega.

- (* tailcall *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_inside_globalenvs; eauto. intros [bound G].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit find_function_agree; eauto. intros (cu & fd' & A & B & C).
  assert (PRIV': range_private F m' m'0 sp' (dstk ctx) f'.(fn_stacksize)).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  { eapply range_private_free_left; eauto. inv FB. rewrite <- H4. auto. }
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
+ (* within the original function *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  inv MS0; try congruence.
(* Goal: @eq val (Regmap.get val (sreg ctx r) rs') (Regmap.get val r rs) *)
(* Goal: @ex program (fun cu : program => @ex fundef (fun fd' : fundef => and (@eq (option fundef) (@Genv.find_funct fundef unit tge (Regmap.get val (sreg ctx r) rs')) (@Some fundef fd')) (and (@eq (Errors.res fundef) (transf_fundef (funenv_program cu) fd) (@OK fundef fd')) (@linkorder program (@Linker_prog fundef unit (Linker_fundef function) Linker_unit) cu prog)))) *)
  assert (X: { m1' | Mem.free m'0 sp' 0 (fn_stacksize f') = Some m1'}).
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    apply Mem.range_perm_free. red; intros.
    destruct (zlt ofs f.(fn_stacksize)).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    replace ofs with (ofs + dstk ctx) by omega. eapply Mem.perm_inject; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply Mem.free_range_perm; eauto. omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    inv FB. eapply range_private_perms; eauto. xomega.
  destruct X as [m1' FREE].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Itailcall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply sig_function_translated; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
  eapply match_stacks_bound with (bound := sp').
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_1; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  erewrite Mem.nextblock_free; eauto. red in VB; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply agree_val_regs; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_right_inject; eauto. eapply Mem.free_left_inject; eauto.
  (* show that no valid location points into the stack block being freed *)
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. rewrite DSTK in PRIV'. exploit (PRIV' (ofs + delta)). omega. intros [P Q].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eelim Q; eauto. replace (ofs + delta - delta) with ofs by omega.
  apply Mem.perm_max with k. apply Mem.perm_implies with p; auto with mem.
+ (* turned into a call *)
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Icall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply sig_function_translated; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_untailcall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply agree_val_regs; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_left_inject; eauto.
+ (* inlined *)
  assert (EQ: fd = Internal f0) by (eapply find_inlined_function; eauto).
  subst fd.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  right; split. simpl; omega. split. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_inlined_tailcall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_val_regs_gen; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_left_inject; eauto.
  red; intros; apply PRIV'.
(* Goal: Z.gt (min_alignment (fn_stacksize f)) Z0 *)
(* Goal: match_stacks_inside F m m' (@cons stackframe (Stackframe res f0 (Vptr sp Ptrofs.zero) pc rs) stk0) stk' f' ctx' sp' rs' *)
    assert (dstk ctx <= dstk ctx'). red in H14; rewrite H14. apply align_le. apply min_alignment_pos.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
    omega.

- (* builtin *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_inside_globalenvs; eauto. intros [bound MG].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_builtin_args; eauto. intros (vargs' & P & Q).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit external_call_mem_inject; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_globals; eauto.
  intros [F1 [v1 [m1' [A [B [C [D [E [J K]]]]]]]]].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Ibuiltin; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) m') r *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  econstructor.
    eapply match_stacks_inside_set_res.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_inside_extcall with (F1 := F) (F2 := F1) (m1 := m) (m1' := m'0); eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    intros; eapply external_call_max_perm; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    intros; eapply external_call_max_perm; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. eauto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct res; simpl; [apply agree_set_reg;auto|idtac|idtac]; eapply agree_regs_incr; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply external_call_valid_block; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply range_private_extcall; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    intros; eapply external_call_max_perm; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. apply SSZ2. eapply external_call_max_perm; eauto.

- (* cond *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
  assert (eval_condition cond rs'##(sregs ctx args) m' = Some b).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply eval_condition_inject; eauto. eapply agree_val_regs; eauto.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Icond; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct b; econstructor; eauto.

- (* jumptable *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  assert (Val.inject F rs#arg rs'#(sreg ctx arg)). eapply agree_val_reg; eauto.
  rewrite H0 in H2; inv H2.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Ijumptable; eauto.
(* Goal: Z.le (dstk ctx) (dstk ctx') *)
(* Goal: match_stacks_inside F m m' (@cons stackframe (Stackframe res f0 (Vptr sp Ptrofs.zero) pc rs) stk0) stk' f' ctx' sp' rs' *)
  rewrite list_nth_z_map. rewrite H1. simpl; reflexivity.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.

- (* return *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_funbody_inv; eauto. intros TR; inv TR.
+ (* not inlined *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  inv MS0; try congruence.
(* Goal: @eq val (Regmap.get val (sreg ctx r) rs') (Regmap.get val r rs) *)
(* Goal: @ex program (fun cu : program => @ex fundef (fun fd' : fundef => and (@eq (option fundef) (@Genv.find_funct fundef unit tge (Regmap.get val (sreg ctx r) rs')) (@Some fundef fd')) (and (@eq (Errors.res fundef) (transf_fundef (funenv_program cu) fd) (@OK fundef fd')) (@linkorder program (@Linker_prog fundef unit (Linker_fundef function) Linker_unit) cu prog)))) *)
  assert (X: { m1' | Mem.free m'0 sp' 0 (fn_stacksize f') = Some m1'}).
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    apply Mem.range_perm_free. red; intros.
    destruct (zlt ofs f.(fn_stacksize)).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    replace ofs with (ofs + dstk ctx) by omega. eapply Mem.perm_inject; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply Mem.free_range_perm; eauto. omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    inv FB. eapply range_private_perms; eauto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
    generalize (Zmax_spec (fn_stacksize f) 0). destruct (zlt 0 (fn_stacksize f)); omega.
  destruct X as [m1' FREE].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Ireturn; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
  eapply match_stacks_bound with (bound := sp').
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_1; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  erewrite Mem.nextblock_free; eauto. red in VB; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct or; simpl. apply agree_val_reg; auto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_right_inject; eauto. eapply Mem.free_left_inject; eauto.
  (* show that no valid location points into the stack block being freed *)
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inversion FB; subst.
  assert (PRIV': range_private F m' m'0 sp' (dstk ctx) f'.(fn_stacksize)).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite H8 in PRIV. eapply range_private_free_left; eauto.
(* Goal: forall _ : and (@eq (list stackframe) stk' (@nil stackframe)) (@eq (option (prod node reg)) (retinfo ctx) (@None (prod node reg))), final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  rewrite DSTK in PRIV'. exploit (PRIV' (ofs + delta)). omega. intros [A B].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eelim B; eauto. replace (ofs + delta - delta) with ofs by omega.
  apply Mem.perm_max with k. apply Mem.perm_implies with p; auto with mem.

+ (* inlined *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  right. split. simpl. omega. split. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_free_3; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  destruct or; simpl. apply agree_val_reg; auto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.free_left_inject; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  inv FB. rewrite H4 in PRIV. eapply range_private_free_left; eauto.

- (* internal function, not inlined *)
  assert (A: exists f', tr_function cunit f f' /\ fd' = Internal f').
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  { Errors.monadInv FD. exists x. split; auto. eapply transf_function_spec; eauto. }
  destruct A as [f' [TR1 EQ]].
  assert (TR: tr_function prog f f').
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  { eapply tr_function_linkorder; eauto. }
  inversion TR; subst.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit Mem.alloc_parallel_inject. eauto. eauto. apply Z.le_refl.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
    instantiate (1 := fn_stacksize f'). inv H1. xomega.
  intros [F' [m1' [sp' [A [B [C [D E]]]]]]].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_function_internal; eauto.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) m') r *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  rewrite H6. econstructor.
  instantiate (1 := F'). apply match_stacks_inside_base.
  assert (SP: sp' = Mem.nextblock m'0) by (eapply Mem.alloc_result; eauto).
  rewrite <- SP in MS0.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_invariant; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. destruct (eq_block b1 stk).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    subst b1. rewrite D in H8; inv H8. eelim Plt_strict; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite E in H8; auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. exploit Mem.perm_alloc_inv. eexact H. eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    destruct (eq_block b1 stk); intros; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    subst b1. rewrite D in H8; inv H8. eelim Plt_strict; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. eapply Mem.perm_alloc_1; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. exploit Mem.perm_alloc_inv. eexact A. eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite dec_eq_false; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto. auto. eauto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite H5. apply agree_regs_init_regs. eauto. auto. inv H1; auto. congruence. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.valid_new_block; eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Mem.perm_alloc_2; eauto. inv H1; xomega.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros; red; intros. exploit Mem.perm_alloc_inv. eexact H. eauto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  destruct (eq_block b stk); intros.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
  subst. rewrite D in H9; inv H9. inv H1; xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite E in H9; auto. eelim Mem.fresh_block_alloc. eexact A. eapply Mem.mi_mappedblocks; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. exploit Mem.perm_alloc_inv; eauto. rewrite dec_eq_true. omega.

- (* internal function, inlined *)
  inversion FB; subst.
  exploit Mem.alloc_left_mapped_inject.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eauto.
    (* sp' is valid *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    instantiate (1 := sp'). auto.
    (* offset is representable *)
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
    instantiate (1 := dstk ctx). generalize (Z.le_max_r (fn_stacksize f) 0). omega.
    (* size of target block is representable *)
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. right. exploit SSZ2; eauto with mem. inv FB; omega.
    (* we have full permissions on sp' at and above dstk ctx *)
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. apply Mem.perm_cur. apply Mem.perm_implies with Freeable; auto with mem.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply range_private_perms; eauto. xomega.
    (* offset is aligned *)
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
    replace (fn_stacksize f - 0) with (fn_stacksize f) by omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    inv FB. apply min_alignment_sound; auto.
    (* nobody maps to (sp, dstk ctx...) *)
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    intros. exploit (PRIV (ofs + delta')); eauto. xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    intros [A B]. eelim B; eauto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
    replace (ofs + delta' - delta') with ofs by omega.
    apply Mem.perm_max with k. apply Mem.perm_implies with p; auto with mem.
  intros [F' [A [B [C D]]]].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit tr_moves_init_regs; eauto. intros [rs'' [P [Q R]]].
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_left. eapply exec_Inop; eauto. eexact P. traceEq.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) m') r *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  econstructor.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_alloc_left; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_invariant; eauto.
(* Goal: and (Z.ge (mstk ctx'0) Z0) (Z.le (Z.add (dstk ctx'0) (mstk ctx'0)) (dstk ctx')) *)
  omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eauto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_regs_incr with F; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  rewrite H2. eapply range_private_alloc_left; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto.

- (* external function *)
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_globalenvs; eauto. intros [bound MG].
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit external_call_mem_inject; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_globalenvs_preserves_globals; eauto.
  intros [F1 [v1 [m1' [A [B [C [D [E [J K]]]]]]]]].
  simpl in FD. inv FD.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_function_external; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: final_state (Returnstate (@nil stackframe) (Vint r) m') r *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  econstructor.
    eapply match_stacks_bound with (Mem.nextblock m'0).
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply match_stacks_extcall with (F1 := F) (F2 := F1) (m1 := m) (m1' := m'0); eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    intros; eapply external_call_max_perm; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    intros; eapply external_call_max_perm; eauto.
(* Goal: Ple (Pos.add (dreg ctx'0) (mreg ctx'0)) (dreg ctx') *)
(* Goal: context_stack_call ctx'0 ctx' *)
    xomega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply external_call_nextblock; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    auto. auto.

- (* return fron noninlined function *)
  inv MS0.
+ (* normal case *)
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
  eapply plus_one. eapply exec_return.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply match_stacks_inside_set_reg; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_set_reg; auto.
+ (* untailcall case *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  inv MS; try congruence.
  rewrite RET in RET0; inv RET0.
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
  eapply plus_one. eapply exec_return.
  eapply match_regular_states.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply match_stacks_inside_set_reg; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eauto. auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  apply agree_set_reg; auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto. auto.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. destruct (zlt ofs (dstk ctx)). apply PAD; omega. apply PRIV; omega.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  auto. auto.

- (* return from inlined function *)
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
  inv MS0; try congruence. rewrite RET0 in RET; inv RET.
  unfold inline_return in AT.
  assert (PRIV': range_private F m m' sp' (dstk ctx' + mstk ctx') f'.(fn_stacksize)).
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
    red; intros. destruct (zlt ofs (dstk ctx)). apply PAD. omega. apply PRIV. omega.
  destruct or.
+ (* with a result *)
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Iop; eauto. simpl. reflexivity.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto. apply match_stacks_inside_set_reg; auto. apply agree_set_reg; auto.
+ (* without a result *)
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  left; econstructor; split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply plus_one. eapply exec_Inop; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto. subst vres. apply agree_set_reg_undef'; auto.
Qed.

Lemma transf_initial_states:
  forall st1, initial_state prog st1 -> exists st2, initial_state tprog st2 /\ match_states st1 st2.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit function_ptr_translated; eauto. intros (cu & tf & FIND & TR & LINK).
(* Goal: and (forall (b : block) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b) (@Some (globvar unit) gv)), @eq (option (prod block Z)) (F b) (@Some (prod block Z) (@pair block Z b Z0))) (forall (b1 b2 : block) (delta : Z) (gv : globvar unit) (_ : @eq (option (globvar unit)) (@Genv.find_var_info fundef unit ge b2) (@Some (globvar unit) gv)) (_ : @eq (option (prod block Z)) (F b1) (@Some (prod block Z) (@pair block Z b2 delta))), @eq block b2 b1) *)
  exists (Callstate nil tf nil m0); split.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply (Genv.init_mem_match TRANSF); eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite symbols_preserved. replace (prog_main tprog) with (prog_main prog). auto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    symmetry; eapply match_program_main; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    rewrite <- H3. eapply sig_function_translated; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  econstructor; eauto.
  instantiate (1 := Mem.flat_inj (Mem.nextblock m0)).
  apply match_stacks_nil with (Mem.nextblock m0).
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  constructor; intros.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    unfold Mem.flat_inj. apply pred_dec_true; auto.
(* Goal: final_state (State stk' f' (Vptr sp' Ptrofs.zero) pc' rs' m') r *)
    unfold Mem.flat_inj in H. destruct (plt b1 (Mem.nextblock m0)); congruence.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply Genv.find_symbol_not_fresh; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply Genv.find_funct_ptr_not_fresh; eauto.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
    eapply Genv.find_var_info_not_fresh; eauto.
    apply Ple_refl.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  eapply Genv.initmem_inject; eauto.
Qed.

Lemma transf_final_states:
  forall st1 st2 r,
  match_states st1 st2 -> final_state st1 r -> final_state st2 r.
Proof.
(* Goal: forall (st1 st2 : RTL.state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H0. inv H.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_empty; eauto. intros EQ; subst. inv VINJ. constructor.
(* Goal: Z.divide (Zpos (xO (xO (xO xH)))) n *)
(* Goal: Z.divide (align_chunk chunk) n *)
  exploit match_stacks_inside_empty; eauto. intros [A B]. congruence.
Qed.

Theorem transf_program_correct:
  forward_simulation (semantics prog) (semantics tprog).
Proof.
  eapply forward_simulation_star.
  apply senv_preserved.
  eexact transf_initial_states.
  eexact transf_final_states.
  eexact step_simulation.
Qed.

End INLINING.
