Require Import Coq.Logic.FunctionalExtensionality.
Require Import VST.veric.juicy_base.
Require Import VST.msl.normalize.
Require Import VST.veric.juicy_mem VST.veric.juicy_mem_lemmas VST.veric.juicy_mem_ops.
Require Import VST.veric.res_predicates.
Require Import VST.veric.extend_tc.
Require Import VST.veric.Clight_seplog.
Require Import VST.veric.Clight_assert_lemmas.
Require Import VST.veric.Clight_new.
Require Import VST.sepcomp.extspec.
Require Import VST.sepcomp.step_lemmas.
Require Import VST.veric.juicy_safety.
Require Import VST.veric.juicy_extspec.
Require Import VST.veric.tycontext.
Require Import VST.veric.expr.
Require Import VST.veric.expr2.
Require Import VST.veric.expr_lemmas.
Require Import VST.veric.expr_lemmas4.
Require Import VST.veric.semax.
Require Import VST.veric.semax_lemmas.
Require Import VST.veric.Clight_lemmas.

Local Open Scope pred.
Section extensions.
Context {CS: compspecs} {Espec: OracleKind}.

(* Scall *)

Lemma opt2list_inj: forall A (a b: option A), opt2list a = opt2list b -> a=b.
Proof.
(* Goal: forall (A : Type) (a b : option A) (_ : @eq (list A) (@opt2list A a) (@opt2list A b)), @eq (option A) a b *)
destruct a; destruct b; intros; inv H; auto.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma age_twin' {A B} `{HA: ageable A} `{HB: ageable B}:
  forall (x: A) (y: B) (x': A),
       level x = level y -> age x x' ->
       exists y', level x' = level y' /\ age y y'.
Proof.
(* Goal: forall (x : A) (y : B) (x' : A) (_ : @eq nat (@level A HA x) (@level B HB y)) (_ : @laterR A HA x x'), @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@laterR B HB y y')) *)
intros x y x' H0 H1.
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@age B HB y y')) *)
unfold fashionR in *.
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@age B HB y y')) *)
destruct (age1_levelS _ _ H1) as [n ?].
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@age B HB y y')) *)
rewrite H0 in H.
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@age B HB y y')) *)
destruct (levelS_age1 _ _ H) as [y' ?].
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@age B HB y y')) *)
exists y'; split.
(* Goal: @eq nat (@level A HA x') (@level B HB y') *)
(* Goal: @age B HB y y' *)
apply age_level in H2.
(* Goal: @eq nat (@level A HA x') (@level B HB y') *)
(* Goal: @age B HB y y' *)
apply age_level in H1.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
congruence.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma later_twin' {A B} `{HA: ageable A} `{HB: ageable B}:
  forall (x: A) (y: B) (x': A),
       level x = level y -> laterR x x' ->
       exists y', level x' = level y' /\ laterR y y'.
Proof.
(* Goal: forall (x : A) (y : B) (x' : A) (_ : @eq nat (@level A HA x) (@level B HB y)) (_ : @laterR A HA x x'), @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@laterR B HB y y')) *)
intros x y x' H0 H1.
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA x') (@level B HB y')) (@laterR B HB y y')) *)
revert y H0; induction H1; intros.
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA y) (@level B HB y')) (@laterR B HB y0 y')) *)
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA z) (@level B HB y')) (@laterR B HB y0 y')) *)
destruct (age_twin' _ _ _ H0 H) as [y' [? ?]].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists y'; split; auto.
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA z) (@level B HB y')) (@laterR B HB y0 y')) *)
specialize (IHclos_trans1 _ H0).
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA z) (@level B HB y')) (@laterR B HB y0 y')) *)
destruct IHclos_trans1 as [y2 [? ?]].
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA z) (@level B HB y')) (@laterR B HB y0 y')) *)
specialize (IHclos_trans2 _ H).
(* Goal: @ex B (fun y' : B => and (@eq nat (@level A HA z) (@level B HB y')) (@laterR B HB y0 y')) *)
destruct IHclos_trans2 as [u [? ?]].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists u; split; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply t_trans with y2; auto.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma later_twin {A} `{ageable A}:
   forall phi1 phi2 phi1',
     level phi1 = level phi2 ->
     laterR phi1 phi1' ->
     exists phi2', level phi1' = level phi2' /\ laterR phi2 phi2'.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply later_twin'; eauto.
Qed.

Lemma someP_inj:  forall A P Q, SomeP A P = SomeP A Q -> P=Q.
Proof. intros. injection H; intro. apply inj_pair2 in H0. auto. Qed.

Lemma function_pointer_aux:
  forall A P P' Q Q' (w: rmap),
    super_non_expansive P ->
    super_non_expansive Q ->
    super_non_expansive P' ->
    super_non_expansive Q' ->
   SomeP (SpecTT A) (fmap (fpi _) (approx (level w)) (approx (level w)) (packPQ P Q)) =
   SomeP (SpecTT A) (fmap (fpi _) (approx (level w)) (approx (level w)) (packPQ P' Q')) ->
   ( (forall ts x vl, (! |> (P' ts x vl <=> P ts x vl)) w) /\
     (forall ts x vl, (! |> (Q' ts x vl <=> Q ts x vl)) w)).
Proof.
(* Goal: forall (A : TypeTree) (P P' Q Q' : forall ts : list Type, _functor (dependent_type_functor_rec ts (AssertTT A)) mpred) (w : rmap) (_ : @super_non_expansive A P) (_ : @super_non_expansive A Q) (_ : @super_non_expansive A P') (_ : @super_non_expansive A Q') (_ : @eq preds (SomeP (SpecTT A) (@fmap (@fpi (list Type) (fun ts : list Type => dependent_type_functor_rec ts (SpecTT A))) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (@packPQ A P Q))) (SomeP (SpecTT A) (@fmap (@fpi (list Type) (fun ts : list Type => dependent_type_functor_rec ts (SpecTT A))) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (@packPQ A P' Q')))), and (forall (ts : list Type) (x : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A0 => fconst A0 | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) A) mpred) (vl : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A0 => fconst A0 | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) (ConstType environ)) mpred), @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w) (forall (ts : list Type) (x : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A0 => fconst A0 | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) A) mpred) (vl : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A0 => fconst A0 | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) (ConstType environ)) mpred), @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w) *)
  intros ? ? ? ? ? ? NEP NEQ NEP' NEQ' H.
(* Goal: and (forall (ts : list Type) (x : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) A) mpred) (vl : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) (ConstType environ)) mpred), @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w) (forall (ts : list Type) (x : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) A) mpred) (vl : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) (ConstType environ)) mpred), @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w) *)
  apply someP_inj in H.
(* Goal: and (forall (ts : list Type) (x : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) A) mpred) (vl : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) (ConstType environ)) mpred), @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w) (forall (ts : list Type) (x : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) A) mpred) (vl : _functor ((fix dtfr (T : TypeTree) : functor := match T with | ConstType A => fconst A | Mpred => fidentity | DependentType n => fconst (@nth Type n ts unit) | ProdType T1 T2 => fpair (dtfr T1) (dtfr T2) | ArrowType T1 T2 => ffunc (dtfr T1) (dtfr T2) | PiType I0 f => @fpi I0 (fun i : I0 => dtfr (f i)) | ListType T0 => flist (dtfr T0) end) (ConstType environ)) mpred), @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w) *)
  unfold packPQ in H; simpl in H.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  split; intros.
  + apply equal_f_dep with ts in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w *)
    apply equal_f with x in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w *)
    apply equal_f with true in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w *)
    apply equal_f with vl in H.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
    simpl in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w *)
    rewrite @later_fash; auto with typeclass_instances.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@fash rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w *)
    intros ? ? m' ?.
(* Goal: @app_pred rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl))) m' *)
    assert (forall m'', necR m' m'' -> (level m'' < level w)%nat).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
    {
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
      intros.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      clear - H0 H1 H2; hnf in H1.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      apply laterR_level in H1.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      apply necR_level in H2; simpl in *.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      omega.
    }
(* Goal: @app_pred rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl))) m' *)
    split; intros m'' ? ?.
    - apply f_equal with (f:= fun x => app_pred x m'') in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply prop_unext in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply approx_p with (level w).
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (P ts x vl)) m'' *)
      rewrite NEP.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
      apply H.
      rewrite <- NEP'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      apply approx_lt; auto.
    - apply f_equal with (f:= fun x => app_pred x m'') in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply prop_unext in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply approx_p with (level w).
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (P' ts x vl)) m'' *)
      rewrite NEP'.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
      apply H.
      rewrite <- NEP.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      apply approx_lt; auto.
  + apply equal_f_dep with ts in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w *)
    apply equal_f with x in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w *)
    apply equal_f with false in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@box nat ag_nat (@laterM nat ag_nat) (@fash rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl)))))) w *)
    apply equal_f with vl in H.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
    simpl in H.
(* Goal: @app_pred rmap ag_rmap (@unfash rmap ag_rmap (@fash rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@andp rmap ag_rmap (@imp rmap ag_rmap (P' ts x vl) (P ts x vl)) (@imp rmap ag_rmap (P ts x vl) (P' ts x vl)))))) w *)
    rewrite @later_fash; auto with typeclass_instances; intros ? ? m' ?.
(* Goal: @app_pred rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl))) m' *)
    assert (forall m'', necR m' m'' -> (level m'' < level w)%nat).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
    {
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
      intros.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      clear - H0 H1 H2; hnf in H1.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      apply laterR_level in H1.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      apply necR_level in H2; simpl in *.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
      omega.
    }
(* Goal: @app_pred rmap ag_rmap (@andp rmap ag_rmap (@imp rmap ag_rmap (Q' ts x vl) (Q ts x vl)) (@imp rmap ag_rmap (Q ts x vl) (Q' ts x vl))) m' *)
    split; intros m'' ? ?.
    - apply f_equal with (f:= fun x => app_pred x m'') in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply prop_unext in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply approx_p with (level w).
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q ts x vl)) m'' *)
      rewrite NEQ.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
      apply H.
      rewrite <- NEQ'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
     apply approx_lt; auto.
    - apply f_equal with (f:= fun x => app_pred x m'') in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply prop_unext in H.
(* Goal: @app_pred rmap ag_rmap (Q' ts x vl) m'' *)
      apply approx_p with (level w).
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts x vl)) m'' *)
      rewrite NEQ'.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
      apply H.
      rewrite <- NEQ.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
     apply approx_lt; auto.
Qed.

Import JuicyMemOps.

(*Lenb: moved alloc_juicy_variables, juicy_mem_alloc_core, and 
  alloc_juicy_variables_e here from to veric/juicy_mem_ops.v*)

Fixpoint alloc_juicy_variables (ge: genv) (rho: env) (jm: juicy_mem) (vl: list (ident*type)) : env * juicy_mem :=
 match vl with
 | nil => (rho,jm)
 | (id,ty)::vars => match JuicyMemOps.juicy_mem_alloc jm 0 (@sizeof ge ty) with
                              (m1,b1) => alloc_juicy_variables ge (PTree.set id (b1,ty) rho) m1 vars
                           end
 end.

Lemma juicy_mem_alloc_core:
  forall jm lo hi jm' b, JuicyMemOps.juicy_mem_alloc jm lo hi = (jm', b) ->
    core (m_phi jm) = core (m_phi jm').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 unfold JuicyMemOps.juicy_mem_alloc, after_alloc; intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 inv H.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 simpl.
(* Goal: @eq rmap (@core rmap Join_rmap Sep_rmap (m_phi (free_juicy_mem jm m b lo hi H))) (@core rmap Join_rmap Sep_rmap (m_phi jm)) *)
 apply rmap_ext.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 repeat rewrite level_core. rewrite level_make_rmap. auto.
(* Goal: forall l : address, and (forall _ : Pos.ge (@fst block Z l) (nextblock (m_dry jm)), @eq resource (resource_at (m_phi jm) l) (NO Share.bot bot_unreadable)) (or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) l)) (resource_at (m_phi jm1) l)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) l)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm1) l) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z l) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm1) l) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) l) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm1) l) (NO Share.bot bot_unreadable)))))))) *)
 intro loc.
 repeat rewrite <- core_resource_at.
(* Goal: @eq resource (@core resource Join_resource Sep_resource (resource_at (m_phi jm) loc)) (@core resource Join_resource Sep_resource (resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))) loc)) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) (ghost_of (@core rmap Join_rmap Sep_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))))) *)
 rewrite resource_at_make_rmap.
(* Goal: @eq resource (@core resource Join_resource Sep_resource (resource_at (m_phi jm) loc)) (@core resource Join_resource Sep_resource (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))) loc)) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) (ghost_of (@core rmap Join_rmap Sep_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))))) *)
 unfold after_alloc'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 if_tac; auto.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable))))))) *)
 destruct loc as [b z].
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
 simpl in H.
(* Goal: @eq resource (@core resource Join_resource Sep_resource (resource_at (m_phi jm) (@pair block Z b z))) (@core resource Join_resource Sep_resource (YES Share.top readable_share_top (VAL Undef) NoneP)) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) (ghost_of (@core rmap Join_rmap Sep_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))))) *)
 rewrite core_YES.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite juicy_mem_alloc_cohere. rewrite core_NO; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 simpl. destruct H.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 revert H; case_eq (alloc (m_dry jm) lo hi); intros.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 simpl in *. subst b0. apply alloc_result in H. subst b; xomega.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite <- (core_ghost_of (proj1_sig _)), ghost_of_make_rmap, core_ghost_of; auto.
Qed.

Lemma alloc_juicy_variables_e:
  forall ge rho jm vl rho' jm',
    alloc_juicy_variables ge rho jm vl = (rho', jm') ->
  Clight.alloc_variables ge rho (m_dry jm) vl rho' (m_dry jm')
   /\ level jm = level jm'
   /\ core (m_phi jm) = core (m_phi jm').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 revert rho jm H; induction vl; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 inv H. split; auto. constructor.
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 unfold alloc_juicy_variables in H; fold alloc_juicy_variables in H.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@var_block Share.top CS a (construct_rho (filter_genv ge) ve te)) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te)))) (m_phi jm') *)
 destruct a as [id ty].
(* Goal: and (alloc_variables ge rho (m_dry jm) (@cons (prod ident type) (@pair ident type id ty) vl) rho' (m_dry jm')) (and (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')) (@eq rmap (@core rmap Join_rmap Sep_rmap (m_phi jm)) (@core rmap Join_rmap Sep_rmap (m_phi jm')))) *)
 revert H; case_eq (JuicyMemOps.juicy_mem_alloc jm 0 (@sizeof ge ty)); intros jm1 b1 ? ?.
(* Goal: and (alloc_variables ge rho (m_dry jm) (@cons (prod ident type) (@pair ident type id ty) vl) rho' (m_dry jm')) (and (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')) (@eq rmap (@core rmap Join_rmap Sep_rmap (m_phi jm)) (@core rmap Join_rmap Sep_rmap (m_phi jm')))) *)
 specialize (IHvl (PTree.set id (b1,ty) rho) jm1 H0).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct IHvl as [? [? ?]]; split3; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply alloc_variables_cons  with  (m_dry jm1) b1; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply JuicyMemOps.juicy_mem_alloc_succeeds in H. auto.
(* Goal: @eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm') *)
(* Goal: @eq rmap (@core rmap Join_rmap Sep_rmap (m_phi jm)) (@core rmap Join_rmap Sep_rmap (m_phi jm')) *)
 apply JuicyMemOps.juicy_mem_alloc_level in H.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
 congruence.
 rewrite <- H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply  juicy_mem_alloc_core; eauto.
Qed.


Lemma alloc_juicy_variables_match_venv:
  forall ge jm vl ve' jm',
     alloc_juicy_variables ge empty_env jm vl = (ve',jm') ->
     match_venv (make_venv ve') vl.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
  intro i.
(* Goal: @eq (option (prod block type)) (make_venv ve' i) (@None (prod block type)) *)
 unfold make_venv.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  destruct (ve' ! i) as [[? ?] | ] eqn:?; auto.
  assert (H0: (exists b, empty_env ! i = Some (b,t)) \/ In (i,t) vl).
2: destruct H0; auto; destruct H0; rewrite PTree.gempty in H0; inv H0.
 forget empty_env as e.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  revert jm e H; induction vl; simpl; intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
  inv H.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  left; eexists; eauto.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
  destruct a.
  apply IHvl in H; clear IHvl.
 destruct (ident_eq i0 i). subst i0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct H; auto. destruct H as [b' ?].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gss in H. inv H. right. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct H; auto. left. destruct H as [b' ?].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gso in H by auto. eauto.
Qed.

Lemma build_call_temp_env:
  forall f vl,
     length (fn_params f) = length vl ->
  exists te,  bind_parameter_temps (fn_params f) vl
                     (create_undef_temps (fn_temps f)) = Some te.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: @ex temp_env (fun te : temp_env => @eq (option temp_env) (bind_parameter_temps (fn_params f) vl (create_undef_temps (fn_temps f))) (@Some temp_env te)) *)
 forget (create_undef_temps (fn_temps f)) as rho.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
 revert rho vl H; induction (fn_params f); destruct vl; intros; inv H; try congruence.
(* Goal: @ex temp_env (fun te : temp_env => @eq (option temp_env) (bind_parameter_temps (@nil (prod ident type)) (@nil val) rho) (@Some temp_env te)) *)
(* Goal: @ex temp_env (fun te : temp_env => @eq (option temp_env) (bind_parameter_temps (@cons (prod ident type) a l) (@cons val v vl) rho) (@Some temp_env te)) *)
 exists rho; reflexivity.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 destruct a; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply IHl. auto.
Qed.

Lemma resource_decay_funassert:
  forall G rho b w w',
         necR (core w) (core w') ->
         resource_decay b w w' ->
         app_pred (funassert G rho) w ->
         app_pred (funassert G rho) w'.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
unfold resource_decay, funassert; intros until w'; intro CORE; intros.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
destruct H.
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
destruct H0.
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
split; [clear H2 | clear H0].
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap ident (fun id : ident => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (@prop rmap ag_rmap (@eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs))) (@exp rmap ag_rmap block (fun b : block => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (func_at fs (@pair block Z b Z0))))))) w' *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
intros id fs w2 Hw2 H3.
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap block (fun b : block => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (func_at fs (@pair block Z b Z0)))) w2 *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
specialize (H0 id fs). cbv beta in H0.
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap block (fun b : block => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (func_at fs (@pair block Z b Z0)))) w2 *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
specialize (H0 _ (necR_refl _) H3).
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap block (fun b : block => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (func_at fs (@pair block Z b Z0)))) w2 *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
destruct H0 as [loc [? ?]].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists loc; split; auto.
(* Goal: @app_pred rmap ag_rmap (func_at fs (@pair block Z loc Z0)) w2 *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
destruct fs as [f cc A a a0].
simpl in H2|-*.
pose proof (necR_resource_at (core w) (core w') (loc,0)
         (PURE (FUN f cc) (SomeP (SpecTT A) (packPQ a a0))) CORE).
pose proof (necR_resource_at _ _ (loc,0)
         (PURE (FUN f cc) (SomeP (SpecTT A) (packPQ a a0))) Hw2).
(* Goal: @eq resource (resource_at w2 (@pair block Z loc Z0)) (PURE (FUN f cc) (SomeP (SpecTT A) (fun (i : list Type) (a1 : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a2 : bool) (a3 : environ) => approx (@level rmap ag_rmap w2) (@packPQ A a a0 i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w2)) (approx (@level rmap ag_rmap w2)) a1) a2 a3)))) *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
apply H5.
clear - H4 H2.
repeat rewrite <- core_resource_at in *.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
spec H4. rewrite H2.  rewrite core_PURE.  simpl.  rewrite level_core; reflexivity.
(* Goal: @eq resource (resource_at w' (@pair block Z loc Z0)) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
destruct (w' @ (loc,0)).
(* Goal: @eq resource (NO sh n) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE (FUN f cc) (SomeP (SpecTT A) (@packPQ A a a0)))) *)
(* Goal: @eq resource (YES sh r k p) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE (FUN f cc) (SomeP (SpecTT A) (@packPQ A a a0)))) *)
(* Goal: @eq resource (PURE k p) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE (FUN f cc) (SomeP (SpecTT A) (@packPQ A a a0)))) *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
 rewrite core_NO in H4; inv H4.
(* Goal: @eq resource (YES sh r k p) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE (FUN f cc) (SomeP (SpecTT A) (@packPQ A a a0)))) *)
(* Goal: @eq resource (PURE k p) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE (FUN f cc) (SomeP (SpecTT A) (@packPQ A a a0)))) *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
 rewrite core_YES in H4; inv H4.
(* Goal: @eq resource (PURE (FUN f cc) (SomeP (SpecTT A) (fun (i : list Type) (a1 : _functor (dependent_type_functor_rec i A) (@pred rmap ag_rmap)) (a2 : bool) (a3 : environ) => approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap w')) (@packPQ A a a0 i (@fmap (dependent_type_functor_rec i A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap w'))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap w'))) a1) a2 a3)))) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE (FUN f cc) (SomeP (SpecTT A) (@packPQ A a a0)))) *)
(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
 rewrite core_PURE in H4; inv H4. rewrite level_core; reflexivity.

(* Goal: @app_pred rmap ag_rmap (@allp rmap ag_rmap block (fun b : block => @allp rmap ag_rmap funspec (fun fs : funspec => @imp rmap ag_rmap (func_at' fs (@pair block Z b Z0)) (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block b))) (@prop rmap ag_rmap (@ex funspec (fun fs0 : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs0))))))))) w' *)
intros loc fs w2 Hw2 H6.
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
specialize (H2 loc fs _ (necR_refl _)).
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
spec H2.
clear - Hw2 CORE H6.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
destruct fs; simpl in *.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_at w (@pair block Z loc Z0)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
destruct H6 as [pp H6].
 rewrite <- resource_at_approx.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
case_eq (w @ (loc,0)); intros.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (YES sh r k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
assert (core w @ (loc,0) = resource_fmap (approx (level (core w))) (approx (level (core w))) (NO _ bot_unreadable)).
 rewrite <- core_resource_at.
simpl; erewrite <- core_NO; f_equal; eassumption.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (YES sh r k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pose proof (necR_resource_at _ _ _ _ CORE H0).
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (YES sh r k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pose proof (necR_resource_at _ _ _ _ (necR_core _ _ Hw2) H1).
rewrite <- core_resource_at in H2; rewrite H6 in H2;
 rewrite core_PURE in H2; inv H2.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (YES sh r k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
assert (core w @ (loc,0) = resource_fmap (approx (level (core w))) (approx (level (core w))) (NO _ bot_unreadable)).
 rewrite <- core_resource_at.
simpl; erewrite <- core_YES; f_equal; eassumption.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (YES sh r k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pose proof (necR_resource_at _ _ _ _ CORE H0).
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (YES sh r k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pose proof (necR_resource_at _ _ _ _ (necR_core _ _ Hw2) H1).
rewrite <- core_resource_at in H2; rewrite H6 in H2;
 rewrite core_PURE in H2; inv H2.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pose proof (resource_at_approx w (loc,0)).
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pattern (w @ (loc,0)) at 1 in H0; rewrite H in H0.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
symmetry in H0.
assert (core (w @ (loc,0)) = core (resource_fmap (approx (level w)) (approx (level w))
       (PURE k p))) by (f_equal; auto).
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
rewrite core_resource_at in H1.
assert (core w @ (loc,0) =
        resource_fmap (approx (level (core w))) (approx (level (core w)))
         (PURE k p)).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite H1.  simpl. rewrite level_core; rewrite core_PURE; auto.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
pose proof (necR_resource_at _ _ _ _ CORE H2).
 assert (w' @ (loc,0) = resource_fmap
       (approx (level w')) (approx (level w')) (PURE k p)).
 rewrite <- core_resource_at in H3. rewrite level_core in H3.
(* Goal: @eq resource (resource_at w' (@pair block Z loc Z0)) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
 destruct (w' @ (loc,0)).
(* Goal: @eq resource (NO sh n) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @eq resource (YES sh r k0 p0) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @eq resource (PURE k0 p0) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
  rewrite core_NO in H3; inv H3.
(* Goal: @eq resource (YES sh r k0 p0) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @eq resource (PURE k0 p0) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
  rewrite core_YES in H3; inv H3.
(* Goal: @eq resource (PURE k0 p0) (resource_fmap (approx (@level rmap ag_rmap w')) (approx (@level rmap ag_rmap w')) (PURE k p)) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
  rewrite core_PURE in H3; inv H3.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
 reflexivity.
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
 pose proof (necR_resource_at _ _ _ _ Hw2 H4).
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
 inversion2 H6 H5.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
 exists p. reflexivity.
 
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
destruct H2 as [id [? ?]].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists id. split; auto.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Definition substopt {A} (ret: option ident) (v: val) (P: environ -> A)  : environ -> A :=
   match ret with
   | Some id => subst id v P
   | None => P
   end.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma fst_split {T1 T2}: forall vl: list (T1*T2), fst (split vl) = map fst vl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
Proof. induction vl; try destruct a; simpl; auto.
  rewrite <- IHvl; clear IHvl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (split vl); simpl in *; auto.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma snd_split {T1 T2}: forall vl: list (T1*T2), snd (split vl) = map snd vl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
Proof. induction vl; try destruct a; simpl; auto.
  rewrite <- IHvl; clear IHvl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (split vl); simpl in *; auto.
Qed.

Lemma eval_exprlist_relate:
  forall (Delta : tycontext) (fsig0 : funsig)
     (bl : list expr) (psi : genv) (vx : env) (tx : temp_env)
     (rho : environ) m,
   denote_tc_assert (typecheck_exprlist Delta (snd (split (fst fsig0))) bl) rho (m_phi m) ->
   typecheck_environ Delta rho ->
   genv_cenv psi = cenv_cs ->
   rho = construct_rho (filter_genv psi) vx tx ->
   forall f : function,
   fsig0 = fn_funsig f ->
   Clight.eval_exprlist psi vx tx (m_dry m) bl
     (type_of_params (fn_params f))
     (eval_exprlist (snd (split (fst fsig0))) bl rho).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  intros.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
  destruct fsig0. unfold fn_funsig in *. inversion H3; clear H3; subst l t. simpl in *.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) bl (type_of_params (fn_params f)) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type (fn_params f))) bl rho) *)
  forget (fn_params f) as vl.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) bl (type_of_params vl) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type vl)) bl rho) *)
  forget (fn_temps f) as tl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
  clear f.
  clear - H0 H1 H2 H.

(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) bl (type_of_params vl) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type vl)) bl rho) *)
 rewrite snd_split. rewrite snd_split in H.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) bl (type_of_params vl) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl rho) *)
 assert (length (map snd vl) = length bl).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply tc_exprlist_length in H; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 revert vl H H3; induction bl; destruct vl; intros; inv H3; simpl.
(* Goal: @Forall A P (@filter A f (@nil A)) *)
 constructor.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (let (_, ty) := p in Tcons ty (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) (@snd ident type p)) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
 destruct p. simpl in *; subst.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
 rewrite !denote_tc_assert_andp in H.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
 super_unfold_lift.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@cons val (force_val (sem_cast (typeof a) t (@eval_expr CS a (construct_rho (filter_genv psi) vx tx)))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl (construct_rho (filter_genv psi) vx tx))) *)
 destruct H as [[? ?] ?].
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@cons val (force_val (sem_cast (typeof a) t (@eval_expr CS a (construct_rho (filter_genv psi) vx tx)))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl (construct_rho (filter_genv psi) vx tx))) *)
 specialize (IHbl _ H3 H5).
 clear - IHbl H3 H H0 H1 H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 constructor 2 with (eval_expr a (construct_rho (filter_genv psi) vx tx)); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply eval_expr_relate; eauto.
(* Goal: @eq (option val) (Cop.sem_cast (@eval_expr CS a (construct_rho (filter_genv psi) vx tx)) (typeof a) t (m_dry m)) (@Some val (force_val (sem_cast (typeof a) t (@eval_expr CS a (construct_rho (filter_genv psi) vx tx))))) *)
 pose proof (cast_exists Delta a _ _ _ H0 H H2).
 forget (force_val
          (sem_cast (typeof a) t
             (eval_expr a
                (construct_rho (filter_genv psi) vx tx)))) as v.
(* Goal: @eq (option val) (Cop.sem_cast (@eval_expr CS a (construct_rho (filter_genv psi) vx tx)) (typeof a) t (m_dry m)) (@Some val v) *)
 rewrite cop2_sem_cast'; try eassumption.
(* Goal: tc_val (typeof a) (@eval_expr CS a (construct_rho (filter_genv psi) vx tx)) *)
 eapply typecheck_expr_sound; eassumption.
Qed.

Lemma bind_parameter_temps_excludes :
forall l1 l2 t id t1,
~In id (map fst l1) ->
(bind_parameter_temps l1 l2 t) = Some t1 ->
t1 ! id = t ! id.
Proof.
induction l1;
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl in *. destruct l2; inv H0. auto.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
simpl in H0. destruct a. destruct l2; inv H0.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
specialize (IHl1 l2 (PTree.set i v t) id t1).
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
simpl in H. intuition. rewrite PTree.gsspec in H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (peq id i). subst; intuition. auto.
Qed.

Lemma pass_params_ni :
  forall  l2
     (te' : temp_env) (id : positive) te l,
   bind_parameter_temps l2 l (te) = Some te' ->
   (In id (map fst l2) -> False) ->
   Map.get (make_tenv te') id = te ! id.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros. eapply bind_parameter_temps_excludes in H.
unfold make_tenv, Map.get.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
apply H. intuition.
Qed.

Lemma bind_exists_te : forall l1 l2 t1 t2 te,
bind_parameter_temps l1 l2 t1 = Some te ->
exists te2, bind_parameter_temps l1 l2 t2 = Some te2.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction l1; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
+ simpl in H. destruct l2; inv H. simpl. eauto.

+ destruct a. simpl in *. destruct l2; inv H. eapply IHl1.
apply H1.
Qed.


Lemma smaller_temps_exists2 : forall l1 l2 t1 t2 te te2 i,
bind_parameter_temps l1 l2 t1 = Some te ->
bind_parameter_temps l1 l2 t2 = Some te2 ->
t1 ! i = t2 ! i ->
te ! i = te2 ! i.
Proof.
induction l1; intros; simpl in *; try destruct a; destruct l2; inv H; inv H0.
apply H1.
eapply IHl1. apply H3. apply H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
repeat rewrite PTree.gsspec. destruct (peq i i0); auto.
Qed.


Lemma smaller_temps_exists' : forall l l1 te te' id i t,
bind_parameter_temps l l1 (PTree.set id Vundef t)=  Some te ->
i <> id ->
(bind_parameter_temps l l1 t = Some te') -> te' ! i = te ! i.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction l; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl in *. destruct l1; inv H. inv H1. rewrite PTree.gso; auto.

(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
simpl in *. destruct a. destruct l1; inv H.
eapply smaller_temps_exists2. apply H1. apply H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
intros. repeat rewrite PTree.gsspec. destruct (peq i i0); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (peq i id). subst. intuition. auto.
Qed.

Lemma smaller_temps_exists'' : forall l l1 te id i t,
bind_parameter_temps l l1 (PTree.set id Vundef t)=  Some te ->
i <> id ->
exists te', (bind_parameter_temps l l1 t = Some te').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply bind_exists_te; eauto.
Qed.

Lemma smaller_temps_exists : forall l l1 te id i t,
bind_parameter_temps l l1 (PTree.set id Vundef t)=  Some te ->
i <> id ->
exists te', (bind_parameter_temps l l1 t = Some te' /\ te' ! i = te ! i).
Proof.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
intros. copy H. eapply smaller_temps_exists'' in H; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct H. exists x. split. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply smaller_temps_exists'; eauto.
Qed.


Lemma alloc_vars_lookup :
forall ge id m1 l ve m2 e ,
list_norepet (map fst l) ->
(forall i, In i (map fst l) -> e ! i = None) ->
Clight.alloc_variables ge (e) m1 l ve m2 ->
(exists v, e ! id = Some v) ->
ve ! id = e ! id.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id e) *)
generalize dependent e.
(* Goal: forall (e : PTree.t (prod block type)) (_ : forall (i : positive) (_ : @In positive i (@map (prod positive type) positive (@fst positive type) l)), @eq (option (prod block type)) (@PTree.get (prod block type) i e) (@None (prod block type))) (_ : alloc_variables ge e m1 l ve m2) (_ : @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id e) (@Some (prod block type) v))), @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id e) *)
revert ve m1 m2.

(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction l; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
inv H1. auto.

(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
inv H1. simpl in *. inv H.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm1 jm' *)
destruct H2.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id e) *)
assert (id <> id0).
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
intro. subst.  specialize (H0 id0). spec H0. auto. congruence.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id e) *)
eapply IHl in H10.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso in H10; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto. intros. rewrite PTree.gsspec. if_tac. subst. intuition.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply H0. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso; auto. eauto.
Qed.

Lemma alloc_vars_lemma : forall ge id l m1 m2 ve ve'
(SD : forall i, In i (map fst l) -> ve ! i = None),
list_norepet (map fst l) ->

Clight.alloc_variables ge ve m1 l ve' m2 ->
(In id (map fst l) ->
exists v, ve' ! id = Some v).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
generalize dependent ve.
(* Goal: forall (ve : PTree.t (prod block type)) (_ : forall (i : positive) (_ : @In positive i (@map (prod positive type) positive (@fst positive type) l)), @eq (option (prod block type)) (@PTree.get (prod block type) i ve) (@None (prod block type))) (_ : alloc_variables ge ve m1 l ve' m2), @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
revert m1 m2.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction l; intros. inv H1.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *. destruct a; simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct H1. subst. inv H0. inv H.  apply alloc_vars_lookup with (id := id) in H9; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite H9. rewrite PTree.gss. eauto. intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (peq i id). subst. intuition. rewrite PTree.gso; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gss; eauto.

(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
inv H0. apply IHl in H10; auto. inv H; auto.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros. rewrite PTree.gsspec. if_tac. subst. inv H. intuition.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto.
Qed.

Lemma semax_call_typecheck_environ:
  forall (Delta : tycontext) (bl : list expr) (psi : genv) (vx : env) (tx : temp_env)
           (jm : juicy_mem) (b : block) (f : function)
     (H17 : list_norepet (map fst (fn_params f) ++ map fst (fn_temps f)))
     (H17' : list_norepet (map fst (fn_vars f)))
     (H16 : Genv.find_funct_ptr psi b = Some (Internal f))
     (ve' : env) (jm' : juicy_mem) (te' : temp_env)
     (H15 : alloc_variables psi empty_env (m_dry jm) (fn_vars f) ve' (m_dry jm'))
    (TC3 : typecheck_temp_environ (make_tenv tx) (temp_types Delta))
    (TC4 : typecheck_var_environ (make_venv vx) (var_types Delta))
    (TC5 : typecheck_glob_environ (filter_genv psi) (glob_types Delta))
   (H : forall (b : ident) (b0 : funspec) (a' : rmap),
    necR (m_phi jm') a' ->
    (glob_specs Delta) ! b = Some b0 ->
(*    (glob_types Delta) ! b = Some (type_of_funspec b0) -> *)
    exists b1 : block,
        filter_genv psi b = Some b1 /\
        func_at b0 (b1,0) a')
    (H1: forall (b : block) (b0 : funspec) (a' : rmap),
     necR (m_phi jm') a' ->
     (func_at' b0 (b, 0)) a' ->
     exists (b1 : ident),
       filter_genv psi b1 = Some b /\
       (exists fs : funspec, (glob_specs Delta) ! b1 = Some fs))
   (l : list ident) (l0 : list type)
    (Heqp : (l, l0) = split (fn_params f))
   (TC2 : denote_tc_assert (typecheck_exprlist Delta l0 bl)
        (mkEnviron (filter_genv psi) (make_venv vx) (make_tenv tx)) (m_phi jm)) (**)
   (H21 : bind_parameter_temps (fn_params f)
        (eval_exprlist l0 bl
           (mkEnviron (filter_genv psi) (make_venv vx) (make_tenv tx)))
        (create_undef_temps (fn_temps f)) = Some te')
   (TE : typecheck_environ
        Delta (mkEnviron (filter_genv psi) (make_venv vx) (make_tenv tx))),
   typecheck_environ
    (mk_tycontext
      (make_tycontext_t (fn_params f) (fn_temps f))
      (make_tycontext_v (fn_vars f))
      (fn_return f)  (glob_types Delta) (glob_specs Delta) (annotations Delta))
     (mkEnviron (filter_genv psi) (make_venv ve') (make_tenv te')).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
 pose (rho3 := mkEnviron (filter_genv psi) (make_venv ve') (make_tenv te')).

unfold typecheck_environ. repeat rewrite andb_true_iff.
split3.
*
clear H H1 H15.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
unfold typecheck_temp_environ in *. intros. simpl.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
unfold temp_types in *. simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply func_tycontext_t_sound in H; auto.
 clear - H21 H TC2 TC3 Heqp H17 TE.

(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
destruct H. (*in params*)
forget (create_undef_temps (fn_temps f)) as temps.
generalize dependent temps.
generalize dependent l. generalize dependent l0.
generalize dependent bl. generalize dependent te'.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
{  induction (fn_params f); intros.
   + inv H.
   + simpl in *.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
     destruct a. simpl in *. remember (split l). destruct p.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
     simpl in *. destruct H.
      - clear IHl. destruct bl. inv H.  inv Heqp. inv TC2.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
        inv H. inv Heqp. simpl in *.
        rewrite !denote_tc_assert_andp in TC2.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
        simpl in *; super_unfold_lift.
        destruct TC2 as [[? ?] ?].
        rewrite (pass_params_ni _ _ id _ _ H21) by (inv H17; contradict H4; apply in_app; auto).
(* Goal: @eq (option val) (@PTree.get val j te') (@PTree.get val j (@PTree.set val j v e)) *)
(* Goal: @eq (option val) (@PTree.get val j (@PTree.set val i v e)) (if match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end then @PTree.get val j te' else @None val) *)
        rewrite PTree.gss.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
        eexists.  split. reflexivity. apply tc_val_tc_val'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
        eapply tc_val_sem_cast; eauto.
      - inv Heqp. destruct bl. inv TC2. inv H17. simpl in TC2.
        repeat (rewrite tc_andp_sound in TC2; simpl in TC2; super_unfold_lift).
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
        destruct TC2 as [[? ?] ?]. assert (i <> id). intro. subst.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
        apply H2. apply in_or_app. left. apply in_map with (f := fst) in H. apply H.
        remember (eval_exprlist (t :: l3) (e :: bl)
            (mkEnviron (filter_genv psi) (make_venv vx) (make_tenv tx))).
        destruct l0; inv H21. simpl in Heql0.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
        super_unfold_lift. inv Heql0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
        eapply IHl; eauto.
}

(*In temps*)
apply list_norepet_app in H17. destruct H17 as [? [? ?]].
generalize dependent (fn_params f). generalize dependent bl.
generalize dependent l0. generalize dependent l. generalize dependent te'.

(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction (fn_temps f); intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
inv H.

(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
simpl in *. destruct H. destruct a. inv H. simpl in *.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
clear IHl. exists Vundef. simpl in *. split; [| hnf; congruence]. inv H1.
assert (In id (map fst (l2)) -> False).
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
unfold list_disjoint in *. eapply H2. eauto. left. auto. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply pass_params_ni with (id := id) in H21; auto.  rewrite PTree.gss in *. auto.


(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
destruct a.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
destruct (peq id i). subst.
apply pass_params_ni with (id := i) in H21.
rewrite PTree.gss in *. exists Vundef. split; [auto | hnf; congruence].
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros. unfold list_disjoint in *. intuition.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply H2. eauto. left. auto. auto.

apply smaller_temps_exists with (i := id) in H21.
destruct H21.  destruct H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply IHl in H3; auto.
destruct H3. destruct H3.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
exists x0. split. unfold Map.get in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
unfold make_tenv in *. rewrite <- H4. auto. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
inv H1; auto. unfold list_disjoint in *. intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply H2. auto. right. auto. apply Heqp. auto.
*

(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
unfold typecheck_var_environ in *. intros.
clear TC3 TC5.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *. unfold typecheck_var_environ in *.
unfold func_tycontext' in *. unfold var_types in *.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite (func_tycontext_v_sound (fn_vars f) id ty); auto.
transitivity ((exists b, empty_env ! id = Some (b,ty) )\/ In (id,ty) (fn_vars f)).
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
clear; intuition. destruct H0. unfold empty_env in H.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
rewrite PTree.gempty in H; inv H.
generalize dependent (m_dry jm).
clear - H17'.
assert (forall id, empty_env ! id <> None -> ~ In id (map fst (fn_vars f))).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
intros. unfold empty_env in H. rewrite PTree.gempty in H. contradiction H; auto.
generalize dependent empty_env.
(* Goal: @eq (option (prod block type)) (@Map.get (prod block type) (make_venv ve) id) (@Some (prod block type) (@pair block type b ty)) *)
unfold Map.get, make_venv.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction (fn_vars f); intros.
inv H15.
(* Goal: @eq (option (prod block type)) (@Some (prod block type) (@pair block type b1 t)) (@None (prod block type)) *)
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve) (@None (prod block type)) *)
destruct (ve' ! id); intuition.
inv H15.
inv H17'.
specialize (IHl H3); clear H3.
specialize (IHl (PTree.set id0 (b1,ty0) e)).
spec IHl.
intros id' H8; specialize (H id').
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (ident_eq id0 id'). subst. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso  in H8 by auto.
specialize (H H8). contradict H.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
right; auto.
specialize (IHl _ H7).
clear - H H2 IHl.
destruct (ident_eq id0 id). subst id0.
rewrite PTree.gss in IHl.
(* Goal: forall _ : @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (VALspec_range (Z.sub hi lo) Share.top (@pair block Z b lo)) (freeable_blocks bl)) P) (m_phi jm), @app_pred rmap ag_rmap P (m_phi jm') *)
split; intro.
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
destruct H0.
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
destruct H0. specialize (H id).
destruct (e!id); try discriminate.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@nil (prod ident type))) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@cons (prod ident type) a vars)) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
inv H0.
spec H; [congruence | ].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
contradiction H. left; auto.
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
destruct H0. inv H0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply IHl. left; eauto.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i e) (@None (prod block type)) *)
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id (@PTree.set (prod block type) id0 (@pair block type b1 ty) e)) (@Some (prod block type) v)) *)
contradiction H2. apply in_map with (f:=fst) in H0. apply H0.
rewrite <- IHl in H0.
(* Goal: @app_pred rmap ag_rmap (funspecs_assert (glob_specs G) rho) w' *)
destruct H0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct H0. inv H0. right; left; auto.
contradiction H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply in_map with (f:=fst) in H0. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso in IHl by auto.
rewrite <- IHl.
(* Goal: @eq (option (prod block type)) (@Some (prod block type) (@pair block type b1 t)) (@None (prod block type)) *)
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve) (@None (prod block type)) *)
intuition. inv H5. inv H0. intuition.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply H4 in H0. apply H1; auto.
*
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
unfold ge_of in *. simpl in *. auto.
Qed.

Lemma free_juicy_mem_level:
  forall jm m b lo hi H, level (free_juicy_mem jm m b lo hi H) = level jm.
Proof.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 intros;  simpl;  unfold inflate_free; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite level_make_rmap. auto.
Qed.

Lemma free_juicy_mem_ghost:
  forall jm m b lo hi H,
    ghost_of (m_phi (free_juicy_mem jm m b lo hi H)) = ghost_of (m_phi jm).
Proof.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 intros;  simpl;  unfold inflate_free; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite ghost_of_make_rmap. auto.
Qed.

Lemma free_list_free:
  forall m b lo hi l' m',
       free_list m ((b,lo,hi)::l') = Some m' ->
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
         {m2 | free m b lo hi = Some m2 /\ free_list m2 l' = Some m'}.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
simpl; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (free m b lo hi). eauto. inv H.
Qed.

Definition freeable_blocks: list (block * BinInt.Z * BinInt.Z) -> mpred :=
  fold_right (fun (bb: block*BinInt.Z * BinInt.Z) a => 
                        match bb with (b,lo,hi) => 
                                          sepcon (VALspec_range (hi-lo) Share.top (b,lo)) a
                        end)
                    emp.

Inductive free_list_juicy_mem:
      forall  (jm: juicy_mem) (bl: list (block * BinInt.Z * BinInt.Z))
                                         (jm': juicy_mem), Prop :=
| FLJM_nil: forall jm, free_list_juicy_mem jm nil jm
| FLJM_cons: forall jm b lo hi bl jm2 jm'
                          (H: free (m_dry jm) b lo hi = Some (m_dry jm2))
                          (H0 : forall ofs : Z,
                        lo <= ofs < hi ->
                        perm_of_res (m_phi jm @ (b, ofs)) = Some Freeable),
                          free_juicy_mem jm (m_dry jm2) b lo hi H = jm2 ->
                          free_list_juicy_mem jm2 bl jm' ->
                          free_list_juicy_mem jm ((b,lo,hi)::bl) jm'.

Lemma perm_of_res_val : forall r, perm_of_res r = Some Freeable ->
  exists v pp, r = YES Share.top readable_share_top (VAL v) pp.
Proof.
(* Goal: forall (r : resource) (_ : @eq (option permission) (perm_of_res r) (@Some permission Freeable)), @ex memval (fun v : memval => @ex preds (fun pp : preds => @eq resource r (YES Share.top readable_share_top (VAL v) pp))) *)
  destruct r; simpl; try if_tac; try discriminate.
(* Goal: forall _ : @eq (option permission) match k with | VAL m => perm_of_sh sh | LK n i => @Some permission Nonempty | FUN f c => @Some permission Nonempty end (@Some permission Freeable), @ex memval (fun v : memval => @ex preds (fun pp : preds => @eq resource (YES sh r k p) (YES Share.top readable_share_top (VAL v) pp))) *)
  destruct k; try discriminate.
(* Goal: forall _ : @eq (option permission) (perm_of_sh sh) (@Some permission Freeable), @ex memval (fun v : memval => @ex preds (fun pp : preds => @eq resource (YES sh r (VAL m) p) (YES Share.top readable_share_top (VAL v) pp))) *)
  unfold perm_of_sh.
(* Goal: forall _ : @eq (option permission) (if writable0_share_dec sh then if @eq_dec Share.t Share.EqDec_share sh Share.top then @Some permission Freeable else @Some permission Writable else if readable_share_dec sh then @Some permission Readable else if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) (@Some permission Freeable), @ex memval (fun v : memval => @ex preds (fun pp : preds => @eq resource (YES sh r (VAL m) p) (YES Share.top readable_share_top (VAL v) pp))) *)
  repeat if_tac; try discriminate.
(* Goal: forall _ : @eq (option permission) (@Some permission Freeable) (@Some permission Freeable), @ex memval (fun v : memval => @ex preds (fun pp : preds => @eq resource (YES sh r (VAL m) p) (YES Share.top readable_share_top (VAL v) pp))) *)
  subst; intro; do 2 eexists; f_equal.
(* Goal: @eq (readable_share Share.top) r readable_share_top *)
  apply proof_irr.
Qed.

Lemma free_list_juicy_mem_i:
  forall jm bl m' F,
   free_list (m_dry jm) bl = Some m' ->
   app_pred (freeable_blocks bl * F) (m_phi jm) ->
   exists jm', free_list_juicy_mem jm bl jm'
                  /\ m_dry jm' = m'
                  /\ level jm = level jm'.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros jm bl; revert jm; induction bl; intros.
*
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 inv H; exists jm; split3; auto. constructor.
*
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) a bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 simpl freeable_blocks in H0. destruct a as [[b lo] hi].
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) (@pair (prod block Z) Z (@pair block Z b lo) hi) bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 rewrite sepcon_assoc in H0.
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) (@pair (prod block Z) Z (@pair block Z b lo) hi) bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 destruct (free_list_free _ _ _ _ _ _ H) as [m2 [? ?]].
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) (@pair (prod block Z) Z (@pair block Z b lo) hi) bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 generalize H0; intro H0'.
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) (@pair (prod block Z) Z (@pair block Z b lo) hi) bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 destruct H0 as [phi1 [phi2 [? [? H6]]]].

(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) (@pair (prod block Z) Z (@pair block Z b lo) hi) bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 assert (H10:= @juicy_free_lemma' jm b lo hi m2 phi1 _ _ H1 H0' H3 H0).
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 match type of H10 with m_phi ?A = _ => set (jm2:=A) in H10 end; subst.

 specialize (IHbl  jm2 m' F H2 H6).
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) (@pair (prod block Z) Z (@pair block Z b lo) hi) bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 destruct IHbl as [jm' [? [? ?]]].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 exists jm'; split3; auto.
 apply (FLJM_cons jm b lo hi bl jm2 jm' H1
   (juicy_free_aux_lemma (m_phi jm) b lo hi (freeable_blocks bl * F) H0') (eq_refl _) H4).
 rewrite <- H7.
(* Goal: @eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm2) *)
 unfold jm2.
(* Goal: @eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable (free_juicy_mem jm m2 b lo hi H1)) *)
 symmetry; apply free_juicy_mem_level.
Qed.

Lemma free_juicy_mem_ext:
  forall jm1 jm2 b lo hi m1 m2 H1 H2,
      jm1=jm2 -> m1=m2 ->
     free_juicy_mem jm1 m1 b lo hi H1 = free_juicy_mem jm2 m2 b lo hi H2.
Proof.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
intros. subst. proof_irr. auto.
Qed.


Lemma free_list_juicy_mem_lem:
  forall P jm bl jm',
     free_list_juicy_mem jm bl jm' ->
     app_pred (freeable_blocks bl * P) (m_phi jm) ->
     app_pred P (m_phi jm').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: @app_pred rmap ag_rmap P (m_phi jm') *)
 revert H0; induction H; simpl freeable_blocks.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 intros.  rewrite emp_sepcon in H0; auto.
(* Goal: forall _ : @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (VALspec_range (Z.sub hi lo) Share.top (@pair block Z b lo)) (freeable_blocks bl)) P) (m_phi jm), @app_pred rmap ag_rmap P (m_phi jm') *)
 rename H0 into H99. rename H1 into H0; rename H2 into H1.
(* Goal: forall _ : @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (VALspec_range (Z.sub hi lo) Share.top (@pair block Z b lo)) (freeable_blocks bl)) P) (m_phi jm), @app_pred rmap ag_rmap P (m_phi jm') *)
 intro.
(* Goal: @app_pred rmap ag_rmap P (m_phi jm') *)
 rewrite sepcon_assoc in H2.
(* Goal: @app_pred rmap ag_rmap P (m_phi jm') *)
 generalize H2; intro H2'.
(* Goal: @app_pred rmap ag_rmap P (m_phi jm') *)
 destruct H2 as [phi1 [phi2 [? [? ?]]]].
(* Goal: resource_decay (nextblock (m_dry jm2)) (m_phi jm2) (m_phi jm') *)
 apply IHfree_list_juicy_mem.
 pose proof  (@juicy_free_lemma' jm b lo hi _ phi1 _ _ H H2' H3 H2).
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (freeable_blocks bl) P) (m_phi jm2) *)
 match type of H5 with m_phi ?A = _ => set (jm3 := A) in H5 end.
 replace jm2 with jm3 by (subst jm3; rewrite <- H0; apply free_juicy_mem_ext; auto).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 subst; auto.
Qed.

Lemma xelements_app:
 forall A (rho: PTree.t A) i al bl,
    PTree.xelements rho i al ++ bl = PTree.xelements rho i (al++bl).
Proof.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 induction rho; simpl; intros; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 destruct o; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite IHrho1. simpl. rewrite IHrho2; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite IHrho1. simpl. rewrite IHrho2; auto.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma PTree_elements_remove: forall {A} (T: PTree.tree A) i e,
  In e (PTree.elements (PTree.remove i T)) ->
  In e (PTree.elements T) /\ fst e <> i.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  intros.
(* Goal: and (@In (prod positive A) e (@PTree.elements A T)) (not (@eq positive (@fst positive A e) i)) *)
  destruct e as [i0 v0].
(* Goal: and (@In (prod positive A) (@pair positive A i0 v0) (@PTree.elements A T)) (not (@eq positive (@fst positive A (@pair positive A i0 v0)) i)) *)
  apply PTree.elements_complete in H.
(* Goal: and (@In (prod positive A) (@pair positive A i0 v0) (@PTree.elements A T)) (not (@eq positive (@fst positive A (@pair positive A i0 v0)) i)) *)
  destruct (peq i0 i).
  + subst.
(* Goal: and (@In (prod positive A) (@pair positive A i v0) (@PTree.elements A T)) (not (@eq positive (@fst positive A (@pair positive A i v0)) i)) *)
    rewrite PTree.grs in H.
(* Goal: and (@In (prod positive A) (@pair positive A i v0) (@PTree.elements A T)) (not (@eq positive (@fst positive A (@pair positive A i v0)) i)) *)
    inversion H.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  + rewrite PTree.gro in H by auto.
(* Goal: and (@In (prod positive A) (@pair positive A i0 v0) (@PTree.elements A T)) (not (@eq positive (@fst positive A (@pair positive A i0 v0)) i)) *)
    split; [| simpl; auto].
(* Goal: @In (prod positive A) (@pair positive A i0 v0) (@PTree.elements A T) *)
    apply PTree.elements_correct.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    auto.
Qed.

Lemma stackframe_of_freeable_blocks:
  forall Delta f rho ge ve,
      genv_cenv ge = cenv_cs ->
      Forall (fun it => complete_type cenv_cs (snd it) = true) (fn_vars f) ->
      list_norepet (map fst (fn_vars f)) ->
      ve_of rho = make_venv ve ->
      guard_environ (func_tycontext' f Delta) (Some f) rho ->
       stackframe_of f rho |-- freeable_blocks (blocks_of_env ge ve).
Proof.
  intros until ve.
  intros HGG COMPLETE.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
 destruct H1. destruct H2 as [H7 _].
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@stackframe_of CS f (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 unfold stackframe_of.
 unfold func_tycontext' in H1.
 unfold typecheck_environ in H1.
 destruct H1 as [_ [?  _]].
 rewrite H0 in H1.
 unfold make_venv in H1.
 unfold var_types in H1.
 simpl in H1. unfold make_tycontext_v in H1.
 unfold blocks_of_env.
match goal with |- ?A |-- _ =>
 replace A
  with (fold_right (@sepcon _ _ _ _ _) emp
            (map (fun idt : ident * type => var_block Share.top idt rho)
            (fn_vars f)))
end.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 2: clear; induction (fn_vars f); simpl; f_equal; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 unfold var_block. unfold eval_lvar. simpl.
(* Goal: @eq (option (prod block type)) (make_venv ve' i) (@None (prod block type)) *)
  rewrite H0. unfold make_venv. forget (ge_of rho) as ZZ. rewrite H0 in H7; clear rho H0.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 revert ve H1 H7; induction (fn_vars f); simpl; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 case_eq (PTree.elements ve); simpl; intros; auto.
 destruct p as [id ?].
 pose proof (PTree.elements_complete ve id p). rewrite H0 in H2. simpl in H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 specialize (H7 id). unfold make_venv in H7. rewrite H2 in H7; auto.
 destruct p; inv H7.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 inv H.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 destruct a as [id ty]. simpl in *.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 simpl in COMPLETE. inversion COMPLETE; subst.
 clear COMPLETE; rename H5 into COMPLETE; rename H2 into COMPLETE_HD.
 specialize (IHl COMPLETE H4 (PTree.remove id ve)).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 assert (exists b, ve ! id = Some (b,ty)). {
  specialize (H1 id ty).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  rewrite PTree.gss in H1. destruct H1 as [[b ?] _]; auto. exists b; apply H.
 }
 destruct H as [b H].
 destruct (@PTree.elements_remove _ id (b,ty) ve H) as [l1 [l2 [? ?]]].
 rewrite H0.
 rewrite map_app. simpl map.
 apply derives_trans with (freeable_blocks ((b,0,@sizeof ge ty) ::  (map (block_of_binding ge) (l1 ++ l2)))).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 2:{
   clear.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   induction l1; simpl; try auto.
   destruct a as [id' [hi lo]]. simpl. rewrite <- sepcon_assoc. 
   rewrite (sepcon_comm (VALspec_range (@sizeof ge ty - 0) Share.top (b, 0))).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   rewrite sepcon_assoc. apply sepcon_derives; auto.
 }
 unfold freeable_blocks; simpl. rewrite <- H2.
 apply sepcon_derives.
(* Goal: @eq (option val) (@Map.get val (fun i : positive => @PTree.get val i e) i) (@Map.get val (te_of (construct_rho (filter_genv ge) ve' te')) i) *)
 unfold Map.get. rewrite H. rewrite eqb_type_refl.
(* case_eq (type_is_volatile ty); intros; simpl negb; cbv iota; *)
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 unfold memory_block. normalize. {
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
(* rewrite andp_assoc; apply derives_extract_prop; intros. *)
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
(* apply derives_extract_prop; intros. *)
  rename H6 into H99.
 normalize. (* don't know why we cannot do normalize at first *)
 rewrite memory_block'_eq.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 2: rewrite Ptrofs.unsigned_zero; omega.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 2:{
 rewrite Ptrofs.unsigned_zero. rewrite Zplus_0_r.
 rewrite Coqlib.nat_of_Z_eq.
 change (Ptrofs.unsigned Ptrofs.zero) with 0 in H99.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 pose proof (sizeof_pos ty); omega.
}
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable))))))) *)
 rewrite Z.sub_0_r.
 unfold memory_block'_alt.
 rewrite if_true by apply readable_share_top.
 rewrite Coqlib.nat_of_Z_eq.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 + rewrite HGG. auto.
 + pose proof (sizeof_pos ty); omega.
}
 eapply derives_trans; [ | apply IHl]; clear IHl.
 clear - H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 induction l; simpl; auto.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 destruct a as [id' ty']. simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply sepcon_derives; auto.
 replace (Map.get (fun id0 : positive => (PTree.remove id ve) ! id0) id')
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   with (Map.get (fun id0 : positive => ve ! id0) id'); auto.
(* Goal: @eq (option val) (@Map.get val (fun i : positive => @PTree.get val i e) i) (@Map.get val (te_of (construct_rho (filter_genv ge) ve' te')) i) *)
 unfold Map.get.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gro; auto.
 intros id' ty'; specialize (H1 id' ty').
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 {split; intro.
 - destruct H1 as [H1 _].
   assert (id<>id').
   intro; subst id'.
   clear - H3 H5; induction l; simpl in *. rewrite PTree.gempty in H5; inv H5.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
   destruct a; simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   rewrite PTree.gso in H5. auto. auto.
   destruct H1 as [v ?].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   rewrite PTree.gso; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   exists v. unfold Map.get. rewrite PTree.gro; auto.
 - unfold Map.get in H1,H5.
   assert (id<>id').
     clear - H5; destruct H5. intro; subst. rewrite PTree.grs in H. inv H.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   rewrite PTree.gro in H5 by auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   rewrite <- H1 in H5. rewrite PTree.gso in H5 by auto. auto.
 }
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 hnf; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (make_venv (PTree.remove id ve) id0) eqn:H5; auto.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (let (_, ty) := p in Tcons ty (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) (@snd ident type p)) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
 destruct p.
 unfold make_venv in H5.
 destruct (peq id id0).
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 subst.  rewrite PTree.grs in H5. inv H5.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gro in H5 by auto.
 specialize (H7 id0). unfold make_venv in H7. rewrite H5 in H7.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
 destruct H7; auto. inv H6; congruence.
Qed.

Definition maybe_retval (Q: environ -> mpred) retty ret :=
 match ret with
 | Some id => fun rho => Q (get_result1 id rho)
 | None =>
    match retty with
    | Tvoid => (fun rho => Q (globals_only rho))
    | _ => fun rho => EX v: val, Q (make_args (ret_temp::nil) (v::nil) rho)
    end
 end.

Lemma VALspec_range_free:
  forall n b phi1 jm,
  app_pred (VALspec_range n Share.top (b, 0)) phi1 ->
  join_sub phi1 (m_phi jm) ->
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {m' | free (m_dry jm) b 0 n = Some m' }.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @sig mem (fun m' : mem => @eq (option mem) (free (m_dry jm) b Z0 n) (@Some mem m')) *)
apply range_perm_free.
(* Goal: range_perm (m_dry jm) b Z0 n Cur Freeable *)
destruct H0 as [phi2 H0].
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
hnf; intros.
(* Goal: perm (m_dry jm) b ofs Cur Freeable *)
pose proof (juicy_mem_access jm (b,ofs)).
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
hnf. unfold access_at in H2. simpl in H2.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
destruct H as [H _]; specialize (H (b,ofs)).
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
hnf in H.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
rewrite if_true in H by (split; auto; omega).
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
destruct H as [v ?].
apply (resource_at_join _ _ _ (b,ofs)) in  H0.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
destruct H.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
hnf in H.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
rewrite H in H0.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
rewrite H2.
(* Goal: @Forall A P (@filter A f (@nil A)) *)
inv H0; simpl; apply join_top in RJ; subst sh3; rewrite perm_of_freeable; constructor.
Qed.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma Forall_filter: forall {A} P (l: list A) f, Forall P l -> Forall P (filter f l).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  intros.
(* Goal: @Forall A P (@filter A f l) *)
  induction l.
  + constructor.
  + inversion H; subst.
(* Goal: @Forall A P (@filter A f (@cons A a l)) *)
    apply IHl in H3.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    simpl.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
    simple_if_tac.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    - constructor; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    - auto.
Qed.

Lemma can_free_list:
  forall Delta F f jm ge ve te
  (NOREP: list_norepet (map (@fst _ _) (fn_vars f)))
  (COMPLETE: Forall (fun it => complete_type cenv_cs (snd it) = true) (fn_vars f))
  (HGG:  genv_cenv ge = cenv_cs),
   guard_environ (func_tycontext' f Delta) (Some f)
        (construct_rho (filter_genv ge) ve te) ->
    (F * stackframe_of f (construct_rho (filter_genv ge)ve te))%pred (m_phi jm) ->
   exists m2, free_list (m_dry jm) (blocks_of_env ge ve) = Some m2.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  intros.
  destruct H0 as [? [? [? [_ ?]]]].
  unfold stackframe_of in H1.
  unfold blocks_of_env in *.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
  destruct H as [_ [H _]]; clear - NOREP COMPLETE HGG H H0 H1. simpl in H.
  pose (F vl := (fold_right
          (fun (P Q : environ -> pred rmap) (rho : environ) => P rho * Q rho)
          (fun _ : environ => emp)
          (map (fun idt : ident * type => var_block Share.top idt) vl))).
  change ((F (fn_vars f)  (construct_rho (filter_genv ge) ve te)) x0) in H1.
  assert (forall id b t, In (id,(b,t)) (PTree.elements ve) ->
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
                In (id,t) (fn_vars f)). {
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
   intros.
    apply PTree.elements_complete in  H2.
    specialize (H id); unfold make_venv in H; rewrite H2 in H.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
     apply H.
  }
  clear H.
  assert (Hve: forall i bt, In (i,bt) (PTree.elements ve) -> ve ! i = Some bt)
    by apply PTree.elements_complete.
  assert (NOREPe: list_norepet (map (@fst _ _) (PTree.elements ve)))
    by apply PTree.elements_keys_norepet.
  forget (PTree.elements ve) as el.
  rename x0 into phi.
  assert (join_sub phi (m_phi jm)).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  econstructor; eauto.
  clear H0.
  forget (fn_vars f) as vl.
  revert vl phi jm H H1 H2 Hve NOREP NOREPe COMPLETE; induction el; intros;
    [ solve [simpl; eauto] | ].
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
  simpl in H2.
  destruct a as [id [b t]]. simpl in NOREPe,H2|-*.
  assert (H2': In (id,t) vl) by (apply H2 with b; auto).
  specialize (IHel (filter (fun idt => negb (eqb_ident (fst idt) id)) vl)).
  replace (F vl (construct_rho (filter_genv ge) ve te))
    with  (var_block Share.top (id,t) (construct_rho (filter_genv ge) ve te)
    * F (filter (fun idt => negb (eqb_ident (fst idt) id)) vl) (construct_rho (filter_genv ge) ve te)) in H1.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  2:{
    clear - H2' NOREP.
    induction vl; inv H2'.
    simpl in NOREP.
    inv NOREP.
(* Goal: or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (make_venv ve') i)) *)
    unfold F; simpl fold_right.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
    f_equal.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
    f_equal.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
    f_equal.
    replace (eqb_ident id id) with true
      by (symmetry; apply (eqb_ident_spec id id); auto).
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    simpl.
    clear - H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    induction vl; simpl; auto.
    replace (negb (eqb_ident (fst a) id)) with true.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
    f_equal.
    apply IHvl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    contradict H1. right; auto.
    pose proof (eqb_ident_spec (fst a) id).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    destruct (eqb_ident (fst a) id) eqn:?; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    elimtype False; apply H1. left. rewrite <- H; auto.
    transitivity
     (var_block Share.top a (construct_rho (filter_genv ge) ve te) *
         F vl (construct_rho (filter_genv ge) ve te)); [ | reflexivity].
    inv NOREP.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    rewrite <- IHvl; auto.
    repeat rewrite <- sepcon_assoc.
    simpl filter.
    replace (eqb_ident (fst a) id) with false.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    simpl.
    unfold F at 1.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    simpl.
    symmetry;
    rewrite (sepcon_comm (var_block _ _ _ )).
    repeat rewrite sepcon_assoc.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
    reflexivity.
    pose proof (eqb_ident_spec (fst a) id).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    destruct (eqb_ident (fst a) id); auto.
    assert (fst a = id) by (apply H0; auto).
    subst id.
    contradiction H2.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
    replace (fst a) with (fst (fst a, t)) by reflexivity.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    apply in_map; auto.
  }
  pose (H0:=True).
  destruct H1 as [phi1 [phi2 [? [? ?]]]].
  unfold var_block in H3.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
  normalize in H3.
  simpl in H3.
  assert (0 <= sizeof t) by (pose proof (sizeof_pos t); omega).
  simpl in H5.
  unfold eval_lvar, Map.get in H3. simpl in H3.
  unfold make_venv in H3.
  rewrite (Hve id (b,t)) in H3 by (left; auto).
  rewrite eqb_type_refl in H3.
  (*destruct (type_is_volatile t) eqn:?; try (simpl in H3; tauto).*)
  simpl in H3; destruct H3 as [H99 H3].
  change nat_of_Z with Z.to_nat in H3.
  rewrite memory_block'_eq in H3;
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
  try rewrite Ptrofs.unsigned_zero; try omega.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  2:{
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
   rewrite Z.add_0_r; rewrite Z2Nat.id by omega. change (Ptrofs.unsigned Ptrofs.zero) with 0 in H99; omega.
  }
  unfold memory_block'_alt in H3.
  rewrite Ptrofs.unsigned_zero in H3.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
  rewrite Z2Nat.id in H3 by omega.
  rewrite if_true in H3 by apply readable_share_top.
  assert (join_sub phi1 (m_phi jm)) as H7
   by ( apply join_sub_trans with phi; auto; eexists; eauto).
  destruct (VALspec_range_free _ _ _ _ H3 H7)
   as [m3 ?H].
  assert (VR: app_pred (VALspec_range (sizeof t-0) Share.top (b, 0) * TT) (m_phi jm)).
    clear - H3 H7. destruct H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  rewrite Z.sub_0_r; exists phi1; exists x; split3; auto.
  pose (jm3 := free_juicy_mem _ _ _ _ _ H8 ).
  destruct H as [phix H].
  destruct (join_assoc H1 H) as [phi3 []].
  assert (phi3 = m_phi jm3).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  { subst jm3; symmetry; eapply juicy_free_lemma'; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    rewrite Z.sub_0_r; auto.
  }
  subst phi3.  assert (join_sub phi2 (m_phi jm3)) as Hphi2 by (eexists; eauto).
  destruct (IHel phi2 jm3 Hphi2) as [m4 ?]; auto; clear IHel.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  + intros.
    specialize (H2 id0 b0 t0).
    spec H2; [ auto |].
    assert (id0 <> id).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
    {
      clear - NOREPe H11.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
      inv NOREPe. intro; subst.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      apply H1. change id with (fst (id,(b0,t0))); apply in_map; auto.
    }
    clear - H2 H12.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    induction vl; simpl in *; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    destruct H2. subst a. simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    replace (eqb_ident id0 id) with false; simpl; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    pose proof (eqb_ident_spec id0 id); destruct (eqb_ident id0 id); simpl in *; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    contradiction H12; apply H; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    pose proof (eqb_ident_spec (fst a) id); destruct (eqb_ident (fst a) id); simpl in *; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  + intros; eapply Hve; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    right; auto.
  + clear - NOREP.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    induction vl; simpl; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    pose proof (eqb_ident_spec (fst a) id); destruct (eqb_ident (fst a) id); simpl in *; auto.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
    assert (fst a = id) by ( apply H; auto); subst.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    apply IHvl; inv NOREP; auto.
    inv NOREP.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    constructor; auto.
    clear - H2.
    contradict H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    induction vl; simpl in *; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    destruct (eqb_ident (fst a0) id); simpl in *; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    destruct H2; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  + inv NOREPe; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  + apply Forall_filter; auto.
  + pose proof (proj1 (Forall_forall _ _) COMPLETE (id, t) H2').
    simpl in H11.
    exists m4.
    rewrite HGG.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    rewrite H8. auto.
Qed.

Lemma necR_m_dry':
  forall jm jm', m_dry jm = m_dry jm' ->
                (necR (m_phi jm) (m_phi jm')) ->
            necR jm jm'.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @necR juicy_mem juicy_mem_ageable jm jm' *)
remember (m_phi jm) as phi.
(* Goal: @necR juicy_mem juicy_mem_ageable jm jm' *)
remember (m_phi jm') as phi'.
unfold necR in *.
rewrite clos_rt_rt1n_iff in *.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
revert jm jm' Heqphi Heqphi' H; induction H0; intros; subst.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm' *)
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm' *)
replace jm' with jm. constructor 1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply juicy_mem_ext; auto.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm' *)
destruct (can_age_jm jm) as [jm1 ?].
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
destruct (age1 (m_phi jm)) eqn:?; congruence.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
constructor 2 with jm1; auto.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm1 jm' *)
apply age1_juicy_mem_unpack in H2. destruct H2.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm1 jm' *)
apply IHclos_refl_trans_1n.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
hnf in H,H2. congruence.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
congruence.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
congruence.
Qed. (* maybe don't need this? *)

Lemma age_juicy_mem_i:
  forall jm jm', m_dry jm = m_dry jm' ->
        age (m_phi jm) (m_phi jm') ->
       age jm jm'.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
hnf in H0 |-*.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold age1; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply age1_juicy_mem_unpack'; auto.
Qed. (* maybe don't need this? *)

Lemma free_juicy_mem_resource_decay:
  forall jm b lo hi m' jm'
     (H : free (m_dry jm) b lo hi = Some m')
     (H0 : forall ofs : Z,  lo <= ofs < hi ->
             perm_of_res (m_phi jm @ (b, ofs)) = Some Freeable),
    free_juicy_mem jm m' b lo hi H = jm' ->
    resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 subst jm'. simpl.
(* Goal: resource_decay (nextblock (m_dry jm)) (m_phi jm) (inflate_free jm b lo hi) *)
 apply (inflate_free_resource_decay _ _ _ _ _ H H0).
Qed.

Lemma free_list_resource_decay:
  forall bl jm jm',
  free_list_juicy_mem jm bl jm' ->
  resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction 1; intros.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
apply resource_decay_refl; intros.
(* Goal: @eq resource (resource_at (m_phi jm) l) (NO Share.bot bot_unreadable) *)
(* Goal: resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm') *)
apply (juicy_mem_alloc_cohere jm l H).
(* Goal: resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm') *)
apply resource_decay_trans with (nextblock (m_dry jm)) (m_phi jm2).
(* Goal: Pos.le (nextblock (m_dry jm')) (nextblock (m_dry jm')) *)
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
apply Pos.le_refl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply free_juicy_mem_resource_decay; eauto.
rewrite <- (nextblock_free _ _ _ _ _ H).
(* Goal: resource_decay (nextblock (m_dry jm2)) (m_phi jm2) (m_phi jm') *)
apply IHfree_list_juicy_mem.
Qed.

Definition tc_fn_return (Delta: tycontext) (ret: option ident) (t: type) :=
 match ret with
 | None => True
 | Some i => match (temp_types Delta) ! i with Some t' => t=t' | _ => False end
 end.

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
Lemma derives_refl' {A: Type}  `{ageable A}:
    forall P Q: pred A, P=Q -> P |-- Q.
Proof.  intros; subst; apply derives_refl. Qed.

 Lemma free_juicy_mem_core:
  forall jm m b lo hi H
   (H0 : forall ofs : Z,
     lo <= ofs < hi -> perm_of_res (m_phi jm @ (b, ofs)) = Some Freeable),
   core (m_phi (free_juicy_mem jm m b lo hi H)) = core (m_phi jm).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: @eq rmap (@core rmap Join_rmap Sep_rmap (m_phi (free_juicy_mem jm m b lo hi H))) (@core rmap Join_rmap Sep_rmap (m_phi jm)) *)
 apply rmap_ext.
 do 2  rewrite level_core.
