Section PROG.
Let a : var := 0.
Let s : var := 1.
Let p : var := 2.
Let r : var := 3.
Let START : adr := 0.
Let LOOP : adr := 1.
Let DONE : adr := 2.
Definition STARTbody := (a::nil,
Do Mem a := a .+ 1; Do Mem (a .+ 1) := a .+ 2; Do Mem (a .+ 2) := Const 0;
Go LOOP ((a.+3)::(Var a)::(Var a)::(Const DONE)::nil)).
Definition LOOPbody := (a::s::p::r::nil,
If p Then (Do p := Mem p; Go LOOP (Var a::Var s::Var p::Var r::nil))
Else Go r (Var a::Var s::nil)).
Definition DONEbody := (a::s::nil,  Go DONE (Var a::Var s::nil)).
Definition myprog : program :=
(START, STARTbody):: (LOOP,  LOOPbody) :: (DONE,   DONEbody) :: nil.
Definition STARTspec : funspec := (a::nil,
fun s => allocpool (eval (Var a) s)).
Definition DONEspec: funspec := (a::s::nil,
fun s' => lseg (eval (Var s) s') (eval (Const 0) s') * allocpool (eval (Var a) s')).
Definition LOOPspec: funspec :=  (a::s::p::r::nil,
fun s' => lseg (eval (Var s) s') (eval (Var p) s')
* lseg (eval (Var p) s') (eval (Const 0) s')
* allocpool (eval (Var a) s')
&& cont DONEspec (eval (Var r) s')).
Definition myspec := (START, STARTspec):: ((LOOP, LOOPspec) :: (DONE, DONEspec) :: nil).
eapply semax_pre; [intro ; call_tac; apply derives_refl |  simpl ].
rewrite' alloc.
apply semax_prop; auto; intros _.
forward.
rewrite' alloc. rewrite' @sepcon_comm.  rewrite' @sepcon_assoc.
forward.
rewrite' alloc. rewrite' @sepcon_comm. do 2  rewrite' @sepcon_assoc.
forward.
forward.
rewrite lseg_eq. normalize.
apply andp_derives;  [ | apply funassert_e; reflexivity].
rewrite (sepcon_comm (allocpool _)). repeat rewrite <- sepcon_assoc.
rewrite (sepcon_comm (next (S (S (s0 a))) _)).
apply sepcon_derives; auto.
repeat rewrite sepcon_assoc. rewrite (next_gt_0 (s0 a)).
normalize.
eapply derives_trans;  [ |  eapply lseg_cons; try omega].
eapply sepcon_derives; [ apply derives_refl |].
rewrite sepcon_comm.
eapply derives_trans;  [ |  eapply lseg_cons; try omega].
eapply sepcon_derives; [ apply derives_refl |].
apply next_lseg;  omega.
Qed.

Lemma prove_LOOP: semax_body myspec LOOPspec LOOPbody.