aboutsummaryrefslogtreecommitdiffstats
path: root/src/hls/DHTLgenproof.v
blob: e0616e83a985c94473fecca3ccd4f9f7b97ead45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
Require Import Coq.micromega.Lia.

Require Import compcert.lib.Maps.
Require Import compcert.common.Errors.
Require Import compcert.common.Globalenvs.
Require compcert.backend.Registers.
Require Import compcert.common.Linking.
Require Import compcert.common.Memory.
Require compcert.common.Globalenvs.
Require Import compcert.lib.Integers.
Require Import compcert.common.AST.

Require Import vericert.common.IntegerExtra.
Require Import vericert.common.Vericertlib.
Require Import vericert.common.ZExtra.
Require Import vericert.hls.Array.
Require Import vericert.hls.AssocMap.
Require Import vericert.hls.DHTL.
Require Import vericert.hls.Gible.
Require Import vericert.hls.GibleSubPar.
Require Import vericert.hls.DHTLgen.
Require Import vericert.hls.Predicate.
Require Import vericert.hls.ValueInt.
Require Import vericert.hls.Verilog.
Require vericert.hls.Verilog.
Require Import vericert.common.Errormonad.
Require Import vericert.hls.DHTLgenproof0.
Import ErrorMonad.
Import ErrorMonadExtra.

Require Import Lia.

Local Open Scope assocmap.

Local Opaque Int.max_unsigned.

#[local] Hint Resolve AssocMap.gss : htlproof.
#[local] Hint Resolve AssocMap.gso : htlproof.

#[local] Hint Unfold find_assocmap AssocMapExt.get_default : htlproof.