(* Goal: @eq nat (@level rmap ag_rmap (m_phi (free_juicy_mem jm m b lo hi H))) (@level rmap ag_rmap (m_phi jm)) *)
(* Goal: forall l : AV.address, @eq resource (resource_at (@core rmap Join_rmap Sep_rmap (m_phi (free_juicy_mem jm m b lo hi H))) l) (resource_at (@core rmap Join_rmap Sep_rmap (m_phi jm)) l) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi (free_juicy_mem jm m b lo hi H)))) (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) *)
 apply free_juicy_mem_level.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
 repeat rewrite <- core_resource_at.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 simpl. unfold inflate_free; simpl;  rewrite resource_at_make_rmap.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (m_phi jm @ l) eqn:?; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 if_tac; rewrite !core_NO; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 if_tac. rewrite core_YES, core_NO; auto. rewrite !core_YES; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 if_tac; auto.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 destruct l; destruct H1; subst. specialize (H0 z).
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@nil (prod ident type))) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@cons (prod ident type) a vars)) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 spec H0; [omega | ]. rewrite Heqr in H0. inv H0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite !ghost_of_core, free_juicy_mem_ghost; auto.
Qed.

Lemma same_glob_funassert':
  forall Delta1 Delta2 rho rho',
     (forall id, (glob_specs Delta1) ! id = (glob_specs Delta2) ! id) ->
      ge_of rho = ge_of rho' ->
              funassert Delta1 rho = funassert Delta2 rho'.
