Fixpoint mkInjectionsN (N:nat)(n1 n2:block)(j k l: meminj)
:  meminj * meminj * block * block :=
match N with O => (j,k,n1,n2)
| S M => mkInjectionsN M (Pos.succ n1) (Pos.succ n2)
(fun b => if eq_block b n1
then Some (n2,0)
else j b)
(fun b => if eq_block b n2 then l n1 else k b)
l
end.
Definition mkInjections (m1 m1' m2:mem) (j k l: meminj)
:  meminj * meminj * block * block :=
if plt (Mem.nextblock m1) (Mem.nextblock m1')
then mkInjectionsN (Pos.to_nat (Mem.nextblock m1') - Pos.to_nat(Mem.nextblock m1))
(Mem.nextblock m1)
(Mem.nextblock m2) j k l
else (j,k,Mem.nextblock m1,Mem.nextblock m2).
Definition removeUndefs (j l j':meminj):meminj :=
fun b => match j b with
None => match l b with
None => None | Some (b1,delta1) => j' b
end
| Some(b2,delta2) => Some(b2, delta2)
end.
Definition inject_memval (j:meminj) (v:memval): memval :=
match v with
Pointer b ofs n =>
match j b with
None => Undef
| Some(b',delta) => Pointer b' (Int.add ofs (Int.repr delta)) n
end
| _ => v
end.
Definition AccessMap_II_Property  (j12 j23 j12' :meminj) (m1 m1' m2 : mem)
(AM:ZMap.t (Z -> perm_kind -> option permission)):Prop :=
forall b2,
(Mem.valid_block m2 b2 -> forall k ofs2,
match j23 b2 with
None => PMap.get b2 AM ofs2 k  = PMap.get b2 m2.(Mem.mem_access) ofs2 k
| Some (b3,d3) =>
match source j12 m1 b2 ofs2 with
Some(b1,ofs1) =>  PMap.get b2 AM ofs2 k =
PMap.get b1 m1'.(Mem.mem_access) ofs1 k
| None =>  PMap.get b2 AM ofs2 k =
PMap.get b2 m2.(Mem.mem_access) ofs2 k
end
end)
/\ (~ Mem.valid_block m2 b2 -> forall k ofs2,
match source j12' m1' b2 ofs2 with
Some(b1,ofs1) => PMap.get b2 AM ofs2 k =
PMap.get b1 m1'.(Mem.mem_access) ofs1 k
| None =>  PMap.get b2 AM ofs2 k = None
end).
Definition Content_II_Property (j12 j12' j23':meminj) (m1 m1' m2:Mem.mem)
(CM:ZMap.t (ZMap.t memval)):=
forall b2,
(Mem.valid_block m2 b2 -> forall ofs2,
match source j12 m1 b2 ofs2 with
Some(b1,ofs1) =>
match j23' b2 with
None => ZMap.get ofs2 (PMap.get b2 CM) =
ZMap.get ofs2 (PMap.get b2 m2.(Mem.mem_contents))
| Some(b3,ofs3) =>
ZMap.get ofs2 (PMap.get b2 CM) =
inject_memval j12'
(ZMap.get ofs1 (PMap.get b1 m1'.(Mem.mem_contents)))
end
| None => ZMap.get ofs2 (PMap.get b2 CM) =
ZMap.get ofs2 (PMap.get b2 m2.(Mem.mem_contents))
end)
/\ (~ Mem.valid_block m2 b2 -> forall ofs2,
match source j12' m1' b2 ofs2 with
None => ZMap.get ofs2 (PMap.get b2 CM) =Undef
| Some(b1,ofs1) =>
ZMap.get ofs2 (PMap.get b2 CM) =
inject_memval j12'
(ZMap.get ofs1 (PMap.get b1 m1'.(Mem.mem_contents)))
end)
/\ fst CM !! b2 = Undef.

Lemma II_ok: forall m1 m2 j12 (MInj12 : Mem.inject j12 m1 m2) m1'
(Fwd1: mem_forward m1 m1') j23 m3
(MInj23 : Mem.inject j23 m2 m3) m3'
(Fwd3: mem_forward m3 m3')
j' (MInj13': Mem.inject j' m1' m3')
(InjIncr: inject_incr (compose_meminj j12 j23) j')
(InjSep: inject_separated (compose_meminj j12 j23) j' m1 m3)
(Unch11': Mem.unchanged_on
(loc_unmapped (compose_meminj j12 j23)) m1 m1')
(Unch33': Mem.unchanged_on
(loc_out_of_reach (compose_meminj j12 j23) m1) m3 m3')
prej12' j23' n1' n2'
(HeqMKI: mkInjections m1 m1' m2 j12 j23 j' =
(prej12', j23', n1', n2'))
j12' (Hj12': j12'= removeUndefs j12 j' prej12')
m2'
(NB: m2'.(Mem.nextblock)=n2')
(CONT:  Content_II_Property j12 j12' j23' m1 m1' m2
(m2'.(Mem.mem_contents)))
(ACCESS: AccessMap_II_Property j12 j23 j12' m1 m1' m2
(m2'.(Mem.mem_access))),
j'=compose_meminj j12' j23' /\
inject_incr j12 j12' /\ inject_incr j23 j23' /\
Mem.inject j12' m1' m2' /\ mem_forward m2 m2' /\
Mem.inject j23' m2' m3' /\
Mem.unchanged_on (loc_out_of_reach j12 m1) m2 m2' /\
inject_separated j12 j12' m1 m2 /\
inject_separated j23 j23' m2 m3 /\
Mem.unchanged_on (loc_unmapped j23) m2 m2' /\
Mem.unchanged_on (loc_out_of_reach j23 m2) m3 m3'.