Section CORRECTNESS.

  Variable prog : GibleSubPar.program.
  Variable tprog : DHTL.program.

  Hypothesis TRANSL : match_prog prog tprog.

  Let ge : GibleSubPar.genv := Globalenvs.Genv.globalenv prog.
  Let tge : DHTL.genv := Globalenvs.Genv.globalenv tprog.

  (* Lemma storev_stack_bounds : *)
  (*   forall m sp v dst m' hi, *)
  (*     Mem.storev Mint32 m (Values.Vptr sp (Ptrofs.repr v)) dst = Some m' -> *)
  (*     stack_bounds (Values.Vptr sp (Ptrofs.repr 0)) hi m -> *)
  (*     v mod 4 = 0 -> *)
  (*     0 <= v < hi. *)
  (* Proof. *)
  (*   intros. unfold stack_bounds in *. *)
  (*   assert (0 <= v < hi \/ hi <= ) *)

  Ltac tac :=
    repeat match goal with
           | [ _ : error _ _ = OK _ _ _ |- _ ] => discriminate
           | [ _ : context[if (?x && ?y) then _ else _] |- _ ] =>
             let EQ1 := fresh "EQ" in
             let EQ2 := fresh "EQ" in
             destruct x eqn:EQ1; destruct y eqn:EQ2; simpl in *
           | [ _ : context[if ?x then _ else _] |- _ ] =>
             let EQ := fresh "EQ" in
             destruct x eqn:EQ; simpl in *
           | [ H : ret _ _ = _  |- _ ] => inv H
           | [ _ : context[match ?x with | _ => _ end] |- _ ] => destruct x
           end.

  Ltac inv_arr_access :=
    match goal with
    | [ _ : translate_arr_access ?chunk ?addr ?args _ _ = OK ?c _ _ |- _] =>
      destruct c, chunk, addr, args; crush; tac; crush
    end.

  Lemma offset_expr_ok :
    forall v z, (Z.to_nat
                   (Integers.Ptrofs.unsigned
                      (Integers.Ptrofs.divu
                         (Integers.Ptrofs.add (Integers.Ptrofs.repr (uvalueToZ v))
                                              (Integers.Ptrofs.of_int (Integers.Int.repr z)))
                         (Integers.Ptrofs.repr 4)))
                 = valueToNat (Int.divu (Int.add v (ZToValue z)) (ZToValue 4))).
  Proof.
    simplify_val. unfold valueToNat. unfold Int.divu, Ptrofs.divu.
    pose proof Integers.Ptrofs.agree32_add as AGR.
    unfold Integers.Ptrofs.agree32 in AGR.
    assert (Ptrofs.unsigned (Ptrofs.add (Ptrofs.repr (Int.unsigned v))
                                        (Ptrofs.repr (Int.unsigned (Int.repr z)))) =
            Int.unsigned (Int.add v (ZToValue z))).
    apply AGR; auto.
    apply Ptrofs.unsigned_repr. apply Int.unsigned_range_2.
    apply Ptrofs.unsigned_repr. apply Int.unsigned_range_2.
    rewrite H. replace (Ptrofs.unsigned (Ptrofs.repr 4)) with 4.
    replace (Int.unsigned (ZToValue 4)) with 4.
    pose proof Ptrofs.agree32_repr. unfold Ptrofs.agree32 in *.
    rewrite H0. trivial. auto.
    unfold ZToValue. symmetry. apply Int.unsigned_repr.
    unfold_constants. lia.
    unfold ZToValue. symmetry. apply Int.unsigned_repr.
    unfold_constants. lia.
  Qed.

  Lemma offset_expr_ok_2 :
    forall v0 v1 z0 z1,
      (Z.to_nat
         (Integers.Ptrofs.unsigned
            (Integers.Ptrofs.divu
               (Integers.Ptrofs.add (Integers.Ptrofs.repr (uvalueToZ v0))
                                    (Integers.Ptrofs.of_int
                                       (Integers.Int.add
                                          (Integers.Int.mul (valueToInt v1) (Integers.Int.repr z1))
                                          (Integers.Int.repr z0)))) (Ptrofs.repr 4))))
      = valueToNat (Int.divu (Int.add (Int.add v0 (ZToValue z0))
                                      (Int.mul v1 (ZToValue z1))) (ZToValue 4)).
    intros. unfold ZToValue, valueToNat, valueToInt, Ptrofs.divu, Int.divu, Ptrofs.of_int.

    assert (H : (Ptrofs.unsigned
             (Ptrofs.add (Ptrofs.repr (uvalueToZ v0))
                (Ptrofs.of_int (Int.add (Int.mul (valueToInt v1) (Int.repr z1)) (Int.repr z0)))) /
           Ptrofs.unsigned (Ptrofs.repr 4))
                = (Int.unsigned (Int.add (Int.add v0 (Int.repr z0)) (Int.mul v1 (Int.repr z1))) /
           Int.unsigned (Int.repr 4))).
    { unfold ZToValue, valueToNat, valueToInt, Ptrofs.divu, Int.divu, Ptrofs.of_int.
      rewrite Ptrofs.unsigned_repr by (unfold_constants; lia).
      rewrite Int.unsigned_repr by (unfold_constants; lia).

      unfold Ptrofs.of_int. rewrite Int.add_commut.
      pose proof Integers.Ptrofs.agree32_add as AGR. unfold Ptrofs.agree32 in *.
      erewrite AGR.
      3: { unfold uvalueToZ. rewrite Ptrofs.unsigned_repr. trivial. apply Int.unsigned_range_2. }
      3: { rewrite Ptrofs.unsigned_repr. trivial. apply Int.unsigned_range_2. }
      rewrite Int.add_assoc. trivial. auto.
    }

    rewrite <- H. auto.

  Qed.

  Lemma offset_expr_ok_3 :
    forall OFFSET,
      Z.to_nat (Ptrofs.unsigned (Ptrofs.divu OFFSET (Ptrofs.repr 4)))
      = valueToNat (ZToValue (Ptrofs.unsigned OFFSET / 4)).
  Proof. auto. Qed.

  Lemma storev_mod_ok' :
    forall m sp' ptr src m',
      0 <= ptr <= Ptrofs.max_unsigned ->
      Mem.storev Mint32 m (Values.Val.offset_ptr (Values.Vptr sp' (Ptrofs.repr 0)) (Ptrofs.repr ptr)) src = Some m' ->
      ptr mod 4 = 0.
  Proof.
    unfold Mem.storev; intros * BOUND **. repeat destruct_match; try discriminate.
    eapply Mem.store_valid_access_3 in H.
    unfold Mem.valid_access in H. inv H. apply Zdivide_mod. cbn -[Ptrofs.max_unsigned]in *.
    inv Heqv. rewrite Ptrofs.add_unsigned in H1.
    rewrite ! Ptrofs.unsigned_repr in H1; try lia. auto.
    rewrite ! Ptrofs.unsigned_repr; lia.
  Qed.

  Lemma loadv_mod_ok' :
    forall m sp' ptr v,
      0 <= ptr <= Ptrofs.max_unsigned ->
      Mem.loadv Mint32 m (Values.Val.offset_ptr (Values.Vptr sp' (Ptrofs.repr 0)) (Ptrofs.repr ptr)) = Some v ->
      ptr mod 4 = 0.
  Proof.
    unfold Mem.loadv; intros * BOUND **. repeat destruct_match; try discriminate.
    eapply Mem.load_valid_access in H.
    unfold Mem.valid_access in H. inv H. apply Zdivide_mod. cbn -[Ptrofs.max_unsigned]in *.
    inv Heqv0. rewrite Ptrofs.add_unsigned in H1.
    rewrite ! Ptrofs.unsigned_repr in H1; try lia. auto.
    rewrite ! Ptrofs.unsigned_repr; lia.
  Qed.

  Lemma offset_ptr_equiv :
    forall sp' v,
      Values.Val.offset_ptr (Values.Vptr sp' (Ptrofs.repr 0)) v = Values.Vptr sp' v.
  Proof.
    unfold Values.Val.offset_ptr; intros.
    replace (Ptrofs.repr 0) with Ptrofs.zero by auto.
    now rewrite Ptrofs.add_zero_l.
  Qed.

  Lemma loadv_mod_ok :
    forall m sp' ptr v,
      0 <= ptr <= Ptrofs.max_unsigned ->
      Mem.loadv Mint32 m (Values.Vptr sp' (Ptrofs.repr ptr)) = Some v ->
      ptr mod 4 = 0.
  Proof.
    intros. eapply loadv_mod_ok'; eauto.
    rewrite offset_ptr_equiv; eauto.
  Qed.

  Lemma storev_mod_ok :
    forall m sp' ptr src m',
      0 <= ptr <= Ptrofs.max_unsigned ->
      Mem.storev Mint32 m (Values.Vptr sp' (Ptrofs.repr ptr)) src = Some m' ->
      ptr mod 4 = 0.
  Proof.
    intros. eapply storev_mod_ok'; eauto.
    rewrite offset_ptr_equiv; eauto.
  Qed.

  Lemma loadv_mod_ok2 :
    forall m sp' v v',
      Mem.loadv Mint32 m (Values.Vptr sp' v) = Some v' ->
      (Ptrofs.unsigned v) mod 4 = 0.
  Proof.
    unfold Mem.loadv; intros. repeat destruct_match; try discriminate.
    eapply Mem.load_valid_access in H.
    unfold Mem.valid_access in H. inv H. apply Zdivide_mod. cbn -[Ptrofs.max_unsigned]in *.
    auto.
  Qed.

  Lemma storev_mod_ok2 :
    forall m sp' src m' v,
      Mem.storev Mint32 m (Values.Vptr sp' v) src = Some m' ->
      (Ptrofs.unsigned v) mod 4 = 0.
  Proof.
    unfold Mem.storev; intros. repeat destruct_match; try discriminate.
    eapply Mem.store_valid_access_3 in H.
    unfold Mem.valid_access in H. inv H. apply Zdivide_mod. cbn -[Ptrofs.max_unsigned]in *.
    auto.
  Qed.

  Lemma storev_exists_ptr:
    forall m v src m',
      Mem.storev Mint32 m v src = Some m' ->
      exists sp v', v = Values.Vptr sp v'.
  Proof.
    intros.
    unfold Mem.storev in *. destruct_match; try discriminate.
    subst. eauto.
  Qed.

  Lemma loadv_exists_ptr:
    forall m v m',
      Mem.loadv Mint32 m v = Some m' ->
      exists sp v', v = Values.Vptr sp v'.
  Proof using.
    intros.
    unfold Mem.loadv in *. destruct_match; try discriminate.
    subst. eauto.
  Qed.

  Lemma val_add_stack_based :
    forall v1 v2 sp,
      stack_based v1 sp ->
      stack_based v2 sp ->
      stack_based (Values.Val.add v1 v2) sp.
  Proof.
    intros. destruct v1, v2; auto.
    inv H. inv H0. cbn; auto.
  Qed.

  Lemma val_mul_stack_based :
    forall v1 v2 sp,
      stack_based v1 sp ->
      stack_based v2 sp ->
      stack_based (Values.Val.mul v1 v2) sp.
  Proof.
    intros. destruct v1, v2; auto.
    inv H. inv H0. cbn; auto.
  Qed.

  Lemma ptrofs_unsigned_add_0:
    forall x0,
      Ptrofs.unsigned (Ptrofs.add (Ptrofs.repr 0) (Ptrofs.repr (Ptrofs.unsigned x0))) = Ptrofs.unsigned x0.
  Proof.
    intros. replace (Ptrofs.repr 0) with (Ptrofs.zero) by auto.
    rewrite Ptrofs.add_zero_l. rewrite Ptrofs.unsigned_repr; auto.
    apply Ptrofs.unsigned_range_2.
  Qed.

  Lemma exists_ptr_add_int :
    forall a b sp' x0,
      Values.Val.add a (Values.Vint b) = Values.Vptr sp' x0 ->
      exists a', a = Values.Vptr sp' a'.
  Proof.
    intros. destruct a; eauto; cbn in *; try discriminate.
    assert (Xx: Archi.ptr64 = false) by auto. rewrite Xx in H. inv H. eauto.
  Qed.

  Lemma transl_arr_access_correct :
    forall addr args e rs ps m sp a chnk src m' s f pc s' m_ asr arr,
      translate_arr_access chnk addr args m_.(DHTL.mod_stk) = OK e ->
      Op.eval_addressing ge sp addr (List.map (fun r => Registers.Regmap.get r rs) args) = Some a ->
      Mem.storev chnk m a (Registers.Regmap.get src rs) = Some m' ->
      match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr arr) ->
      exists x, expr_runp tt asr arr e x.
  Proof.
    assert (HARCH: Archi.ptr64 = false) by auto.
    intros. unfold translate_arr_access in *. repeat destr.
    destruct_match; try discriminate; repeat destr.
    - inv H. cbn in *. unfold Op.eval_addressing in *. rewrite HARCH in H0.
      cbn in *. inv H0. inv H2. unfold stack_bounds in *.
      exploit storev_exists_ptr; eauto. simplify.
      assert (stack_based (Values.Vint (Int.repr z)) sp') by (cbn; auto).
      assert (stack_based (rs !! r) sp') by (cbn; auto).
      eapply val_add_stack_based in H0; eauto. rewrite H in H0. cbn in *. inv H0.
      rewrite H in H1. exploit storev_mod_ok2; eauto; intros.
      specialize (BOUNDS (Ptrofs.unsigned x0) rs !! src).
      pose proof (ptrofs_unsigned_lt_int_max x0).
      assert (0 <= Ptrofs.unsigned x0 < fn_stacksize f \/ fn_stacksize f <= Ptrofs.unsigned x0 <= Int.max_unsigned) by lia.
      inv H4.
      + inv MARR. inv H4. eexists. econstructor. econstructor. econstructor. econstructor.
        eauto. econstructor. cbn. eauto. econstructor. cbn. unfold ZToValue.
        unfold Int.zero. unfold Int.eq. rewrite ! Int.unsigned_repr by crush.
        cbn. eauto. (* exploit exists_ptr_add_int; eauto. intros (a & HPTR). *)
        (* rewrite HPTR in H. cbn in H. *)
        assert (HARCHI: Archi.ptr64 = false) by auto.
        unfold arr_assocmap_lookup. setoid_rewrite H6. eauto.
      + apply BOUNDS in H5; auto. inv H5. rewrite ptrofs_unsigned_add_0 in H6.
        unfold Mem.storev in H1. rewrite H6 in H1. discriminate.
    - inv H. inv H2. inv MARR. inv H. repeat econstructor. unfold arr_assocmap_lookup.
      setoid_rewrite H2. auto.
    - inv H. inv H2. inv MARR. inv H. repeat econstructor. unfold arr_assocmap_lookup.
      setoid_rewrite H2. auto.
  Qed.

  Lemma stack_correct_transl:
    forall f m_,
      transl_module f = OK m_ ->
      0 <= fn_stacksize f /\ fn_stacksize f < Ptrofs.modulus /\ fn_stacksize f mod 4 = 0.
  Proof.
    intros; unfold transl_module, Errors.bind, ret in *; repeat destr. inv H.
    cbn in *. eapply stack_correct_inv; eauto.
  Qed.

  Lemma stack_correct_match_states:
    forall s f sp pc rs pr s' m_ asr asa m,
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      0 <= fn_stacksize f /\ fn_stacksize f < Ptrofs.modulus /\ fn_stacksize f mod 4 = 0.
  Proof.
    inversion 1; subst. unfold transl_module, Errors.bind, ret in *; repeat destr. inv TF.
    cbn in *. eapply stack_correct_inv; eauto.
  Qed.

  Lemma load_exists_pointer_offset :
    forall s f pc rs pr m v v' sp s' m_ asr asa,
      stack_based v sp ->
      Mem.loadv Mint32 m v = Some v' ->
      match_states (GibleSubPar.State s f (Values.Vptr sp (Ptrofs.repr 0)) pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      exists ptr, 0 <= ptr < fn_stacksize f / 4 /\ v = Values.Val.offset_ptr (Values.Vptr sp (Ptrofs.repr 0)) (Ptrofs.repr (4 * ptr)).
  Proof.
    intros * HSTACK HMEM HMATCH.
    exploit loadv_exists_ptr; eauto. intros (sp0 & v0 & HVAL).
    unfold Values.Val.offset_ptr. subst; exploit loadv_mod_ok2; eauto.
    intros. inv HMATCH. unfold stack_bounds in *.
    specialize (BOUNDS (Ptrofs.unsigned v0)).
    pose proof (ptrofs_unsigned_lt_int_max v0) as HY.
    assert (HX: 0 <= Ptrofs.unsigned v0 < fn_stacksize f \/ fn_stacksize f <= Ptrofs.unsigned v0 <= Int.max_unsigned) by lia.
    inv HX.
    + inv MARR. 
      assert (Ptrofs.unsigned v0 = (Ptrofs.unsigned v0 / 4) * 4).
      { erewrite Z_div_mod_eq_full at 1. rewrite H. lia. }
      assert (fn_stacksize f = (fn_stacksize f / 4) * 4).
      { erewrite Z_div_mod_eq_full at 1. exploit stack_correct_transl; eauto; intros (STK1 & STK2 & STK3). rewrite STK3. lia. }
      assert (0 <= Ptrofs.unsigned v0 / 4 < fn_stacksize f / 4) by lia. eexists; split; eauto.
      replace (4 * (Ptrofs.unsigned v0 / 4)) with (Ptrofs.unsigned v0) by lia.
      rewrite Ptrofs.repr_unsigned by eauto. rewrite Ptrofs.add_zero_l.
      inv SP. cbn in HSTACK; subst. auto.
    + apply BOUNDS in H0; auto. inv H0. rewrite ptrofs_unsigned_add_0 in H2.
      unfold Mem.loadv in HMEM. inv SP. cbn in HSTACK. subst. rewrite HMEM in H2. discriminate.
  Qed.

  Lemma div_ineq :
    forall a x y, 0 <= x <= a -> 0 <= y <= a -> y <> 0 -> 0 <= x / y <= a.
  Proof.
    intros. pose proof (Z_div_pos x y ltac:(lia) ltac:(lia)). split; auto.
    pose proof (Z.div_le_mono x a y ltac:(lia) ltac:(lia)).
    assert (a / y <= a).
    { eapply Z.div_le_upper_bound; try lia. nia. }
    lia.
  Qed.

  Lemma expr_runp_load_1 :
    forall z s f sp pc rs pr' m' s' m_ asr asa r0 v,
      check_address_parameter_signed z = true ->
      match_states (GibleSubPar.State s f (Values.Vptr sp (Ptrofs.repr 0)) pc rs pr' m') (DHTL.State s' m_ pc asr asa) ->
      Mem.loadv Mint32 m' (Values.Val.add rs !! r0 (Values.Vint (Int.repr z))) = Some v ->
      Ple r0 (max_reg_function f) ->
      exists v' : value,
      expr_runp tt asr asa (Vvari (DHTL.mod_stk m_) (Vbinop Vdivu (boplitz Vadd r0 z) (Vlit (ZToValue 4)))) v' /\
      val_value_lessdef v v'.
  Proof.
    intros * HCHECK HMATCH HLOAD HPLE.
    exploit load_exists_pointer_offset. 2: { eauto. } 2: { eauto. } inv HMATCH. inv SP. eapply val_add_stack_based; eauto. now cbn.
    intros (ptr & HSIZE & HVAL).
    inv HMATCH. inv MARR; simplify. rename H2 into HPTR1, H4 into HPTR2, H0 into HSTACK, H into HLEN1, H1 into HLEN2, H3 into HEQ.
    rewrite HVAL in HLOAD. specialize (HEQ ptr ltac:(lia)).
    unfold Mem.loadv in HLOAD. rewrite HLOAD in HEQ.
    inv HEQ. exists (get_mem (Z.to_nat ptr) stack); split; auto.
    repeat (econstructor; eauto).
    unfold arr_assocmap_lookup, get_mem. setoid_rewrite HSTACK.
    do 4 f_equal.
    destruct (rs !! r0) eqn:HRS; try discriminate.
    cbn in *. replace Archi.ptr64 with false in HVAL by auto.
    rewrite Ptrofs.add_zero_l in HVAL.
    assert (Ptrofs.unsigned (Ptrofs.repr (4 * ptr)) = Ptrofs.unsigned (Ptrofs.add i (Ptrofs.of_int (Int.repr z)))).
    { inv HVAL; eauto. }
    assert (HR: forall a b, a < b -> a * 4 < b * 4) by lia. eapply HR in HPTR2.
    exploit stack_correct_transl; eauto; intros (HSTK1 & HSTK2 & HSTK3).
    replace (fn_stacksize f / 4 * 4) with (4 * (fn_stacksize f / 4)) in HPTR2 by lia.
    rewrite <- Z_div_exact_2 with (b := 4) in HPTR2 by lia.
    assert (0 <= 4 * ptr < fn_stacksize f) by lia.
    exploit stack_correct_transl; eauto; intros (STK1 & STK2 & STK3).
    assert (0 <= fn_stacksize f <= Ptrofs.max_unsigned) by crush.
    rewrite Ptrofs.unsigned_repr in H by lia.
    assert (forall a b c, a = b -> a / c = b / c) by (intros; subst; auto).
    apply H3 with (c := 4) in H.
    replace (4 * ptr / 4) with (ptr) in H. 2: { replace (4 * ptr) with (ptr * 4) by lia. now rewrite Z_div_mult. } subst.
    rewrite <- offset_expr_ok. f_equal.
    replace 4 with (Ptrofs.unsigned (Ptrofs.repr 4)) at 2 by eauto.
    replace (Ptrofs.unsigned (Ptrofs.add i (Ptrofs.of_int (Int.repr z))) / Ptrofs.unsigned (Ptrofs.repr 4)) with 
      (Ptrofs.unsigned (Ptrofs.divu (Ptrofs.add i (Ptrofs.of_int (Int.repr z))) (Ptrofs.repr 4))).
    2: { unfold Ptrofs.divu. rewrite Ptrofs.unsigned_repr. auto.
         assert (0 <= Ptrofs.unsigned (Ptrofs.add i (Ptrofs.of_int (Int.repr z))) <= Ptrofs.max_unsigned) by auto using Ptrofs.unsigned_range_2.
         assert (0 <= Ptrofs.unsigned (Ptrofs.repr 4) <= Ptrofs.max_unsigned) by auto using Ptrofs.unsigned_range_2.
         eapply div_ineq; eauto. crush.
       }
   repeat f_equal. unfold uvalueToZ. inv MASSOC. eapply H in HPLE.  rewrite HRS in HPLE. inv HPLE; auto.
  Qed.

  Lemma expr_runp_load_2 :
    forall z s f sp pc rs pr' m' s' m_ asr asa r0 v r1 z0,
      check_address_parameter_signed z = true ->
      match_states (GibleSubPar.State s f (Values.Vptr sp (Ptrofs.repr 0)) pc rs pr' m') (DHTL.State s' m_ pc asr asa) ->
      Mem.loadv Mint32 m' (Values.Val.add rs !! r0 (Values.Val.add (Values.Val.mul rs !! r1 (Values.Vint (Int.repr z))) (Values.Vint (Int.repr z0)))) = Some v ->
      Ple r0 (max_reg_function f) ->
      Ple r1 (max_reg_function f) ->
      exists v' : value,
      expr_runp tt asr asa (Vvari m_.(DHTL.mod_stk)
               (Vbinop Vdivu (Vbinop Vadd (boplitz Vadd r0 z0) (boplitz Vmul r1 z)) (Vlit (ZToValue 4)))) v' /\
      val_value_lessdef v v'.
  Proof.
    intros * HCHECK HMATCH HLOAD HPLE1 HPLE2.
    exploit load_exists_pointer_offset. 2: { eauto. } 2: { eauto. } inv HMATCH. inv SP. eapply val_add_stack_based; eauto.
    eapply val_add_stack_based; cbn; eauto. eapply val_mul_stack_based; cbn; eauto.
    intros (ptr & HSIZE & HVAL).
    inv HMATCH. inv MARR; simplify. rename H2 into HPTR1, H4 into HPTR2, H0 into HSTACK, H into HLEN1, H1 into HLEN2, H3 into HEQ.
    rewrite HVAL in HLOAD. specialize (HEQ ptr ltac:(lia)).
    unfold Mem.loadv in HLOAD. rewrite HLOAD in HEQ.
    inv HEQ. exists (get_mem (Z.to_nat ptr) stack); split; auto.
    repeat (econstructor; eauto).
    unfold arr_assocmap_lookup, get_mem. setoid_rewrite HSTACK.
    repeat f_equal.
    destruct (rs !! r0) eqn:HRS1; destruct (rs !! r1) eqn:HRS2; try discriminate.
    cbn in *. replace Archi.ptr64 with false in HVAL by auto.
    rewrite Ptrofs.add_zero_l in HVAL.
    assert (Ptrofs.unsigned (Ptrofs.repr (4 * ptr)) = Ptrofs.unsigned (Ptrofs.add i (Ptrofs.of_int (Int.add (Int.mul i0 (Int.repr z)) (Int.repr z0))))).
    { inv HVAL; eauto. }
    assert (HR: forall a b, a < b -> a * 4 < b * 4) by lia. eapply HR in HPTR2.
    exploit stack_correct_transl; eauto; intros (HSTK1 & HSTK2 & HSTK3).
    replace (fn_stacksize f / 4 * 4) with (4 * (fn_stacksize f / 4)) in HPTR2 by lia.
    rewrite <- Z_div_exact_2 with (b := 4) in HPTR2 by lia.
    assert (0 <= 4 * ptr < fn_stacksize f) by lia.
    exploit stack_correct_transl; eauto; intros (STK1 & STK2 & STK3).
    assert (0 <= fn_stacksize f <= Ptrofs.max_unsigned) by crush.
    rewrite Ptrofs.unsigned_repr in H by lia.
    assert (forall a b c, a = b -> a / c = b / c) by (intros; subst; auto).
    apply H3 with (c := 4) in H.
    replace (4 * ptr / 4) with (ptr) in H. 2: { replace (4 * ptr) with (ptr * 4) by lia. now rewrite Z_div_mult. } subst.
    rewrite <- offset_expr_ok_2. f_equal.
    replace 4 with (Ptrofs.unsigned (Ptrofs.repr 4)) at 2 by eauto.
    match goal with |- _ = Ptrofs.unsigned ?a / Ptrofs.unsigned ?b => replace (Ptrofs.unsigned a / Ptrofs.unsigned b) with (Ptrofs.unsigned (Ptrofs.divu a b)) end.
    2: { unfold Ptrofs.divu. rewrite Ptrofs.unsigned_repr. auto.
         match goal with |- _ <= Ptrofs.unsigned ?a / Ptrofs.unsigned ?b <= _ => 
           assert (0 <= Ptrofs.unsigned a <= Ptrofs.max_unsigned) by auto using Ptrofs.unsigned_range_2;
           assert (0 <= Ptrofs.unsigned b <= Ptrofs.max_unsigned) by auto using Ptrofs.unsigned_range_2
         end.
         eapply div_ineq; eauto. crush.
       }
   repeat f_equal. 
   - unfold uvalueToZ. inv MASSOC. eapply H in HPLE1. rewrite HRS1 in HPLE1. inv HPLE1; auto.
   - unfold uvalueToZ. inv MASSOC. eapply H in HPLE2. rewrite HRS2 in HPLE2. inv HPLE2; auto.
  Qed.

  Lemma expr_runp_load_3 :
    forall s f sp pc rs pr' m' s' m_ asr asa v i,
      match_states (GibleSubPar.State s f (Values.Vptr sp (Ptrofs.repr 0)) pc rs pr' m') (DHTL.State s' m_ pc asr asa) ->
      Mem.loadv Mint32 m' (Values.Val.offset_ptr (Values.Vptr sp (Ptrofs.repr 0)) i) = Some v ->
      exists v' : value,
      expr_runp tt asr asa (Vvari m_.(DHTL.mod_stk) (Vlit (ZToValue (Ptrofs.unsigned i / 4)))) v' /\
      val_value_lessdef v v'.
  Proof.
    intros * HMATCH HLOAD.
    exploit load_exists_pointer_offset. 2: { eauto. } 2: { eauto. } inv HMATCH. inv SP. unfold Values.Val.offset_ptr; cbn; auto.
    intros (ptr & HSIZE & HVAL).
    inv HMATCH. inv MARR; simplify. rename H2 into HPTR1, H4 into HPTR2, H0 into HSTACK, H into HLEN1, H1 into HLEN2, H3 into HEQ.
    specialize (HEQ ptr ltac:(lia)). rewrite ! Ptrofs.add_zero_l in *.
    assert (HUNSG: Ptrofs.unsigned i = (Ptrofs.unsigned (Ptrofs.repr (4 * ptr)))) by (inv HVAL; eauto). rewrite <- HUNSG in HEQ.
    unfold Mem.loadv in HLOAD. rewrite HLOAD in HEQ.
    inv HEQ. exists (get_mem (Z.to_nat ptr) stack); split; auto.
    repeat (econstructor; eauto).
    unfold arr_assocmap_lookup, get_mem. setoid_rewrite HSTACK.
    repeat f_equal.
    unfold valueToNat, ZToValue. f_equal. rewrite Int.unsigned_repr.
    2: { eapply div_ineq; eauto using ptrofs_unsigned_lt_int_max; crush. }
    rewrite Ptrofs.unsigned_repr in HUNSG. rewrite HUNSG. replace (4 * ptr) with (ptr * 4) by lia.
    now rewrite Z_div_mult by lia.
    exploit stack_correct_transl; eauto; simplify. lia.
    assert (HR: forall a b, a < b -> a * 4 < b * 4) by lia. eapply HR in HPTR2.
    replace (fn_stacksize f / 4 * 4) with (4 * (fn_stacksize f / 4)) in HPTR2 by lia.
    rewrite <- Z_div_exact_2 with (b := 4) in HPTR2 by lia. lia.
  Qed.

  Lemma reset_transl_module :
    forall f m_,
      transl_module f = OK m_ ->
      m_.(DHTL.mod_reset) = Pos.succ (Pos.succ (Pos.succ (Pos.succ (Pos.succ (Pos.succ (max_resource_function f)))))).
  Proof.
    unfold transl_module, Errors.bind, ret. intros. repeat destr; inv H; auto.
  Qed.

  Opaque translate_predicate.
  Lemma transl_load_correct :
    forall m0 a0 l f e m_ asa asr asa0 asr0 pr next_p sp rs m rs' pr' m' o r s pc s' a stmnt,
      translate_arr_access m0 a0 l (ctrl_stack (mk_ctrl f)) = OK e ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr next_p = true ->
      truthy pr o ->
      step_instr ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) (RBload o m0 a0 l r) 
        (Iexec {| is_rs := rs'; is_ps := pr'; is_mem := m' |}) ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      Forall (fun x : positive => Ple x (max_pred_function f)) (pred_uses (RBload o m0 a0 l r)) ->
      Ple (max_predicate next_p) (max_pred_function f) ->
      Ple (max_reg_instr a (RBload o m0 a0 l r)) (max_reg_function f) ->
      exists (asr' : AssocMap.t value) (asa' : AssocMap.t arr),
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0)
          (Vseq stmnt (translate_predicate Vblock (Some (Pand next_p (dfltp o))) (Vvar (reg_enc r)) e)) 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa') /\
        match_states (GibleSubPar.State s f sp pc rs' pr' m') (DHTL.State s' m_ pc asr' asa') /\ eval_predf pr' next_p = true.
  Proof.
    intros * HTRANSL HSTMNT HEVAL HTRUTHY HSTEP HMATCH HFRL HPLE1 HPLE2.
    unfold translate_arr_access in HTRANSL; repeat destr; destruct_match; try discriminate; repeat destr;
    inv HTRANSL.
    - inv HSTEP. rename H4 into HADDR, H12 into MEMLOAD, H13 into HTRUTHY'. clear HTRUTHY'.
      2: { inv H3. inv HTRUTHY. cbn in *. now rewrite H1 in H0. }
      unfold Op.eval_addressing in *. replace Archi.ptr64 with false in HADDR by auto. cbn in *. inv HADDR.
      assert (HEXPR: exists v', expr_runp tt asr asa (Vvari (Pos.succ (Pos.succ (Pos.succ (Pos.succ (max_resource_function f))))) (Vbinop Vdivu (boplitz Vadd r0 z) (Vlit (ZToValue 4)))) v' /\ val_value_lessdef v v').
      { inv HMATCH; exploit mk_ctrl_correct; eauto. simplify. rewrite H. eapply expr_runp_load_1; eauto. econstructor; eauto. extlia. }
      destruct HEXPR as (v' & HEXPR' & HVAL).
      exists (AssocMap.set (reg_enc r) v' asr), asa; split; [|split].
      econstructor; eauto.
      eapply transl_predicate_correct2_true; try eassumption.
      { cbn. instantiate (1:=max_pred_function f).
        destruct o; cbn; [eapply le_max_pred in HFRL|]; extlia. }
      inv HMATCH; eassumption.
      rewrite eval_predf_Pand. rewrite HEVAL. rewrite truthy_dflt; eauto.
      assert (HREG: Ple r (max_reg_function f)) by extlia.
      inv HMATCH. econstructor; eauto. eapply regs_lessdef_add_match; auto. eauto.
      eapply state_st_wf_write; eauto.
      { symmetry in TF; eapply mk_ctrl_correct in TF. inv TF. rewrite <- H. cbn.
        eapply ple_max_resource_function in HREG. extlia. }
      unfold reg_stack_based_pointers; intros. destruct (peq r r1); subst; [|rewrite PMap.gso by auto; eauto].
      rewrite PMap.gss. unfold arr_stack_based_pointers in ASBP.
      exploit load_exists_pointer_offset. 2: { eauto. } eapply val_add_stack_based; eauto.
      now cbn. econstructor; eauto. intros (ptr & HSIZE & HVAL').
      eapply ASBP in HSIZE. rewrite HVAL' in MEMLOAD. rewrite MEMLOAD in HSIZE. eauto.
      exploit mk_ctrl_correct; eauto. simplify. inv CONST.
      assert (HX: Ple r (max_reg_function f)) by extlia. eapply ple_max_resource_function in HX.
      exploit reset_transl_module; eauto; intros.
      econstructor; rewrite AssocMap.gso by extlia; auto.
      auto.
    - inv HSTEP. rename H4 into HADDR, H12 into MEMLOAD, H13 into HTRUTHY'. clear HTRUTHY'.
      2: { inv H3. inv HTRUTHY. cbn in *. now rewrite H1 in H0. }
      unfold Op.eval_addressing in *. replace Archi.ptr64 with false in HADDR by auto. cbn in *. inv HADDR.
      assert (HEXPR: exists v', expr_runp tt asr asa (Vvari (Pos.succ (Pos.succ (Pos.succ (Pos.succ (max_resource_function f)))))
               (Vbinop Vdivu (Vbinop Vadd (boplitz Vadd r0 z0) (boplitz Vmul r1 z)) (Vlit (ZToValue 4)))) v' /\ val_value_lessdef v v').
      { inv HMATCH; exploit mk_ctrl_correct; eauto. simplify. rewrite H. eapply expr_runp_load_2; eauto. econstructor; eauto. extlia. extlia. }
      destruct HEXPR as (v' & HEXPR' & HVAL).
      exists (AssocMap.set (reg_enc r) v' asr), asa; split; [|split].
      econstructor; eauto.
      eapply transl_predicate_correct2_true; try eassumption.
      { cbn. instantiate (1:=max_pred_function f).
        destruct o; cbn; [eapply le_max_pred in HFRL|]; extlia. }
      inv HMATCH; eassumption.
      rewrite eval_predf_Pand. rewrite HEVAL. rewrite truthy_dflt; eauto.
      assert (HREG: Ple r (max_reg_function f)) by extlia.
      inv HMATCH. econstructor; eauto. eapply regs_lessdef_add_match; auto. eauto.
      eapply state_st_wf_write; eauto.
      { symmetry in TF; eapply mk_ctrl_correct in TF. inv TF. rewrite <- H. cbn.
        eapply ple_max_resource_function in HREG. extlia. }
      unfold reg_stack_based_pointers; intros. destruct (peq r r2); subst; [|rewrite PMap.gso by auto; eauto].
      rewrite PMap.gss. unfold arr_stack_based_pointers in ASBP.
      exploit load_exists_pointer_offset. 2: { eauto. } eapply val_add_stack_based; eauto.
      eapply val_add_stack_based; cbn; eauto. eapply val_mul_stack_based; cbn; eauto.
      econstructor; eauto. intros (ptr & HSIZE & HVAL').
      eapply ASBP in HSIZE. rewrite HVAL' in MEMLOAD. rewrite MEMLOAD in HSIZE. eauto.
      exploit mk_ctrl_correct; eauto. simplify. inv CONST.
      assert (HX: Ple r (max_reg_function f)) by extlia. eapply ple_max_resource_function in HX.
      exploit reset_transl_module; eauto; intros.
      econstructor; rewrite AssocMap.gso by extlia; auto.
      auto.
    - inv HSTEP. rename H4 into HADDR, H12 into MEMLOAD, H13 into HTRUTHY'. clear HTRUTHY'.
      2: { inv H3. inv HTRUTHY. cbn in *. now rewrite H1 in H0. }
      unfold Op.eval_addressing in *. replace Archi.ptr64 with false in HADDR by auto. cbn in *. 
      replace Archi.ptr64 with false in * by auto. inv HADDR.
      assert (HEXPR: exists v', expr_runp tt asr asa (Vvari (Pos.succ (Pos.succ (Pos.succ (Pos.succ (max_resource_function f))))) (Vlit (ZToValue (Ptrofs.unsigned i / 4)))) v' /\ val_value_lessdef v v').
      { inv HMATCH; exploit mk_ctrl_correct; eauto. simplify. rewrite H. eapply expr_runp_load_3; try eassumption. econstructor; eauto. eauto. }
      destruct HEXPR as (v' & HEXPR' & HVAL).
      exists (AssocMap.set (reg_enc r) v' asr), asa; split; [|split].
      econstructor; eauto.
      eapply transl_predicate_correct2_true; try eassumption.
      { cbn. instantiate (1:=max_pred_function f).
        destruct o; cbn; [eapply le_max_pred in HFRL|]; extlia. }
      inv HMATCH; eassumption.
      rewrite eval_predf_Pand. rewrite HEVAL. rewrite truthy_dflt; eauto.
      assert (HREG: Ple r (max_reg_function f)) by extlia.
      inv HMATCH. econstructor; eauto. eapply regs_lessdef_add_match; auto. eauto.
      eapply state_st_wf_write; eauto.
      { symmetry in TF; eapply mk_ctrl_correct in TF. inv TF. rewrite <- H. cbn.
        eapply ple_max_resource_function in HREG. extlia. }
      unfold reg_stack_based_pointers; intros. destruct (peq r r0); subst; [|rewrite PMap.gso by auto; eauto].
      rewrite PMap.gss. unfold arr_stack_based_pointers in ASBP.
      exploit load_exists_pointer_offset. 2: { eauto. } replace Archi.ptr64 with false by auto. 
      unfold Values.Val.offset_ptr. cbn. eauto.
      econstructor; eauto. intros (ptr & HSIZE & HVAL').
      eapply ASBP in HSIZE. rewrite HVAL' in MEMLOAD. rewrite MEMLOAD in HSIZE. eauto.
      exploit mk_ctrl_correct; eauto. simplify. inv CONST.
      assert (HX: Ple r (max_reg_function f)) by extlia. eapply ple_max_resource_function in HX.
      exploit reset_transl_module; eauto; intros.
      econstructor; rewrite AssocMap.gso by extlia; auto.
      auto.
  Qed.

  Lemma exec_expr_store_1 :
    forall f m_ rs' pr' asr r0 z sp v' asa,
      transl_module f = OK m_ ->
      match_assocmaps (max_reg_function f) (max_pred_function f) rs' pr' asr ->
      Values.Val.add rs' !! r0 (Values.Vint (Int.repr z)) = Values.Vptr sp v' ->
      reg_stack_based_pointers sp rs' ->
      Ple r0 (max_reg_function f) ->
      expr_runp tt asr asa (Vbinop Vdivu (boplitz Vadd r0 z) (Vlit (ZToValue 4))) (Int.divu (ptrToValue v') (ZToValue 4)).
  Proof.
    intros. rename H3 into HPLE. repeat econstructor. cbn.
    replace (Int.eq (ZToValue 4) Int.zero) with false.
    2: { unfold Int.eq. destruct_match; auto. exfalso. unfold Int.zero, ZToValue in *. 
         clear Heqs. rewrite ! Int.unsigned_repr in e; [discriminate| |]; crush. }
    unfold ptrToValue.
    destruct (rs' !! r0) eqn:?; crush.
    replace Archi.ptr64 with false in H1 by auto. inv H1.
    f_equal. unfold Ptrofs.to_int.
    symmetry; rewrite <- Int.repr_unsigned at 1. symmetry. f_equal.
    apply Ptrofs.agree32_add; auto; unfold ZToValue.
    2: { apply Ptrofs.agree32_of_int; auto. }
    inv H0. exploit H1; eauto; intros. rewrite Heqv in H0. inv H0. unfold valueToPtr.
    apply Ptrofs.agree32_of_int; auto.
  Qed.

  Lemma transl_store_correct :
    forall m0 a0 l f e asr0 asa0 asr asa next_p sp o m_ rs pr m r rs' pr' m' s pc s' a stmnt,
      translate_arr_access m0 a0 l (ctrl_stack (mk_ctrl f)) = OK e ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr next_p = true ->
      truthy pr o ->
      step_instr ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) (RBstore o m0 a0 l r) (Iexec {| is_rs := rs'; is_ps := pr'; is_mem := m' |}) ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      Forall (fun x : positive => Ple x (max_pred_function f)) (pred_uses (RBstore o m0 a0 l r)) ->
      Ple (max_predicate next_p) (max_pred_function f) ->
      Ple (max_reg_instr a (RBstore o m0 a0 l r)) (max_reg_function f) ->
      exists (asr' : AssocMap.t value) (asa' : AssocMap.t arr),
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0)
          (Vseq stmnt (Vcond (Vbinop Vand (pred_expr next_p) (pred_expr (dfltp o))) (Vblock e (Vvar (reg_enc r))) Vskip)) (e_assoc asr')
          (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa') /\
        match_states (GibleSubPar.State s f sp pc rs' pr' m') (DHTL.State s' m_ pc asr' asa') /\ eval_predf pr' next_p = true.
  Proof.
    intros * HTRANSL HSTMNT HEVAL HTRUTHY HSTEP HMATCH HFRL HPLE1 HPLE2.
    unfold translate_arr_access in HTRANSL; repeat destr; destruct_match; try discriminate; repeat destr;
    inv HTRANSL.
    - inv HSTEP. rename H4 into HADDR, H12 into MEMLOAD, H13 into HTRUTHY'. clear HTRUTHY'.
      2: { inv H3; cbn in *. inv HTRUTHY. congruence. }
      unfold Op.eval_addressing in HADDR. replace Archi.ptr64 with false in HADDR by auto; cbn in *. inv HADDR.
      exploit storev_exists_ptr; eauto. intros (sp0 & v' & HADD).
      exists asr, (arr_assocmap_set m_.(DHTL.mod_stk) (valueToNat (Int.divu (ptrToValue v') (ZToValue 4))) (find_assocmap 32 (reg_enc r) asr) asa).
      split; [|split].
      + repeat (econstructor; try eassumption).
        * eapply pred_expr_correct; intros. inv HMATCH. inv MASSOC. eapply H1; eauto. extlia.
        * eapply pred_expr_correct; intros. inv HMATCH. inv MASSOC. eapply H1; eauto. destruct o. cbn in *.
          eapply le_max_pred in HFRL. extlia. cbn in *. extlia.
        * rewrite int_and_boolToValue. rewrite valueToBool_correct. rewrite HEVAL. now rewrite truthy_dflt. 
        * cbn. inv HMATCH. exploit mk_ctrl_correct; eauto; simplify. rewrite H.
          econstructor. eapply exec_expr_store_1; eauto; try extlia.
          destruct (rs' !! r0) eqn:?; crush. replace Archi.ptr64 with false in HADD by auto. inv HADD.
          unfold reg_stack_based_pointers in *. unfold stack_based in *. pose proof (RSBP r0). rewrite Heqv in H2.
          now subst.
      + inv HMATCH; econstructor; eauto.
        * inv MARR. destruct H as (HARR1 & HARR2 & HARR3 & HARR4). econstructor.
          repeat split.
          -- erewrite arr_assocmap_set_gss by eassumption. eauto.
          -- now rewrite <- array_set_len.
          -- now rewrite <- array_set_len.
          -- intros * HBOUND. rewrite <- array_set_len in HBOUND. apply HARR4 in HBOUND. 
             unfold Mem.loadv, Mem.storev in *. move MEMLOAD after HBOUND. repeat destr.
             destruct (rs' !! r0) eqn:?; crush. replace Archi.ptr64 with false in Heqv0 by auto.
             inv Heqv0. inv HADD. inv Heqv.
             exploit Mem.load_store_other.
             exploit Mem.load_store_same; eauto; intros. inv HADD. cbn in Heqv. inv Heqv.
             inv Heqv0.
      + auto.
    - admit.
    - admit.
  Admitted.

  Lemma transl_step_state_correct_single_instr :
    forall s f sp pc curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa n i a,
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n (mk_ctrl f) (curr_p, stmnt) i = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr curr_p = true ->
      step_instr ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) i
             (Iexec {| is_rs := rs'; is_ps := pr'; is_mem := m' |}) ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      Forall (fun x : positive => Ple x (max_pred_function f)) (pred_uses i) ->
      Ple (max_predicate curr_p) (max_pred_function f) ->
      Ple (max_reg_instr a i) (max_reg_function f) ->
      exists asr' asa',
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')
        /\ match_states (GibleSubPar.State s f sp pc rs' pr' m') (DHTL.State s' m_ pc asr' asa')
        /\ eval_predf pr' next_p = true.
  Proof.
    intros * HTRANSF HSTMNT HEVAL HSTEP HMATCH HFRL1 HPLE HREGMAX.
    unfold transf_instr, Errors.bind in HTRANSF. 
    destruct_match; repeat destr; subst; inv HTRANSF.
    - (* RBnop *) inv HSTEP. exists asr, asa; auto. inv H3.
    - (* RBop  *) inversion HSTEP; subst. 
      + exploit transl_iop_correct; eauto. cbn. rewrite Heqr0. cbn. eauto.
        cbn in *. eapply Forall_forall; intros.
        assert (Ple x (fold_left Pos.max l (Pos.max r a))) by (eapply fold_left_max; tauto); extlia.
        assert (Ple r (fold_left Pos.max l (Pos.max r a))) by (eapply fold_left_max; extlia).
        intros (asr'' & HSTMNT' & HMATCH'). cbn in *. extlia.
        exists asr'', asa. split; eauto.
      + inv H3; cbn in *. 
        assert (eval_predf pr' (Pand next_p p) = false).
        { rewrite eval_predf_Pand. rewrite H0; auto with bool. }
        assert (Ple (max_predicate (Pand next_p p)) (max_pred_function f)).
        { cbn. eapply le_max_pred in HFRL1. extlia. }
        inv HMATCH. exploit transl_predicate_correct2; eauto.
        intros (asr'' & HSTMNT' & HASR1 & HASR2).
        exists asr'', asa. split; [|split]; auto. econstructor; eauto.
        eapply unchanged_implies_match.
        split; [|split]; eauto. econstructor; eauto.
    - (* RBload *) inversion HSTEP; subst.
      + exploit transl_load_correct; eauto.
      + inv H3; cbn in *. 
        assert (eval_predf pr' (Pand next_p p) = false).
        { rewrite eval_predf_Pand. rewrite H0; auto with bool. }
        assert (Ple (max_predicate (Pand next_p p)) (max_pred_function f)).
        { cbn. eapply le_max_pred in HFRL1. extlia. }
        inv HMATCH. exploit transl_predicate_correct2; eauto.
        intros (asr'' & HSTMNT' & HASR1 & HASR2).
        exists asr'', asa. split; [|split]; auto. econstructor; eauto.
        eapply unchanged_implies_match.
        split; [|split]; eauto. econstructor; eauto.
    - (* RBstore *)
    - (* RBsetpred *) admit.
    - (* RBexit *) inv HSTEP. 
        inv H3; cbn -[eval_predf] in *.
        assert (eval_predf pr' (Pand curr_p p) = false).
        { rewrite eval_predf_Pand. rewrite H0; auto with bool. }
        assert (Ple (max_predicate (Pand curr_p p)) (max_pred_function f)).
        { cbn. eapply le_max_pred in HFRL1. extlia. }
        unfold translate_cfi, Errors.bind in *.
        inv HMATCH. repeat (destin Heqr0; try discriminate); subst.
        + unfold state_cond in *. inv Heqr0.
          exploit transl_predicate_correct2; eauto.
          intros (asr' & HSTMNT' & HEQUIV & HFORALL).
          exists asr', asa. split; [|split].
          econstructor; eauto.
          eapply unchanged_implies_match; eauto.
          unfold unchanged. split; [|split]; eauto. econstructor; eauto.
          rewrite eval_predf_Pand. rewrite HEVAL. rewrite eval_predf_negate. now rewrite H0.
        + unfold state_cond, state_goto in *. inv Heqr0.
          exploit transl_predicate_correct2; eauto.
          intros (asr' & HSTMNT' & HEQUIV & HFORALL).
          assert (X': unchanged asr asa asr' asa).
          { unfold unchanged; split; [|split]; eauto. }
          pose proof X' as Y1.
          eapply unchanged_implies_match in X'; eauto. 2: { econstructor; eauto. }
          inversion X'; subst.
          exploit transl_predicate_correct2; eauto.
          intros (asr'' & HSTMNT'' & HEQUIV' & HFORALL').
          assert (X'': unchanged asr' asa asr'' asa).
          { unfold unchanged; split; [|split]; eauto. }
          pose proof X'' as Y2.
          eapply unchanged_implies_match in X''; eauto.
          inversion X''; subst.
          exploit transl_predicate_correct2; eauto.
          intros (asr''' & HSTMNT''' & HEQUIV'' & HFORALL'').
          assert (X''': unchanged asr'' asa asr''' asa).
          { unfold unchanged; split; [|split]; eauto. }
          pose proof X''' as Y3.
          eapply unchanged_match_assocmaps in X'''; eauto.
          exists asr''', asa. split; [|split].
          econstructor; eauto.
          econstructor. econstructor. eauto. eauto. eauto. 
          eapply unchanged_implies_match; eauto.
          rewrite eval_predf_Pand. rewrite eval_predf_negate.
          rewrite HEVAL. now rewrite H0.
        + unfold state_cond, state_goto in *. inv Heqr0.
          exploit transl_predicate_correct2; eauto.
          intros (asr' & HSTMNT' & HEQUIV & HFORALL).
          assert (X': unchanged asr asa asr' asa).
          { unfold unchanged; split; [|split]; eauto. }
          pose proof X' as Y1.
          eapply unchanged_implies_match in X'; eauto. 2: { econstructor; eauto. }
          inversion X'; subst.
          exploit transl_predicate_correct2; eauto.
          intros (asr'' & HSTMNT'' & HEQUIV' & HFORALL').
          assert (X'': unchanged asr' asa asr'' asa).
          { unfold unchanged; split; [|split]; eauto. }
          pose proof X'' as Y2.
          eapply unchanged_implies_match in X''; eauto.
          inversion X''; subst.
          exploit transl_predicate_correct2; eauto.
          intros (asr''' & HSTMNT''' & HEQUIV'' & HFORALL'').
          assert (X''': unchanged asr'' asa asr''' asa).
          { unfold unchanged; split; [|split]; eauto. }
          pose proof X''' as Y3.
          eapply unchanged_match_assocmaps in X'''; eauto.
          exists asr''', asa. split; [|split].
          econstructor; eauto.
          econstructor. econstructor. eauto. eauto. eauto. 
          eapply unchanged_implies_match; eauto.
          rewrite eval_predf_Pand. rewrite eval_predf_negate.
          rewrite HEVAL. now rewrite H0.
        + unfold state_cond, state_goto in *. inv Heqr0.
          exploit transl_predicate_correct2; eauto.
          intros (asr' & HSTMNT' & HEQUIV & HFORALL).
          exists asr', asa. split; [|split].
          econstructor; eauto.
          eapply unchanged_implies_match; eauto.
          unfold unchanged. split; [|split]; eauto. econstructor; eauto.
          rewrite eval_predf_Pand. rewrite HEVAL. rewrite eval_predf_negate. now rewrite H0.
  Admitted.
      
  Transparent translate_predicate.
  Transparent translate_predicate_cond.

  Lemma transl_step_state_correct_single_instr_term :
    forall s f sp pc curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa n i cf pc',
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n (mk_ctrl f) (curr_p, stmnt) i = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr curr_p = true ->
      step_instr ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) i
             (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) ->
      step_cf_instr ge (GibleSubPar.State s f sp pc rs' pr' m') cf Events.E0 (GibleSubPar.State s f sp pc' rs' pr' m') ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      exists asr' asa',
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')
        /\ match_states (GibleSubPar.State s f sp pc' rs' pr' m') (DHTL.State s' m_ pc' asr' asa')
        /\ eval_predf pr' next_p = false.
  Proof. Admitted.

  Lemma transl_step_state_correct_single_instr_term_return :
    forall s f sp pc curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa n i cf v m'',
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n (mk_ctrl f) (curr_p, stmnt) i = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr curr_p = true ->
      step_instr ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) i
             (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) ->
      step_cf_instr ge (GibleSubPar.State s f sp pc rs' pr' m') cf Events.E0 (GibleSubPar.Returnstate s v m'') ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      exists retval,
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc (AssocMap.set m_.(DHTL.mod_st) (posToValue n) 
            (AssocMap.set (m_.(DHTL.mod_return)) retval (AssocMap.set (m_.(DHTL.mod_finish)) (ZToValue 1) asr)))) 
          (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa)
        /\ val_value_lessdef v retval
        /\ eval_predf pr' next_p = false.
  Proof. Admitted.

  Lemma transl_step_state_correct_single_false_standard :
    forall ctrl bb curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n max_reg max_pred rs ps,
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n ctrl (curr_p, stmnt) bb = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      Forall (fun x => Ple x max_pred) (pred_uses bb) ->
      Ple (max_predicate curr_p) max_pred ->
      eval_predf ps curr_p = false ->
      match_assocmaps max_reg max_pred rs ps asr ->
      (forall a b c d e, bb <> RBstore a b c d e) ->
      (forall a b, bb <> RBexit a b) ->
      exists asr', stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
        (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) 
        /\ unchanged asr asa asr' asa
        /\ Ple (max_predicate next_p) max_pred 
        /\ eval_predf ps next_p = false.
  Proof.
    destruct bb; intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH HNO_RBSTORE HNO_EXIT.
    - cbn in HTRANSF. inv HTRANSF. exists asr; repeat split; eauto.
    - cbn -[translate_predicate deep_simplify] in HTRANSF; unfold Errors.bind in HTRANSF.
      destruct_match; try discriminate.
      assert (forall A a b, @OK A a = OK b -> a = b) by now inversion 1. apply H in HTRANSF.
      assert (forall A B (a b: A) (c d: B), (a, c) = (b, d) -> a = b /\ c = d) by now inversion 1. 
      apply H0 in HTRANSF. destruct HTRANSF. rewrite H1 in *. rewrite <- H2 in *.
      assert (eval_predf ps (Pand next_p (dfltp o)) = false).
      { rewrite eval_predf_Pand. subst. rewrite HEVAL. auto. }
      assert (Ple (max_predicate (Pand next_p (dfltp o))) max_pred).
      { cbn. cbn in HFRL. destruct o; cbn; [|unfold Ple in *; lia].
        eapply le_max_pred in HFRL. unfold Ple in *; lia. }
      exploit transl_predicate_correct2; eauto. intros (asr' & HSTMNT' & FRL).
      exists asr'. repeat split; eauto.
      econstructor; eauto. simplify.
      crush. crush.
    - cbn -[translate_predicate deep_simplify] in HTRANSF; unfold Errors.bind in HTRANSF.
      destruct_match; try discriminate.
      assert (forall A a b, @OK A a = OK b -> a = b) by now inversion 1. apply H in HTRANSF.
      assert (forall A B (a b: A) (c d: B), (a, c) = (b, d) -> a = b /\ c = d) by now inversion 1. 
      apply H0 in HTRANSF. destruct HTRANSF. rewrite H1 in *. rewrite <- H2 in *.
      assert (eval_predf ps (Pand next_p (dfltp o)) = false).
      { rewrite eval_predf_Pand. subst. rewrite HEVAL. auto. }
      assert (Ple (max_predicate (Pand next_p (dfltp o))) max_pred).
      { cbn. cbn in HFRL. destruct o; cbn; [|unfold Ple in *; lia].
        eapply le_max_pred in HFRL. unfold Ple in *; lia. }
      exploit transl_predicate_correct2; eauto. intros (asr' & HSTMNT' & FRL).
      exists asr'. repeat split; eauto.
      econstructor; eauto. crush. crush.
    - exfalso; eapply HNO_RBSTORE; auto.
    - cbn -[translate_predicate deep_simplify] in HTRANSF; unfold Errors.bind in HTRANSF.
      destruct_match; try discriminate.
      assert (forall A a b, @OK A a = OK b -> a = b) by now inversion 1. apply H in HTRANSF.
      assert (forall A B (a b: A) (c d: B), (a, c) = (b, d) -> a = b /\ c = d) by now inversion 1. 
      apply H0 in HTRANSF. destruct HTRANSF. rewrite H1 in *. rewrite <- H2 in *.
      assert (eval_predf ps (Pand next_p (dfltp o)) = false).
      { rewrite eval_predf_Pand. subst. rewrite HEVAL. auto. }
      assert (Ple (max_predicate (Pand next_p (dfltp o))) max_pred).
      { cbn. cbn in HFRL. destruct o; cbn; [|unfold Ple in *; lia]. inv HFRL.
        eapply le_max_pred in H7. unfold Ple in *; lia. }
      exploit transl_predicate_correct2; eauto. intros (asr' & HSTMNT' & FRL).
      exists asr'. repeat split; eauto.
      econstructor; eauto. crush. crush.
    - exfalso; eapply HNO_EXIT; auto.
  Qed.

  Lemma transl_arr_access_exists_vari :
    forall chunk addr args stack e,
      translate_arr_access chunk addr args stack = OK e ->
      exists e', e = Vvari stack e'.
  Proof.
    destruct chunk, addr; intros; cbn in *; repeat destr; try discriminate; inv H; eauto.
  Qed.

  Opaque translate_predicate.
  (* Lemma transl_step_state_correct_single_false_standard_top_store : *)
  (*   forall f s s' pc curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n rs ps sp m p chunk addr args src, *)
  (*     (* (fn_code f) ! pc = Some bb -> *) *)
  (*     transf_instr n (mk_ctrl f) (curr_p, stmnt) (RBstore p chunk addr args src) = OK (next_p, stmnt') -> *)
  (*     stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0)  *)
  (*       stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) -> *)
  (*     Forall (fun x => Ple x (max_pred_function f)) (pred_uses (RBstore p chunk addr args src)) -> *)
  (*     Ple (max_predicate curr_p) (max_pred_function f) -> *)
  (*     eval_predf ps curr_p = false -> *)
  (*     match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr asa) -> *)
  (*     exists asr' asa',  *)
  (*       stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt'  *)
  (*         (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')  *)
  (*       /\ match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr' asa') *)
  (*       /\ Ple (max_predicate next_p) (max_pred_function f) *)
  (*       /\ eval_predf ps next_p = false. *)
  (* Proof. *)
  (*   intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH.  *)
  (*   cbn -[translate_predicate_cond] in *; unfold Errors.bind in *. *)
  (*   destruct (translate_arr_access chunk addr args (Pos.succ (Pos.succ (Pos.succ (Pos.succ (max_resource_function f)))))) eqn:HT; try discriminate. *)
  (*   inv HTRANSF. exploit transl_arr_access_exists_vari; eauto. intros (e' & HVARI). *)
  (*   subst. inv HMATCH. exploit mk_ctrl_correct; eauto. intros (HCTRLST' & HCTRLSTACK' & HCTRLFIN' & HCTRLRETURN'). *)
  (*   cbn -[translate_predicate_cond] in *. rewrite HCTRLSTACK' in HT. *)
  (*   assert (X: Ple (max_predicate (Pand next_p (dfltp p))) (max_pred_function f)). *)
  (*   { unfold Ple; cbn. destruct p; cbn. apply le_max_pred in HFRL. unfold Ple in *. lia. unfold Ple in *. lia. } *)
  (*   exploit transl_predicate_cond_correct_arr2; eauto. *)
  (*   rewrite eval_predf_Pand. now rewrite HEVAL. *)
  (*   intros HSTMNT'. exists asr, asa.  *)
  (*   split; [|split; [|split]]. *)
  (*   econstructor; eauto. cbn in *. *)
  (*   econstructor; eauto. auto. auto. *)
  (* Qed. *)

  Lemma transl_step_state_correct_single_false_standard_top_store :
    forall ctrl curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n p chunk addr args src max_reg max_pred rs ps,
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n ctrl (curr_p, stmnt) (RBstore p chunk addr args src) = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) 
        stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      Forall (fun x => Ple x max_pred) (pred_uses (RBstore p chunk addr args src)) ->
      Ple (max_predicate curr_p) max_pred ->
      eval_predf ps curr_p = false ->
      match_assocmaps max_reg max_pred rs ps asr ->
      exists asr', 
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) 
        /\ unchanged asr asa asr' asa
        /\ Ple (max_predicate next_p) max_pred
        /\ eval_predf ps next_p = false.
  Proof.
    intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH.
    cbn -[translate_predicate_cond] in *; unfold Errors.bind in *.
    destruct (translate_arr_access chunk addr args (ctrl_stack ctrl)) eqn:HT; try discriminate.
    inv HTRANSF. exploit transl_arr_access_exists_vari; eauto. intros (e' & HVARI).
    subst.
    cbn -[translate_predicate_cond] in *.
    assert (X: Ple (max_predicate (Pand next_p (dfltp p))) max_pred).
    { unfold Ple; cbn. destruct p; cbn. apply le_max_pred in HFRL. unfold Ple in *. lia. unfold Ple in *. lia. }
    exploit transl_predicate_cond_correct_arr2; eauto.
    rewrite eval_predf_Pand. now rewrite HEVAL.
    intros HSTMNT'. exists asr. 
    split; [|split; [|split]].
    econstructor; eauto. cbn in *.
    econstructor; eauto. auto. auto.
  Qed.

  Lemma transl_step_state_correct_single_false_standard_top_exit :
    forall ctrl curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n rs ps p cfi max_pred max_reg,
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n ctrl (curr_p, stmnt) (RBexit p cfi) = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) 
        stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      Forall (fun x => Ple x max_pred) (pred_uses (RBexit p cfi)) ->
      Ple (max_predicate curr_p) max_pred ->
      eval_predf ps curr_p = false ->
      match_assocmaps max_reg max_pred rs ps asr ->
      exists asr',
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) 
        /\ unchanged asr asa asr' asa
        /\ Ple (max_predicate next_p) max_pred
        /\ eval_predf ps next_p = false.
  Proof.
    intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH. 
    cbn -[translate_predicate_cond translate_predicate] in *; unfold Errors.bind in *. 
    repeat (destin HTRANSF; try discriminate; []). inv HTRANSF.
    assert (X: Ple (max_predicate (Pand curr_p (dfltp p))) max_pred).
    { unfold Ple; cbn. destruct p; cbn. apply le_max_pred in HFRL. unfold Ple in *. lia. unfold Ple in *. lia. }
    unfold translate_cfi, Errors.bind in *.
    repeat (destin DIN0; try discriminate).
    - unfold state_cond in *. inv DIN0.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr' & HSTMNT' & HEQUIV & HFORALL).
      exists asr'. split; [|split; [|split]].
      econstructor; eauto.
      unfold unchanged. split; [|split]; eauto.
      unfold Ple in *; cbn. rewrite max_predicate_negate.
      destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia.
      rewrite eval_predf_Pand. now rewrite HEVAL.
    - unfold state_cond, state_goto in *. inv DIN0.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr' & HSTMNT' & HEQUIV & HFORALL).
      assert (X': unchanged asr asa asr' asa).
      { unfold unchanged; split; [|split]; eauto. }
      pose proof X' as Y1.
      eapply unchanged_match_assocmaps in X'; eauto.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr'' & HSTMNT'' & HEQUIV' & HFORALL').
      assert (X'': unchanged asr' asa asr'' asa).
      { unfold unchanged; split; [|split]; eauto. }
      pose proof X'' as Y2.
      eapply unchanged_match_assocmaps in X''; eauto.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr''' & HSTMNT''' & HEQUIV'' & HFORALL'').
      assert (X''': unchanged asr'' asa asr''' asa).
      { unfold unchanged; split; [|split]; eauto. }
      pose proof X''' as Y3.
      eapply unchanged_match_assocmaps in X'''; eauto.
      exists asr'''. split; [|split; [|split]].
      econstructor; eauto.
      econstructor. econstructor. eauto.
      eauto. eauto. eapply unchanged_trans; eauto. eapply unchanged_trans; eauto.
      unfold Ple in *; cbn. rewrite max_predicate_negate.
      destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia.
      rewrite eval_predf_Pand. now rewrite HEVAL.
    - unfold state_cond, state_goto in *. inv DIN0.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr' & HSTMNT' & HEQUIV & HFORALL).
      assert (X': unchanged asr asa asr' asa).
      { unfold unchanged; split; [|split]; eauto. }
      pose proof X' as Y1.
      eapply unchanged_match_assocmaps in X'; eauto.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr'' & HSTMNT'' & HEQUIV' & HFORALL').
      assert (X'': unchanged asr' asa asr'' asa).
      { unfold unchanged; split; [|split]; eauto. }
      pose proof X'' as Y2.
      eapply unchanged_match_assocmaps in X''; eauto.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr''' & HSTMNT''' & HEQUIV'' & HFORALL'').
      assert (X''': unchanged asr'' asa asr''' asa).
      { unfold unchanged; split; [|split]; eauto. }
      pose proof X''' as Y3.
      eapply unchanged_match_assocmaps in X'''; eauto.
      exists asr'''. split; [|split; [|split]].
      econstructor; eauto.
      econstructor. econstructor. eauto.
      eauto. eauto. eapply unchanged_trans; eauto. eapply unchanged_trans; eauto.
      unfold Ple in *; cbn. rewrite max_predicate_negate.
      destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia.
      rewrite eval_predf_Pand. now rewrite HEVAL.
    - unfold state_cond, state_goto in *. inv DIN0.
      exploit transl_predicate_correct2; eauto.
      rewrite eval_predf_Pand. rewrite HEVAL. eauto.
      intros (asr' & HSTMNT' & HEQUIV & HFORALL).
      exists asr'. split; [|split; [|split]].
      econstructor; eauto.
      econstructor; eauto.
      unfold Ple in *; cbn. rewrite max_predicate_negate.
      destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia.
      rewrite eval_predf_Pand. now rewrite HEVAL.
  Qed.

  (* Lemma transl_step_state_correct_single_false_standard_top_exit' : *)
  (*   forall f s s' pc curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n rs ps sp m p cfi, *)
  (*     (* (fn_code f) ! pc = Some bb -> *) *)
  (*     transf_instr n (mk_ctrl f) (curr_p, stmnt) (RBexit p cfi) = OK (next_p, stmnt') -> *)
  (*     stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0)  *)
  (*       stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) -> *)
  (*     Forall (fun x => Ple x (max_pred_function f)) (pred_uses (RBexit p cfi)) -> *)
  (*     Ple (max_predicate curr_p) (max_pred_function f) -> *)
  (*     eval_predf ps curr_p = false -> *)
  (*     match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr asa) -> *)
  (*     exists asr' asa',  *)
  (*       stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt'  *)
  (*         (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')  *)
  (*       /\ match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr' asa') *)
  (*       /\ Ple (max_predicate next_p) (max_pred_function f) *)
  (*       /\ eval_predf ps next_p = false. *)
  (* Proof.  *)
  (*   intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH.  *)
  (*   cbn -[translate_predicate_cond translate_predicate] in *; unfold Errors.bind in *.  *)
  (*   repeat (destin HTRANSF; try discriminate; []). inv HTRANSF. *)
  (*   assert (X: Ple (max_predicate (Pand curr_p (dfltp p))) (max_pred_function f)). *)
  (*   { unfold Ple; cbn. destruct p; cbn. apply le_max_pred in HFRL. unfold Ple in *. lia. unfold Ple in *. lia. } *)
  (*   unfold translate_cfi, Errors.bind in *. *)
  (*   repeat (destin DIN0; try discriminate). *)
  (*   - unfold state_cond in *. inv DIN0. *)
  (*     inv HMATCH. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr' & HSTMNT' & HEQUIV & HFORALL). *)
  (*     exists asr', asa. split; [|split; [|split]]. *)
  (*     econstructor; eauto. *)
  (*     eapply unchanged_implies_match; eauto. instantiate (2:=asr). instantiate (1:=asa). *)
  (*     unfold unchanged. split; [|split]; eauto. *)
  (*     econstructor; eauto. *)
  (*     unfold Ple in *; cbn. rewrite max_predicate_negate. *)
  (*     destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia. *)
  (*     rewrite eval_predf_Pand. now rewrite HEVAL. *)
  (*   - unfold state_cond, state_goto in *. inv DIN0. *)
  (*     inv HMATCH. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr' & HSTMNT' & HEQUIV & HFORALL). *)
  (*     assert (X': unchanged asr asa asr' asa). *)
  (*     { unfold unchanged; split; [|split]; eauto. } *)
  (*     pose proof unchanged_implies_match as Y. eapply Y in X'; [|econstructor; eauto]. *)
  (*     inv X'. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr'' & HSTMNT'' & HEQUIV' & HFORALL'). *)
  (*     assert (X': unchanged asr' asa asr'' asa). *)
  (*     { unfold unchanged; split; [|split]; eauto. } *)
  (*     pose proof unchanged_implies_match as Y'. eapply Y' in X'; [|econstructor; eauto]. *)
  (*     inv X'. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr''' & HSTMNT''' & HEQUIV'' & HFORALL''). *)
  (*     assert (X': unchanged asr'' asa asr''' asa). *)
  (*     { unfold unchanged; split; [|split]; eauto. } *)
  (*     pose proof unchanged_implies_match as Y''. eapply Y'' in X'; [|econstructor; eauto]. *)
  (*     inv X'. *)
  (*     exists asr''', asa. split; [|split; [|split]]. *)
  (*     econstructor; eauto. *)
  (*     econstructor. econstructor. eauto. *)
  (*     eauto. eauto. econstructor; eauto. *)
  (*     unfold Ple in *; cbn. rewrite max_predicate_negate. *)
  (*     destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia. *)
  (*     rewrite eval_predf_Pand. now rewrite HEVAL. *)
  (*   - unfold state_cond, state_goto in *. inv DIN0. *)
  (*     inv HMATCH. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr' & HSTMNT' & HEQUIV & HFORALL). *)
  (*     assert (X': unchanged asr asa asr' asa). *)
  (*     { unfold unchanged; split; [|split]; eauto. } *)
  (*     pose proof unchanged_implies_match as Y. eapply Y in X'; [|econstructor; eauto]. *)
  (*     inv X'. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr'' & HSTMNT'' & HEQUIV' & HFORALL'). *)
  (*     assert (X': unchanged asr' asa asr'' asa). *)
  (*     { unfold unchanged; split; [|split]; eauto. } *)
  (*     pose proof unchanged_implies_match as Y'. eapply Y' in X'; [|econstructor; eauto]. *)
  (*     inv X'. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr''' & HSTMNT''' & HEQUIV'' & HFORALL''). *)
  (*     assert (X': unchanged asr'' asa asr''' asa). *)
  (*     { unfold unchanged; split; [|split]; eauto. } *)
  (*     pose proof unchanged_implies_match as Y''. eapply Y'' in X'; [|econstructor; eauto]. *)
  (*     inv X'. *)
  (*     exists asr''', asa. split; [|split; [|split]]. *)
  (*     econstructor; eauto. *)
  (*     econstructor. econstructor. eauto. *)
  (*     eauto. eauto. econstructor; eauto. *)
  (*     unfold Ple in *; cbn. rewrite max_predicate_negate. *)
  (*     destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia. *)
  (*     rewrite eval_predf_Pand. now rewrite HEVAL. *)
  (*   - unfold state_cond, state_goto in *. inv DIN0. *)
  (*     inv HMATCH. exploit transl_predicate_correct2; eauto. *)
  (*     rewrite eval_predf_Pand. rewrite HEVAL. eauto. *)
  (*     intros (asr' & HSTMNT' & HEQUIV & HFORALL). *)
  (*     exists asr', asa. split; [|split; [|split]]. *)
  (*     econstructor; eauto. *)
  (*     eapply unchanged_implies_match; eauto. instantiate (2:=asr). instantiate (1:=asa). *)
  (*     unfold unchanged. split; [|split]; eauto. *)
  (*     econstructor; eauto. *)
  (*     unfold Ple in *; cbn. rewrite max_predicate_negate. *)
  (*     destruct p; cbn; try lia. apply le_max_pred in HFRL. unfold Ple in *; lia. *)
  (*     rewrite eval_predf_Pand. now rewrite HEVAL. *)
  (* Qed. *)

  Transparent translate_predicate.

  Lemma transl_step_state_correct_single_false_standard_top :
    forall ctrl bb curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n rs ps max_reg max_pred,
      (* (fn_code f) ! pc = Some bb -> *)
      transf_instr n ctrl (curr_p, stmnt) bb = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) 
        stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      Forall (fun x => Ple x max_pred) (pred_uses bb) ->
      Ple (max_predicate curr_p) max_pred ->
      eval_predf ps curr_p = false ->
      match_assocmaps max_reg max_pred rs ps asr ->
      exists asr', 
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) 
        /\ unchanged asr asa asr' asa
        /\ Ple (max_predicate next_p) max_pred
        /\ eval_predf ps next_p = false.
  Proof.
    intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH. destruct bb.
    - eapply transl_step_state_correct_single_false_standard; eauto; try discriminate.
    - eapply transl_step_state_correct_single_false_standard; eauto; try discriminate.
    - eapply transl_step_state_correct_single_false_standard; eauto; try discriminate.
    - eapply transl_step_state_correct_single_false_standard_top_store; eauto.
    - eapply transl_step_state_correct_single_false_standard; eauto; try discriminate.
    - eapply transl_step_state_correct_single_false_standard_top_exit; eauto.
  Qed.

  (* Lemma transl_step_state_correct_single_false_standard_top : *)
  (*   forall f s s' pc bb curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n rs ps sp m, *)
  (*     (* (fn_code f) ! pc = Some bb -> *) *)
  (*     transf_instr n (mk_ctrl f) (curr_p, stmnt) bb = OK (next_p, stmnt') -> *)
  (*     stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0)  *)
  (*       stmnt (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) -> *)
  (*     Forall (fun x => Ple x (max_pred_function f)) (pred_uses bb) -> *)
  (*     Ple (max_predicate curr_p) (max_pred_function f) -> *)
  (*     eval_predf ps curr_p = false -> *)
  (*     match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr asa) -> *)
  (*     exists asr' asa',  *)
  (*       stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt'  *)
  (*         (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')  *)
  (*       /\ match_states (GibleSubPar.State s f sp pc rs ps m) (DHTL.State s' m_ pc asr' asa') *)
  (*       /\ Ple (max_predicate next_p) (max_pred_function f) *)
  (*       /\ eval_predf ps next_p = false. *)
  (* Proof. *)
  (*   intros * HTRANSF HSTMNT HFRL HPLE HEVAL HMATCH. destruct bb. *)
  (*   - inv HMATCH. *)
  (*     exploit transl_step_state_correct_single_false_standard; eauto; try discriminate. *)
  (*     intros (asr' & asa' & HSTMNT' & HUNCHG & HPLE' & HEVAL'). *)
  (*     exists asr', asa'. repeat split; auto. eapply unchanged_implies_match; eauto. *)
  (*     econstructor; eauto. *)
  (*   - inv HMATCH. *)
  (*     exploit transl_step_state_correct_single_false_standard; eauto; try discriminate. *)
  (*     intros (asr' & asa' & HSTMNT' & HUNCHG & HPLE' & HEVAL'). *)
  (*     exists asr', asa'. repeat split; auto. eapply unchanged_implies_match; eauto. *)
  (*     econstructor; eauto. *)
  (*   - inv HMATCH. *)
  (*     exploit transl_step_state_correct_single_false_standard; eauto; try discriminate. *)
  (*     intros (asr' & asa' & HSTMNT' & HUNCHG & HPLE' & HEVAL'). *)
  (*     exists asr', asa'. repeat split; auto. eapply unchanged_implies_match; eauto. *)
  (*     econstructor; eauto. *)
  (*   - eapply transl_step_state_correct_single_false_standard_top_store; eauto. *)
  (*   - inv HMATCH. *)
  (*     exploit transl_step_state_correct_single_false_standard; eauto; try discriminate. *)
  (*     intros (asr' & asa' & HSTMNT' & HUNCHG & HPLE' & HEVAL'). *)
  (*     exists asr', asa'. repeat split; auto. eapply unchanged_implies_match; eauto. *)
  (*     econstructor; eauto. *)
  (*   - eapply transl_step_state_correct_single_false_standard_top_exit; eauto. *)
  (* Qed. *)

  Lemma iterm_intermediate_state :
    forall bb sp rs pr m rs' pr' m' cf,
      SubParBB.step_instr_list ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb
               (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) ->
      exists bb' i bb'', 
        SubParBB.step_instr_list ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb'
               (Iexec {| is_rs := rs'; is_ps := pr'; is_mem := m' |})
        /\ step_instr ge sp (Iexec {| is_rs := rs'; is_ps := pr'; is_mem := m' |}) i 
             (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf)
        /\ bb = bb' ++ (i :: bb'').
  Proof. 
    induction bb; intros * HSUBPAR. 
    - inv HSUBPAR. 
    - inv HSUBPAR. destruct i1; destruct i.
      + exploit IHbb; eauto. intros (bb' & i & bb'' & HSTEPLIST & HSTEP & HLIST). 
        subst. exists (a :: bb'), i, bb''. split; [|split]; auto.
        econstructor; eauto.
      + exists nil, a, bb. exploit step_instr_finish; eauto; inversion 1; subst; clear H.
        exploit step_list_inter_not_term; eauto; inversion 1. inv H0.
        split; [|split]; auto. constructor.
  Qed.

  Lemma transl_step_state_correct_instr :
    forall s f sp bb pc curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa n,
      (* (fn_code f) ! pc = Some bb -> *)
      mfold_left (transf_instr n (mk_ctrl f)) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr curr_p = true ->
      SubParBB.step_instr_list ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb
             (Iexec {| is_rs := rs'; is_ps := pr'; is_mem := m' |}) ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      exists asr' asa',
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')
        /\ match_states (GibleSubPar.State s f sp pc rs' pr' m') (DHTL.State s' m_ pc asr' asa')
        /\ eval_predf pr' next_p = true.
  Proof. 
    induction bb; intros * HFOLD HSTMNT HEVAL HSUBPAR HMATCH.
    - inv HSUBPAR. exists asr, asa. cbn in *. inv HFOLD. auto.
    - exploit mfold_left_cons; eauto.
      intros (x' & y' & HFOLD' & HTRANS & HINV). inv HINV. destruct y'. clear HFOLD.
      inv HSUBPAR. destruct i1; [|inv H5]. destruct i.
      exploit transl_step_state_correct_single_instr; eauto.
      intros (asr' & asa' & HSTMNT' & HMATCH' & HEVAL').
      eauto.
  Qed.

  Lemma transl_step_state_correct_instr_false :
    forall ctrl bb curr_p next_p m_ stmnt stmnt' asr0 asa0 asr asa n rs ps max_reg max_pred,
      (* (fn_code f) ! pc = Some bb -> *)
      mfold_left (transf_instr n ctrl) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      Forall (fun i => Forall (fun x : positive => Ple x max_pred) (pred_uses i)) bb ->
      Ple (max_predicate curr_p) max_pred ->
      eval_predf ps curr_p = false ->
      match_assocmaps max_reg max_pred rs ps asr ->
      exists asr', 
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa)
        /\ unchanged asr asa asr' asa
        /\ eval_predf ps next_p = false
        /\ Ple (max_predicate next_p) max_pred.
  Proof.
    induction bb; intros * HFOLD HSTMNT HFRL HPLE HEVAL HMATCH.
    - cbn in *. inv HFOLD. exists asr; repeat split; auto. eauto.
    - exploit mfold_left_cons; eauto.
      intros (x' & y' & HFOLD' & HTRANS & HINV). inv HINV. destruct y'.
      exploit transl_step_state_correct_single_false_standard_top; eauto. inv HFRL; eauto.
      intros (asr' & HSTMNT' & HMATCH' & HPLE' & HEVAL').
      inv HFRL. pose proof HMATCH' as HMATCHB.
      eapply unchanged_match_assocmaps in HMATCH'; eauto. exploit IHbb; eauto.
      intros (asr'' & HSTMNT'' & HMATCH'' & HPLE'' & HEVAL'').
      exists asr''; repeat (split; auto; []).
      eapply unchanged_trans; eauto.
  Qed.

  Lemma transl_step_state_correct_instr_state :
    forall s f sp bb pc curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa cf pc' n,
      (* (fn_code f) ! pc = Some bb -> *)
      mfold_left (transf_instr n (mk_ctrl f)) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr curr_p = true ->
      SubParBB.step_instr_list ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb
             (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) ->
      step_cf_instr ge (GibleSubPar.State s f sp pc rs' pr' m') cf Events.E0 (GibleSubPar.State s f sp pc' rs' pr' m') ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      Forall (fun i0 : instr => Forall (fun x : positive => Ple x (max_pred_function f)) (pred_uses i0)) bb ->
      Ple (max_predicate curr_p) (max_pred_function f) ->
      exists asr' asa',
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')
        /\ match_states (GibleSubPar.State s f sp pc' rs' pr' m') (DHTL.State s' m_ pc' asr' asa').
  Proof. 
    intros * HFOLD HSTMNT HEVAL HSTEP HSTEPCF HMATCH HFRL HPLE.
    exploit iterm_intermediate_state; eauto.
    intros (bb' & i & bb'' & HSTEP' & HSTEPINSTR & HBB). subst.
    exploit mfold_left_app; eauto. intros (y' & HFOLD1 & HFOLD2).
    exploit mfold_left_cons; eauto. intros (x' & y_other & HFOLDFINAL & HINSTR & HSUBST).
    inv HSUBST.
    destruct x'. destruct y_other.
    exploit transl_step_state_correct_instr; try eapply HFOLD1; eauto.
    intros (asr' & asa' & HSTMNT' & HMATCH' & HNEXT).
    exploit transl_step_state_correct_single_instr_term; eauto.
    intros (asr'0 & asa'0 & HSTMNT'' & HMATCH'' & HNEXT'').
    inv HMATCH''.
    exploit transl_step_state_correct_instr_false; eauto.
    { eapply Forall_app in HFRL. inv HFRL. inv H0. eauto. }
    { eapply all_le_max_predicate_instr; eauto. eapply Forall_app in HFRL. inv HFRL. inv H0. eauto. 
      eapply all_le_max_predicate; eauto. eapply Forall_app in HFRL. inv HFRL. inv H0. eauto. }
    intros (asr'' & HSTMNT''' & HUNCHANGED & HEVAL' & HPLE').
    exists asr'', asa'0. split; auto.
    eapply unchanged_implies_match; eauto. econstructor; eauto.
  Qed.

  Lemma transl_step_state_correct_instr_return :
    forall s f sp bb pc curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa cf v m'' n,
      mfold_left (transf_instr n (mk_ctrl f)) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') ->
      stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt 
        (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) ->
      eval_predf pr curr_p = true ->
      SubParBB.step_instr_list ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb
             (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) ->
      step_cf_instr ge (GibleSubPar.State s f sp pc rs' pr' m') cf Events.E0 (GibleSubPar.Returnstate s v m'') ->
      match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) ->
      Forall (fun i0 : instr => Forall (fun x : positive => Ple x (max_pred_function f)) (pred_uses i0)) bb ->
      Ple (max_predicate curr_p) (max_pred_function f) ->
      exists asr' asa' retval,
        stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt' 
          (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa')
        /\ val_value_lessdef v retval
        /\ asr'!(m_.(DHTL.mod_finish)) = Some (ZToValue 1)
        /\ asr'!(m_.(DHTL.mod_return)) = Some retval
        /\ asr'!(m_.(DHTL.mod_st)) = Some (posToValue n).
  Proof.
    intros * HFOLD HSTMNT HEVAL HSTEP HSTEPCF HMATCH HFRL HPLE.
    exploit iterm_intermediate_state; eauto.
    intros (bb' & i & bb'' & HSTEP' & HSTEPINSTR & HBB). subst.
    exploit mfold_left_app; eauto. intros (y' & HFOLD1 & HFOLD2).
    exploit mfold_left_cons; eauto. intros (x' & y_other & HFOLDFINAL & HINSTR & HSUBST).
    inv HSUBST.
    destruct x'. destruct y_other.
    exploit transl_step_state_correct_instr; try eapply HFOLD1; eauto.
    intros (asr' & asa' & HSTMNT' & HMATCH' & HNEXT).
    exploit transl_step_state_correct_single_instr_term_return; eauto.
    intros (v' & HSTMNT'' & HEVAL2 & HEVAL3).
    inv HMATCH'.
    exploit transl_step_state_correct_instr_false; eauto.
    { eapply Forall_app in HFRL. inv HFRL. inv H0. eauto. }
    { eapply all_le_max_predicate_instr; eauto. eapply Forall_app in HFRL. inv HFRL. inv H0. eauto. 
      eapply all_le_max_predicate; eauto. eapply Forall_app in HFRL. inv HFRL. inv H0. eauto. }
    { unfold transl_module, Errors.bind, ret in TF. repeat (destruct_match; try discriminate; []).
      inv TF. repeat eapply regs_lessdef_add_greater; eauto; cbn; unfold Plt; lia. }
    intros (asr'' & HSTMNT''' & HUNCHANGED & HEVAL' & HPLE').
    exists asr'', asa', v'. repeat (split; auto; []).
    inv HUNCHANGED. inv H0. unfold transl_module, Errors.bind, ret in TF. repeat (destruct_match; try discriminate; []).
    inv TF; cbn in *.
    split; [|split]; eapply H1; repeat rewrite AssocMap.gso by lia; now rewrite AssocMap.gss.
  Qed.

  (* Lemma transl_step_state_correct_chained_state : *)
  (*   forall s f sp bb pc pc' curr_p next_p rs rs' m m' pr pr' m_ s' stmnt stmnt' asr0 asa0 asr asa cf n, *)
  (*     (* (fn_code f) ! pc = Some bb -> *) *)
  (*     mfold_left (transf_chained_block n (mk_ctrl f)) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') -> *)
  (*     stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt  *)
  (*       (e_assoc asr) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa) -> *)
  (*     eval_predf pr curr_p = true -> *)
  (*     SubParBB.step ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb *)
  (*            (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) -> *)
  (*     step_cf_instr ge (GibleSubPar.State s f sp pc rs' pr' m') cf Events.E0 (GibleSubPar.State s f sp pc' rs' pr' m') -> *)
  (*     match_states (GibleSubPar.State s f sp pc rs pr m) (DHTL.State s' m_ pc asr asa) -> *)
  (*     exists asr' asa', *)
  (*       stmnt_runp tt (e_assoc asr0) (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa0) stmnt'  *)
  (*         (e_assoc asr') (e_assoc_arr (DHTL.mod_stk m_) (DHTL.mod_stk_len m_) asa') *)
  (*       /\ match_states (GibleSubPar.State s f sp pc' rs' pr' m') (DHTL.State s' m_ pc' asr' asa'). *)
  (* Proof. Abort. *)

  Lemma transl_step_through_cfi' :
    forall bb ctrl curr_p stmnt next_p stmnt' p cf n,
      mfold_left (transf_instr n ctrl) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') ->
      In (RBexit p cf) bb ->
      exists curr_p' stmnt'', 
        translate_cfi n ctrl (Some (Pand curr_p' (dfltp p))) cf = OK stmnt'' 
        /\ check_cfi n cf = OK tt.
  Proof.
    induction bb.
    - inversion 2.
    - intros * HFOLD HIN.
      exploit mfold_left_cons; eauto.
      intros (x' & y' & HFOLD' & HTRANSF & HSUBST).
      inversion HSUBST. inv H0. clear HSUBST.
      inv HIN; destruct y'; eauto.
      cbn in HTRANSF. unfold Errors.bind in HTRANSF. repeat (destruct_match; try discriminate; []).
      inv HTRANSF. destruct u. eauto.
  Qed.

  Lemma transl_step_through_cfi :
    forall bb ctrl curr_p stmnt next_p stmnt' l p cf n,
      mfold_left (transf_chained_block n ctrl) bb (OK (curr_p, stmnt)) = OK (next_p, stmnt') ->
      In l bb ->
      In (RBexit p cf) l ->
      exists curr_p' stmnt'', translate_cfi n ctrl (Some (Pand curr_p' (dfltp p))) cf = OK stmnt''.
  Proof.
    induction bb.
    - inversion 2.
    - intros * HFOLD HIN1 HIN2.
      exploit mfold_left_cons; eauto.
      intros (x' & y' & HFOLD' & HTRANSF & HSUBST).
      destruct y'. inv HSUBST.
      inv HIN1; eauto.
      exploit transl_step_through_cfi'; eauto.
      simplify. eauto.
  Qed.

  Lemma cf_in_bb_subparbb' :
    forall sp bb a b cf,
      SubParBB.step_instr_list ge sp (Iexec a) bb (Iterm b cf) ->
      exists curr_p, In (RBexit curr_p cf) bb /\ truthy (is_ps b) curr_p.
  Proof.
    induction bb.
    - intros. inv H.
    - intros. inv H.
      destruct i1.
      + exploit IHbb; eauto. intros (curr_p & HIN & HTRY).
        exists curr_p. split; auto. now apply in_cons.
      + inv H4. exists p. inv H6; split; auto; now constructor.
  Qed.

  Lemma cf_in_bb_subparbb :
    forall sp bb a b cf,
      SubParBB.step_instr_seq ge sp (Iexec a) bb (Iterm b cf) ->
      exists l curr_p, In l bb /\ In (RBexit curr_p cf) l /\ truthy (is_ps b) curr_p.
  Proof.
    induction bb.
    - intros. inv H.
    - intros. inv H.
      + exploit IHbb; eauto. intros (l & curr_p & HIN2 & HIN & HTRY).
        exists l, curr_p. split; auto. now apply in_cons.
      + exploit cf_in_bb_subparbb'; eauto. 
        intros (curr_p & HIN & HTR).
        exists a, curr_p. split; auto. now constructor.
  Qed.

  Lemma match_states_cf_events_translate_cfi:
    forall T1 cf T2 p t ctrl stmnt n,
      translate_cfi n ctrl p cf = OK stmnt ->
      step_cf_instr ge T1 cf t T2 ->
      t = Events.E0.
  Proof.
    intros * HGET HSTEP.
    destruct cf; try discriminate; inv HSTEP; eauto.
  Qed.

  Lemma match_states_cf_states_translate_cfi:
    forall T1 cf T2 p t ctrl stmnt n,
      translate_cfi n ctrl p cf = OK stmnt ->
      step_cf_instr ge T1 cf t T2 ->
      exists s f sp pc rs pr m,
        T1 = GibleSubPar.State s f sp pc rs pr m
        /\ ((exists pc', T2 = GibleSubPar.State s f sp pc' rs pr m)
            \/ (exists v m' stk, Mem.free m stk 0 (fn_stacksize f) = Some m' 
                /\ sp = Values.Vptr stk Ptrofs.zero 
                /\ T2 = GibleSubPar.Returnstate s v m')).
  Proof.
    intros * HGET HSTEP.
    destruct cf; try discriminate; inv HSTEP; try (now repeat econstructor).
  Qed.

  Lemma translate_cfi_goto:
    forall pr curr_p pc s ctrl asr asa n,
      (forall r, Ple r (max_predicate curr_p) ->
        find_assocmap 1 (pred_enc r) asr = boolToValue (PMap.get r pr)) ->
      eval_predf pr curr_p = true ->
      translate_cfi n ctrl (Some curr_p) (RBgoto pc) = OK s ->
      stmnt_runp tt (e_assoc asr) asa s
        (e_assoc (AssocMap.set ctrl.(ctrl_st) (posToValue pc) asr)) asa.
  Proof.
    intros * HPLE HEVAL HTRANSL. unfold translate_cfi in *.
    inversion_clear HTRANSL as []. destruct_match.
    - constructor. constructor. econstructor. eapply pred_expr_correct.
      intros. unfold Ple in *. eapply HPLE.
      now apply max_predicate_deep_simplify in H.
      eauto. constructor. rewrite eval_predf_deep_simplify. rewrite HEVAL. auto.
    - repeat constructor.
  Qed.

  Lemma translate_cfi_goto_none:
    forall pc s ctrl asr asa n,
      translate_cfi n ctrl None (RBgoto pc) = OK s ->
      stmnt_runp tt (e_assoc asr) asa s
        (e_assoc (AssocMap.set ctrl.(ctrl_st) (posToValue pc) asr)) asa.
  Proof. intros; inversion_clear H as []; repeat constructor. Qed.

  Lemma transl_module_ram_none :
    forall f m_,
      transl_module f = OK m_ ->
      m_.(mod_ram) = None.
  Proof.
    unfold transl_module, Errors.bind, Errors.bind2, ret; intros.
    repeat (destruct_match; try discriminate). inversion_clear H as []. auto.
  Qed.

  Lemma match_states_ok_transl :
    forall s f sp pc rs pr mem R1,
      match_states (GibleSubPar.State s f sp pc rs pr mem) R1 ->
      exists m asr asa s', transl_module f = OK m /\ R1 = DHTL.State s' m pc asr asa.
  Proof. inversion 1; subst. now repeat eexists. Qed.

  Lemma transl_spec_in_output :
    forall l ctrl d_in d_out pc stmnt,
      mfold_left (transf_seq_block ctrl) l (OK d_in) = OK d_out ->
      d_in ! pc = Some stmnt ->
      d_out ! pc = Some stmnt.
  Proof.
    induction l; intros * HFOLD HIN.
    - cbn in *; now (inversion HFOLD; subst).
    - exploit mfold_left_cons; eauto. 
      intros (x' & y' & HFOLD_EXP & TRANSFSEQ & HINV). inv HINV.
      unfold transf_seq_block in TRANSFSEQ. repeat (destruct_match; try discriminate; []).
      destruct (peq pc n); subst.
      + now rewrite Heqo in HIN.
      + unfold Errors.bind2 in TRANSFSEQ. repeat (destruct_match; try discriminate; []).
        inv TRANSFSEQ. eapply IHl; eauto. now rewrite PTree.gso by auto.
  Qed.

  Lemma transl_spec_correct' :
    forall l ctrl d_in d_out pc bb,
      mfold_left (transf_seq_block ctrl) l (OK d_in) = OK d_out ->
      In (pc, bb) l ->
      exists n pred' stmnt, transf_chained_block n ctrl (Ptrue, Vskip) (concat bb) = OK (pred', stmnt)
        /\ d_out ! pc = Some stmnt.
  Proof.
    induction l; [now inversion 2|].
    cbn -[mfold_left]. intros * HFOLD HIN.
    exploit mfold_left_cons; eauto. 
    intros (x' & y' & HFOLD_EXP & TRANSFSEQ & HINV). inv HINV.
    inversion_clear HIN as [HIN' | HIN']; eauto.
    inversion HIN' as [HIN_CLEAR]; subst. clear HIN_CLEAR.
    unfold transf_seq_block, Errors.bind2 in TRANSFSEQ. 
    repeat (destruct_match; try discriminate; []).
    inversion TRANSFSEQ as []; subst. clear TRANSFSEQ.
    exploit transl_spec_in_output; eauto. now rewrite PTree.gss.
  Qed.

  Lemma transl_spec_correct :
    forall ctrl d_in d_out pc bb c,
      mfold_left (transf_seq_block ctrl) (PTree.elements c) (OK d_in) = OK d_out ->
      c ! pc = Some bb ->
      exists n pred' stmnt, transf_chained_block n ctrl (Ptrue, Vskip) (concat bb) = OK (pred', stmnt)
        /\ d_out ! pc = Some stmnt.
  Proof. intros. eapply transl_spec_correct'; eauto using PTree.elements_correct. Qed.

  Lemma lt_check_step_cf_instr :
    forall s f sp pc rs pr m cf t s' f' sp' x rs' pr' m' ctrl p st n,
      translate_cfi n ctrl p cf = OK st ->
      check_cfi n cf = OK tt ->
      step_cf_instr ge (GibleSubPar.State s f sp pc rs pr m) cf t
            (GibleSubPar.State s' f' sp' x rs' pr' m') ->
      Z.pos x <= Int.max_unsigned.
  Proof.
    intros.
    destruct cf; cbn in *; try discriminate; unfold check_cfi, assert_, Errors.bind in *; 
      repeat (destruct_match; try discriminate); simplify; inv H1; try destruct_match; try lia.
    (* This was used for case statement translation *)
    (* apply list_nth_z_in in H19. *)
    (* eapply forallb_forall in Heqb; eauto. lia. *)
  Qed.

  Lemma lt_check_step_cf_instr2 :
    forall cf n,
      check_cfi n cf = OK tt ->
      Z.pos n <= Int.max_unsigned.
  Proof.
    intros.
    destruct cf; cbn in *; try discriminate; unfold check_cfi, assert_, Errors.bind in *; 
      repeat (destruct_match; try discriminate); simplify; try destruct_match; try lia.
  Qed.

Lemma max_pred_instr_lt :
  forall y a,
    (y <= max_pred_instr y a)%positive.
Proof.
  unfold max_pred_instr; intros.
  destruct a; try destruct o; lia.
Qed.

Lemma max_pred_instr_fold_lt :
  forall b y,
    (y <= fold_left max_pred_instr b y)%positive.
Proof.
  induction b; crush.
  transitivity (max_pred_instr y a); auto.
  apply max_pred_instr_lt.
Qed.

Lemma max_pred_block_lt :
  forall y a b,
    (y <= max_pred_block y a b)%positive.
Proof.
  unfold max_pred_block, SubParBB.foldl; intros.
  apply max_pred_instr_fold_lt.
Qed.

Lemma max_fold_left_initial :
  forall l y,
    (y <= fold_left (fun (a : positive) (p0 : positive * SubParBB.t) => max_pred_block a (fst p0) (snd p0)) l y)%positive.
Proof.
  induction l; crush.
  transitivity (max_pred_block y (fst a) (snd a)); eauto.
  apply max_pred_block_lt.
Qed.

Lemma max_pred_in_max :
  forall y p i,
    In p (pred_uses i) ->
    (p <= max_pred_instr y i)%positive.
Proof.
  intros. unfold max_pred_instr. destruct i; try destruct o; cbn in *; try easy.
  - eapply predicate_lt in H; lia.
  - eapply predicate_lt in H; lia.
  - eapply predicate_lt in H; lia.
  - inv H; try lia. eapply predicate_lt in H0; lia.
  - eapply predicate_lt in H; lia.
Qed.

Lemma fold_left_in_max :
  forall bb p y i,
    In i bb ->
    In p (pred_uses i) ->
    (p <= fold_left max_pred_instr bb y)%positive.
Proof.
  induction bb; crush. inv H; eauto.
  transitivity (max_pred_instr y i); [|eapply max_pred_instr_fold_lt].
  apply max_pred_in_max; auto.
Qed.

Lemma max_pred_function_use' :
  forall l pc bb p i y,
    In (pc, bb) l ->
    In i (concat bb) ->
    In p (pred_uses i) ->
    (p <= fold_left (fun (a : positive) (p0 : positive * SubParBB.t) => max_pred_block a (fst p0) (snd p0)) l y)%positive.
Proof.
  induction l; crush. inv H; eauto.
  transitivity (max_pred_block y (fst (pc, bb)) (snd (pc, bb))); eauto;
    [|eapply max_fold_left_initial].
  cbn. unfold SubParBB.foldl.
  eapply fold_left_in_max; eauto.
Qed.

Lemma max_pred_function_use :
  forall f pc bb i p,
    f.(fn_code) ! pc = Some bb ->
    In i (concat bb) ->
    In p (pred_uses i) ->
    (p <= max_pred_function f)%positive.
Proof.
  unfold max_pred_function; intros.
  rewrite PTree.fold_spec.
  eapply max_pred_function_use'; eauto.
  eapply PTree.elements_correct; eauto.
Qed.

  (* Lemma lt_max_resources_in_block : *)
  (*   forall bb a pc x x0, *)
  (*     In x (concat bb) -> *)
  (*     In x0 (pred_uses x) -> *)
  (*     Ple x0 (max_pred_block a pc bb). *)
  (* Proof.  *)
  (*   induction bb. *)
  (*   - intros. cbn in *. inv H. *)
  (*   - intros. cbn in *. *)

  (* Lemma lt_max_resources_lt_a : *)
  (*   forall bb x x0 a k v, *)
  (*     In x (concat bb) -> *)
  (*     In x0 (pred_uses x) -> *)
  (*     Ple x0 a -> *)
  (*     Ple x0 (max_pred_block a k v). *)
  (* Proof. Admitted. *)

  (* Definition inductive_p final fld := *)
  (*   forall pc bb,  *)
  (*     final ! pc = Some bb -> *)
  (*     Forall (fun i0 : instr => Forall (fun x2 : positive => Ple x2 fld) (pred_uses i0)) (concat bb). *)

  (* Lemma lt_max_resource_predicate_Forall : *)
  (*   forall f pc bb, *)
  (*   f.(GibleSubPar.fn_code) ! pc = Some bb -> *)
  (*   Forall (fun i0 : instr => Forall (fun x2 : positive => Ple x2 (max_pred_function f)) (pred_uses i0)) (concat bb). *)
  (* Proof. *)
  (*   unfold max_pred_function. *)
  (*   intro f.  *)
  (*   match goal with |- ?g => replace g with (inductive_p (fn_code f) (PTree.fold max_pred_block (fn_code f) 1%positive)) by auto end. *)
  (*   eapply PTree_Properties.fold_rec; unfold inductive_p; intros; cbn in *. *)
  (*   - eapply H0. erewrite H. eauto. *)
  (*   - now rewrite PTree.gempty in H. *)
  (*   - destruct (peq k pc); subst. *)
  (*     + rewrite PTree.gss in H2. inv H2. *)
  (*       eapply Forall_forall; intros. eapply Forall_forall; intros. eauto using lt_max_resources_in_block. *)
  (*     + rewrite PTree.gso in H2 by auto. *)
  (*       eapply H1 in H2. eapply Forall_forall; intros. eapply Forall_forall; intros. *)
  (*       eapply Forall_forall in H2; eauto. eapply Forall_forall in H2; eauto. *)
  (*       eauto using lt_max_resources_lt_a. *)
  (* Qed. *)

  Lemma lt_max_resource_predicate_Forall :
    forall f pc bb,
    f.(GibleSubPar.fn_code) ! pc = Some bb ->
    Forall (fun i0 : instr => Forall (fun x2 : positive => Ple x2 (max_pred_function f)) (pred_uses i0)) (concat bb).
  Proof.
    intros. do 2 (eapply Forall_forall; intros). unfold Ple. eapply max_pred_function_use; eauto.
  Qed.

  Lemma transl_step_state_correct :
    forall s f sp pc rs rs' m m' bb pr pr' state cf t,
      (fn_code f) ! pc = Some bb ->
      SubParBB.step ge sp (Iexec {| is_rs := rs; is_ps := pr; is_mem := m |}) bb
             (Iterm {| is_rs := rs'; is_ps := pr'; is_mem := m' |} cf) ->
      step_cf_instr ge (GibleSubPar.State s f sp pc rs' pr' m') cf t state ->
      forall R1 : DHTL.state,
        match_states (GibleSubPar.State s f sp pc rs pr m) R1 ->
        exists R2 : DHTL.state, 
          Smallstep.plus DHTL.step tge R1 t R2 /\ match_states state R2.
  Proof.
    intros * HIN HSTEP HCF * HMATCH.
    exploit match_states_ok_transl; eauto.
    intros (m0 & asr & asa & s' & HTRANSL & ?). subst.
    unfold transl_module, Errors.bind, bind, ret in HTRANSL.
    repeat (destruct_match; try discriminate; []).
    exploit transl_spec_correct; eauto.
    intros (n & pred' & stmnt0 & HTRANSF & HGET).
    exploit step_exec_concat; eauto; intros HCONCAT.
    exploit cf_in_bb_subparbb'; eauto. intros (exit_p & HINEXIT & HTRUTHY).
    exploit transl_step_through_cfi'; eauto. intros (curr_p & _stmnt & HCFI & HCHECK).
    exploit match_states_cf_states_translate_cfi; eauto. 
    intros (s0 & f1 & sp0 & pc0 & rs0 & pr0 & m2 & HPARSTATE & HEXISTS).
    exploit match_states_cf_events_translate_cfi; eauto; intros; subst.
    inv HEXISTS.
    - inv HPARSTATE. inv H. exploit transl_step_state_correct_instr_state; eauto.
      constructor. eapply lt_max_resource_predicate_Forall; eauto.
      cbn; unfold Ple; lia. intros (asr' & asa' & HSTMNTRUN & HMATCH').
      do 2 apply match_states_merge_empty_all in HMATCH'.
      eexists. split; eauto. inv HMATCH. inv CONST.
      apply Smallstep.plus_one. econstructor; eauto.
      inv HTRANSL. auto. erewrite transl_module_ram_none by eauto. constructor.
      inv HMATCH'. unfold state_st_wf in WF0. auto.
      eapply lt_check_step_cf_instr; eauto.
    - inv HPARSTATE; simplify. exploit transl_step_state_correct_instr_return; eauto.
      constructor. eapply lt_max_resource_predicate_Forall; eauto.
      cbn; unfold Ple; lia.
      intros (asr' & asa' & retval & HSTMNT_RUN & HVAL & HASR1 & HASR2 & HASR3).
      inv HMATCH. inv CONST.
      econstructor. split.
      eapply Smallstep.plus_two.
      econstructor.
      + eauto.
      + eauto.
      + eauto.
      + inv HTRANSL. eauto.
      + eauto.
      + erewrite transl_module_ram_none by eauto. constructor.
      + eauto.
      + eauto.
      + unfold merge_regs. rewrite AssocMapExt.merge_base_1. rewrite AssocMapExt.merge_base_1. eauto.
      + eapply lt_check_step_cf_instr2; eauto.
      + eapply DHTL.step_finish.
        * now do 2 rewrite AssocMapExt.merge_base_1.
        * do 2 rewrite AssocMapExt.merge_base_1; eauto.
      + auto.
      + constructor. auto. auto.
  Qed.

  Theorem transl_step_correct:
    forall (S1 : GibleSubPar.state) t S2,
      GibleSubPar.step ge S1 t S2 ->
      forall (R1 : DHTL.state),
        match_states S1 R1 ->
        exists R2, Smallstep.plus DHTL.step tge R1 t R2 /\ match_states S2 R2.
  Proof.
    induction 1.
    - now (eapply transl_step_state_correct; eauto).
    - now apply transl_callstate_correct.
    - inversion 1.
    - now apply transl_returnstate_correct.
  Qed.
  #[local] Hint Resolve transl_step_correct : htlproof.

#[local] Hint Resolve transl_returnstate_correct : htlproof.
#[local] Hint Resolve transl_final_states : htlproof.
#[local] Hint Resolve transl_initial_states : htlproof.

  Theorem transf_program_correct:
    Smallstep.forward_simulation (GibleSubPar.semantics prog) (DHTL.semantics tprog).
  Proof.
    eapply Smallstep.forward_simulation_plus; eauto with htlproof.
    apply senv_preserved; eauto.
  Qed.

End CORRECTNESS.