Proof.
assert (forall Delta Delta' rho rho',
             (forall id, (glob_specs Delta) ! id = (glob_specs Delta') ! id) ->
             ge_of rho = ge_of rho' ->
             funassert Delta rho |-- funassert Delta' rho').
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @derives rmap ag_rmap (funassert Delta rho) (funassert Delta' rho') *)
(* Goal: forall (Delta1 Delta2 : tycontext) (rho rho' : environ) (_ : forall id : positive, @eq (option funspec) (@PTree.get funspec id (glob_specs Delta1)) (@PTree.get funspec id (glob_specs Delta2))) (_ : @eq genviron (ge_of rho) (ge_of rho')), @eq mpred (funassert Delta1 rho) (funassert Delta2 rho') *)
unfold funassert.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
intros w [? ?]; split.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
clear H2; intro id. rewrite <- (H id), <- H0; auto.
intros loc fs w' Hw' H4; destruct (H2 loc fs w' Hw' H4)  as [id H3].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists id; rewrite <- (H id), <- H0; auto.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply pred_ext; apply H; intros; auto.
Qed.

Lemma semax_call_external:
forall (Delta : tycontext) (A : TypeTree)
  (P Q Q' : forall ts, dependent_type_functor_rec ts (AssertTT A) mpred)
  (ts: list Type) (x : dependent_type_functor_rec ts A mpred)
  (F : environ -> pred rmap) (F0 : assert)
  (ret : option ident) (params : list (ident * type)) (retty : type) cc
  (a : expr) (bl : list expr) (R : ret_assert) (psi : genv) (vx : env)
  (tx : temp_env) (k : cont) (rho : environ) (ora : OK_ty) (jm : juicy_mem)
  (b : block)
 (Hora : juicy_mem_op (ext_compat ora) jm)
 (TC0 : Cop.classify_fun (typeof a) =
      Cop.fun_case_f (type_of_params params) retty cc)
 (TCret : tc_fn_return Delta ret retty)
 (TC1 : (|> tc_expr Delta a rho) (m_phi jm))
 (TC2 : (|> tc_exprlist Delta (map snd params) bl rho) (m_phi jm))
 (TC3 : guard_environ Delta (current_function k) rho)
 (TC5 : retty = Tvoid -> ret = None)
 (H : closed_wrt_modvars (Scall ret a bl) F0)
 (HR : RA_normal R =
     (fun rho0 : environ =>
      EX  old : val,
      substopt ret old F rho0 * maybe_retval (Q ts x) retty ret rho0))
 (HGG:  genv_cenv psi = cenv_cs)
 (H0 : rho = construct_rho (filter_genv psi) vx tx)
 (H3 : eval_expr a rho = Vptr b Ptrofs.zero)
 (H4 : (funassert Delta rho) (m_phi jm))
 (H1 : (rguard Espec psi Delta
        (frame_ret_assert R F0) k) (level (m_phi jm)))
 (H11 : forall vl : environ, (!|>(Q' ts x vl <=> Q ts x vl)) (m_phi jm))
 (H14 : (|>(F0 rho * F rho *
          P ts x
            (make_args (map fst params)
               (eval_exprlist (map snd params) bl rho) rho))) (m_phi jm))
 (n : nat)
 (H2 : level (m_phi jm) = S n)
 (H15 : (believe_external Espec psi (Vptr b Ptrofs.zero) (params, retty) cc A P Q')
        (level (m_phi jm))),
 exists (c' : corestate) (m' : juicy_mem),
  jstep (cl_core_sem psi) (State vx tx (Kseq (Scall ret a bl) :: k)) jm c' m' /\
  jm_bupd ora (jsafeN OK_spec psi n ora c') m'.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
destruct TC3 as [TC3 TC3'].
rewrite <- snd_split in TC2.
rewrite <- level_juice_level_phi in H2.
destruct (levelS_age1 _ _ H2) as [jm' Hage].
specialize (TC1 (m_phi jm') (t_step _ _ _ _ (age_jm_phi Hage))).
specialize (TC2 (m_phi jm') (t_step _ _ _ _ (age_jm_phi Hage))).
assert (H21 := eval_exprlist_relate Delta (params,retty) bl psi vx tx _
      jm' TC2 TC3 HGG H0
      (mkfunction retty cc params nil nil Sskip)
      (eq_refl _)). simpl in H21.
rewrite snd_split in TC2.

unfold believe_external in H15.
destruct (Genv.find_funct psi (Vptr b Ptrofs.zero)) eqn:H22; try (contradiction H15).
destruct f eqn:Ef; try (contradiction H15).

destruct H15 as [[H5 H15] Hretty]. hnf in H5.
destruct H5 as [H5 [H5' [Eef Hlen]]]. subst c.
inversion H5. subst t0. rename t into tys. subst rho.
specialize (H15 psi ts x n).
spec H15; [constructor 1; rewrite <- level_juice_level_phi, H2; constructor | ].
specialize (H15
  (F0 (construct_rho (filter_genv psi) vx tx) *
          F (construct_rho (filter_genv psi) vx tx))
   (typlist_of_typelist tys)
  (eval_exprlist (snd (split params)) bl
                  (construct_rho (filter_genv psi) vx tx))
   jm').
spec H15; [ apply age_level in Hage; omega | ].
specialize (H15 _ (necR_refl _)).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
spec H15. { clear H15.
assert ((|> (P ts x
      (make_ext_args (filter_genv psi) (map fst params)
         (eval_exprlist (snd (split params)) bl
            (construct_rho (filter_genv psi) vx tx))) *
    (F0 (construct_rho (filter_genv psi) vx tx) *
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
     F (construct_rho (filter_genv psi) vx tx)))) (m_phi jm)). {
eapply later_derives; try apply H14.
rewrite sepcon_comm.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply sepcon_derives; auto.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
apply derives_refl'. f_equal.
rewrite H7 in TC2.
clear - TC2 H7 Hlen.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
revert bl tys TC2 H7 Hlen; induction params; destruct bl; simpl; intros; auto.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
{ destruct tys; try congruence.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
simpl in Hlen. destruct a. destruct (split params). inv Hlen.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
destruct a. revert TC2. case_eq (split params). intros l1 l2 Heq. simpl.
  intros; inv TC2.
}
destruct tys.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
simpl in Hlen. destruct a. destruct (split params). inv Hlen.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
destruct a. revert TC2. case_eq (split params). intros l1 l2 Heq.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
  simpl in *. intros TC2.
unfold tc_exprlist in TC2; simpl in TC2.
repeat rewrite denote_tc_assert_andp in TC2.
destruct TC2 as [[? ?] ?].
inversion H7.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
rewrite Heq in *. simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
specialize (IHparams _ _ H1). spec IHparams. inv H3; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite IHparams; auto.
}
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
rewrite fst_split.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{ (* typechecking arguments *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  rewrite Eef; simpl.
  clear -TC2 TC3.
  revert bl TC2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  induction params. now auto.
  destruct a as (id, ty).
  intros [ | b bl] TC2.
  - rewrite snd_split in *.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
    simpl in *.
    inversion TC2.
  - rewrite snd_split.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
    simpl in *.
    unfold tc_exprlist in *.
    simpl in TC2.
    repeat rewrite denote_tc_assert_andp in TC2.
    destruct TC2 as ((tcb & tcb') & tcbl).
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
    split.
    + pose proof tc_val_sem_cast _ _ _ _ _ TC3 tcb tcb' as tc.
      apply tc_val_has_type.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      auto.
    + rewrite <-snd_split.
      apply (IHparams bl).
      apply tcbl.
}
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i e) (@None (prod block type)) *)
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id (@PTree.set (prod block type) id0 (@pair block type b1 ty) e)) (@Some (prod block type) v)) *)
apply H0.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm *)
(* Goal: @eq juicy_mem jm jm' *)
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm' *)
constructor 1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply age_jm_phi; auto.
}
clear H14 TC2.
destruct H15 as [x' H15].
clear H5.
destruct H15 as [H5 H15].
specialize (H15 (opttyp_of_type retty)).
do 3 red in H15.

assert (Hty: type_of_params params = tys).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{ clear -H7 Hlen.
  rewrite H7. clear H7. revert tys Hlen. induction params.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  simpl. destruct tys; auto. inversion 1.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  intros; simpl. destruct a. case_eq (split params). intros l1 l2 Heq. simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  destruct tys; auto. simpl. rewrite Heq in IHparams. rewrite IHparams; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  simpl in Hlen|-*. rewrite Heq in Hlen. inv Hlen. rewrite Heq. auto. }
eexists; exists jm'.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: @Forall A P (@filter A f (@nil A)) *)
econstructor.
rewrite (age_jm_dry Hage).
eapply step_call_external; try eassumption.
eapply eval_expr_relate; try eassumption.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
reflexivity.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) ty) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) ty) match @Map.get (prod block type) (make_venv ve) id with | Some (pair b ty' as p) => if eqb_type (@snd ident type (@pair ident type id ty)) ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end))) (m_phi j) *)
rewrite H3.
rewrite H22.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
rewrite Hty. reflexivity.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply age1_resource_decay; auto.
split; [apply age_level; auto|].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply age1_ghost_of, age_jm_phi; auto.
(* Goal: forall _ : @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (VALspec_range (Z.sub hi lo) Share.top (@pair block Z b lo)) (freeable_blocks bl)) P) (m_phi jm), @app_pred rmap ag_rmap P (m_phi jm') *)
apply jm_bupd_intro.
destruct n as [ | n ].
(* Goal: @Forall A P (@filter A f (@nil A)) *)
constructor.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply jsafeN_external with (x0 := x'); eauto.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
reflexivity.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite Eef. subst tys. apply H5; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
{ eapply pred_hereditary; eauto. }
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
specialize (H15 ret0 z').
change ((ext_spec_post' Espec e x' (genv_symb_injective psi) (opttyp_of_type retty) ret0 z' >=>
        juicy_mem_op
          (Q' ts x (make_ext_rval  (filter_genv psi) ret0) *
              (F0 (construct_rho (filter_genv psi) vx tx) *
               F (construct_rho (filter_genv psi) vx tx)))) (level jm')) in H15.
assert (level jm' > level m')%nat.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct H6 as (?&?&?); auto.
}
apply (pred_nec_hereditary _ _ (level m')) in H15;
 [ | apply nec_nat; omega].
rewrite Eef in *.
specialize (H15 m' (le_refl _) _ (necR_refl _) H8).

pose (tx' := match ret,ret0 with
                   | Some id, Some v => PTree.set id v tx
                   | _, _ => tx
                   end).

specialize (H1 EK_normal None tx' vx (m_phi m')).
spec H1.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{ clear - Hage H9.
  change (level jm >= level m')%nat.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
  apply age_level in Hage. omega.
}
rewrite proj_frame_ret_assert in H1.
simpl proj_ret_assert in H1.
rewrite HR in H1; clear R HR.
simpl exit_cont in H1.
do 3 red in H5.
specialize (H1 _ (necR_refl _)).

assert (Htc: tc_option_val retty ret0).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{clear - TCret TC3 H0 TC5 H15 Hretty Hretty0 H6 H9 Hage.
 destruct H15 as [phi1 [phi2 [Ha [Hb Hc]]]].
 specialize (Hretty ts x ret0 phi1).
 spec Hretty.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 { apply join_level in Ha. destruct Ha as [? ?].
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
   rewrite H. cut ((level jm > level jm')%nat). intros.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
   simpl. unfold natLevel. do 2 rewrite <-level_juice_level_phi. omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
   apply age_level in Hage. omega.
 }
 specialize (Hretty phi1).
 spec Hretty. apply rt_refl.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 spec Hretty. split. apply Hb. apply Hretty0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 simpl in Hretty. auto.
}

(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
spec H1. { clear H1.
split; [split; [split |] |].
*
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 split.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 2:{
 clear - TC3.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 destruct TC3; simpl in *.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i e) (@None (prod block type)) *)
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id (@PTree.set (prod block type) id0 (@pair block type b1 ty) e)) (@Some (prod block type) v)) *)
 destruct ret; try apply H0.
 }
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 simpl.
 destruct TC3 as [TC3 _].
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 destruct ret; try apply TC3. {
 clear - TCret TC3 H0 TC5 H15 Hretty Hretty0 H6 H8 Hage.
 simpl in TCret.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
 destruct ((temp_types Delta) ! i) eqn:?; try contradiction.
 subst retty.
 unfold tx' in *; clear tx'. simpl in TC3.
 assert (Hu: exists u, opttyp_of_type t = Some u).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 { clear - TC5; destruct t as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ];
   try (eexists; reflexivity).
   spec TC5; [auto | congruence].
 }
 destruct Hu as [u Hu]. (*rewrite Hu in *.*) clear TC5.
 destruct H15 as [phi1 [phi2 [Ha [Hb Hc]]]].
 specialize (Hretty ts x ret0 phi1).
 spec Hretty.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 { apply join_level in Ha. destruct Ha as [? ?].
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
   rewrite H. cut ((level jm > level jm')%nat). intros.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
   simpl. unfold natLevel. do 2 rewrite <-level_juice_level_phi. destruct H6; omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
   apply age_level in Hage. omega.
 }
 specialize (Hretty phi1).
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 spec Hretty. apply rt_refl. spec Hretty. split. apply Hb. apply Hretty0. simpl in Hretty.
 unfold typecheck_temp_environ. intros id ty Hty.
 destruct (ident_eq i id).
 + subst i.
 rewrite Heqo in Hty.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct ret0; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 inversion Hty. subst t. simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 exists v. split. rewrite <-map_ptree_rel, Map.gss; auto.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
 assert (ty <> Tvoid). { destruct ty; try inv Hu; intros C; congruence. }
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 assert (tc_val ty v). { destruct ty; auto. }
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply tc_val_tc_val'; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 inversion Hty. subst t. simpl.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 assert (ty = Tvoid). { destruct ty; auto; inv Hretty. } subst ty.
 simpl in Hu. inv Hu.
 + destruct ret0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite <-map_ptree_rel, Map.gso; auto.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 assert (t = Tvoid). { destruct t; auto; inv Hretty. } subst t.
 simpl in Hu. inv Hu.
}
*
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (current_function k); auto.
*
do 3 red in H15.
rewrite (sepcon_comm (F0 _)) in H15.
rewrite <- sepcon_assoc in H15.
assert (H15': ((!!tc_option_val retty ret0 && Q ts x (make_ext_rval (filter_genv psi) ret0)) *
       F (construct_rho (filter_genv psi) vx tx) *
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
       F0 (construct_rho (filter_genv psi) vx tx))%pred (m_phi m')). {
rewrite sepcon_assoc in H15|-*.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct H15 as [w1 [w2 [? [? ?]]]]; exists w1; exists w2; split3; auto.
clear - H1 H9 H10 H11 Hage Hretty Hretty0.
specialize (H11 (make_ext_rval (filter_genv psi) ret0) (level (m_phi jm'))).
specialize (Hretty ts x ret0 w1).
spec H11.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm *)
(* Goal: @eq juicy_mem jm jm' *)
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm' *)
constructor 1.
repeat rewrite <- level_juice_level_phi.
apply age_level in Hage. rewrite Hage.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
reflexivity.
spec Hretty.
repeat rewrite <- level_juice_level_phi.
apply age_level in Hage. rewrite Hage.
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
apply join_level in H1. destruct H1.
rewrite H. change (S (level jm') >= level m')%nat.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
omega.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply Hretty; auto. split; auto.
destruct (H11 w1) as [? _].
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
apply join_level in H1. destruct H1.
rewrite <- level_juice_level_phi in *.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
omega.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply H; auto.
}
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
clear H15.
revert Htc.
normalize in H15'.
do 2 red in H1.
intros Htc.
rewrite (sepcon_comm (Q _ _ _)) in H15'.
unfold seplog.sepcon, seplog.LiftSepLog .
rewrite <- exp_sepcon1.
eapply sepcon_derives; [apply sepcon_derives | | apply H15']; clear H15'.

+ (* F *)
  destruct TC3 as [TC3 _].
  hnf in TC3; simpl in TC3.
 hnf in TCret.
apply exp_right with
  match ret with
       | Some id =>
           match tx ! id with
           | Some old => old
           | None => Vundef
           end
       | None => Vundef
       end.
unfold substopt.
unfold tx' in *; clear tx'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct ret; auto.
destruct ((temp_types Delta) ! i) as [ti|] eqn:H29; try contradiction.
specialize (TC3 _ _ H29).
destruct TC3 as [v [? ?]].
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
unfold subst.
apply derives_refl'.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
f_equal.
unfold env_set, construct_rho.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl. f_equal.
unfold Map.set,Map.get, make_tenv in H10 |- *; rewrite H10.
destruct (type_eq retty Tvoid).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
spec TC5; auto. inv TC5.
extensionality j.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
if_tac. subst j. auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct ret0; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso; auto.
+ (* Q *)
destruct (type_eq retty Tvoid).
subst retty. unfold maybe_retval.
hnf in H1.
destruct ret0; try contradiction.
simpl make_ext_rval.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
spec TC5; auto. unfold tx' in *; subst ret.
apply derives_refl.
destruct ret0; hnf in H1; simpl in H1.
assert (tc_val retty v).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct retty; try congruence; auto.
clear H1.
unfold maybe_retval.
destruct ret.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
 apply derives_refl'; f_equal.
unfold tx'.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold make_ext_rval, get_result1; simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold ret_temp, eval_id, env_set; simpl.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
f_equal.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold Map.get, make_tenv; simpl.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
rewrite PTree.gss; reflexivity.
apply derives_trans with
  (EX  v0 : val, Q ts x (make_args (ret_temp :: nil) (v0 :: nil) (construct_rho (filter_genv psi) vx tx'))).
apply exp_right with v.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold make_args, make_ext_rval; simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold env_set, globals_only; simpl.
apply derives_refl.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
destruct retty; try congruence.
repeat match goal with
| _ => destruct retty; try contradiction
| _ => congruence (* 8.5 *)
end.
+
clear - H.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
apply derives_refl'; apply H; intros.
unfold tx'; clear.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold modifiedvars; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct ret; simpl; auto.
destruct (ident_eq i0 i).
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
subst.
(* Goal: @In positive i (@map (prod ident type) ident (@fst ident type) (@cons (prod ident type) (@pair ident type i0 t) vars)) *)
left. unfold insert_idset. rewrite PTree.gss; apply I.
(* Goal: or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (make_venv ve') i)) *)
right.
unfold Map.get, make_tenv.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct ret0; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso by auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto.
*
assert (H4': (funassert Delta (construct_rho (filter_genv psi) vx tx)) (m_phi m')).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{ clear - Hage H6 H4.
destruct H6 as (?&?&?).
destruct H4.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
assert (Hnec: necR (m_phi jm) (m_phi jm')). {
  cut (age jm jm'). intro Hx.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  constructor. apply age_jm_phi in Hx; auto. eauto.
}
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
* intros id fs ???.
specialize (H2 id fs (m_phi jm')).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
specialize (H2 Hnec); spec H2; auto.
destruct H2 as [b [? ?]].
destruct H1 as [H1 H1'].
specialize (H1 (b,0)).
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
unfold func_at in H6. destruct fs; simpl in *.
rewrite H6 in H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply (necR_PURE (m_phi m') a') in H1; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists b. split; auto. rewrite H1. simpl.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
f_equal. f_equal.
assert (Hlev1: (level (m_phi m') >= level a')%nat).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
{ apply necR_level in H4; auto. }
extensionality ts x.
extensionality b0 rho.
rewrite !fmap_app.
match goal with
| |- ?A (?B (?C ?D)) = _ => change (A (B (C D))) with ((A oo B oo C) D)
end.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
rewrite approx_oo_approx' by omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
rewrite approx_oo_approx' by omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
rewrite approx'_oo_approx by omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
rewrite approx'_oo_approx by omega.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto.
* intros b fs ???.
specialize (H3 b fs (m_phi jm')).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
specialize (H3 Hnec); spec H3; auto.
destruct H1 as [H1 H1'].
specialize (H1' (b,0)).
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
destruct fs; simpl in *.
destruct H5 as [b0 ?].
destruct (m_phi m' @ (b,0)) eqn:?.
eapply necR_NOx in Heqr; try apply H4. inversion2 H5 Heqr.
eapply necR_YES in Heqr; try apply H4. inversion2 H5 Heqr.
destruct H1' as [pp ?].
rewrite H6.
exists pp.
assert (H9 := necR_PURE _ _ _ _ _ H4 Heqr).
rewrite H5 in H9. inv H9.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
f_equal.
pose proof (resource_at_approx (m_phi jm') (b,0)).
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
rewrite H6 in H; simpl in H.
(* Goal: @eq (forall ts : list Type, _functor (dependent_type_functor_rec ts A) (@pred rmap ag_rmap)) P Q *)
injection H; intro. symmetry in H7. apply H7.
}
match type of H4' with ?A => match goal with |- ?B => replace B with A; auto end end.
}
exists
match ret0 with
| Some v =>
    match ret with
    | Some id => (State vx (PTree.set id v tx) k)
    | None => (State vx tx k) (* bogus *)
    end
| None => match ret with
          | Some _ => (State vx tx k) (* bogus *)
          | None => (State vx tx k)
          end
end.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
unfold cl_after_external.
revert Htc TC5.
destruct (type_eq retty Tvoid).
+ subst retty. simpl. destruct ret0; try solve[inversion 1].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  intros _. intros X; spec X; auto. rewrite X; auto.
+ intros Hret0.
  assert (Hv: exists v, ret0 = Some v).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  { revert Hret0.
    destruct retty; destruct ret0; simpl;
      try solve[intros _; eexists; eauto]; try inversion 1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    exfalso; auto. }
  revert TCret.
  unfold tc_fn_return.
  destruct Hv as [v Hv]. rewrite Hv.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  destruct ret; auto.
+
destruct H6 as (?&?&?).
subst n'.
rewrite level_juice_level_phi.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct ret; destruct ret0; apply assert_safe_jsafe; auto.
Qed.

Lemma alloc_juicy_variables_age:
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  forall {ge rho jm jm1 vl rho' jm' jm1'},
   age jm jm1 -> age jm' jm1' ->
   alloc_juicy_variables ge rho jm vl = (rho', jm') ->
   alloc_juicy_variables ge rho jm1 vl = (rho', jm1').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  intros.
  revert jm jm1 H rho H1.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  induction vl; intros.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
    simpl in *; inv H1.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
    hnf in H0,H. congruence.
  }
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
  destruct a.
  simpl in H1|-*.
  eapply IHvl; [| rewrite <- (age_jm_dry H); eassumption].
  apply age_juicy_mem_i; [simpl; rewrite (age_jm_dry H); auto |].
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  simpl.
  apply rmap_age_i.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    unfold after_alloc; simpl. repeat rewrite level_make_rmap.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    apply age_level. apply age_jm_phi; auto.
  }
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  intro. unfold resource_fmap; simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  unfold after_alloc; simpl.
(* Goal: @eq resource (@core resource Join_resource Sep_resource (resource_at (m_phi jm) loc)) (@core resource Join_resource Sep_resource (resource_at (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))) loc)) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) (ghost_of (@core rmap Join_rmap Sep_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))))) *)
  do 2  rewrite resource_at_make_rmap.
(* Goal: @eq resource (@core resource Join_resource Sep_resource (resource_at (m_phi jm) loc)) (@core resource Join_resource Sep_resource (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))) loc)) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) (ghost_of (@core rmap Join_rmap Sep_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))))) *)
  unfold after_alloc'.
  if_tac; [rewrite if_true | rewrite if_false].
  + f_equal.
  + rewrite <- (age_jm_dry H); assumption.
  + clear H1.
    destruct (m_phi jm @ l) eqn:?.
    - symmetry;  eapply necR_NOx; try apply Heqr.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      constructor 1. apply age_jm_phi; auto.
    - symmetry.
(* Goal: @eq nat (@level rmap ag_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (fun loc : AV.address => if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) loc then NO Share.bot bot_unreadable else resource_at (m_phi jm) loc)) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (fun loc : AV.address => if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) loc then NO Share.bot bot_unreadable else resource_at (m_phi jm) loc) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (@FunctionalExtensionality.functional_extensionality AV.address resource (fun x : AV.address => resource_fmap (approx (@level rmap ag_rmap (m_phi jm))) (approx (@level rmap ag_rmap (m_phi jm))) (if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) x then NO Share.bot bot_unreadable else resource_at (m_phi jm) x)) (fun loc : AV.address => if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) loc then NO Share.bot bot_unreadable else resource_at (m_phi jm) loc) (fun l : AV.address => let (b', ofs') as p return (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm))) (approx (@level rmap ag_rmap (m_phi jm))) (if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) p then NO Share.bot bot_unreadable else resource_at (m_phi jm) p)) (if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) p then NO Share.bot bot_unreadable else resource_at (m_phi jm) p)) := l in if adr_range_dec (@pair block Z b lo) (Z.sub hi lo) (@pair block Z b' ofs') as s return (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm))) (approx (@level rmap ag_rmap (m_phi jm))) (if s then NO Share.bot bot_unreadable else resource_at (m_phi jm) (@pair block Z b' ofs'))) (if s then NO Share.bot bot_unreadable else resource_at (m_phi jm) (@pair block Z b' ofs'))) then @eq_refl resource (NO Share.bot bot_unreadable) else resource_at_approx (m_phi jm) (@pair block Z b' ofs'))) (ghost_of_approx (m_phi jm))))) (@level rmap ag_rmap (m_phi jm)) *)
      rewrite level_make_rmap.
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm *)
(* Goal: @eq juicy_mem jm jm' *)
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm jm' *)
      eapply necR_YES. constructor 1. eapply age_jm_phi. eassumption.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      auto.
    - rewrite level_make_rmap.
(* Goal: @eq (option val) (@Some val v) (@PTree.get val i te') *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
      symmetry.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
      eapply necR_PURE. constructor 1. eapply age_jm_phi. eassumption.  auto.
  + rewrite <- (age_jm_dry H); assumption.
  + unfold after_alloc; rewrite !ghost_of_make_rmap, level_make_rmap.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    symmetry; apply age1_ghost_of, age_jm_phi; auto.
Qed.

Lemma alloc_juicy_variables_resource_decay:
  forall ge rho jm vl rho' jm',
    alloc_juicy_variables ge rho jm vl = (rho', jm') ->
    resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm') /\
    (nextblock (m_dry jm) <= nextblock (m_dry jm'))%positive.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 revert rho jm H; induction vl; intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 inv H. split. apply resource_decay_refl.
(* Goal: Pos.le (nextblock (m_dry jm')) (nextblock (m_dry jm')) *)
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
   apply juicy_mem_alloc_cohere. apply Pos.le_refl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@var_block Share.top CS a (construct_rho (filter_genv ge) ve te)) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te)))) (m_phi jm') *)
 destruct a as [id ty].
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 unfold alloc_juicy_variables in H; fold alloc_juicy_variables in H.
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 revert H; case_eq (juicy_mem_alloc jm 0 (@sizeof ge ty)); intros jm1 b1 ? ?.
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 pose proof (juicy_mem_alloc_succeeds _ _ _ _ _ H).
(*  rewrite (juicy_mem_alloc_core _ _ _ _ _ H) in H1. *)
(*  rewrite H2 in H1. *)
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 specialize (IHvl _ _ H0).
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 symmetry in H1; pose proof (nextblock_alloc _ _ _ _ _ H1).
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 destruct IHvl.
 split; [ |  rewrite H2 in H4; xomega].
 eapply resource_decay_trans; try eassumption.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 rewrite H2; xomega.
 clear - H H1.
(* Goal: resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm1) *)
 pose proof (juicy_mem_alloc_level _ _ _ _ _ H).
(* Goal: resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm1) *)
 unfold resource_decay.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 split. repeat rewrite <- level_juice_level_phi; rewrite H0; auto.
(* Goal: forall l : address, and (forall _ : Pos.ge (@fst block Z l) (nextblock (m_dry jm)), @eq resource (resource_at (m_phi jm) l) (NO Share.bot bot_unreadable)) (or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) l)) (resource_at (m_phi jm1) l)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) l)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm1) l) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z l) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm1) l) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) l) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm1) l) (NO Share.bot bot_unreadable)))))))) *)
 intro loc.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 split.
(* Goal: forall _ : Pos.ge (@fst block Z loc) (nextblock (m_dry jm)), @eq resource (resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (resource_at (m_phi jm1) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm1) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm1) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm1) loc) (NO Share.bot bot_unreadable))))))) *)
 apply juicy_mem_alloc_cohere.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (resource_at (m_phi jm1) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm1) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm1) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm1) loc) (NO Share.bot bot_unreadable))))))) *)
 rewrite (juicy_mem_alloc_at _ _ _ _ _ H).
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable))))))) *)
 rewrite Z.sub_0_r.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable))))))) *)
 destruct loc as [b z]. simpl in *.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 if_tac. destruct H2; subst b1.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 right. right. left. split. apply alloc_result in H1; subst b; xomega.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eauto.
 rewrite <- H0. left. apply resource_at_approx.
Qed.

Lemma make_args_close_precondition:
  forall params args ge ve te m tx ve' te' m' P vars,
    list_norepet (map fst params) ->
    bind_parameter_temps params args tx = Some te' ->
    alloc_juicy_variables ge empty_env m vars = (ve', m') ->
    P (make_args (map fst params) args (construct_rho (filter_genv ge) ve te))
   |-- close_precondition params vars P (construct_rho (filter_genv ge) ve' te').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @derives rmap ag_rmap (P (make_args (@map (prod ident type) ident (@fst ident type) params) args (construct_rho (filter_genv ge) ve te))) (close_precondition params vars P (construct_rho (filter_genv ge) ve' te')) *)
intros phi ?.
exists (Map.empty (block * type)).
assert (exists e : temp_env,
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
    forall i, e ! i = if in_dec ident_eq i (map fst params) then te' ! i else None). {
 clear - H H0.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@nil (prod ident type))) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@cons (prod ident type) a vars)) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 revert args tx H H0; induction params as [ | [? ?]]; destruct args; intros; simpl in *; inv H0.
(* Goal: @ex temp_env (fun e : temp_env => forall i : positive, @eq (option val) (@PTree.get val i e) (@None val)) *)
(* Goal: @ex temp_env (fun e : temp_env => forall i0 : positive, @eq (option val) (@PTree.get val i0 e) (if match ident_eq i i0 with | left e0 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e0) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
 exists empty_tenv; intros; apply PTree.gempty.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 inv H.
(* Goal: @ex temp_env (fun e : temp_env => forall i0 : positive, @eq (option val) (@PTree.get val i0 e) (if match ident_eq i i0 with | left e0 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e0) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
 destruct (IHparams _ _ H4 H2) as [e ?]; clear IHparams.
(* Goal: @ex temp_env (fun e : temp_env => forall i0 : positive, @eq (option val) (@PTree.get val i0 e) (if match ident_eq i i0 with | left e0 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e0) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
 exists (PTree.set i v e); intro j; specialize (H j).
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 destruct (ident_eq i j). subst.
(* Goal: @eq (option val) (@Some val v) (@PTree.get val i te') *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 symmetry.
(* Goal: @eq (option val) (@PTree.get val j te') (@PTree.get val j (@PTree.set val j v e)) *)
(* Goal: @eq (option val) (@PTree.get val j (@PTree.set val i v e)) (if match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end then @PTree.get val j te' else @None val) *)
 rewrite PTree.gss.
(* Goal: @eq (option val) (@PTree.get val j te') (@Some val v) *)
(* Goal: @eq (option val) (@PTree.get val j (@PTree.set val i v e)) (if match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end then @PTree.get val j te' else @None val) *)
 assert ((PTree.set j v tx) ! j = Some v) by (apply PTree.gss).
(* Goal: @eq (option val) (@PTree.get val j te') (@Some val v) *)
(* Goal: @eq (option val) (@PTree.get val j (@PTree.set val i v e)) (if match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end then @PTree.get val j te' else @None val) *)
 forget (PTree.set j v tx) as tz.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 clear - H0 H2 H3; revert args tz H0 H2; induction params as [ | [? ? ]]; destruct args; intros; simpl in *; inv H2; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply IHparams with args (PTree.set i v0 tz); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gso; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gso by auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (in_dec ident_eq j (map fst params)); auto.
}
(* Goal: @ex (Map.t val) (fun te'0 : Map.t val => and (forall (i : ident) (_ : @In ident i (@map (prod ident type) ident (@fst ident type) params)), @eq (option val) (@Map.get val te'0 i) (@Map.get val (te_of (construct_rho (filter_genv ge) ve' te')) i)) (and (forall i : ident, or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (ve_of (construct_rho (filter_genv ge) ve' te')) i))) (@app_pred rmap ag_rmap (P (mkEnviron (ge_of (construct_rho (filter_genv ge) ve' te')) (Map.empty (prod block type)) te'0)) phi))) *)
destruct H3 as [e ?].
(* Goal: @ex (Map.t val) (fun te'0 : Map.t val => and (forall (i : ident) (_ : @In ident i (@map (prod ident type) ident (@fst ident type) params)), @eq (option val) (@Map.get val te'0 i) (@Map.get val (te_of (construct_rho (filter_genv ge) ve' te')) i)) (and (forall i : ident, or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (ve_of (construct_rho (filter_genv ge) ve' te')) i))) (@app_pred rmap ag_rmap (P (mkEnviron (ge_of (construct_rho (filter_genv ge) ve' te')) (Map.empty (prod block type)) te'0)) phi))) *)
exists (fun i => e!i).
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
split3; intros.
*
(* Goal: @eq (option val) (@Map.get val (fun i : positive => @PTree.get val i e) i) (@Map.get val (te_of (construct_rho (filter_genv ge) ve' te')) i) *)
unfold Map.get.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl. specialize (H3 i). rewrite if_true in H3 by auto. auto.
*
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl.
(* Goal: or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (make_venv ve') i)) *)
 destruct (in_dec ident_eq i (map fst vars)).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 auto.
(* Goal: or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (make_venv ve') i)) *)
 right.
(* Goal: @eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (make_venv ve') i) *)
 unfold Map.get, Map.empty.
(* Goal: @eq (option val) (@Some val v) (@PTree.get val i te') *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 symmetry.
 clear - H1 n.
(* Goal: @eq (option (prod block type)) (make_venv ve' i) (@None (prod block type)) *)
 unfold make_venv.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve') (@None (prod block type)) *)
 assert (empty_env ! i = None) by apply PTree.gempty.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve') (@None (prod block type)) *)
 forget empty_env as ve.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 revert ve m H H1; induction vars as [ | [? ?]]; intros. inv H1; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 spec IHvars. contradict n. right; auto.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve') (@None (prod block type)) *)
 unfold alloc_juicy_variables in H1; fold alloc_juicy_variables in H1.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve') (@None (prod block type)) *)
 destruct (juicy_mem_alloc m 0 (@sizeof ge t)).
 eapply IHvars; try apply H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gso; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 contradict n. left. auto.
*
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 simpl.
 replace (mkEnviron (filter_genv ge) (Map.empty (block * type)) (fun i : positive => e ! i))
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
   with  (make_args (map fst params) args (construct_rho (filter_genv ge) ve te)); auto.
 replace (fun i : positive => e ! i)
  with (fun i => if in_dec ident_eq i (map fst params) then te' ! i else None)
   by (extensionality j; auto).
 clear - H H0.
(* Goal: @eq environ (make_args (@map (prod ident type) ident (@fst ident type) params) args (construct_rho (filter_genv ge) ve te)) (mkEnviron (filter_genv ge) (Map.empty (prod block type)) (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val)) *)
 change (filter_genv ge) with (ge_of (construct_rho (filter_genv ge) ve te)) at 2.
 forget (construct_rho (filter_genv ge) ve te) as rho. clear - H H0.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@nil (prod ident type))) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@cons (prod ident type) a vars)) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 revert args tx rho H H0; induction params as [ | [? ? ]]; destruct args; intros; inv H0.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
 reflexivity.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
 inv H.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite (IHparams args (PTree.set i v tx) rho); auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 unfold env_set. simpl.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
 f_equal. unfold Map.set; extensionality j.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 destruct (ident_eq j i). subst.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite if_true by auto.
 clear - H2 H3.
(* Goal: @eq (option val) (@Some val v) (@PTree.get val i te') *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 symmetry.
(* Goal: @eq (option val) (@PTree.get val i te') (@Some val v) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 replace (Some v) with ((PTree.set i v tx) ! i) by (rewrite PTree.gss; auto).
(* Goal: @eq (option val) (@PTree.get val i te') (@PTree.get val i (@PTree.set val i v tx)) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 forget (PTree.set i v tx) as tz.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 revert args tz H2 H3; induction params as [ | [? ? ]]; destruct args; intros; inv H2; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite (IHparams args (PTree.set i0 v0 tz)); auto. apply PTree.gso; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 contradict H3; left; auto. contradict H3; right; auto.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
 destruct (ident_eq i j); try congruence.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (in_dec ident_eq j (map fst params)); auto.
Qed.

Lemma juicy_mem_alloc_block:
 forall jm n jm2 b F,
   juicy_mem_alloc jm 0 n = (jm2, b) ->
   app_pred F (m_phi jm)  ->
   0 <= n < Ptrofs.modulus ->
   app_pred (F * memory_block Share.top n (Vptr b Ptrofs.zero)) (m_phi jm2).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros. rename H1 into Hn.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
inv H.
unfold after_alloc; simpl m_phi.
match goal with |- context [proj1_sig ?A] => destruct A; simpl proj1_sig end.
rename x into phi2.
destruct a as (? & ? & Hg).
unfold after_alloc' in H1.
destruct (allocate (m_phi jm)
    (fun loc : address =>
      if adr_range_dec (snd (alloc (m_dry jm) 0 n), 0) (n - 0) loc
      then YES Share.top readable_share_top (VAL Undef) NoneP
      else core (m_phi jm @ loc)) (core (ghost_of phi2)))
  as [phi3 [phi4  [? [? Hg']]]].
* extensionality loc; unfold compose.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
  if_tac. unfold resource_fmap. rewrite preds_fmap_NoneP. reflexivity.
  repeat rewrite core_resource_at.
  rewrite <- level_core.
(* Goal: @eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm))) (approx (@level rmap ag_rmap (m_phi jm))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z)) *)
  apply resource_at_approx.
*
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 simpl; if_tac.
 exists (YES Share.top readable_share_top (VAL Undef) NoneP).
(* Goal: clos_refl_trans_1n juicy_mem (@age juicy_mem juicy_mem_ageable) jm1 jm' *)
 destruct l as [b ofs]; destruct H2.
(* Goal: @eq resource (@core resource Join_resource Sep_resource (resource_at (m_phi jm) (@pair block Z b z))) (NO Share.bot bot_unreadable) *)
(* Goal: @eq ghost (ghost_of (@core rmap Join_rmap Sep_rmap (m_phi jm))) (ghost_of (@core rmap Join_rmap Sep_rmap (@proj1_sig rmap (fun phi : rmap => and (@eq nat (@level rmap ag_rmap phi) (@level rmap ag_rmap (m_phi jm))) (and (@eq (forall _ : AV.address, resource) (resource_at phi) (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi))))) (@eq ghost (ghost_of phi) (ghost_of (m_phi jm))))) (make_rmap (after_alloc' lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of (m_phi jm)) (@level rmap ag_rmap (m_phi jm)) (after_alloc'_ok lo hi (@snd mem' block (alloc (m_dry jm) lo hi)) (m_phi jm) (juicy_mem_alloc_aux1 jm lo hi (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le lo ofs) (Z.gt lo ofs) (zle lo ofs)) (@proj_sumbool (Z.lt ofs hi) (Z.ge ofs hi) (zlt ofs hi)) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) lo hi b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) lo hi b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) lo hi)))) (ghost_of_approx (m_phi jm)))))) *)
 rewrite juicy_mem_alloc_cohere. constructor.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply join_unit1; auto.
 destruct (alloc (m_dry jm) 0 n) eqn:?H.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 apply alloc_result in H4. subst. simpl.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 xomega.
 exists (m_phi jm @ l).
 apply join_comm.
 apply core_unit.
*
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite ghost_core; auto.
*
rewrite <- Hg; eexists; apply join_comm, core_unit.
*
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
assert (phi4 = phi2). {
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
 apply rmap_ext. apply join_level in H2. destruct H2; omega.
 intro loc; apply (resource_at_join _ _ _ loc) in H2.
(* Goal: @eq resource (resource_at (@core rmap Join_rmap Sep_rmap w) (@pair block Z loc Z0)) (resource_fmap (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap w))) (approx (@level rmap ag_rmap (@core rmap Join_rmap Sep_rmap w))) (PURE k p)) *)
(* Goal: @ex preds (fun b : preds => @eq resource (resource_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) (PURE k p)) (PURE (FUN f c) (preds_fmap (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) b))) *)
(* Goal: @app_pred rmap ag_rmap (@exp rmap ag_rmap ident (fun id : ident => @andp rmap ag_rmap (@prop rmap ag_rmap (@eq (option block) (@Map.get block (ge_of rho) id) (@Some block loc))) (@prop rmap ag_rmap (@ex funspec (fun fs : funspec => @eq (option funspec) (@PTree.get funspec id (glob_specs G)) (@Some funspec fs)))))) w2 *)
 rewrite H3 in H2; rewrite H1.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 if_tac.
 inv H2; apply YES_ext; apply (join_top _ _ (join_comm RJ)).
 apply join_comm in H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply core_identity in H2. auto.
 apply ghost_of_join in H2.
 rewrite Hg' in H2.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
 apply join_comm, core_identity in H2; congruence.
}
subst phi4.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists (m_phi jm), phi3; split3; auto.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
do 3 red.
rewrite Ptrofs.unsigned_zero.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
omega.
rewrite Ptrofs.unsigned_zero.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
rewrite memory_block'_eq; try omega.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
2: rewrite Coqlib.nat_of_Z_eq; omega.
unfold memory_block'_alt.
rewrite if_true by apply readable_share_top.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: forall l : address, and (forall _ : Pos.ge (@fst block Z l) (nextblock (m_dry jm)), @eq resource (resource_at (m_phi jm) l) (NO Share.bot bot_unreadable)) (or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) l)) (resource_at (m_phi jm1) l)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) l)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm1) l) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z l) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm1) l) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) l) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm1) l) (NO Share.bot bot_unreadable)))))))) *)
intro loc. hnf.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
rewrite Coqlib.nat_of_Z_eq by omega.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
if_tac.
exists Undef.
exists readable_share_top.
(* Goal: perm (m_dry jm) b ofs Cur Freeable *)
hnf.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) ty) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) ty) match @Map.get (prod block type) (make_venv ve) id with | Some (pair b ty' as p) => if eqb_type (@snd ident type (@pair ident type id ty)) ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end))) (m_phi j) *)
rewrite H3.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable))))))) *)
rewrite Z.sub_0_r.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite if_true by auto.
rewrite preds_fmap_NoneP.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
f_equal.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold noat. simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) ty) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) ty) match @Map.get (prod block type) (make_venv ve) id with | Some (pair b ty' as p) => if eqb_type (@snd ident type (@pair ident type id ty)) ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end))) (m_phi j) *)
rewrite H3.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc)) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) loc)) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z loc) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) loc) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (Z.sub (@sizeof (genv_cenv ge) ty) Z0) loc then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) loc) (NO Share.bot bot_unreadable))))))) *)
rewrite Z.sub_0_r.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite if_false by auto.
apply core_identity.
simpl; rewrite Hg'; apply core_identity.
Qed.

Lemma alloc_juicy_variables_lem2:
  forall jm f (ge: genv) ve te jm' (F: pred rmap)
      (HGG:  genv_cenv ge = cenv_cs)
      (COMPLETE: Forall (fun it => complete_type cenv_cs (snd it) = true) (fn_vars f))
      (Hsize: Forall (fun var => @sizeof ge (snd var) <= Ptrofs.max_unsigned) (fn_vars f)),
      list_norepet (map fst (fn_vars f)) ->
      alloc_juicy_variables ge empty_env jm (fn_vars f) = (ve, jm') ->
      app_pred F (m_phi jm) ->
      app_pred (F * stackframe_of f (construct_rho (filter_genv ge) ve te)) (m_phi jm').
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@stackframe_of CS f (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
unfold stackframe_of.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
forget (fn_vars f) as vars. clear f.
(*assert (Hmatch := alloc_juicy_variables_match_venv _ _ _ _ H0). *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
forget empty_env as ve0.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
revert F ve0 jm Hsize H0 H1; induction vars; intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@nil (prod ident type))) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@cons (prod ident type) a vars)) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
simpl in H0. inv H0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl fold_right. rewrite sepcon_emp; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) (@cons (prod ident type) a vars)) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
inv Hsize. rename H4 into Hsize'; rename H5 into Hsize.
(* Goal: or (@In ident i (@map (prod ident type) ident (@fst ident type) vars)) (@eq (option (prod block type)) (@Map.get (prod block type) (Map.empty (prod block type)) i) (@Map.get (prod block type) (make_venv ve') i)) *)
simpl fold_right.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id ve1) *)
unfold alloc_juicy_variables in H0; fold alloc_juicy_variables in H0.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@var_block Share.top CS a (construct_rho (filter_genv ge) ve te)) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te)))) (m_phi jm') *)
destruct a as [id ty].
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te)) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te)))) (m_phi jm') *)
destruct (juicy_mem_alloc jm 0 (@sizeof ge ty)) eqn:?H.
rewrite <- sepcon_assoc.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
inv H.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
simpl in COMPLETE; inversion COMPLETE; subst.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
rename H7 into COMPLETE'.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (@fold_right (forall _ : environ, @pred rmap ag_rmap) (forall _ : environ, @pred rmap ag_rmap) (fun (P Q : forall _ : environ, @pred rmap ag_rmap) (rho : environ) => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (P rho) (Q rho)) (fun _ : environ => @emp rmap Join_rmap Perm_rmap Sep_rmap ag_rmap Age_rmap) (@map (prod ident type) (forall _ : environ, mpred) (fun idt : prod ident type => @var_block Share.top CS idt) vars) (construct_rho (filter_genv ge) ve te))) (m_phi jm') *)
rename H4 into COMPLETE_HD.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply IHvars; eauto. clear IHvars.
(* pose proof (juicy_mem_alloc_succeeds _ _ _ _ _ H2). *)
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
pose proof I.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
unfold var_block, eval_lvar.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) (@snd ident type (@pair ident type id ty))) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) (@snd ident type (@pair ident type id ty))) match @Map.get (prod block type) (ve_of (construct_rho (filter_genv ge) ve te)) (@fst ident type (@pair ident type id ty)) with | Some (pair b ty' as p) => if eqb_type (@snd ident type (@pair ident type id ty)) ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end))) (m_phi j) *)
simpl sizeof; simpl typeof.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) ty) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) ty) match @Map.get (prod block type) (ve_of (construct_rho (filter_genv ge) ve te)) (@fst ident type (@pair ident type id ty)) with | Some (pair b ty' as p) => if eqb_type (@snd ident type (@pair ident type id ty)) ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end))) (m_phi j) *)
simpl Map.get. simpl ge_of.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
assert (Map.get (make_venv ve) id = Some (b,ty)). {
 clear - H0 H5.
(* Goal: @eq (option (prod block type)) (@Map.get (prod block type) (make_venv ve) id) (@Some (prod block type) (@pair block type b ty)) *)
 unfold Map.get, make_venv.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@Some (prod block type) (@pair block type b ty)) *)
 assert ((PTree.set id (b,ty) ve0) ! id = Some (b,ty)) by (apply PTree.gss).
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@Some (prod block type) (@pair block type b ty)) *)
 forget (PTree.set id (b, ty) ve0) as ve1.
 rewrite <- H; clear H.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 revert ve1 j H0 H5; induction vars; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 inv H0; auto.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id ve1) *)
 unfold alloc_juicy_variables in H0; fold alloc_juicy_variables in H0.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id ve1) *)
 destruct a as [id' ty'].
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id ve1) *)
 destruct (juicy_mem_alloc j 0 (@sizeof ge ty')) eqn:?H.
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id ve1) *)
 rewrite (IHvars _ _ H0).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite PTree.gso; auto. contradict H5. subst; left; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 contradict H5; right; auto.
}
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) ty) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) ty) (if eqb_type (@snd ident type (@pair ident type id ty)) ty then Vptr b Ptrofs.zero else Vundef)))) (m_phi j) *)
rewrite H3. rewrite eqb_type_refl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@andp rmap ag_rmap (@prop rmap ag_rmap (Z.le (@sizeof (@cenv_cs CS) ty) Ptrofs.max_unsigned)) (memory_block Share.top (@sizeof (@cenv_cs CS) ty) (Vptr b Ptrofs.zero)))) (m_phi j) *)
simpl in Hsize'.
rewrite <- HGG.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite prop_true_andp by auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
assert (0 <= @sizeof ge ty <= Ptrofs.max_unsigned) by (pose proof (@sizeof_pos ge ty); omega).
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl.
(* Goal: @ex rmap (fun y : rmap => @ex rmap (fun z : rmap => and (@join rmap Join_rmap y z (m_phi j)) (and (@app_pred rmap ag_rmap F y) (and (Z.lt (Z.add (Ptrofs.unsigned Ptrofs.zero) (@sizeof (genv_cenv ge) ty)) Ptrofs.modulus) (@app_pred rmap ag_rmap (memory_block' Share.top (nat_of_Z (@sizeof (genv_cenv ge) ty)) b (Ptrofs.unsigned Ptrofs.zero)) z))))) *)
forget (@sizeof ge ty) as n.
clear - H2 H1 H4.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply juicy_mem_alloc_block; eauto.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
unfold Ptrofs.max_unsigned in H4; omega.
Qed.

Lemma free_list_juicy_mem_ghost: forall m l m', free_list_juicy_mem m l m' ->
  ghost_of (m_phi m') = ghost_of (m_phi m).
Proof.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  induction 1; auto.
  rewrite IHfree_list_juicy_mem, <- H1.
(* Goal: @eq ghost (ghost_of (m_phi (free_juicy_mem jm (m_dry jm2) b lo hi H))) (ghost_of (m_phi jm)) *)
  apply free_juicy_mem_ghost.
Qed.

Lemma alloc_juicy_variables_ghost: forall l ge rho jm,
  ghost_of (m_phi (snd (alloc_juicy_variables ge rho jm l))) = ghost_of (m_phi jm).
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  induction l; auto; simpl; intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  destruct a; simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  rewrite IHl; simpl.
(* Goal: @eq ghost (ghost_of (after_alloc Z0 (@sizeof (genv_cenv ge) t) (@snd mem' block (alloc (m_dry jm) Z0 (@sizeof (genv_cenv ge) t))) (m_phi jm) (juicy_mem_alloc_aux1 jm Z0 (@sizeof (genv_cenv ge) t) (mkmem (@PMap.set (prod memval (PTree.t memval)) (nextblock (m_dry jm)) (@ZMap.init memval Undef) (mem_contents (m_dry jm))) (@PMap.set (forall (_ : Z) (_ : perm_kind), option permission) (nextblock (m_dry jm)) (fun (ofs : Z) (_ : perm_kind) => if andb (@proj_sumbool (Z.le Z0 ofs) (Z.gt Z0 ofs) (zle Z0 ofs)) (@proj_sumbool (Z.lt ofs (@sizeof (genv_cenv ge) t)) (Z.ge ofs (@sizeof (genv_cenv ge) t)) (zlt ofs (@sizeof (genv_cenv ge) t))) then @Some permission Freeable else @None permission) (mem_access (m_dry jm))) (Pos.succ (nextblock (m_dry jm))) (fun (b : positive) (ofs : Z) => Memory.Mem.alloc_obligation_1 (m_dry jm) Z0 (@sizeof (genv_cenv ge) t) b ofs) (fun (b : positive) (ofs : Z) (k : perm_kind) (H : not (Coqlib.Plt b (Pos.succ (nextblock (m_dry jm))))) => Memory.Mem.alloc_obligation_2 (m_dry jm) Z0 (@sizeof (genv_cenv ge) t) b ofs k H) (fun b : positive => Memory.Mem.alloc_obligation_3 (m_dry jm) b)) (nextblock (m_dry jm)) (@eq_refl (prod mem' block) (alloc (m_dry jm) Z0 (@sizeof (genv_cenv ge) t)))))) (ghost_of (m_phi jm)) *)
  apply ghost_of_make_rmap.
Qed.

Lemma semax_call_aux:
 forall (Delta : tycontext)
  (A : TypeTree)
  (P Q Q' : forall ts, dependent_type_functor_rec ts (AssertTT A) mpred)
  (NEP: super_non_expansive P) (NEQ': super_non_expansive Q')
  (ts: list Type) (x : dependent_type_functor_rec ts A mpred)
  (F : environ -> pred rmap)
  (F0 : assert) (ret : option ident) (fsig0 : funsig) cc (a : expr)
  (bl : list expr) (R : ret_assert) (psi : genv) (vx:env) (tx:Clight.temp_env) (k : cont) (rho : environ)
  (ora : OK_ty) (jm : juicy_mem) (b : block) (id : ident)
  (Hora : juicy_mem_op (ext_compat ora) jm),
   Cop.classify_fun (typeof a) =
   Cop.fun_case_f (type_of_params (fst fsig0)) (snd fsig0) cc ->
   tc_fn_return Delta ret (snd fsig0) ->
   (|>tc_expr Delta a rho) (m_phi jm) ->
   (|>tc_exprlist Delta (snd (split (fst fsig0))) bl rho) (m_phi jm) ->
    (*map typeof bl = map (@snd _ _) (fst fsig) ->*)
    guard_environ Delta (current_function k) rho ->
    (snd fsig0 =Tvoid -> ret=None) ->
    closed_wrt_modvars (Scall ret a bl) F0 ->
    RA_normal R = (fun rho0 : environ => EX old:val, substopt ret old F rho0 * maybe_retval (Q ts x) (snd fsig0) ret rho0) ->
(*    Forall (fun it => complete_type (composite_types Delta) (snd it) = true) (fn_vars (snd fsig)) ->*)
    genv_cenv psi = cenv_cs ->
    rho = construct_rho (filter_genv psi) vx tx ->
    (*filter_genv psi = ge_of rho ->*)
    eval_expr a rho = Vptr b Ptrofs.zero ->
    (funassert Delta rho) (m_phi jm) ->
    (rguard Espec psi Delta (frame_ret_assert R F0) k) (level (m_phi jm)) ->
    (believe Espec Delta psi Delta) (level (m_phi jm)) ->
    (glob_specs Delta)!id = Some (mk_funspec fsig0 cc A P Q' NEP NEQ') ->
    Genv.find_symbol psi id = Some b ->
    (forall vl : environ, (!|>(Q' ts x vl <=> Q ts x vl)) (m_phi jm)) ->
    (|>(F0 rho * F rho *
           P ts x (make_args (map (@fst  _ _) (fst fsig0))
             (eval_exprlist (snd (split (fst fsig0))) bl rho) rho)
            )) (m_phi jm) ->
   jsafeN (@OK_spec Espec) psi (level (m_phi jm)) ora
     (State (vx) (tx) (Kseq (Scall ret a bl) :: k)) jm.
Proof.
intros Delta A P Q Q' NEP NEQ' ts x F F0 ret fsig cc a bl R psi vx tx k rho ora jm b id Hora.
intros TC0 TCret TC1 TC2 TC3 TC5 H HR HGG H0 H3 H4 H1 Prog_OK H8 H7 H11 H14.
pose (H6:=True); pose (H9 := True); pose (H16:=True);
pose (H12:=True); pose (H10 := True); pose (H5:=True).
(*************************************************)
assert (Prog_OK' := Prog_OK).
specialize (Prog_OK' (Vptr b Ptrofs.zero) fsig cc A P Q' _ (necR_refl _)).
(*************************************************)
case_eq (level (m_phi jm)); [solve [simpl; constructor] | intros n H2].
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl.
rewrite <- level_juice_level_phi in H2.
destruct (levelS_age1 _ _ H2) as [jmx H13].
assert (LATER: laterR (level (m_phi jm)) n) by (constructor 1; rewrite <- level_juice_level_phi, H2; reflexivity).
spec Prog_OK'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
hnf. exists id, NEP, NEQ'; split; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
exists b; split; auto.
clear H16.
clear H10 H6 H5 H8.
do 4 (pose proof I).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
destruct Prog_OK'. {
clear H5 H6 H8 H10 H9 H12.
destruct fsig as [params retty].
simpl @fst in *; simpl @snd in *.
rewrite @snd_split in *.
clear LATER.
clear id H7.
clear jmx H13.
clear Prog_OK.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
edestruct semax_call_external; eauto.
destruct H5 as [? [? ?]].
inv H5.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
econstructor; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl. constructor; auto.
}
specialize (TC1 _ (age_laterR (age_jm_phi H13))).
specialize (TC2 _ (age_laterR (age_jm_phi H13))).
specialize (H14 _ (age_laterR (age_jm_phi H13))).
destruct H15 as [b' [f [[? [? [COMPLETE [? ?]]]] ?]]].
destruct H18 as [H17' [Hvars [H18 H18']]].
inversion H15; clear H15; subst b'.
specialize (H19 ts x n LATER).
rewrite semax_fold_unfold in H19.
apply (pred_nec_hereditary _ _ n (laterR_necR LATER)) in Prog_OK.
pose (F0F := fun _: environ => F0 rho * F rho).
specialize (H19 _ _ _ (necR_refl _) (conj (tycontext_sub_refl _) HGG)  _ (necR_refl _) (Prog_OK)
                      ((*Kseq (Sreturn None) ::*) Kcall ret f (vx) (tx) :: k)
                       F0F _ (necR_refl _)).
unfold F0F in *; clear F0F.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
spec H19 ; [clear H19 |]. {
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 split.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
 repeat intro; f_equal.
 intros ek vl te ve.
 rewrite !proj_frame_ret_assert.
 unfold seplog.sepcon, seplog.LiftSepLog .
 remember ((construct_rho (filter_genv psi) ve te)) as rho'.
 replace (stackframe_of' psi f rho') with (stackframe_of f rho')
   by (rewrite HGG; auto).
 simpl seplog.sepcon.
 rewrite <- (sepcon_comm (stackframe_of f rho')).
 unfold function_body_ret_assert.
 destruct ek; simpl proj_ret_assert; try solve [normalize].
(* apply prop_andp_subp; intros _.*)
 rewrite andp_assoc.
(* Goal: forall _ : @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (VALspec_range (Z.sub hi lo) Share.top (@pair block Z b lo)) (freeable_blocks bl)) P) (m_phi jm), @app_pred rmap ag_rmap P (m_phi jm') *)
 apply prop_andp_subp; intro. simpl in H15.
 repeat rewrite andp_assoc.
 apply subp_trans' with
  (F0 rho * F rho * (stackframe_of f rho' * bind_ret vl (fn_return f) (Q ts x) rho') && funassert Delta rho').
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply andp_subp'; auto.
 rewrite (sepcon_comm (F0 rho * F rho)).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply sepcon_subp'; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply sepcon_subp'; auto.
 unfold bind_ret.
 destruct vl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply andp_subp'; auto.
 apply pred_eq_e1; apply (H11 _ _ LATER).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct (fn_return f); auto.
 apply pred_eq_e1; apply (H11 _ _ LATER).
 clear Q' NEQ' H11.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
 pose proof I.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
 pose proof I.

 intros wx ? w' ? ?.
 assert (n >= level w')%nat.
 apply necR_level in H21.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply le_trans with (level wx); auto.
 clear wx H20 H21.
(* Goal: forall _ : @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (VALspec_range (Z.sub hi lo) Share.top (@pair block Z b lo)) (freeable_blocks bl)) P) (m_phi jm), @app_pred rmap ag_rmap P (m_phi jm') *)
 apply own.bupd_intro.
 intros ora' jm' Hora' VR ?.
 subst w'.
 pose (H20:=True).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 assert (FL: exists m2, free_list (m_dry jm')  (Clight.blocks_of_env psi ve) = Some m2). {
 subst rho'.
 rewrite (sepcon_comm (stackframe_of f _)) in H22.
 repeat rewrite <- sepcon_assoc in H22.
 clear - COMPLETE HGG H17' H22 H15.
 destruct H22 as [H22 _].
 eapply can_free_list; try eassumption.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite <- HGG. auto.
}
destruct FL as [m2 FL2].
 rewrite HGG in COMPLETE.
 assert (ve_of rho' = make_venv ve) by (subst rho'; reflexivity).
 assert (SFFB := stackframe_of_freeable_blocks Delta _ rho' _ ve HGG COMPLETE H17' H21 H15).
 clear H21.
 destruct (free_list_juicy_mem_i _ _ _ (F0 rho * F rho * bind_ret vl (fn_return f) (Q ts x) rho') FL2)
 as [jm2 [FL [? FL3]]].
 eapply sepcon_derives. apply SFFB. apply derives_refl.
 forget (F0 rho * F rho) as F0F.
 rewrite <- sepcon_assoc.
 rewrite (sepcon_comm (stackframe_of _ _)). rewrite sepcon_assoc.
 destruct H22 as [H22 _].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 auto.
 subst m2.
pose (rval := match vl with Some v => v | None => Vundef end).
pose (te2 := match ret with
            | None => tx
            | Some rid => PTree.set rid rval tx
            end).
specialize (H1 EK_normal None te2 vx).
rewrite proj_frame_ret_assert in H1.
simpl proj_ret_assert in H1.
rewrite HR in H1; clear R HR. simpl exit_cont in H1.
unfold seplog.sepcon,  seplog.LiftSepLog  in H1.
specialize (H1 (m_phi jm2)).
spec H1.
clear - FL3 H2 H23.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
repeat rewrite <- level_juice_level_phi in *. omega.
specialize (H1 _ (necR_refl _)). simpl in H15.
spec H1; [clear H1 | ].
split; [split(*; [split |]*) |].
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
destruct H22 as [H22 _].
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl. unfold te2. destruct ret; unfold rval.
destruct vl.
assert (tc_val' (fn_return f) v).
 apply tc_val_tc_val'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 clear - H22; unfold bind_ret in H22; normalize in H22; try contradiction; auto.
unfold construct_rho. rewrite <- map_ptree_rel.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply guard_environ_put_te'. subst rho; auto.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 cut (t = fn_return f). intros. rewrite H24; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
hnf in TCret; rewrite H21 in TCret. subst; auto.
assert (f.(fn_return)=Tvoid).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
clear - H22; unfold bind_ret in H22; destruct (f.(fn_return)); normalize in H22; try contradiction; auto.
unfold fn_funsig in H18. rewrite H1 in H18. rewrite H18 in TC5. simpl in TC5.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
specialize (TC5 (eq_refl _)); congruence.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite <- H0. auto.
}
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
 destruct H22 as [H22a H22b].
 rewrite seplog.sepcon_comm.
 rewrite <- exp_sepcon1.
 simpl seplog.sepcon.
  rewrite <- sepcon_assoc.
 rewrite sepcon_comm in H22a|-*.
  rewrite sepcon_assoc in H22a.
 assert (bind_ret vl (fn_return f) (Q ts x) rho' * (F0 rho * F rho)
            |-- (maybe_retval (Q ts x) (snd fsig) ret (construct_rho (filter_genv psi) vx te2) *
 (F0 (construct_rho (filter_genv psi) vx te2) *
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  EX old: val, substopt ret old F (construct_rho (filter_genv psi) vx te2)))). {
apply sepcon_derives.
*
 clear dependent a. clear H11 H19 H20 H10 H9 H12 H5 H6 H8 Hora Hora'.
 clear Prog_OK ora ora'.  subst rho' fsig.
 clear H22b VR. clear FL jm2 FL2 FL3.
 clear b H16 H7. clear bl TC2 H14.
 unfold te2; clear te2. unfold rval; clear rval.
 unfold bind_ret.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
 unfold get_result1. simpl.
 unfold bind_ret.
 destruct vl.
 +apply derives_extract_prop; intro.
    unfold maybe_retval.
   destruct ret.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
   unfold get_result1. simpl.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
   apply derives_refl'. f_equal.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
   unfold env_set; simpl.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
   f_equal. unfold eval_id; simpl.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
   f_equal. unfold Map.get. unfold make_tenv. rewrite PTree.gss. reflexivity.
   destruct (fn_return f); try contradiction H;
   apply exp_right with v;    apply derives_refl.
 +
   unfold fn_funsig in TC5. simpl in TC5.
   destruct (fn_return f) eqn:?; try apply FF_derives.
   specialize (TC5 (eq_refl _)). subst ret.
   unfold maybe_retval. apply derives_refl.
*
  subst rho.
  unfold te2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct ret; apply sepcon_derives; auto.
 +
  clear - H.
  apply derives_refl'.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  apply H. intros. destruct (ident_eq i i0).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  subst; left; hnf; simpl. unfold insert_idset. rewrite PTree.gss; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  right; unfold Map.get; simpl; unfold make_tenv; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  rewrite PTree.gso; auto.
+
  simpl in TCret.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
  destruct ((temp_types Delta) ! i) eqn:?; try contradiction.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
  subst t.
  destruct TC3 as [[TC3 _] _].
  hnf in TC3; simpl in TC3.
  specialize (TC3 _ _ Heqo).
  destruct TC3 as [old [? _]].
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
  apply exp_right with old. unfold substopt, subst.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
  apply derives_refl'. f_equal.
  unfold env_set, construct_rho.
(* Goal: @eq environ (mkEnviron (ge_of rho) (Map.empty (prod block type)) (@Map.set val i v (fun i : positive => if @in_dec positive ident_eq i (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val i te' else @None val))) (mkEnviron (ge_of rho) (Map.empty (prod block type)) (fun i0 : positive => if match ident_eq i i0 with | left e => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq i0 (@map (prod ident type) ident (@fst ident type) params) with | left i1 => @left (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) i1) | right n0 => @right (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i i0) (@In positive i0 (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val i0 te' else @None val)) *)
   f_equal. unfold make_tenv. extensionality j.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
  simpl. unfold Map.set. if_tac. subst.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  apply H0. rewrite PTree.gso; auto.
+
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  apply exp_right with Vundef; simpl; auto.
}
 apply H1; clear H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply free_list_juicy_mem_lem; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply sepcon_derives; try apply H22a; auto.
}
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
 destruct H22 as [H22a H22b].
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
 rewrite VR in H22b; clear - FL H22b. {
 unfold te2; clear te2.
 rewrite corable_funassert in H22b.
 rewrite corable_funassert.
 replace (core (m_phi jm2)) with (core (m_phi jm')).
 apply H22b.
 clear - FL.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 induction FL; auto.
 rewrite <-IHFL.
 rewrite <- H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite free_juicy_mem_core; auto.
}
}
case_eq (@level rmap ag_rmap (m_phi jm')); intros; [solve [constructor] |].
rewrite <- level_juice_level_phi in H21.
destruct (levelS_age1 jm' _ H21) as [jm'' ?].
rewrite -> level_juice_level_phi in H21.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (age_twin' jm' jm2 jm'') as [jm2'' [? ?]]; auto.
apply jsafeN_step with (c' := State (vx)(te2) k) (m' := jm2'').
replace n0 with (level jm2'')
 by (rewrite <- H25;
      apply age_level in H24;
      try rewrite <- level_juice_level_phi in H21;
      clear - H21 H24; omega).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
split; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl.
rewrite (age_jm_dry H26) in FL2.
destruct vl.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
2:{
unfold fn_funsig in H18.
rewrite H18 in TC5. simpl in TC5.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
assert (fn_return f = Tvoid). {
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 clear - H22; unfold bind_ret in H22; normalize in H22; try contradiction; auto.
 destruct H22. repeat rewrite <- sepcon_assoc in H.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct H as [? [? [? [_ ?]]]]. destruct (fn_return f); try contradiction H1. auto.
}
specialize (TC5 H27).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply step_return with f ret Vundef (tx); simpl; auto.
unfold te2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite TC5. split; auto.
}
assert (tc_val (fn_return f) v).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 clear - H22; unfold bind_ret in H22; normalize in H22; try contradiction; auto.
(* Goal: @app_pred rmap ag_rmap (approx (@level rmap ag_rmap w) (Q' ts (@fmap (dependent_type_functor_rec ts A) (@pred rmap ag_rmap) (@pred rmap ag_rmap) (approx (@level rmap ag_rmap w)) (approx (@level rmap ag_rmap w)) x) vl)) m'' *)
 destruct H22. destruct H. apply H.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
simpl.
unfold rval.
destruct ret.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply step_return with (zap_fn_return f) None Vundef (PTree.set i v tx); simpl; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply step_return with f None Vundef tx; simpl; auto.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
split; [ | split; [rewrite <- H25; apply age_level; auto|]]. {
 rewrite (age_jm_dry H26) in FL2.
 clear FL3 H1.
 apply resource_decay_trans with (nextblock (m_dry jm')) (m_phi jm2).
(* Goal: Pos.le (nextblock (m_dry jm')) (nextblock (m_dry jm')) *)
(* Goal: and (resource_decay (nextblock (m_dry jm)) (m_phi jm) (m_phi jm')) (Pos.le (nextblock (m_dry jm)) (nextblock (m_dry jm'))) *)
 apply Pos.le_refl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply free_list_resource_decay; eauto.
 replace (nextblock (m_dry jm')) with (nextblock (m_dry jm2)).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply age1_resource_decay; auto.
(* Goal: @eq (option val) (@Some val v) (@PTree.get val i te') *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
 symmetry.
 rewrite (age_jm_dry H26).
 clear - FL2.
 forget (m_dry jm') as m.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 revert m FL2; induction (blocks_of_env psi ve); intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 simpl in FL2. inv FL2; auto.
(* Goal: @ex juicy_mem (fun jm' : juicy_mem => and (free_list_juicy_mem jm (@cons (prod (prod block Z) Z) a bl) jm') (and (@eq mem (m_dry jm') m') (@eq nat (@level juicy_mem juicy_mem_ageable jm) (@level juicy_mem juicy_mem_ageable jm')))) *)
 simpl in FL2. destruct a as [[b lo] hi].
 destruct (free m b lo hi) eqn:?; inv FL2.
 rewrite <- (IHl _ H0).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply nextblock_free in Heqo; auto.
}
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{ rewrite <- (free_list_juicy_mem_ghost _ _ _ FL).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  apply age1_ghost_of, age_jm_phi; auto. }
replace n0 with (level jm2'')
 by (rewrite <- H25;
      apply age_level in H24;
      try rewrite <- level_juice_level_phi in H21;
      clear - H21 H24; omega).
eapply assert_safe_jsafe, pred_hereditary, H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply age_jm_phi; auto.
}
(* END OF  "spec H19" *)

remember (alloc_juicy_variables psi empty_env jm (fn_vars f)) eqn:AJV.
destruct p as [ve' jm']; symmetry in AJV.
destruct (alloc_juicy_variables_e _ _ _ _ _ _ AJV) as [H15 [H20' CORE]].
assert (MATCH := alloc_juicy_variables_match_venv _ _ _ _ _ AJV).
assert (H20 := alloc_juicy_variables_resource_decay _ _ _ _ _ _ AJV).
rewrite <- Genv.find_funct_find_funct_ptr in H16.
destruct (build_call_temp_env f (eval_exprlist (snd (split (fst fsig))) bl rho))
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
as [te' ?]; auto.
simpl in TC2.
apply tc_exprlist_length in TC2.
clear - H18 TC2.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
unfold fn_funsig in *; subst; simpl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
revert bl TC2; induction (fn_params f); destruct bl; intros; auto.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
simpl in TC2. destruct a. destruct (split l). inv TC2.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
destruct a. simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (split l); simpl in *. unfold_lift; simpl. f_equal; auto.
destruct (levelS_age1 jm' n) as [jm'' H20x]. rewrite <- H20'; assumption.
apply jsafeN_step
  with (c' := State ve' te' (Kseq f.(fn_body) :: Kseq (Sreturn None)
                                              :: Kcall ret f (vx) (tx) :: k))
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
       (m' := jm''); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
split; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply step_call_internal with (vargs:=eval_exprlist (snd (split (fst fsig))) bl rho); eauto.
rewrite <- H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
erewrite age_jm_dry by eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply eval_expr_relate; try solve[rewrite H0; auto]; auto. destruct TC3; eassumption. eauto.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) bl (type_of_params (fn_params f)) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type (@fst (list (prod ident type)) type (@pair (list (prod ident type)) type l t)))) bl rho) *)
destruct (fsig). unfold fn_funsig in *. inv H18.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
erewrite age_jm_dry by eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply eval_exprlist_relate; try eassumption; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct TC3 ; auto.
unfold type_of_function.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite H18'; destruct fsig; inv H18; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite <- (age_jm_dry H20x); auto.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct H20;  apply resource_decay_trans with (nextblock (m_dry jm')) (m_phi jm'); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply age1_resource_decay; auto.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
 split.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite H20'; apply age_level; auto.
 erewrite <- (alloc_juicy_variables_ghost _ _ _ jm), AJV; simpl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply age1_ghost_of, age_jm_phi; auto.

assert (n >= level jm'')%nat.
clear - H2 H20' H20x.
(* Goal: lt (@level rmap ag_rmap m'') (@level rmap ag_rmap w) *)
apply age_level in H20x; omega.
pose (rho3 := mkEnviron (ge_of rho) (make_venv ve') (make_tenv te')).
assert (app_pred (funassert Delta rho3) (m_phi jm'')).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
apply (resource_decay_funassert _ _ (nextblock (m_dry jm)) _ (m_phi jm'')) in H4.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
2: apply laterR_necR; apply age_laterR; auto.
unfold rho3; clear rho3.
apply H4.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite CORE. apply age_core. apply age_jm_phi; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 destruct H20;  apply resource_decay_trans with (nextblock (m_dry jm')) (m_phi jm'); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 apply age1_resource_decay; auto.
}
specialize (H19 te' ve' _ H22 _ (necR_refl _)).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
spec H19; [clear H19|]. {
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
split; [split (*; [split|]*) |]; auto.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
3:{
unfold rho3 in H23. unfold construct_rho. rewrite H0 in H23.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl ge_of in H23. auto.
}
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
2:{ simpl.
split; [ | reflexivity].
apply MATCH.
}
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
rewrite (age_jm_dry H20x) in H15.
unfold func_tycontext'.
unfold construct_rho.


clear - H0 TC2 TC3 H18 H16 H21 H15 H23 H17 H17' H13.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
unfold rho3 in *. simpl in *. destruct H23.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
destruct rho. inv H0. simpl in *.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (let (_, ty) := p in Tcons ty (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) (@snd ident type p)) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
remember (split (fn_params f)). destruct p.
assert (TE := TC3).
 destruct TC3 as [TC3 TC3'].
destruct TC3 as [TC3 [TC4 TC5]].
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
simpl in *. if_tac in H16; try congruence. clear H0.
eapply semax_call_typecheck_environ with (jm := jmx); try eassumption.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
erewrite <- age_jm_dry by eauto; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct TE; intros; auto.
}
normalize.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
split; auto. unfold rho3 in H23. unfold construct_rho. rewrite H0 in H23.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl ge_of in H23. auto.
unfold bind_args.
unfold tc_formals.
normalize.
rewrite <- sepcon_assoc.
normalize.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
split.
(* Goal: perm (m_dry jm) b ofs Cur Freeable *)
hnf.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
subst fsig.
destruct TC3 as [TC3 _].
clear - TC3 TC2 H21 H17.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *.
rewrite list_norepet_app in H17. destruct H17 as [H17 [_ _]].
forget (create_undef_temps (fn_temps f)) as te.
revert  bl te TC2 H21 H17.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
induction (fn_params f); simpl; intros; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
destruct a. destruct (split l) eqn:?. simpl. simpl @snd in *. simpl @fst in *.
destruct bl; try solve [inv H21].
simpl in H21.
unfold_lift in H21.
inv H17.
unfold tc_exprlist in TC2;
simpl in TC2. repeat rewrite denote_tc_assert_andp in TC2.
(* Goal: match @PMap.get (forall (_ : Z) (_ : perm_kind), option permission) b (mem_access (m_dry jm)) ofs Cur with | Some p' => perm_order p' Freeable | None => False end *)
destruct TC2. destruct H.
set (te1 := PTree.set i (force_val (sem_cast (typeof e) t (eval_expr e rho))) te) in *.
specialize (IHl bl te1 H0 H21 H2).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
split; auto.
assert (eval_id i (construct_rho (filter_genv psi) ve' te') =
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
             force_val (sem_cast (typeof e) t (eval_expr e rho))). {
clear - H21 H1.
forget (force_val (sem_cast (typeof e) t (eval_expr e rho))) as v.
unfold te1 in *; clear te1.
forget (eval_exprlist l1 bl rho) as dl.
assert ((PTree.set i v te) ! i = Some v).
apply PTree.gss.
forget  (PTree.set i v te) as te0.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
revert te0 H1 dl H21 H; induction l; simpl; intros.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
unfold eval_id. simpl. destruct dl; inv H21.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
unfold make_tenv, Map.get. rewrite H. reflexivity.
(* Goal: @eq (option val) (@PTree.get val id t1) (@PTree.get val id t) *)
destruct a.
destruct dl; try solve [inv H21].
(* Goal: @ex temp_env (fun te : temp_env => @eq (option temp_env) (bind_parameter_temps l vl (@PTree.set val i v rho)) (@Some temp_env te)) *)
eapply IHl.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
contradict H1; auto.
eassumption.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite PTree.gso; auto.
}
rewrite H4.
eapply tc_val_sem_cast; eassumption.
}
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
forget (F0 rho * F rho) as Frame.
subst fsig.
rewrite @snd_split in *.
simpl @fst in *.
 apply (alloc_juicy_variables_age H13 H20x) in AJV.
 forget (fn_params f) as params.
 forget (eval_exprlist (map snd params) bl rho) as args.
 clear - H21 H14 AJV H17 H17' H0 Hvars HGG COMPLETE.
 assert (app_pred (Frame * close_precondition params (fn_vars f) (P ts x)
                               (construct_rho (filter_genv psi) ve' te')) (m_phi jmx)).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply sepcon_derives; try apply H14; auto.
 subst rho.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply make_args_close_precondition; eauto.
(* Goal: @eq (option (prod block type)) (@Some (prod block type) (@pair block type b1 t)) (@None (prod block type)) *)
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) i ve) (@None (prod block type)) *)
 apply list_norepet_app in H17; intuition.
 clear H14.
 forget (Frame *
     close_precondition params (fn_vars f) (P ts x)
       (construct_rho (filter_genv psi) ve' te')) as Frame2.
 clear - H17' H21 AJV H Hvars HGG COMPLETE.
 rewrite HGG. change (stackframe_of' cenv_cs) with stackframe_of.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 eapply alloc_juicy_variables_lem2; eauto.
 unfold var_sizes_ok in Hvars;
 rewrite Forall_forall in Hvars, COMPLETE |- *.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
 intros.
 specialize (COMPLETE x H0).
 specialize (Hvars x H0).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
 rewrite <- HGG; auto.
}
(* end   "spec H19" *)
}
replace n with (level jm'').
eapply assert_safe_jsafe, own.bupd_mono, H19.
intros ? Hsafe ?? Hora0 ??.
subst; specialize (Hsafe ora0 _ Hora0 eq_refl eq_refl).
clear - Hsafe.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
destruct (level (m_phi jm0)); simpl in *. constructor.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
inv Hsafe. econstructor; eauto. inv H0. inv H. split; auto.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
simpl in *. congruence.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
simpl in *. unfold cl_halted in H. contradiction.
apply age_level in H20x.
(* Goal: @eq mem (m_dry jm1) (m_dry jm') *)
rewrite <- level_juice_level_phi in *; congruence.
Qed.

Lemma func_at_func_at':
 forall fs loc, func_at fs loc |-- func_at' fs loc.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
unfold func_at, func_at'; destruct fs; intros. hnf; intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eexists; eauto.
Qed.

Lemma semax_call:
  forall Delta (A: TypeTree)
  (P Q : forall ts, dependent_type_functor_rec ts (AssertTT A) mpred)
  (NEP: super_non_expansive P) (NEQ: super_non_expansive Q)
  (ts: list Type) (x : dependent_type_functor_rec ts A mpred)
   F ret argsig retsig cc a bl,
           Cop.classify_fun (typeof a) =
           Cop.fun_case_f (type_of_params argsig) retsig cc ->
            (retsig = Tvoid -> ret = None) ->
          tc_fn_return Delta ret retsig ->
  semax Espec Delta
       (fun rho => (|>(tc_expr Delta a rho && tc_exprlist Delta (snd (split argsig)) bl rho))  &&
           (func_ptr (mk_funspec (argsig,retsig) cc A P Q NEP NEQ) (eval_expr a rho) &&
          (|>(F rho * P ts x (make_args (map (@fst  _ _) argsig)
                (eval_exprlist (snd (split argsig)) bl rho) rho )))))
         (Scall ret a bl)
         (normal_ret_assert
          (fun rho => (EX old:val, substopt ret old F rho * maybe_retval (Q ts x) retsig ret rho))).
Proof.
(* Goal: forall (Delta : tycontext) (A : TypeTree) (P Q : forall ts : list Type, _functor (dependent_type_functor_rec ts (AssertTT A)) mpred) (NEP : @super_non_expansive A P) (NEQ : @super_non_expansive A Q) (ts : list Type) (x : _functor (dependent_type_functor_rec ts A) mpred) (F : forall _ : environ, @pred rmap ag_rmap) (ret : option ident) (argsig : list (prod ident type)) (retsig : type) (cc : calling_convention) (a : expr) (bl : list expr) (_ : @eq classify_fun_cases (classify_fun (typeof a)) (fun_case_f (type_of_params argsig) retsig cc)) (_ : forall _ : @eq type retsig Tvoid, @eq (option ident) ret (@None ident)) (_ : tc_fn_return Delta ret retsig), @semax CS Espec Delta (fun rho : environ => @andp rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@andp rmap ag_rmap (@tc_expr CS Delta a rho) (@tc_exprlist CS Delta (@snd (list ident) (list type) (@split ident type argsig)) bl rho))) (@andp rmap ag_rmap (func_ptr (mk_funspec (@pair (list (prod ident type)) type argsig retsig) cc A P Q NEP NEQ) (@eval_expr CS a rho)) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho)))))) (Scall ret a bl) (normal_ret_assert (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho)))) *)
rewrite semax_unfold. intros ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TCF TC5 TC7.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
(* Goal: @app_pred nat ag_nat (guard Espec psi Delta' (fun rho : environ => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (@andp rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@andp rmap ag_rmap (@tc_expr CS Delta a rho) (@tc_exprlist CS Delta (@snd (list ident) (list type) (@split ident type argsig)) bl rho))) (@andp rmap ag_rmap (func_ptr (mk_funspec (@pair (list (prod ident type)) type argsig retsig) cc A P Q NEP NEQ) (@eval_expr CS a rho)) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho))))))) (@cons cont' (Kseq (Scall ret a bl)) k)) w *)
rename H0 into H1.
(* Goal: @app_pred nat ag_nat (guard Espec psi Delta' (fun rho : environ => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (@andp rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@andp rmap ag_rmap (@tc_expr CS Delta a rho) (@tc_exprlist CS Delta (@snd (list ident) (list type) (@split ident type argsig)) bl rho))) (@andp rmap ag_rmap (func_ptr (mk_funspec (@pair (list (prod ident type)) type argsig retsig) cc A P Q NEP NEQ) (@eval_expr CS a rho)) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho))))))) (@cons cont' (Kseq (Scall ret a bl)) k)) w *)
intros tx vx.
(* Goal: @app_pred nat ag_nat (let rho := construct_rho (filter_genv psi) vx tx in @fash rmap ag_rmap (@imp rmap ag_rmap (@andp rmap ag_rmap (@andp rmap ag_rmap (@prop rmap ag_rmap (guard_environ Delta' (current_function (@cons cont' (Kseq (Scall ret a bl)) k)) rho)) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (@andp rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@andp rmap ag_rmap (@tc_expr CS Delta a rho) (@tc_exprlist CS Delta (@snd (list ident) (list type) (@split ident type argsig)) bl rho))) (@andp rmap ag_rmap (func_ptr (mk_funspec (@pair (list (prod ident type)) type argsig retsig) cc A P Q NEP NEQ) (@eval_expr CS a rho)) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho)))))))) (funassert Delta' rho)) (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho))) w *)
intros ? ? ? ? [[TC3 ?] ?].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
assert (H0': necR w (level a')).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply nec_nat. apply necR_level in H2. apply le_trans with (level y); auto.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
eapply pred_nec_hereditary in H1; [ | apply H0'].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
eapply pred_nec_hereditary in Prog_OK; [ | apply H0'].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
clear w H0' H0 y H2.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
rename a' into w.

(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
rewrite !later_andp in H3.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
apply extend_sepcon_andp in H3; auto.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct H3 as [H2 H3].
normalize in H3. unfold func_ptr in *.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct H3 as [[b [H3 H6]] H5].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
generalize H4; intros [_ H7].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct (level w) eqn: Hl.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{ apply own.bupd_intro; repeat intro.
  rewrite Hl; constructor. }
rewrite <- Hl in *.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct (levelS_age w n) as (w' & Hage & Hw'); auto.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
specialize (H7 (b) (mk_funspec (argsig,retsig) cc A P Q NEP NEQ) _ (necR_refl _)).
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
spec H7.
(* Goal: @app_pred rmap ag_rmap (func_at' (mk_funspec (@pair (list (prod ident type)) type argsig retsig) cc A P Q NEP NEQ) (@pair block Z b Z0)) w *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
1: apply func_at_func_at'; apply H6.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct H7 as [id [H7 H9]].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
hnf in H9.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct H2 as [TC1 TC2].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
generalize H9; intros [fs H8].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
generalize H4; intros [H10 _].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
specialize (H10 id fs _ (necR_refl _) H8).
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct H10 as [v' [H10 H13]].
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
assert (H11: filter_genv psi = ge_of (construct_rho (filter_genv psi) vx tx)) by reflexivity.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
simpl in H10. simpl in H7. inversion2 H7 H10.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
unfold func_at in H13.
(* rewrite H12 in H13.*)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
destruct fs as [fsig' cc' A' P' Q' NEP' NEQ'].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
hnf in H6,H13.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
inversion2 H13 H6.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
apply inj_pair2 in H14. rename H14 into H15.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
pose (H6:=True).
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
clear H9; pose (H9:=True).

(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
unfold filter_genv in H7.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
remember (construct_rho (filter_genv psi) vx tx) as rho.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
set (args := eval_exprlist (snd (split argsig)) bl rho).
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
fold args in H5.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
rename H11 into H10'.

(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
destruct (function_pointer_aux A' P P' Q Q' w NEP NEQ NEP' NEQ') as [H10 H11].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
f_equal; auto.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
clear H15.
specialize (H10 ts x (make_args (map (@fst  _ _) argsig) (eval_exprlist (snd (split argsig))bl rho) rho)).
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
specialize (H11 ts x).
assert (H14: app_pred (|> (F0 rho * F rho * P' ts x (make_args (map (@fst  _ _) argsig)
  (eval_exprlist (snd (split argsig)) bl rho) rho))) w).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
(* Goal: @app_pred rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (F rho)) (P' ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho)))) w *)
  do 3 red in H10.
(* Goal: @app_pred rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (F rho)) (P' ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho)))) w *)
  apply eqp_later1 in H10.
(* Goal: @app_pred rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (F rho)) (P' ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho)))) w *)
  apply pred_eq_e2 in H10.
(* Goal: @app_pred rmap ag_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (F rho)) (P' ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho)))) w *)
  rewrite later_sepcon.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  eapply (sepcon_subp' (|>(F0 rho * F rho)) _ (|> P ts x (make_args (map (@fst  _ _) argsig) (eval_exprlist (snd (split argsig)) bl rho) rho)) _ (level w)); eauto.
  rewrite <- later_sepcon, sepcon_assoc, later_sepcon.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (F0 rho)) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho))))) w *)
  eapply derives_e, H5.
(* Goal: @derives rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F0 rho) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) args rho))))) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@box rmap ag_rmap (@laterM rmap ag_rmap) (F0 rho)) (@box rmap ag_rmap (@laterM rmap ag_rmap) (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (F rho) (P ts x (make_args (@map (prod ident type) ident (@fst ident type) argsig) (@eval_exprlist CS (@snd (list ident) (list type) (@split ident type argsig)) bl rho) rho))))) *)
  apply sepcon_derives, derives_refl; apply now_later.
}
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
assert (typecheck_environ Delta rho) as TC4.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
(* Goal: typecheck_environ Delta rho *)
  destruct TC3 as [TC3 TC4].
(* Goal: typecheck_environ Delta rho *)
  eapply typecheck_environ_sub in TC3; [| eauto].
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  auto.
}
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
eapply later_derives in TC2; [|apply (tc_exprlist_sub _ _ _ TS); auto].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
eapply later_derives in TC1; [|apply (tc_expr_sub _ _ _ TS); auto].
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
assert (TC7': tc_fn_return Delta' ret retsig).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
{
  clear - TC7 TS.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  hnf in TC7|-*. destruct ret; auto.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
  destruct ((temp_types Delta) ! i) eqn:?; try contradiction.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
  destruct TS.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
  specialize (H i); rewrite Heqo in H. subst t.
(* Goal: match @PTree.get type i (temp_types Delta') with | Some t' => @eq type retsig t' | None => False end *)
  destruct ((temp_types Delta') ! i ).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  destruct H; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  auto.
}
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
clear TC7.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
apply own.bupd_intro; repeat intro; subst.
eapply semax_call_aux; try eassumption;
 try solve [simpl; assumption].
(* Goal: @eq (forall _ : environ, mpred) (RA_normal (normal_ret_assert (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl RA_normal.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto.
Qed.

Lemma semax_call_alt:
 forall Delta (A: TypeTree)
   (P Q : forall ts, dependent_type_functor_rec ts (AssertTT A) mpred)
   (NEP: super_non_expansive P) (NEQ: super_non_expansive Q)
     ts x F ret argsig retsig cc a bl,
           Cop.classify_fun (typeof a) =
           Cop.fun_case_f (type_of_params argsig) retsig cc ->
            (retsig = Tvoid -> ret = None) ->
          tc_fn_return Delta ret retsig ->
  semax Espec Delta
       (fun rho => (|> (tc_expr Delta a rho && tc_exprlist Delta (snd (split argsig)) bl rho))  &&
           (func_ptr (mk_funspec (argsig,retsig) cc A P Q NEP NEQ) (eval_expr a rho) &&
          (|>(F rho * P ts x (make_args (map (@fst  _ _) argsig)
                (eval_exprlist (snd (split argsig)) bl rho) rho )))))
         (Scall ret a bl)
         (normal_ret_assert
          (fun rho => (EX old:val, substopt ret old F rho * maybe_retval (Q ts x) retsig ret rho))).
Proof. exact semax_call. Qed.

Lemma semax_call_ext:
   forall (IF_ONLY: False),
     forall Delta P Q ret a tl bl a' bl',
      typeof a = typeof a' ->
      map typeof bl = map typeof bl' ->
      (forall rho,
          !! (typecheck_environ Delta rho) && P rho |--
            tc_expr Delta a rho && tc_exprlist Delta tl bl rho &&
            tc_expr Delta a' rho && tc_exprlist Delta tl bl' rho &&
            !! (eval_expr a rho = eval_expr a' rho /\
                eval_exprlist tl bl rho = eval_exprlist tl bl' rho)) ->
  semax Espec Delta P (Scall ret a bl) Q ->
  semax Espec  Delta P (Scall ret a' bl') Q.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros until 2. intro Hbl. intros.
rewrite semax_unfold in H1|-*.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
rename H1 into H2. pose proof I.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
specialize (H2 psi Delta' w TS HGG Prog_OK k F H3 H4).
intros tx vx; specialize (H2 tx vx).
intros ? ? ? ? ?.
specialize (H2 y H5 a'0 H6 H7).
destruct H7 as[[? ?] _].
hnf in H7.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
pose proof I.
(* Goal: @eq (option val) (@Some val v) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
(* Goal: @eq (option val) (if @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) then @PTree.get val j te' else @None val) (if match ident_eq i j with | left e => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_introl (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) e) | right n => match @in_dec positive ident_eq j (@map (prod ident type) ident (@fst ident type) params) with | left i0 => @left (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (@or_intror (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) i0) | right n0 => @right (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params))) (not (or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)))) (fun H0 : or (@eq positive i j) (@In positive j (@map (prod ident type) ident (@fst ident type) params)) => match H0 with | or_introl Hc1 => n Hc1 | or_intror Hc2 => n0 Hc2 end) end end then @PTree.get val j te' else @None val) *)
intros ? J; destruct (H2 _ J) as (? & J' & m' & Hl & Hr & ? & Hsafe); subst.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eexists; split; eauto; exists m'; repeat split; auto.
hnf in Hsafe|-*; intros ?? Hora ??.
specialize (Hsafe ora jm Hora H10).
eapply convergent_controls_jsafe; try apply Hsafe.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
reflexivity.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
simpl; intros ? ?. unfold cl_after_external. destruct ret0; auto.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
reflexivity.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
specialize (Hsafe H11).
destruct H8 as [w1 [w2 [H8' [_ ?]]]]. subst m'.
assert (H8'': extendM w2 a'0) by (eexists; eauto). clear H8'.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
remember (construct_rho (filter_genv psi) vx tx) as rho.
assert (H7': typecheck_environ Delta rho).
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct H7; eapply typecheck_environ_sub; eauto.
destruct H7 as [H7 _].
specialize (H0 rho w2 (conj H7' H8)).
destruct H0 as [[[[TCa TCbl] TCa'] TCbl'] [? ?]].
apply (boxy_e _ _ (extend_tc_expr _ _ _) _ _ H8'') in TCa.
apply (boxy_e _ _ (extend_tc_exprlist _ _ _ _) _ _ H8'') in TCbl.
apply (boxy_e _ _ (extend_tc_expr _ _ _) _ _ H8'') in TCa'.
apply (boxy_e _ _ (extend_tc_exprlist _ _ _ _) _ _ H8'') in TCbl'.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply denote_tc_resource with (a'1 := m_phi jm) in TCa; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply denote_tc_resource with (a'1 := m_phi jm) in TCa'; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply denote_tc_resource with (a'1 := m_phi jm) in TCbl; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply denote_tc_resource with (a'1 := m_phi jm) in TCbl'; auto.
assert (forall vf, Clight.eval_expr psi vx tx (m_dry jm) a vf
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
               -> Clight.eval_expr psi vx tx (m_dry jm) a' vf). {
clear - TCa TCa' H7 H7' H0 Heqrho HGG TS.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
intros.
eapply tc_expr_sub in TCa; [| eauto | eauto].
pose proof (eval_expr_relate _ _ _ _ _ _ jm HGG Heqrho H7 TCa).
pose proof (eval_expr_fun H H1). subst vf.
rewrite H0.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
eapply eval_expr_relate; eauto.
}
assert (forall tyargs vargs,
             Clight.eval_exprlist psi vx tx (m_dry jm) bl tyargs vargs ->
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
             Clight.eval_exprlist psi vx tx (m_dry jm) bl' tyargs vargs). {
clear - TS IF_ONLY TCbl TCbl' Hbl H7 H7' H13 Heqrho HGG.
revert bl bl' Hbl TCbl TCbl' H13; induction tl; destruct bl, bl'; simpl; intros; auto;
 try (clear IF_ONLY; contradiction).
(*unfold_lift in H13; simpl in H13.
inversion H13; clear H13.
*)
 unfold tc_exprlist in TCbl,TCbl'. simpl in TCbl, TCbl'.
repeat rewrite denote_tc_assert_andp in TCbl, TCbl'.
destruct TCbl as [[TCe ?] ?].
destruct TCbl' as [[TCe0 ?] ?].
inversion H; clear H. subst bl0 tyargs vargs.
inversion Hbl; clear Hbl. rewrite <- H5 in *.
eapply (tc_expr_sub _ _ _ TS e rho H7' (m_phi jm)) in TCe.
pose proof (eval_expr_relate _ _ _ _ _ _ _ HGG Heqrho H7 TCe).
pose proof (eval_expr_fun H H6).
repeat rewrite <- cop2_sem_cast in *.
unfold force_val in H1.
rewrite H10 in *.
contradiction IF_ONLY.  (* this needs plenty of work. *)
}
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct H12; split; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
inv H12; [eapply step_call_internal | eapply step_call_external ]; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite <- H; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
rewrite <- H; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
auto.
Qed.

Lemma call_cont_idem: forall k, call_cont (call_cont k) = call_cont k.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
induction k; intros.
(* Goal: @eq cont (call_cont (call_cont (@nil cont'))) (call_cont (@nil cont')) *)
(* Goal: @eq cont (call_cont (call_cont (@cons cont' a k))) (call_cont (@cons cont' a k)) *)
reflexivity.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
destruct a; simpl; auto.
Qed.

Definition cast_expropt (e: option expr) t : environ -> option val :=
 match e with Some e' => `Some (eval_expr (Ecast e' t))  | None => `None end.

Lemma call_cont_current_function:
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  forall {k i f e t l}, call_cont k = Kcall i f e t :: l -> current_function k = Some f.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
Proof. intros. induction k; try destruct a; simpl in *; inv H; auto.
Qed.

Definition tc_expropt Delta (e: option expr) (t: type) : environ -> mpred :=
   match e with None => `!!(t=Tvoid)
                     | Some e' => denote_tc_assert (typecheck_expr Delta (Ecast e' t))
   end.

Lemma  semax_return:
   forall Delta R ret,
      semax Espec Delta
                (fun rho => tc_expropt Delta ret (ret_type Delta) rho &&
                             RA_return R (cast_expropt ret (ret_type Delta) rho) rho)
                (Sreturn ret)
                R.
Proof.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  intros.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
  hnf; intros.
  rewrite semax_fold_unfold.
  intros psi Delta'.
  apply prop_imp_i. intros [TS HGG].
  replace (ret_type Delta) with (ret_type Delta')
    by (destruct TS as [_ [_ [? _]]]; auto).
  apply derives_imp.
  clear n.
  intros w ? k F.
  intros w' ? ?.
  clear H.
  clear w H0.
  rename w' into w.
(* Goal: @ex (prod block type) (fun v : prod block type => @eq (option (prod block type)) (@PTree.get (prod block type) id ve') (@Some (prod block type) v)) *)
  destruct H1.
  do 3 red in H.
  intros te ve.
  intros n ? w' ? ?.
  assert (necR w (level w')).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {
(* Goal: @necR nat ag_nat w (@level rmap ag_rmap a') *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
    apply nec_nat.
(* Goal: le (@level rmap ag_rmap a') w *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) a' *)
    apply necR_level in H2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    apply le_trans with (level n); auto.
  }
  apply (pred_nec_hereditary _ _ _ H4) in H0.
  clear w n H2 H1 H4.
  destruct H3 as [[H3 ?] ?].
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (@var_block Share.top CS (@pair ident type id ty) (construct_rho (filter_genv ge) ve te))) (m_phi j) *)
  pose proof I.
  remember ((construct_rho (filter_genv psi) ve te)) as rho.
  assert (H1': ((F rho * proj_ret_assert R EK_return (cast_expropt ret (ret_type Delta') rho) rho))%pred w').
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    eapply sepcon_derives; try apply H1; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    apply andp_left2; auto.
  }
  assert (TC: (tc_expropt Delta ret (ret_type Delta') rho) w').
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
    clear - H1. destruct H1 as [w1 [w2 [? [? [? ?]]]]]. intros.
    unfold tc_expropt in *.
    destruct ret; try apply H1.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  apply (boxy_e _ _ (extend_tc_expr _ _ _) w2); auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  exists w1; auto.
  }
  clear H1; rename H1' into H1.
  specialize (H0 EK_return (cast_expropt ret (ret_type Delta') rho) te ve).
  specialize (H0 _ (le_refl _) _ (necR_refl _)).
(* Goal: False *)
(* Goal: @eq (option (prod block type)) (@PTree.get (prod block type) id ve) (@PTree.get (prod block type) id e) *)
  spec H0.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
  {
    rewrite <- Heqrho.
    rewrite proj_frame_ret_assert.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    split; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    split; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    rewrite seplog.sepcon_comm; auto.
  }
  unfold tc_expropt in TC; destruct ret; simpl in TC.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  + eapply own.bupd_mono, bupd_denote_tc, H0; eauto.
    clear TC; intros ? [TC Hsafe] ?? Hora ??.
    specialize (Hsafe ora jm Hora (eq_refl _) H6).
    eapply convergent_controls_jsafe; try apply Hsafe.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    1: simpl; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    1: intros ? ?; simpl; unfold cl_after_external; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    1: simpl; auto.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
    intros.
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) (construct_rho (filter_genv psi) vx tx)) w *)
    simpl in H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    destruct H7; split; auto.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
    revert H7; simpl.
    unfold_lift.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
    case_eq (call_cont k); intros.
    - inv H9.
      inv H14.
    - destruct c.
      elimtype False; clear - H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
       revert l H7; induction k; try destruct a; simpl; intros; try discriminate; eauto.
      elimtype False; clear - H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
       revert l H7; induction k; try destruct a; simpl; intros; try discriminate; eauto.
      elimtype False; clear - H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
       revert l H7; induction k; try destruct a; simpl; intros; try discriminate; eauto.
      elimtype False; clear - H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
       revert l H7; induction k; try destruct a; simpl; intros; try discriminate; eauto.
      destruct l0.
      * clear H0 H2 H8.
        inv H9. fold denote_tc_assert in TC.
        inv H11.
        destruct H17.
(* Goal: @app_pred rmap ag_rmap (@sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap F (memory_block Share.top (@sizeof (genv_cenv ge) ty) (Vptr b Ptrofs.zero))) (m_phi j) *)
        econstructor; try eassumption; simpl.
        2: split; [congruence | eassumption].
        exists (eval_expr e (construct_rho (filter_genv psi) ve te)).
        destruct H3 as [H3' H6].
        assert (typecheck_environ Delta (construct_rho (filter_genv psi) ve te)) as H3
          by (eapply typecheck_environ_sub; eauto).
        assert (TCe: (denote_tc_assert (typecheck_expr Delta e)  (construct_rho (filter_genv psi) ve te)) (m_phi jm)).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
        {
(* Goal: perm (m_dry jm) b ofs Cur Freeable *)
          hnf.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
          simpl in *.
          rewrite !denote_tc_assert_andp in TC.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
          simpl in *; super_unfold_lift.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          destruct TC; auto.
        }
        pose proof tc_expr_sub _ _ _ TS _ _ H3 (m_phi jm) TCe as TCe'.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
        split.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
        {
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          apply eval_expr_relate with (Delta0 := Delta')(m:= jm); auto.
        }
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
        {
          simpl in H6; rewrite (call_cont_current_function H7) in H6.
          destruct H6 as [_ ?].
          rewrite H6.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
          super_unfold_lift.
          rewrite !denote_tc_assert_andp in TC.
          destruct TC as [TC1 TC2]. rewrite H6 in TC2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          rewrite cop2_sem_cast'; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          2: eapply typecheck_expr_sound; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          apply cast_exists with (Delta0 := Delta)(phi := m_phi jm); auto.
        }
      * fold denote_tc_assert in TC.
        inv H9.
        symmetry in H14; inv H14.
        destruct H20.
        subst te''. clear H6.
        econstructor; try eassumption; [| simpl; auto].
        exists (eval_expr e (construct_rho (filter_genv psi) ve te)).
        destruct H3 as [H3' H6].
        assert (typecheck_environ Delta (construct_rho (filter_genv psi) ve te)) as H3
          by (eapply typecheck_environ_sub; eauto).
        assert (TCe: (denote_tc_assert (typecheck_expr Delta e)  (construct_rho (filter_genv psi) ve te)) (m_phi jm)).
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
        {
(* Goal: perm (m_dry jm) b ofs Cur Freeable *)
          hnf.
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge (@fst block Z (@pair block Z b z)) (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (if adr_range_dec (@pair block Z b1 Z0) (@sizeof (genv_cenv ge) ty) (@pair block Z b z) then YES Share.top readable_share_top (VAL Undef) NoneP else resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
          simpl in *.
          rewrite !denote_tc_assert_andp in TC.
(* Goal: Clight.eval_exprlist psi vx tx (m_dry m) (@cons expr a bl) (Tcons t (type_of_params vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr CS a))) (@eval_exprlist CS (@map (prod ident type) type (@snd ident type) vl) bl) (construct_rho (filter_genv psi) vx tx)) *)
          simpl in *; super_unfold_lift.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          destruct TC; auto.
        }
        pose proof tc_expr_sub _ _ _ TS _ _ H3 (m_phi jm) TCe as TCe'.
(* Goal: and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (YES Share.top readable_share_top (VAL Undef) NoneP) (YES Share.top readable_share_top (VAL v) NoneP))) *)
(* Goal: or (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (resource_at (m_phi jm) (@pair block Z b z))) (or (@ex share (fun sh : share => @ex (writable0_share sh) (fun wsh : writable0_share sh => @ex memval (fun v : memval => @ex memval (fun v' : memval => and (@eq resource (resource_fmap (approx (@level rmap ag_rmap (m_phi jm1))) (approx (@level rmap ag_rmap (m_phi jm1))) (resource_at (m_phi jm) (@pair block Z b z))) (YES sh (@writable0_readable sh wsh) (VAL v) NoneP)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES sh (@writable0_readable sh wsh) (VAL v') NoneP))))))) (or (and (Pos.ge b (nextblock (m_dry jm))) (@ex memval (fun v : memval => @eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) NoneP)))) (@ex memval (fun v : memval => @ex preds (fun pp : preds => and (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (YES Share.top readable_share_top (VAL v) pp)) (@eq resource (resource_at (m_phi jm) (@pair block Z b z)) (NO Share.bot bot_unreadable))))))) *)
        split.
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
        {
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          apply eval_expr_relate with (Delta0 := Delta); auto.
        }
(* Goal: tc_fn_return Delta' ret retsig *)
(* Goal: @app_pred rmap ag_rmap (assert_safe Espec psi vx tx (@cons cont' (Kseq (Scall ret a bl)) k) rho) w *)
        {
          simpl in H6; rewrite (call_cont_current_function H7) in H6.
          destruct H6 as [_ ?].
          rewrite !denote_tc_assert_andp in TC.
          destruct TC as [TC1 TC2]. rewrite H6 in TC2.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          rewrite cop2_sem_cast'; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          2: eapply typecheck_expr_sound; eauto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
          apply cast_exists with (Delta0 := Delta)(phi := m_phi jm); auto.
        }
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
  + eapply own.bupd_mono, H0; eauto.
    intros ? Hsafe ?? Hora ??.
    specialize (Hsafe ora jm Hora (eq_refl _) H6).
    eapply convergent_controls_jsafe; try apply Hsafe.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    1: simpl; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    1: intros ? ?; simpl; unfold cl_after_external; auto.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    1: simpl; auto.
(* Goal: forall (k : cont) (i : option ident) (f : function) (e : env) (t : temp_env) (l : list cont') (_ : @eq cont (call_cont k) (@cons cont' (Kcall i f e t) l)), @eq (option function) (current_function k) (@Some function f) *)
    intros.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    destruct H7; split; auto.
    inv H7.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    rewrite call_cont_idem in H13; auto.
    econstructor; try eassumption.
(* Goal: @eq (forall _ : environ, mpred) (fun rho : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho) (maybe_retval (Q ts x) retsig ret rho))) (fun rho0 : environ => @exp rmap ag_rmap val (fun old : val => @sepcon rmap Join_rmap Perm_rmap ag_rmap Age_rmap (@substopt (@pred rmap ag_rmap) ret old F rho0) (maybe_retval (Q ts x) (@snd (list (prod ident type)) type (@pair (list (prod ident type)) type argsig retsig)) ret rho0))) *)
    auto.
Qed.

End extensions.
