File: epd.c

package info (click to toggle)
crafty 23.4-9
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid, trixie
  • size: 3,292 kB
  • sloc: ansic: 30,650; cpp: 5,829; makefile: 890; sh: 178; perl: 30
file content (4482 lines) | stat: -rw-r--r-- 120,447 bytes parent folder | download | duplicates (5)
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
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
/*>>> epd.c: Extended Position Description routines */
#if defined(EPD)
/* Revised: 1996.06.23 */
/*
 Copyright (C) 1996 by Steven J. Edwards (sje@mv.mv.com)
 All rights reserved.  This code may be freely redistibuted and used by
 both research and commerical applications.  No warranty exists.
 */
/*
 Everything in this source file is independent of the host program, as
 are the prototypes in the epd.h include file.  Requests for changes
 and additions should be communicated to the author via the e-mail
 address given above.
 */
/*
 This file was originally prepared on an Apple Macintosh using the
 Metrowerks CodeWarrior 6 ANSI C compiler.  Tabs are set at every
 four columns.  Further testing and development was performed on a
 generic PC running Linux 1.3.20 and using the gcc 2.7.0 compiler.
 */
/* system includes */
#  include <ctype.h>
#  include <time.h>
#  include "chess.h"
#  include "data.h"
/* EPD definitions (host independent) */
#  include "epddefs.h"
/* EPD routine prototypes (host independent) */
#  include "epd.h"
/* ASCII character constants */
#  define ascii_nul ((char) 0x00)
#  define ascii_sp  ((char) 0x20)
/* tree limit; adjust according to memory availability */
#  define treeL 16384
/* tree overrun safety margin */
#  define treemarginL 256
/* played moves history limit; adjust according to memory availability */
#  define pmhL 512
/* data flows (input and output) */
#  define flowL 2
/* character case mapping */
#  define map_lower(ch) (isupper((ch)) ? tolower((ch)) : (ch))
#  define map_upper(ch) (islower((ch)) ? toupper((ch)) : (ch))
/* identifier character check */
#  define IdentChar(ch) (isalpha((ch)) || isdigit((ch)) || ((ch) == '_'))
/* vacancy check */
#  define Vacant(sq) (EPDboard.rbv[(sq)] == cp_v0)
/* token record type (for token chain) */
typedef struct tknS {
  charptrT tkn_str;             /* allocated token string value */
  struct tknS *tkn_prev;        /* previous record */
  struct tknS *tkn_next;        /* next record */
} tknT, *tknptrT;

/* tree stack entry record type */
typedef struct tseS {
  siT tse_count;                /* entry count for this level */
  mptrT tse_base;               /* first move in moveset */
  mptrT tse_curr;               /* current move of interest in moveset */
} tseT, *tseptrT;

/* color to move strings */
/* global game chain anchors */
static gamptrT head_gamptr;
static gamptrT tail_gamptr;

/* EPD standard opcode mnemonics */
static charptrT epdsostrv[epdsoL];

/* EPD refcom operand strings */
static charptrT refcomstrv[refcomL];

/* EPD refreq operand strings */
static charptrT refreqstrv[refreqL];

/* PGN Seven Tag Roster names */
static charptrT pgnstrstrv[pgnstrL];

/* game termination indication marker strings */
static charptrT gtimstrv[gtimL];

/* player name strings */
/* character conversion vectors (colors and pieces) */
static char asccv[rcL];
static char ascpv[rpL];

/* character conversion vectors (ranks and files) */
static char ascrv[rankL];
static char ascfv[fileL];

/* promotion piece from special case move code coversion vector */
static pT cv_p_scmvv[scmvL];

/* various color and piece conversion vectors */
static cpT cv_cp_c_pv[rcL][rpL];
static cT cv_c_cpv[cpL];
static pT cv_p_cpv[cpL];
static cT inv_cv[rcL];

/* direction vectors */
static dvT dvv[dxL];
static xdvT xdvv[dxL];

/* extension board (border detection) */
static xbT xb;

/* token chain anchors */
static tknptrT head_tknptr;
static tknptrT tail_tknptr;

/* local SAN vector and its index */
static sanT lsan;
static siT lsani;

/* census vectors */
static siT count_cv[rcL];
static siT count_cpv[rcL][rpL];

/* the current board */
static rbT EPDboard;

/* the current environment stack entry  */
static eseT ese;

/* the current tree stack entry */
static tseT tse;

/* the master ply index */
static siT ply;

/* the base of the move tree and its current pointer */
static mptrT treebaseptr;
static mptrT treeptr;

/* the base of the tree stack entry stack and its current pointer */
static tseptrT tsebaseptr;
static tseptrT tseptr;

/* base of the environment stack and its current pointer */
static eseptrT esebaseptr;
static eseptrT eseptr;

/* return area for board data */
static rbT ret_rb;

/* return area for move data */
static mT ret_m;

/*--> EPDFatal: emit fatal diagnostic and quit */
nonstatic void EPDFatal(charptrT s) {
  fprintf(stderr, "EPD Fatal error: %s.\n", s);
  exit(1);
}

/*--> EPDSwitchFault: emit switch fault diagnostic and quit */
nonstatic void EPDSwitchFault(charptrT s) {
  fprintf(stderr, "Switch fault detected.\n");
  EPDFatal(s);
  return;
}

/*--> EPDMemoryGrab: allocate memory */
nonstatic voidptrT EPDMemoryGrab(liT n) {
  voidptrT ptr;

  ptr = (voidptrT) malloc((n == 0) ? 1 : n);
  if (ptr == NULL)
    EPDFatal("EPDMemoryGrab");
  return (ptr);
}

/*--> EPDMemoryFree: deallocate memory */
nonstatic void EPDMemoryFree(voidptrT ptr) {
  if (ptr != NULL)
    free(ptr);
  return;
}

/*--> EPDStringGrab: allocate and copy a string */
nonstatic charptrT EPDStringGrab(charptrT s) {
  charptrT ptr;

  ptr = (charptrT) EPDMemoryGrab(strlen(s) + 1);
  strcpy(ptr, s);
  return (ptr);
}

/*--> EPDStringAppendChar: append a character to a string */
nonstatic charptrT EPDStringAppendChar(charptrT s, char c) {
  charptrT ptr;
  liT length;

/* the first argument is deallocated */
  length = strlen(s);
  ptr = (charptrT) EPDMemoryGrab(length + 2);
  strcpy(ptr, s);
  EPDMemoryFree(s);
  *(ptr + length) = c;
  *(ptr + length + 1) = ascii_nul;
  return (ptr);
}

/*--> EPDStringAppendStr: append a string to a string */
nonstatic charptrT EPDStringAppendStr(charptrT s0, charptrT s1) {
  charptrT ptr;
  liT length;

/* the first argument is deallocated */
  length = strlen(s0) + strlen(s1);
  ptr = (charptrT) EPDMemoryGrab(length + 1);
  strcpy(ptr, s0);
  strcat(ptr, s1);
  EPDMemoryFree(s0);
  return (ptr);
}

/*--> EPDNewGPM: allocate and initialize a new GPM record */
static gpmptrT EPDNewGPM(mptrT mptr) {
  gpmptrT gpmptr;
  cT c;
  sqT sq;
  cpT cp0, cp1;

  gpmptr = (gpmptrT) EPDMemoryGrab(sizeof(gpmT));
  gpmptr->gpm_m = *mptr;
  gpmptr->gpm_ese.ese_actc = ese.ese_actc;
  gpmptr->gpm_ese.ese_cast = ese.ese_cast;
  gpmptr->gpm_ese.ese_epsq = ese.ese_epsq;
  gpmptr->gpm_ese.ese_hmvc = ese.ese_hmvc;
  gpmptr->gpm_ese.ese_fmvn = ese.ese_fmvn;
  for (c = c_w; c <= c_b; c++)
    gpmptr->gpm_ese.ese_ksqv[c] = ese.ese_ksqv[c];
  for (sq = sq_a1; sq <= sq_h8; sq += 2) {
    cp0 = EPDboard.rbv[sq + 0];
    cp1 = EPDboard.rbv[sq + 1];
    gpmptr->gpm_nbv[sq >> 1] = ((cp1 << nybbW) | cp0);
  };
  gpmptr->gpm_prev = gpmptr->gpm_next = NULL;
  return (gpmptr);
}

/*--> EPDReleaseGPM: release a GPM record */
static
void EPDReleaseGPM(gpmptrT gpmptr) {
  if (gpmptr != NULL)
    EPDMemoryFree(gpmptr);
  return;
}

/*--> EPDAppendGPM: append a GPM record to a game  */
static
void EPDAppendGPM(gamptrT gamptr, gpmptrT gpmptr) {
  if (gamptr->gam_tailgpm == NULL)
    gamptr->gam_headgpm = gpmptr;
  else
    gamptr->gam_tailgpm->gpm_next = gpmptr;
  gpmptr->gpm_prev = gamptr->gam_tailgpm;
  gpmptr->gpm_next = NULL;
  gamptr->gam_tailgpm = gpmptr;
  return;
}

/*--> EPDUnthreadGPM: unthread a GPM record from a game */
static
void EPDUnthreadGPM(gamptrT gamptr, gpmptrT gpmptr) {
  if (gpmptr->gpm_prev == NULL)
    gamptr->gam_headgpm = gpmptr->gpm_next;
  else
    gpmptr->gpm_prev->gpm_next = gpmptr->gpm_next;
  if (gpmptr->gpm_next == NULL)
    gamptr->gam_tailgpm = gpmptr->gpm_prev;
  else
    gpmptr->gpm_next->gpm_prev = gpmptr->gpm_prev;
  return;
}

/*--> EPDReleaseGPMoveChain: release the game played moves chain */
static
void EPDReleaseGPMoveChain(gamptrT gamptr) {
  gpmptrT gpmptr;

  while (gamptr->gam_headgpm != NULL) {
    gpmptr = gamptr->gam_tailgpm;
    EPDUnthreadGPM(gamptr, gpmptr);
    EPDReleaseGPM(gpmptr);
  };
  return;
}

/*--> EPDNewGAM: allocate and initialize a new GAM record */
static gamptrT EPDNewGAM(void) {
  gamptrT gamptr;
  pgnstrT pgnstr;

  gamptr = (gamptrT) EPDMemoryGrab(sizeof(gamT));
  for (pgnstr = 0; pgnstr < pgnstrL; pgnstr++)
    gamptr->gam_strv[pgnstr] = EPDStringGrab("");
  gamptr->gam_gtim = gtim_u;
  gamptr->gam_headgpm = gamptr->gam_tailgpm = NULL;
  gamptr->gam_prev = gamptr->gam_next = NULL;
  return (gamptr);
}

/*--> EPDReleaseGAM: release a GAM record */
static
void EPDReleaseGAM(gamptrT gamptr) {
  pgnstrT pgnstr;

  if (gamptr != NULL) {
    for (pgnstr = 0; pgnstr < pgnstrL; pgnstr++)
      if (gamptr->gam_strv[pgnstr] != NULL)
        EPDMemoryFree(gamptr->gam_strv[pgnstr]);
    EPDReleaseGPMoveChain(gamptr);
    EPDMemoryFree(gamptr);
  };
  return;
}

/*--> EPDAppendGAM: append a GAM record to the game chain */
static
void EPDAppendGAM(gamptrT gamptr) {
  if (tail_gamptr == NULL)
    head_gamptr = gamptr;
  else
    tail_gamptr->gam_next = gamptr;
  gamptr->gam_prev = tail_gamptr;
  gamptr->gam_next = NULL;
  tail_gamptr = gamptr;
  return;
}

/*--> EPDUnthreadGAM: unthread a GAM record from the game chain */
static
void EPDUnthreadGAM(gamptrT gamptr) {
  if (gamptr->gam_prev == NULL)
    head_gamptr = gamptr->gam_next;
  else
    gamptr->gam_prev->gam_next = gamptr->gam_next;
  if (gamptr->gam_next == NULL)
    tail_gamptr = gamptr->gam_prev;
  else
    gamptr->gam_next->gam_prev = gamptr->gam_prev;
  return;
}

/*--> EPDReleaseGameChain: release the game chain */
static
void EPDReleaseGameChain(void) {
  gamptrT gamptr;

  while (head_gamptr != NULL) {
    gamptr = tail_gamptr;
    EPDUnthreadGAM(gamptr);
    EPDReleaseGAM(gamptr);
  };
  return;
}

/*--> EPDGamePlyCount: return ply count of a game */
static siT EPDGamePlyCount(gamptrT gamptr) {
  siT count;
  gpmptrT gpmptr;

  count = 0;
  gpmptr = gamptr->gam_headgpm;
  while (gpmptr != NULL) {
    count++;
    gpmptr = gpmptr->gpm_next;
  };
  return (count);
}

/*--> EPDGameOpen: create/open a new game structure */
nonstatic gamptrT EPDGameOpen(void) {
  gamptrT gamptr;

  gamptr = EPDNewGAM();
  EPDAppendGAM(gamptr);
  return (gamptr);
}

/*--> EPDGameClose: close/destroy a game structure */
nonstatic void EPDGameClose(gamptrT gamptr) {
  if (gamptr != NULL) {
    EPDUnthreadGAM(gamptr);
    EPDReleaseGAM(gamptr);
  };
  return;
}

/*--> EPDGameAppendMove: append a move to a game structure */
nonstatic void EPDGameAppendMove(gamptrT gamptr, mptrT mptr) {
  gpmptrT gpmptr;

  gpmptr = EPDNewGPM(mptr);
  EPDAppendGPM(gamptr, gpmptr);
  return;
}

/*--> EPDNewTKN: allocate and initialize a new TKN record */
static tknptrT EPDNewTKN(charptrT s) {
  tknptrT tknptr;

  tknptr = (tknptrT) EPDMemoryGrab(sizeof(tknT));
  tknptr->tkn_str = EPDStringGrab(s);
  tknptr->tkn_prev = tknptr->tkn_next = NULL;
  return (tknptr);
}

/*--> EPDReleaseTKN: release a TKN record */
static
void EPDReleaseTKN(tknptrT tknptr) {
  if (tknptr != NULL) {
    if (tknptr->tkn_str != NULL)
      EPDMemoryFree(tknptr->tkn_str);
    EPDMemoryFree(tknptr);
  };
  return;
}

/*--> EPDAppendTKN: append a TKN record to the token chain */
static
void EPDAppendTKN(tknptrT tknptr) {
  if (tail_tknptr == NULL)
    head_tknptr = tknptr;
  else
    tail_tknptr->tkn_next = tknptr;
  tknptr->tkn_prev = tail_tknptr;
  tknptr->tkn_next = NULL;
  tail_tknptr = tknptr;
  return;
}

/*--> EPDUnthreadTKN: unthread a TKN record from the token chain */
static
void EPDUnthreadTKN(tknptrT tknptr) {
  if (tknptr->tkn_prev == NULL)
    head_tknptr = tknptr->tkn_next;
  else
    tknptr->tkn_prev->tkn_next = tknptr->tkn_next;
  if (tknptr->tkn_next == NULL)
    tail_tknptr = tknptr->tkn_prev;
  else
    tknptr->tkn_next->tkn_prev = tknptr->tkn_prev;
  return;
}

/*--> EPDReleaseTokenChain: release the token chain */
static
void EPDReleaseTokenChain(void) {
  tknptrT tknptr;

  while (head_tknptr != NULL) {
    tknptr = tail_tknptr;
    EPDUnthreadTKN(tknptr);
    EPDReleaseTKN(tknptr);
  };
  return;
}

/*--> EPDTokenize: create the token chain */
nonstatic void EPDTokenize(charptrT s) {
  siT i;
  char c;
  tknptrT tknptr;
  charptrT str;

/* first, release any existing chain */
  EPDReleaseTokenChain();
/* scan the input until end of string */
  i = 0;
  c = *(s + i++);
  while (c != ascii_nul) {
/* skip leading whitespace */
    while ((c != ascii_nul) && isspace(c))
      c = *(s + i++);
/* if not at end of string, then a token has started */
    if (c != ascii_nul) {
      str = EPDStringGrab("");
      while ((c != ascii_nul) && !isspace(c)) {
        str = EPDStringAppendChar(str, c);
        c = *(s + i++);
      };
      tknptr = EPDNewTKN(str);
      EPDAppendTKN(tknptr);
      EPDMemoryFree(str);
    };
  };
  return;
}

/*--> EPDTokenCount: count the tokens in the token chain */
nonstatic siT EPDTokenCount(void) {
  siT n;
  tknptrT tknptr;

  n = 0;
  tknptr = head_tknptr;
  while (tknptr != NULL) {
    n++;
    tknptr = tknptr->tkn_next;
  };
  return (n);
}

/*--> EPDTokenFetch: fetch n-th (zero origin) token from the token chain */
nonstatic charptrT EPDTokenFetch(siT n) {
  charptrT s;
  siT i;
  tknptrT tknptr;

  i = 0;
  tknptr = head_tknptr;
  while ((tknptr != NULL) && (i < n)) {
    i++;
    tknptr = tknptr->tkn_next;
  };
  if (tknptr == NULL)
    s = NULL;
  else
    s = tknptr->tkn_str;
  return (s);
}

/*--> EPDCICharEqual: test for case independent character equality */
nonstatic siT EPDCICharEqual(char ch0, char ch1) {
  siT flag;

  if (map_lower(ch0) == map_lower(ch1))
    flag = 1;
  else
    flag = 0;
  return (flag);
}

/*--> EPDPieceFromCP: fetch piece from color-piece */
nonstatic pT EPDPieceFromCP(cpT cp) {
  pT p;

  p = cv_p_cpv[cp];
  return (p);
}

/*--> EPDCheckPiece: test if a character is a piece letter */
nonstatic siT EPDCheckPiece(char ch) {
  siT flag;
  pT p;

  flag = 0;
  p = p_p;
  while (!flag && (p <= p_k))
    if (EPDCICharEqual(ch, ascpv[p]))
      flag = 1;
    else
      p++;
  return (flag);
}

/*--> EPDEvaluatePiece: evaluate a piece letter character */
nonstatic pT EPDEvaluatePiece(char ch) {
  pT p;
  siT flag;

  flag = 0;
  p = p_p;
  while (!flag && (p <= p_k))
    if (EPDCICharEqual(ch, ascpv[p]))
      flag = 1;
    else
      p++;
  if (!flag)
    p = p_nil;
  return (p);
}

/*--> EPDCheckColor: test if a character is a color letter */
nonstatic siT EPDCheckColor(char ch) {
  siT flag;
  cT c;

  flag = 0;
  c = c_w;
  while (!flag && (c <= c_b))
    if (EPDCICharEqual(ch, asccv[c]))
      flag = 1;
    else
      c++;
  return (flag);
}

/*--> EPDEvaluateColor: evaluate a color letter character */
nonstatic cT EPDEvaluateColor(char ch) {
  cT c;
  siT flag;

  flag = 0;
  c = c_w;
  while (!flag && (c <= c_b))
    if (EPDCICharEqual(ch, asccv[c]))
      flag = 1;
    else
      c++;
  if (!flag)
    c = c_nil;
  return (c);
}

/*--> EPDCheckRank: test if a character is a rank character */
nonstatic siT EPDCheckRank(char ch) {
  siT flag;
  rankT rank;

  flag = 0;
  rank = rank_1;
  while (!flag && (rank <= rank_8))
    if (EPDCICharEqual(ch, ascrv[rank]))
      flag = 1;
    else
      rank++;
  return (flag);
}

/*--> EPDEvaluateRank: evaluate a color rank character */
nonstatic rankT EPDEvaluateRank(char ch) {
  rankT rank;
  siT flag;

  flag = 0;
  rank = rank_1;
  while (!flag && (rank <= rank_8))
    if (EPDCICharEqual(ch, ascrv[rank]))
      flag = 1;
    else
      rank++;
  if (!flag)
    rank = rank_nil;
  return (rank);
}

/*--> EPDCheckFile: test if a character is a file character */
nonstatic siT EPDCheckFile(char ch) {
  siT flag;
  fileT file;

  flag = 0;
  file = file_a;
  while (!flag && (file <= file_h))
    if (EPDCICharEqual(ch, ascfv[file]))
      flag = 1;
    else
      file++;
  return (flag);
}

/*--> EPDEvaluateFile: evaluate a color file character */
nonstatic rankT EPDEvaluateFile(char ch) {
  fileT file;
  siT flag;

  flag = 0;
  file = file_a;
  while (!flag && (file <= file_h))
    if (EPDCICharEqual(ch, ascfv[file]))
      flag = 1;
    else
      file++;
  if (!flag)
    file = file_nil;
  return (file);
}

/*--> EPDNewEOV: allocate a new EOV record */
nonstatic eovptrT EPDNewEOV(void) {
  eovptrT eovptr;

  eovptr = (eovptrT) EPDMemoryGrab(sizeof(eovT));
  eovptr->eov_eob = eob_nil;
  eovptr->eov_str = NULL;
  eovptr->eov_prev = eovptr->eov_next = NULL;
  return (eovptr);
}

/*--> EPDReleaseEOV: release an EOV record */
nonstatic void EPDReleaseEOV(eovptrT eovptr) {
  if (eovptr != NULL) {
    if (eovptr->eov_str != NULL)
      EPDMemoryFree(eovptr->eov_str);
    EPDMemoryFree(eovptr);
  };
  return;
}

/*--> EPDAppendEOV: append an EOV record */
nonstatic void EPDAppendEOV(eopptrT eopptr, eovptrT eovptr) {
  if (eopptr->eop_taileov == NULL)
    eopptr->eop_headeov = eovptr;
  else
    eopptr->eop_taileov->eov_next = eovptr;
  eovptr->eov_prev = eopptr->eop_taileov;
  eovptr->eov_next = NULL;
  eopptr->eop_taileov = eovptr;
  return;
}

/*--> EPDUnthreadEOV: unthread an EOV record */
static
void EPDUnthreadEOV(eopptrT eopptr, eovptrT eovptr) {
  if (eovptr->eov_prev == NULL)
    eopptr->eop_headeov = eovptr->eov_next;
  else
    eovptr->eov_prev->eov_next = eovptr->eov_next;
  if (eovptr->eov_next == NULL)
    eopptr->eop_taileov = eovptr->eov_prev;
  else
    eovptr->eov_next->eov_prev = eovptr->eov_prev;
  return;
}

/*--> EPDCreateEOVSym: create a new EOV record with a symbol value */
nonstatic eovptrT EPDCreateEOVSym(charptrT sym) {
  eovptrT eovptr;

  eovptr = EPDNewEOV();
  eovptr->eov_eob = eob_symbol;
  eovptr->eov_str = EPDStringGrab(sym);
  return (eovptr);
}

/*--> EPDCreateEOVInt: create a new EOV record with an integer value */
nonstatic eovptrT EPDCreateEOVInt(liT lval) {
  eovptrT eovptr;
  char tv[tL];

  sprintf(tv, "%ld", lval);
  eovptr = EPDNewEOV();
  eovptr->eov_eob = eob_symbol;
  eovptr->eov_str = EPDStringGrab(tv);
  return (eovptr);
}

/*--> EPDLocateEOV: try to locate 1st EOV record with given string value */
nonstatic eovptrT EPDLocateEOV(eopptrT eopptr, charptrT strval) {
  eovptrT eovptr;
  siT flag;

  flag = 0;
  eovptr = eopptr->eop_headeov;
  while (!flag && (eovptr != NULL))
    if (strcmp(strval, eovptr->eov_str) == 0)
      flag = 1;
    else
      eovptr = eovptr->eov_next;
  if (!flag)
    eovptr = NULL;
  return (eovptr);
}

/*--> EPDReplaceEOVStr: replace EOV string value with given string value */
nonstatic void EPDReplaceEOVStr(eovptrT eovptr, charptrT str) {
  if (eovptr->eov_str != NULL) {
    EPDMemoryFree(eovptr->eov_str);
    eovptr->eov_str = NULL;
  };
  if (str != NULL)
    eovptr->eov_str = EPDStringGrab(str);
  return;
}

/*--> EPDNewEOP: allocate a new EOP record */
nonstatic eopptrT EPDNewEOP(void) {
  eopptrT eopptr;

  eopptr = (eopptrT) EPDMemoryGrab(sizeof(eopT));
  eopptr->eop_opsym = NULL;
  eopptr->eop_headeov = eopptr->eop_taileov = NULL;
  eopptr->eop_prev = eopptr->eop_next = NULL;
  return (eopptr);
}

/*--> EPDReleaseEOP: release an EOP record */
nonstatic void EPDReleaseEOP(eopptrT eopptr) {
  eovptrT eovptr0, eovptr1;

  if (eopptr != NULL) {
    if (eopptr->eop_opsym != NULL)
      EPDMemoryFree(eopptr->eop_opsym);
    eovptr0 = eopptr->eop_headeov;
    while (eovptr0 != NULL) {
      eovptr1 = eovptr0->eov_next;
      EPDUnthreadEOV(eopptr, eovptr0);
      EPDReleaseEOV(eovptr0);
      eovptr0 = eovptr1;
    };
    EPDMemoryFree(eopptr);
  };
  return;
}

/*--> EPDAppendEOP: append an EOP record */
nonstatic void EPDAppendEOP(epdptrT epdptr, eopptrT eopptr) {
  if (epdptr->epd_taileop == NULL)
    epdptr->epd_headeop = eopptr;
  else
    epdptr->epd_taileop->eop_next = eopptr;
  eopptr->eop_prev = epdptr->epd_taileop;
  eopptr->eop_next = NULL;
  epdptr->epd_taileop = eopptr;
  return;
}

/*--> EPDUnthreadEOP: unthread an EOP record */
static
void EPDUnthreadEOP(epdptrT epdptr, eopptrT eopptr) {
  if (eopptr->eop_prev == NULL)
    epdptr->epd_headeop = eopptr->eop_next;
  else
    eopptr->eop_prev->eop_next = eopptr->eop_next;
  if (eopptr->eop_next == NULL)
    epdptr->epd_taileop = eopptr->eop_prev;
  else
    eopptr->eop_next->eop_prev = eopptr->eop_prev;
  return;
}

/*--> EPDCreateEOP: create a new EOP record with opsym */
nonstatic eopptrT EPDCreateEOP(charptrT opsym) {
  eopptrT eopptr;

  eopptr = EPDNewEOP();
  eopptr->eop_opsym = EPDStringGrab(opsym);
  return (eopptr);
}

/*--> EPDCreateEOPCode: create a new EOP record using opsym index */
nonstatic eopptrT EPDCreateEOPCode(epdsoT epdso) {
  eopptrT eopptr;

  eopptr = EPDCreateEOP(EPDFetchOpsym(epdso));
  return (eopptr);
}

/*--> EPDLocateEOP: attempt to locate EOP record with given opsym */
nonstatic eopptrT EPDLocateEOP(epdptrT epdptr, charptrT opsym) {
  eopptrT eopptr;
  siT flag;

  flag = 0;
  eopptr = epdptr->epd_headeop;
  while (!flag && (eopptr != NULL))
    if (strcmp(opsym, eopptr->eop_opsym) == 0)
      flag = 1;
    else
      eopptr = eopptr->eop_next;
  if (!flag)
    eopptr = NULL;
  return (eopptr);
}

/*--> EPDLocateEOPCode: attempt to locate EOP record with given code */
nonstatic eopptrT EPDLocateEOPCode(epdptrT epdptr, epdsoT epdso) {
  return (EPDLocateEOP(epdptr, epdsostrv[epdso]));
}

/*--> EPDDropIfLocEOPCode: try to locate/drop EOP record with given code */
nonstatic void EPDDropIfLocEOPCode(epdptrT epdptr, epdsoT epdso) {
  eopptrT eopptr;

  eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso]);
  if (eopptr != NULL) {
    EPDUnthreadEOP(epdptr, eopptr);
    EPDReleaseEOP(eopptr);
  };
  return;
}

/*--> EPDAddOpInt: add a single integer operand operation */
nonstatic void EPDAddOpInt(epdptrT epdptr, epdsoT epdso, liT val) {
  eopptrT eopptr;
  eovptrT eovptr;

  eovptr = EPDCreateEOVInt(val);
  eopptr = EPDCreateEOPCode(epdso);
  EPDAppendEOV(eopptr, eovptr);
  EPDDropIfLocEOPCode(epdptr, epdso);
  EPDAppendEOP(epdptr, eopptr);
  return;
}

/*--> EPDAddOpSym: add a single symbol operand operation */
nonstatic void EPDAddOpSym(epdptrT epdptr, epdsoT epdso, charptrT s) {
  eopptrT eopptr;
  eovptrT eovptr;

  eovptr = EPDCreateEOVSym(s);
  eopptr = EPDCreateEOPCode(epdso);
  EPDAppendEOV(eopptr, eovptr);
  EPDDropIfLocEOPCode(epdptr, epdso);
  EPDAppendEOP(epdptr, eopptr);
  return;
}

/*--> EPDNewEPD: allocate a new EPD record */
nonstatic epdptrT EPDNewEPD(void) {
  epdptrT epdptr;
  siT i;

  epdptr = (epdptrT) EPDMemoryGrab(sizeof(epdT));
  for (i = 0; i < nbL; i++)
    epdptr->epd_nbv[i] = ((cp_v0 << nybbW) | cp_v0);
  epdptr->epd_actc = c_v;
  epdptr->epd_cast = 0;
  epdptr->epd_epsq = sq_nil;
  epdptr->epd_headeop = epdptr->epd_taileop = NULL;
  return (epdptr);
}

/*--> EPDReleaseOperations: release EPD operation list */
nonstatic void EPDReleaseOperations(epdptrT epdptr) {
  eopptrT eopptr0, eopptr1;

  if (epdptr != NULL) {
    eopptr0 = epdptr->epd_headeop;
    while (eopptr0 != NULL) {
      eopptr1 = eopptr0->eop_next;
      EPDUnthreadEOP(epdptr, eopptr0);
      EPDReleaseEOP(eopptr0);
      eopptr0 = eopptr1;
    };
    epdptr->epd_headeop = NULL;
    epdptr->epd_taileop = NULL;
  };
  return;
}

/*--> EPDReleaseEPD: release an EPD record */
nonstatic void EPDReleaseEPD(epdptrT epdptr) {
  if (epdptr != NULL) {
    EPDReleaseOperations(epdptr);
    EPDMemoryFree(epdptr);
  };
  return;
}

/*--> EPDFetchOpsym: return a pointer to the indicated mnemonic */
nonstatic charptrT EPDFetchOpsym(epdsoT epdso) {
  return (epdsostrv[epdso]);
}

/*--> EPDCountOperands: count operands */
static siT EPDCountOperands(eopptrT eopptr) {
  siT count;
  eovptrT eovptr;

  count = 0;
  eovptr = eopptr->eop_headeov;
  while (eovptr != NULL) {
    count++;
    eovptr = eovptr->eov_next;
  };
  return (count);
}

/*--> EPDCountOperations: count operations */
static siT EPDCountOperations(epdptrT epdptr) {
  siT count;
  eopptrT eopptr;

  count = 0;
  eopptr = epdptr->epd_headeop;
  while (eopptr != NULL) {
    count++;
    eopptr = eopptr->eop_next;
  };
  return (count);
}

/*--> EPDSortOperands: sort operands according to string value */
static
void EPDSortOperands(eopptrT eopptr) {
  siT count;
  siT pass, flag;
  eovptrT ptr0, ptr1, ptr2, ptr3;

  count = EPDCountOperands(eopptr);
  if (count > 1) {
    flag = 1;
    pass = 0;
    while (flag && (pass < (count - 1))) {
      flag = 0;
      ptr0 = eopptr->eop_headeov;
      ptr1 = ptr0->eov_next;
      while (ptr1 != NULL) {
        if (strcmp(ptr0->eov_str, ptr1->eov_str) > 0) {
          flag = 1;
          ptr2 = ptr0->eov_prev;
          ptr3 = ptr1->eov_next;
          ptr0->eov_prev = ptr1;
          ptr0->eov_next = ptr3;
          ptr1->eov_prev = ptr2;
          ptr1->eov_next = ptr0;
          if (ptr2 == NULL)
            eopptr->eop_headeov = ptr1;
          else
            ptr2->eov_next = ptr1;
          if (ptr3 == NULL)
            eopptr->eop_taileov = ptr0;
          else
            ptr3->eov_prev = ptr0;
        } else
          ptr0 = ptr1;
        ptr1 = ptr0->eov_next;
      };
      pass++;
    };
  };
  return;
}

/*--> EPDSortOperations: sort operations according to opcode */
static
void EPDSortOperations(epdptrT epdptr) {
  siT count;
  siT pass, flag;
  eopptrT ptr0, ptr1, ptr2, ptr3;

  count = EPDCountOperations(epdptr);
  if (count > 1) {
    flag = 1;
    pass = 0;
    while (flag && (pass < (count - 1))) {
      flag = 0;
      ptr0 = epdptr->epd_headeop;
      ptr1 = ptr0->eop_next;
      while (ptr1 != NULL) {
        if (strcmp(ptr0->eop_opsym, ptr1->eop_opsym) > 0) {
          flag = 1;
          ptr2 = ptr0->eop_prev;
          ptr3 = ptr1->eop_next;
          ptr0->eop_prev = ptr1;
          ptr0->eop_next = ptr3;
          ptr1->eop_prev = ptr2;
          ptr1->eop_next = ptr0;
          if (ptr2 == NULL)
            epdptr->epd_headeop = ptr1;
          else
            ptr2->eop_next = ptr1;
          if (ptr3 == NULL)
            epdptr->epd_taileop = ptr0;
          else
            ptr3->eop_prev = ptr0;
        } else
          ptr0 = ptr1;
        ptr1 = ptr0->eop_next;
      };
      pass++;
    };
  };
  return;
}

/*--> EPDNormalize: apply normalizing sorts */
static
void EPDNormalize(epdptrT epdptr) {
  eopptrT eopptr;
  charptrT opsym;
  siT flag;

/* sort all operations */
  EPDSortOperations(epdptr);
/* sort operands for selected standard operations */
  eopptr = epdptr->epd_headeop;
  while (eopptr != NULL) {
    flag = 0;
    opsym = eopptr->eop_opsym;
    if (!flag && (strcmp(opsym, epdsostrv[epdso_am]) == 0)) {
      EPDSortOperands(eopptr);
      flag = 1;
    };
    if (!flag && (strcmp(opsym, epdsostrv[epdso_bm]) == 0)) {
      EPDSortOperands(eopptr);
      flag = 1;
    };
    eopptr = eopptr->eop_next;
  };
  return;
}

/*--> EPDCloneEPDBase: clone an EPD structure, base items only */
nonstatic epdptrT EPDCloneEPDBase(epdptrT epdptr) {
  epdptrT nptr;
  siT index;

  nptr = EPDNewEPD();
  for (index = 0; index < nbL; index++)
    nptr->epd_nbv[index] = epdptr->epd_nbv[index];
  nptr->epd_actc = epdptr->epd_actc;
  nptr->epd_cast = epdptr->epd_cast;
  nptr->epd_epsq = epdptr->epd_epsq;
  return (nptr);
}

/*--> EPDCloneEOV: clone an EOV structure */
nonstatic eovptrT EPDCloneEOV(eovptrT eovptr) {
  eovptrT nptr;

  nptr = EPDNewEOV();
  nptr->eov_eob = eovptr->eov_eob;
  if (eovptr->eov_str != NULL)
    nptr->eov_str = EPDStringGrab(eovptr->eov_str);
  return (nptr);
}

/*--> EPDCloneEOP: clone an EOP structure */
nonstatic eopptrT EPDCloneEOP(eopptrT eopptr) {
  eopptrT nptr;
  eovptrT eovptr, rptr;

  nptr = EPDNewEOP();
  if (eopptr->eop_opsym != NULL)
    nptr->eop_opsym = EPDStringGrab(eopptr->eop_opsym);
  rptr = eopptr->eop_headeov;
  while (rptr != NULL) {
    eovptr = EPDCloneEOV(rptr);
    EPDAppendEOV(nptr, eovptr);
    rptr = rptr->eov_next;
  };
  return (nptr);
}

/*--> EPDSetKings: set the king location vector */
static
void EPDSetKings(void) {
  sqT sq;

/* this operates only on the local environment */
  ese.ese_ksqv[c_w] = ese.ese_ksqv[c_b] = sq_nil;
  for (sq = sq_a1; sq <= sq_h8; sq++)
    switch (EPDboard.rbv[sq]) {
      case cp_wk:
        ese.ese_ksqv[c_w] = sq;
        break;
      case cp_bk:
        ese.ese_ksqv[c_b] = sq;
        break;
      default:
        break;
    };
  return;
}

/*--> EPDSet: set up an EPD structure for the given position */
nonstatic epdptrT EPDSet(rbptrT rbptr, cT actc, castT cast, sqT epsq) {
  epdptrT epdptr;
  sqT sq;
  cpT cp0, cp1;

/* this does not reference the current position */
  epdptr = EPDNewEPD();
  for (sq = sq_a1; sq <= sq_h8; sq += 2) {
    cp0 = rbptr->rbv[sq + 0];
    cp1 = rbptr->rbv[sq + 1];
    epdptr->epd_nbv[sq >> 1] = ((cp1 << nybbW) | cp0);
  };
  epdptr->epd_actc = actc;
  epdptr->epd_cast = cast;
  epdptr->epd_epsq = epsq;
  return (epdptr);
}

/*--> EPDSetCurrentPosition: set current position */
nonstatic void EPDSetCurrentPosition(rbptrT rbptr, cT actc, castT cast,
    sqT epsq, siT hmvc, siT fmvn) {
  sqT sq;

/* this changes the current position */
  for (sq = sq_a1; sq <= sq_h8; sq++)
    EPDboard.rbv[sq] = rbptr->rbv[sq];
  ese.ese_actc = actc;
  ese.ese_cast = cast;
  ese.ese_epsq = epsq;
  ese.ese_hmvc = hmvc;
  ese.ese_fmvn = fmvn;
  EPDSetKings();
  return;
}

/*--> EPDGetCurrentPosition: return EPD structure for current position */
nonstatic epdptrT EPDGetCurrentPosition(void) {
  epdptrT epdptr;
  sqT sq;
  cpT cp0, cp1;

  epdptr = EPDNewEPD();
  for (sq = sq_a1; sq <= sq_h8; sq += 2) {
    cp0 = EPDboard.rbv[sq + 0];
    cp1 = EPDboard.rbv[sq + 1];
    epdptr->epd_nbv[sq >> 1] = ((cp1 << nybbW) | cp0);
  };
  epdptr->epd_actc = ese.ese_actc;
  epdptr->epd_cast = ese.ese_cast;
  epdptr->epd_epsq = ese.ese_epsq;
  return (epdptr);
}

/*--> EPDFetchACTC: fetch current active color */
nonstatic cT EPDFetchACTC(void) {
/* return the value of the current active color */
  return (ese.ese_actc);
}

/*--> EPDFetchCAST: fetch current castling availability */
nonstatic castT EPDFetchCAST(void) {
/* return the value of the current castling availability */
  return (ese.ese_cast);
}

/*--> EPDFetchEPSQ: fetch current en passant target square */
nonstatic sqT EPDFetchEPSQ(void) {
/* return the value of the current en passant target square */
  return (ese.ese_epsq);
}

/*--> EPDFetchHMVC: fetch current halfmove clock */
nonstatic siT EPDFetchHMVC(void) {
/* return the value of the current halfmove clock */
  return (ese.ese_hmvc);
}

/*--> EPDFetchFMVN: fetch current fullmove number */
nonstatic siT EPDFetchFMVN(void) {
/* return the value of the current fullmove number */
  return (ese.ese_fmvn);
}

/*--> EPDFetchBoard: fetch current board */
nonstatic rbptrT EPDFetchBoard(void) {
/* copy from the local board into the designated static return area */
  ret_rb = EPDboard;
  return (&ret_rb);
}

/*--> EPDFetchCP: fetch color-piece */
nonstatic cpT EPDFetchCP(sqT sq) {
  cpT cp;

/* fetch from the local board */
  cp = EPDboard.rbv[sq];
  return (cp);
}

/*--> EPDGetGTIM: get game termination marker indicator */
nonstatic gtimT EPDGetGTIM(gamptrT gamptr) {
  return (gamptr->gam_gtim);
}

/*--> EPDPutGTIM: put game termination marker indicator */
nonstatic void EPDPutGTIM(gamptrT gamptr, gtimT gtim) {
  gamptr->gam_gtim = gtim;
  return;
}

/*--> EPDGenBasic: generate basic EPD notation for a given position */
nonstatic charptrT EPDGenBasic(rbptrT rbptr, cT actc, castT cast, sqT epsq) {
  charptrT ptr;
  epdptrT epdptr;

/* this does not reference the current position */
  epdptr = EPDSet(rbptr, actc, cast, epsq);
  ptr = EPDEncode(epdptr);
  EPDReleaseEPD(epdptr);
  return (ptr);
}

/*--> EPDGenBasicCurrent: generate basic EPD for current position */
nonstatic charptrT EPDGenBasicCurrent(void) {
  charptrT ptr;

/* this references but does not change the current position */
  ptr = EPDGenBasic(&EPDboard, ese.ese_actc, ese.ese_cast, ese.ese_epsq);
  return (ptr);
}

/*--> EPDDecode: read an EPD structure from a string */
nonstatic epdptrT EPDDecode(charptrT s) {
  epdptrT epdptr;
  eopptrT eopptr;
  eovptrT eovptr;
  siT flag, quoteflag;
  siT ch;
  liT i;
  siT j, d;
  byteptrT bptr;
  fileT file;
  rankT rank;
  sqT sq;
  cT c;
  pT p;
  cpT cp;

/* this does not reference the current position */
/* set up */
  flag = 1;
  i = 0;
  ch = *(s + i++);
/* initialize the return structure */
  epdptr = EPDNewEPD();
/* skip whitespace */
  if (flag) {
    while (flag && (ch != ascii_nul) && isspace(ch))
      ch = *(s + i++);
    if (ch == ascii_nul)
      flag = 0;
  };
/* process piece placement data */
  if (flag) {
    rank = rank_8;
    file = file_a;
    while (flag && (ch != ascii_nul) && !isspace(ch)) {
      switch (ch) {
        case '/':
          if ((file != fileL) || (rank == rank_1))
            flag = 0;
          else {
            rank--;
            file = file_a;
          };
          break;
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
          d = ch - '0';
          if ((file + d) > fileL)
            flag = 0;
          else
            for (j = 0; j < d; j++) {
              sq = map_sq(rank, file);
              bptr = &epdptr->epd_nbv[sq >> 1];
              if ((sq % 2) == 0) {
                *bptr &= ~nybbM;
                *bptr |= cp_v0;
              } else {
                *bptr &= ~(nybbM << nybbW);
                *bptr |= (cp_v0 << nybbW);
              };
              file++;
            };
          break;
        default:
          if (!EPDCheckPiece((char) ch) || (file >= fileL))
            flag = 0;
          else {
            p = EPDEvaluatePiece((char) ch);
            if (isupper(ch))
              c = c_w;
            else
              c = c_b;
            sq = map_sq(rank, file);
            bptr = &epdptr->epd_nbv[sq >> 1];
            cp = cv_cp_c_pv[c][p];
            if ((sq % 2) == 0) {
              *bptr &= ~nybbM;
              *bptr |= cp;
            } else {
              *bptr &= ~(nybbM << nybbW);
              *bptr |= (cp << nybbW);
            };
            file++;
          };
          break;
      };
      ch = *(s + i++);
    };
    if (flag)
      if ((file != fileL) || (rank != rank_1))
        flag = 0;
  };
/* need at least one whitespace character */
  if (flag)
    if ((ch == ascii_nul) || !isspace(ch))
      flag = 0;
/* skip whitespace */
  if (flag) {
    while (flag && (ch != ascii_nul) && isspace(ch))
      ch = *(s + i++);
    if (ch == ascii_nul)
      flag = 0;
  };
/* process active color */
  if (flag) {
    if (!EPDCheckColor((char) ch))
      flag = 0;
    else {
      epdptr->epd_actc = EPDEvaluateColor((char) ch);
      ch = *(s + i++);
    };
  };
/* need at least one whitespace character */
  if (flag)
    if ((ch == ascii_nul) || !isspace(ch))
      flag = 0;
/* skip whitespace */
  if (flag) {
    while (flag && (ch != ascii_nul) && isspace(ch))
      ch = *(s + i++);
    if (ch == ascii_nul)
      flag = 0;
  };
/* process castling availability */
  if (flag) {
    epdptr->epd_cast = 0;
    if (ch == '-')
      ch = *(s + i++);
    else {
/* white kingside castling availability */
      if (flag && (ch == map_upper(ascpv[p_k]))) {
        epdptr->epd_cast |= cf_wk;
        ch = *(s + i++);
        if (ch == ascii_nul)
          flag = 0;
      };
/* white queenside castling availability */
      if (flag && (ch == map_upper(ascpv[p_q]))) {
        epdptr->epd_cast |= cf_wq;
        ch = *(s + i++);
        if (ch == ascii_nul)
          flag = 0;
      };
/* black kingside castling availability */
      if (flag && (ch == map_lower(ascpv[p_k]))) {
        epdptr->epd_cast |= cf_bk;
        ch = *(s + i++);
        if (ch == ascii_nul)
          flag = 0;
      };
/* black queenside castling availability */
      if (flag && (ch == map_lower(ascpv[p_q]))) {
        epdptr->epd_cast |= cf_bq;
        ch = *(s + i++);
        if (ch == ascii_nul)
          flag = 0;
      };
    };
  };
/* need at least one whitespace character */
  if (flag)
    if ((ch == ascii_nul) || !isspace(ch))
      flag = 0;
/* skip whitespace */
  if (flag) {
    while (flag && (ch != ascii_nul) && isspace(ch))
      ch = *(s + i++);
    if (ch == ascii_nul)
      flag = 0;
  };
/* process en passant target */
  if (flag) {
    if (ch == '-') {
      epdptr->epd_epsq = sq_nil;
      ch = *(s + i++);
      if ((ch != ascii_nul) && !isspace(ch))
        flag = 0;
    } else {
      if (!EPDCheckFile((char) ch))
        flag = 0;
      else {
        file = EPDEvaluateFile((char) ch);
        ch = *(s + i++);
        if ((ch == ascii_nul) || !EPDCheckRank((char) ch))
          flag = 0;
        else {
          epdptr->epd_epsq = map_sq(EPDEvaluateRank((char) ch), file);
          ch = *(s + i++);
          if ((ch != ascii_nul) && !isspace(ch))
            flag = 0;
        };
      };
    }
  }
/* skip whitespace (end-of-line is not an error) */
  if (flag)
    while ((ch != ascii_nul) && isspace(ch))
      ch = *(s + i++);
/* process operation sequence (if any) */
  if (flag) {
    while (flag && (ch != ascii_nul)) {
/* allocate a new operation */
      eopptr = EPDNewEOP();
/* form opsym (first character) */
      if (IdentChar(ch)) {
        eopptr->eop_opsym = EPDStringGrab("");
        eopptr->eop_opsym = EPDStringAppendChar(eopptr->eop_opsym, (char) ch);
        ch = *(s + i++);
      } else
        flag = 0;
/* form remainder of opsym */
      while (IdentChar(ch)) {
        eopptr->eop_opsym = EPDStringAppendChar(eopptr->eop_opsym, (char) ch);
        ch = *(s + i++);
      };
/* skip whitespace */
      if (flag) {
        while (flag && (ch != ascii_nul) && isspace(ch))
          ch = *(s + i++);
        if (ch == ascii_nul)
          flag = 0;
      };
/* process operand list */
      while (flag && (ch != ';')) {
/* allocate operand value */
        eovptr = EPDNewEOV();
/* set quoted string as appropriate */
        if (ch == '"') {
          quoteflag = 1;
          eovptr->eov_eob = eob_string;
          ch = *(s + i++);
        } else {
          quoteflag = 0;
          eovptr->eov_eob = eob_symbol;
        };
        eovptr->eov_str = EPDStringGrab("");
        if (quoteflag) {
          while (flag && (ch != '"')) {
            if (ch == ascii_nul)
              flag = 0;
            else {
              eovptr->eov_str =
                  EPDStringAppendChar(eovptr->eov_str, (char) ch);
              ch = *(s + i++);
            };
          };
          if (ch == '"')
            ch = *(s + i++);
        } else {
          while (flag && !isspace(ch) && (ch != ';')) {
            if (ch == ascii_nul)
              flag = 0;
            else {
              eovptr->eov_str =
                  EPDStringAppendChar(eovptr->eov_str, (char) ch);
              ch = *(s + i++);
            };
          };
        };
/* append operand onto operation */
        if (flag)
          EPDAppendEOV(eopptr, eovptr);
        else
          EPDReleaseEOV(eovptr);
/* skip whitespace */
        while (flag && (ch != ascii_nul) && isspace(ch))
          ch = *(s + i++);
      };
/* process semicolon */
      if (flag) {
        if (ch == ';')
          ch = *(s + i++);
        else
          flag = 0;
      }
/* append operation */
      if (flag)
        EPDAppendEOP(epdptr, eopptr);
      else
        EPDReleaseEOP(eopptr);
/* skip whitespace (end-of-line is not an error) */
      if (flag)
        while (flag && (ch != ascii_nul) && isspace(ch))
          ch = *(s + i++);
    };
  };
/* check for fault */
  if (!flag) {
    EPDReleaseEPD(epdptr);
    epdptr = NULL;
  };
/* normalize */
  if (epdptr != NULL)
    EPDNormalize(epdptr);
  return (epdptr);
}

/*--> EPDEncode: write an EPD structure to a string */
nonstatic charptrT EPDEncode(epdptrT epdptr) {
  charptrT ptr;
  sqT sq;
  cpT cp;
  rankT rank;
  fileT file;
  siT bi, ps, ch;
  char bv[tL];
  eopptrT eopptr;
  eovptrT eovptr;
  charptrT s0, s1;

/* this does not reference the current position */
  bi = 0;
/* normalize */
  EPDNormalize(epdptr);
/* output board */
  for (rank = rank_8; rank >= rank_1; rank--) {
    ps = 0;
    for (file = file_a; file <= file_h; file++) {
      sq = map_sq(rank, file);
      if ((sq % 2) == 0)
        cp = (epdptr->epd_nbv[sq >> 1] & nybbM);
      else
        cp = ((epdptr->epd_nbv[sq >> 1] >> nybbW) & nybbM);
      if (cp == cp_v0)
        ps++;
      else {
        if (ps != 0) {
          bv[bi++] = '0' + ps;
          ps = 0;
        };
        ch = ascpv[cv_p_cpv[cp]];
        if (cv_c_cpv[cp] == c_w)
          ch = map_upper(ch);
        else
          ch = map_lower(ch);
        bv[bi++] = ch;
      };
    };
    if (ps != 0) {
      bv[bi++] = '0' + ps;
      ps = 0;
    };
    if (rank != rank_1)
      bv[bi++] = '/';
  };
  bv[bi++] = ascii_sp;
/* output active color (lower case) */
  bv[bi++] = map_lower(asccv[epdptr->epd_actc]);
  bv[bi++] = ascii_sp;
/* output castling availablility */
  if (epdptr->epd_cast == 0)
    bv[bi++] = '-';
  else {
    if (epdptr->epd_cast & cf_wk)
      bv[bi++] = map_upper(ascpv[p_k]);
    if (epdptr->epd_cast & cf_wq)
      bv[bi++] = map_upper(ascpv[p_q]);
    if (epdptr->epd_cast & cf_bk)
      bv[bi++] = map_lower(ascpv[p_k]);
    if (epdptr->epd_cast & cf_bq)
      bv[bi++] = map_lower(ascpv[p_q]);
  };
  bv[bi++] = ascii_sp;
/* output ep capture square */
  if (epdptr->epd_epsq == sq_nil)
    bv[bi++] = '-';
  else {
    bv[bi++] = ascfv[map_file(epdptr->epd_epsq)];
    bv[bi++] = ascrv[map_rank(epdptr->epd_epsq)];
  };
/* NUL termination */
  bv[bi++] = ascii_nul;
/* allocate and copy basic result */
  ptr = EPDStringGrab(bv);
/* construct and append operations */
  eopptr = epdptr->epd_headeop;
  while (eopptr != NULL) {
/* leading space */
    s0 = EPDStringGrab(" ");
/* opcode */
    s0 = EPDStringAppendStr(s0, eopptr->eop_opsym);
/* construct and append operands */
    eovptr = eopptr->eop_headeov;
    while (eovptr != NULL) {
/* leading space */
      s1 = EPDStringGrab(" ");
/* conjure operand value */
      switch (eovptr->eov_eob) {
        case eob_string:
          s1 = EPDStringAppendChar(s1, '"');
          s1 = EPDStringAppendStr(s1, eovptr->eov_str);
          s1 = EPDStringAppendChar(s1, '"');
          break;
        case eob_symbol:
          s1 = EPDStringAppendStr(s1, eovptr->eov_str);
          break;
        default:
          EPDSwitchFault("EPDEncode");
          break;
      };
/* append */
      s0 = EPDStringAppendStr(s0, s1);
      EPDMemoryFree(s1);
/* next operand */
      eovptr = eovptr->eov_next;
    };
/* trailing semicolon */
    s0 = EPDStringAppendChar(s0, ';');
/* append operation */
    ptr = EPDStringAppendStr(ptr, s0);
    EPDMemoryFree(s0);
/* advance */
    eopptr = eopptr->eop_next;
  };
  return (ptr);
}

/*--> EPDRealize: set the current position according to EPD */
nonstatic void EPDRealize(epdptrT epdptr) {
  sqT sq;
  cpT cp;
  eopptrT eopptr;
  eovptrT eovptr;

/* this changes the current position */
  for (sq = sq_a1; sq <= sq_h8; sq++) {
    if ((sq % 2) == 0)
      cp = (epdptr->epd_nbv[sq >> 1] & nybbM);
    else
      cp = ((epdptr->epd_nbv[sq >> 1] >> nybbW) & nybbM);
    EPDboard.rbv[sq] = cp;
  };
  ese.ese_actc = epdptr->epd_actc;
  ese.ese_cast = epdptr->epd_cast;
  ese.ese_epsq = epdptr->epd_epsq;
  eopptr = EPDLocateEOPCode(epdptr, epdso_hmvc);
  if ((eopptr != NULL) && ((eovptr = eopptr->eop_headeov) != NULL))
    ese.ese_hmvc = atoi(eovptr->eov_str);
  else
    ese.ese_hmvc = 0;
  eopptr = EPDLocateEOPCode(epdptr, epdso_fmvn);
  if ((eopptr != NULL) && ((eovptr = eopptr->eop_headeov) != NULL))
    ese.ese_fmvn = atoi(eovptr->eov_str);
  else
    ese.ese_fmvn = 1;
  EPDSetKings();
  return;
}

/*--> EPDInitArray: set the current position to the initial array */
nonstatic void EPDInitArray(void) {
  sqT sq;

/* this changes the current position */
  for (sq = sq_a1; sq <= sq_h8; sq++)
    EPDboard.rbv[sq] = cp_v0;
  EPDboard.rbv[sq_a1] = EPDboard.rbv[sq_h1] = cp_wr;
  EPDboard.rbv[sq_b1] = EPDboard.rbv[sq_g1] = cp_wn;
  EPDboard.rbv[sq_c1] = EPDboard.rbv[sq_f1] = cp_wb;
  EPDboard.rbv[sq_d1] = cp_wq;
  EPDboard.rbv[sq_e1] = cp_wk;
  for (sq = sq_a2; sq <= sq_h2; sq++)
    EPDboard.rbv[sq] = cp_wp;
  EPDboard.rbv[sq_a8] = EPDboard.rbv[sq_h8] = cp_br;
  EPDboard.rbv[sq_b8] = EPDboard.rbv[sq_g8] = cp_bn;
  EPDboard.rbv[sq_c8] = EPDboard.rbv[sq_f8] = cp_bb;
  EPDboard.rbv[sq_d8] = cp_bq;
  EPDboard.rbv[sq_e8] = cp_bk;
  for (sq = sq_a7; sq <= sq_h7; sq++)
    EPDboard.rbv[sq] = cp_bp;
  ese.ese_actc = c_w;
  ese.ese_cast = cf_wk | cf_wq | cf_bk | cf_bq;
  ese.ese_epsq = sq_nil;
  ese.ese_hmvc = 0;
  ese.ese_fmvn = 1;
  EPDSetKings();
  return;
}

/*--> EPDSANEncodeChar: encode SAN character */
static
void EPDSANEncodeChar(char ch) {
  if ((lsani < (sanL - 1)) || ((ch == '\0') && (lsani < sanL)))
    lsan[lsani++] = ch;
  else
    EPDFatal("EPDSANEncodeChar: overflow");
  return;
}

/*--> EPDSANEncodeStr: encode a SAN string */
static
void EPDSANEncodeStr(charptrT s) {
  charptrT p;

  p = s;
  while (*p)
    EPDSANEncodeChar(*p++);
  return;
}

/*--> EPDSANEncodeFile: encode SAN file from square */
static
void EPDSANEncodeFile(sqT sq) {
  EPDSANEncodeChar(ascfv[map_file(sq)]);
  return;
}

/*--> EPDSANEncodeRank: encode SAN rank from square */
static
void EPDSANEncodeRank(sqT sq) {
  EPDSANEncodeChar(ascrv[map_rank(sq)]);
  return;
}

/*--> EPDSANEncodeSq: encode SAN square */
static
void EPDSANEncodeSq(sqT sq) {
  EPDSANEncodeFile(sq);
  EPDSANEncodeRank(sq);
  return;
}

/*--> EPDSANEncodeCI: encode an appropriate capture indicator */
static
void EPDSANEncodeCI(siT index) {
  switch (index) {
    case 0:
      EPDSANEncodeChar('x');
      break;
    case 1:
      break;
    case 2:
      EPDSANEncodeChar(':');
      break;
    case 3:
      EPDSANEncodeChar('*');
      break;
    case 4:
      EPDSANEncodeChar('-');
      break;
  };
  return;
}

/*--> EPDSANEncodeAux: encode SAN format move with variants */
static
void EPDSANEncodeAux(mptrT mptr, sanT san, ssavT ssav) {
  siT i;

/* reset local index */
  lsani = 0;
/* busted? */
  if (mptr->m_flag & mf_bust)
    EPDSANEncodeChar('*');
/* process according to moving piece */
  switch (cv_p_cpv[mptr->m_frcp]) {
    case p_p:
      switch (mptr->m_scmv) {
        case scmv_reg:
          if (mptr->m_tocp != cp_v0) {
            EPDSANEncodeFile(mptr->m_frsq);
            if (ssav[ssa_edcr] == 1)
              EPDSANEncodeRank(mptr->m_frsq);
            EPDSANEncodeCI(ssav[ssa_capt]);
            if (ssav[ssa_ptar] == 0)
              EPDSANEncodeSq(mptr->m_tosq);
            else
              EPDSANEncodeFile(mptr->m_tosq);
          } else {
            EPDSANEncodeFile(mptr->m_frsq);
            if (ssav[ssa_edcr] == 1)
              EPDSANEncodeRank(mptr->m_frsq);
            if (ssav[ssa_move] == 1)
              EPDSANEncodeChar('-');
            if (ssav[ssa_edcf] == 1)
              EPDSANEncodeFile(mptr->m_tosq);
            EPDSANEncodeRank(mptr->m_tosq);
          };
          break;
        case scmv_epc:
          EPDSANEncodeFile(mptr->m_frsq);
          if (ssav[ssa_edcr] == 1)
            EPDSANEncodeRank(mptr->m_frsq);
          EPDSANEncodeCI(ssav[ssa_capt]);
          if (ssav[ssa_ptar] == 0)
            EPDSANEncodeSq(mptr->m_tosq);
          else
            EPDSANEncodeFile(mptr->m_tosq);
          if (ssav[ssa_epct] == 1)
            EPDSANEncodeStr("ep");
          break;
        case scmv_ppn:
        case scmv_ppb:
        case scmv_ppr:
        case scmv_ppq:
          if (mptr->m_tocp != cp_v0) {
            EPDSANEncodeFile(mptr->m_frsq);
            if (ssav[ssa_edcr] == 1)
              EPDSANEncodeRank(mptr->m_frsq);
            EPDSANEncodeCI(ssav[ssa_capt]);
            if (ssav[ssa_ptar] == 0)
              EPDSANEncodeSq(mptr->m_tosq);
            else
              EPDSANEncodeFile(mptr->m_tosq);
          } else {
            EPDSANEncodeFile(mptr->m_frsq);
            if (ssav[ssa_edcr] == 1)
              EPDSANEncodeRank(mptr->m_frsq);
            if (ssav[ssa_move] == 1)
              EPDSANEncodeChar('-');
            if (ssav[ssa_edcf] == 1)
              EPDSANEncodeFile(mptr->m_tosq);
            EPDSANEncodeRank(mptr->m_tosq);
          };
          switch (ssav[ssa_prom]) {
            case 0:
              EPDSANEncodeChar('=');
              EPDSANEncodeChar(ascpv[cv_p_scmvv[mptr->m_scmv]]);
              break;
            case 1:
              EPDSANEncodeChar(ascpv[cv_p_scmvv[mptr->m_scmv]]);
              break;
            case 2:
              EPDSANEncodeChar('/');
              EPDSANEncodeChar(ascpv[cv_p_scmvv[mptr->m_scmv]]);
              break;
            case 3:
              EPDSANEncodeChar('(');
              EPDSANEncodeChar(ascpv[cv_p_scmvv[mptr->m_scmv]]);
              EPDSANEncodeChar(')');
              break;
          };
          break;
      };
      break;
    case p_n:
    case p_b:
    case p_r:
    case p_q:
      EPDSANEncodeChar(ascpv[cv_p_cpv[mptr->m_frcp]]);
      if (((mptr->m_flag & mf_sanf) || (ssav[ssa_edcf] == 1)) ||
          ((mptr->m_flag & mf_sanr) && (ssav[ssa_edcf] == 2)))
        EPDSANEncodeFile(mptr->m_frsq);
      if (((mptr->m_flag & mf_sanr) || (ssav[ssa_edcr] == 1)) ||
          ((mptr->m_flag & mf_sanf) && (ssav[ssa_edcr] == 2)))
        EPDSANEncodeRank(mptr->m_frsq);
      if (mptr->m_tocp != cp_v0)
        EPDSANEncodeCI(ssav[ssa_capt]);
      else if (ssav[ssa_move] == 1)
        EPDSANEncodeChar('-');
      EPDSANEncodeSq(mptr->m_tosq);
      break;
    case p_k:
      switch (mptr->m_scmv) {
        case scmv_reg:
          EPDSANEncodeChar(ascpv[p_k]);
          if (ssav[ssa_edcf] == 1)
            EPDSANEncodeFile(mptr->m_frsq);
          if (ssav[ssa_edcr] == 1)
            EPDSANEncodeRank(mptr->m_frsq);
          if (mptr->m_tocp != cp_v0)
            EPDSANEncodeCI(ssav[ssa_capt]);
          else if (ssav[ssa_move] == 1)
            EPDSANEncodeChar('-');
          EPDSANEncodeSq(mptr->m_tosq);
          break;
        case scmv_cks:
          switch (ssav[ssa_cast]) {
            case 0:
              EPDSANEncodeStr("O-O");
              break;
            case 1:
              EPDSANEncodeStr("0-0");
              break;
            case 2:
              EPDSANEncodeStr("OO");
              break;
            case 3:
              EPDSANEncodeStr("00");
              break;
            case 4:
              EPDSANEncodeChar(ascpv[p_k]);
              if (ssav[ssa_edcf] == 1)
                EPDSANEncodeFile(mptr->m_frsq);
              if (ssav[ssa_edcr] == 1)
                EPDSANEncodeRank(mptr->m_frsq);
              if (ssav[ssa_move] == 1)
                EPDSANEncodeChar('-');
              EPDSANEncodeSq(mptr->m_tosq);
              break;
          };
          break;
        case scmv_cqs:
          switch (ssav[ssa_cast]) {
            case 0:
              EPDSANEncodeStr("O-O-O");
              break;
            case 1:
              EPDSANEncodeStr("0-0-0");
              break;
            case 2:
              EPDSANEncodeStr("OOO");
              break;
            case 3:
              EPDSANEncodeStr("000");
              break;
            case 4:
              EPDSANEncodeChar(ascpv[p_k]);
              if (ssav[ssa_edcf] == 1)
                EPDSANEncodeFile(mptr->m_frsq);
              if (ssav[ssa_edcr] == 1)
                EPDSANEncodeRank(mptr->m_frsq);
              if (ssav[ssa_move] == 1)
                EPDSANEncodeChar('-');
              EPDSANEncodeSq(mptr->m_tosq);
              break;
          };
          break;
      };
      break;
  };
/* insert markers */
  if ((mptr->m_flag & mf_chec) && !(mptr->m_flag & mf_chmt))
    switch (ssav[ssa_chec]) {
      case 0:
        EPDSANEncodeChar('+');
        break;
      case 1:
        break;
      case 2:
        EPDSANEncodeStr("ch");
        break;
    };
  if (mptr->m_flag & mf_chmt)
    switch (ssav[ssa_chmt]) {
      case 0:
        EPDSANEncodeChar('#');
        break;
      case 1:
        break;
      case 2:
        EPDSANEncodeChar('+');
        break;
      case 3:
        EPDSANEncodeStr("++");
        break;
    };
  if (mptr->m_flag & mf_draw)
    if (ssav[ssa_draw] == 1)
      EPDSANEncodeChar('=');
/* map to lower case if indicated */
  if (ssav[ssa_case] == 1)
    for (i = 0; i < lsani; i++)
      lsan[i] = map_lower(lsan[i]);
/* pad and copy */
  while (lsani < sanL)
    EPDSANEncodeChar('\0');
  for (i = 0; i < sanL; i++)
    san[i] = lsan[i];
  return;
}

/*--> EPDSANEncode: encode a move into a SAN string */
nonstatic void EPDSANEncode(mptrT mptr, sanT san) {
  ssaT ssa;
  ssavT ssav;

/* select canonical encoding (zero point in variant space) */
  for (ssa = 0; ssa < ssaL; ssa++)
    ssav[ssa] = 0;
  EPDSANEncodeAux(mptr, san, ssav);
  return;
}

/*--> EPDSANDecodeBump: increment a style vector and return overflow */
static siT EPDSANDecodeBump(ssavT ssav, ssavT bssav) {
  siT flag;
  ssaT ssa;

  flag = 1;
  ssa = 0;
  while (flag && (ssa < ssaL)) {
    flag = 0;
    ssav[ssa]++;
    if (ssav[ssa] == bssav[ssa]) {
      flag = 1;
      ssav[ssa] = 0;
    };
    ssa++;
  };
  return (flag);
}

/*--> EPDSANDecodeFlex: locate a move from SAN (flexible interpretation) */
static mptrT EPDSANDecodeFlex(sanT san) {
  mptrT mptr;
  ssavT ssav, bssav;
  siT i, flag;
  mptrT rmptr;
  sanT lcsan, rsan;

/* set default return value */
  mptr = NULL;
/* set minimal upper bounds */
  for (i = 0; i < ssaL; i++)
    bssav[i] = 1;
/* scan for upper bound conditions */
  rmptr = tse.tse_base;
  for (i = 0; i < tse.tse_count; i++) {
/* letter case */
    bssav[ssa_case] = 2;
/* capturing */
    if ((rmptr->m_tocp != cp_v0) || (rmptr->m_scmv == scmv_epc))
      bssav[ssa_capt] = 5;
/* checking */
    if (rmptr->m_flag & mf_chec)
      bssav[ssa_chec] = 3;
/* castling */
    if ((rmptr->m_scmv == scmv_cks) || (rmptr->m_scmv == scmv_cqs))
      bssav[ssa_cast] = 5;
/* promoting */
    if ((rmptr->m_scmv == scmv_ppn) || (rmptr->m_scmv == scmv_ppb) ||
        (rmptr->m_scmv == scmv_ppr) || (rmptr->m_scmv == scmv_ppq))
      bssav[ssa_prom] = 4;
/* pawn destination target */
    if (cv_p_cpv[rmptr->m_frcp] == p_p)
      bssav[ssa_ptar] = 2;
/* checkmating */
    if (rmptr->m_flag & mf_chmt)
      bssav[ssa_chmt] = 4;
/* en passant capturing */
    if (rmptr->m_scmv == scmv_epc)
      bssav[ssa_epct] = 2;
/* drawing */
    if (rmptr->m_flag & mf_draw)
      bssav[ssa_draw] = 2;
/* moving (non-capturing) */
    if ((rmptr->m_tocp == cp_v0) && (rmptr->m_scmv != scmv_epc))
      bssav[ssa_move] = 2;
/* extra disambiguation: file */
    if (!(rmptr->m_flag & mf_sanf))
      bssav[ssa_edcf] = 3;
/* extra disambiguation: rank */
    if (!(rmptr->m_flag & mf_sanr))
      bssav[ssa_edcr] = 3;
    rmptr++;
  };
/* make a lower case copy of the input */
  for (i = 0; i < sanL; i++)
    lcsan[i] = map_lower(san[i]);
/* initialize the index style vector */
  for (i = 0; i < ssaL; i++)
    ssav[i] = 0;
/* search */
  flag = 0;
  while (!flag && (mptr == NULL)) {
    rmptr = tse.tse_base;
    i = 0;
/* scan candidate moves */
    while ((mptr == NULL) && (i < tse.tse_count)) {
/* encode the current style version of a candidate */
      EPDSANEncodeAux(rmptr, rsan, ssav);
/* select either original or lower case comparison */
      if (ssav[ssa_case] == 0) {
        if (strcmp(san, rsan) == 0)
          mptr = rmptr;
      } else {
        if (strcmp(lcsan, rsan) == 0)
          mptr = rmptr;
      };
/* next candidate */
      rmptr++;
      i++;
    };
/* update the overflow termination flag */
    flag = EPDSANDecodeBump(ssav, bssav);
  };
  return (mptr);
}

/*--> EPDSANDecode: locate a move from SAN (strict interpretation) */
static mptrT EPDSANDecode(sanT san) {
  mptrT mptr;
  mptrT rmptr;
  sanT rsan;
  siT i;

/* set default return value */
  mptr = NULL;
/* assume current moveset properly generated */
  rmptr = tse.tse_base;
  i = 0;
/* search */
  while ((mptr == NULL) && (i < tse.tse_count)) {
    EPDSANEncode(rmptr, rsan);
    if (strcmp(san, rsan) == 0)
      mptr = rmptr;
    else {
      rmptr++;
      i++;
    };
  };
  return (mptr);
}

/*--> EPDSANDecodeAux: locate a move from SAN */
nonstatic mptrT EPDSANDecodeAux(sanT san, siT strict) {
  mptrT mptr;

  if (strict)
    mptr = EPDSANDecode(san);
  else
    mptr = EPDSANDecodeFlex(san);
  return (mptr);
}

/*--> EPDAttack: determine if a color attacks a square */
static siT EPDAttack(cT c, sqT sq) {
  siT flag;
  dxT dx;
  dvT dv;
  xdvT xdv;
  sqptrT sqptr0, sqptr1;
  xsqptrT xsqptr0, xsqptr1;

/* clear result */
  flag = 0;
/* set origin square pointers  */
  sqptr0 = &EPDboard.rbv[sq];
  xsqptr0 = &xb.xbv[map_xsq_sq(sq)];
/* process according to specified color */
  if (c == c_w) {
/* pawn attacks */
    if ((*(xsqptr0 + xdv_7) == cp_v0) && (*(sqptr0 + dv_7) == cp_wp))
      flag = 1;
    else if ((*(xsqptr0 + xdv_6) == cp_v0) && (*(sqptr0 + dv_6) == cp_wp))
      flag = 1;
/* knight attacks */
    if (!flag) {
      dx = dx_8;
      while (!flag && (dx <= dx_f))
        if ((*(xsqptr0 + xdvv[dx]) == cp_v0) &&
            (*(sqptr0 + dvv[dx]) == cp_wn))
          flag = 1;
        else
          dx++;
    };
/* orthogonal sweeps */
    if (!flag) {
      dx = dx_0;
      while (!flag && (dx <= dx_3)) {
        dv = dvv[dx];
        xdv = xdvv[dx];
        sqptr1 = sqptr0;
        xsqptr1 = xsqptr0;
        while ((*(xsqptr1 += xdv) == cp_v0) && (*(sqptr1 += dv) == cp_v0));
        if ((*xsqptr1 == cp_v0) && ((*sqptr1 == cp_wq) || (*sqptr1 == cp_wr)))
          flag = 1;
        else
          dx++;
      };
    };
/* diagonal sweeps */
    if (!flag) {
      dx = dx_4;
      while (!flag && (dx <= dx_7)) {
        dv = dvv[dx];
        xdv = xdvv[dx];
        sqptr1 = sqptr0;
        xsqptr1 = xsqptr0;
        while ((*(xsqptr1 += xdv) == cp_v0) && (*(sqptr1 += dv) == cp_v0));
        if ((*xsqptr1 == cp_v0) && ((*sqptr1 == cp_wq) || (*sqptr1 == cp_wb)))
          flag = 1;
        else
          dx++;
      };
    };
/* king attacks */
    if (!flag) {
      dx = dx_0;
      while (!flag && (dx <= dx_7))
        if ((*(xsqptr0 + xdvv[dx]) == cp_v0) &&
            (*(sqptr0 + dvv[dx]) == cp_wk))
          flag = 1;
        else
          dx++;
    };
  } else {
/* pawn attacks */
    if ((*(xsqptr0 + xdv_4) == cp_v0) && (*(sqptr0 + dv_4) == cp_bp))
      flag = 1;
    else if ((*(xsqptr0 + xdv_5) == cp_v0) && (*(sqptr0 + dv_5) == cp_bp))
      flag = 1;
/* knight attacks */
    if (!flag) {
      dx = dx_8;
      while (!flag && (dx <= dx_f))
        if ((*(xsqptr0 + xdvv[dx]) == cp_v0) &&
            (*(sqptr0 + dvv[dx]) == cp_bn))
          flag = 1;
        else
          dx++;
    };
/* orthogonal sweeps */
    if (!flag) {
      dx = dx_0;
      while (!flag && (dx <= dx_3)) {
        dv = dvv[dx];
        xdv = xdvv[dx];
        sqptr1 = sqptr0;
        xsqptr1 = xsqptr0;
        while ((*(xsqptr1 += xdv) == cp_v0) && (*(sqptr1 += dv) == cp_v0));
        if ((*xsqptr1 == cp_v0) && ((*sqptr1 == cp_bq) || (*sqptr1 == cp_br)))
          flag = 1;
        else
          dx++;
      };
    };
/* diagonal sweeps */
    if (!flag) {
      dx = dx_4;
      while (!flag && (dx <= dx_7)) {
        dv = dvv[dx];
        xdv = xdvv[dx];
        sqptr1 = sqptr0;
        xsqptr1 = xsqptr0;
        while ((*(xsqptr1 += xdv) == cp_v0) && (*(sqptr1 += dv) == cp_v0));
        if ((*xsqptr1 == cp_v0) && ((*sqptr1 == cp_bq) || (*sqptr1 == cp_bb)))
          flag = 1;
        else
          dx++;
      };
    };
/* king attacks */
    if (!flag) {
      dx = dx_0;
      while (!flag && (dx <= dx_7))
        if ((*(xsqptr0 + xdvv[dx]) == cp_v0) &&
            (*(sqptr0 + dvv[dx]) == cp_bk))
          flag = 1;
        else
          dx++;
    };
  };
  return (flag);
}

/*--> EPDWhiteAttacks: check if White attacks a square */
static siT EPDWhiteAttacks(sqT sq) {
  return (EPDAttack(c_w, sq));
}

/*--> EPDBlackAttacks: check if White attacks a square */
static siT EPDBlackAttacks(sqT sq) {
  return (EPDAttack(c_b, sq));
}

/*--> EPDTestAKIC: test for active king in check */
static siT EPDTestAKIC(void) {
  siT flag;

  if (ese.ese_actc == c_w)
    flag = EPDBlackAttacks(ese.ese_ksqv[c_w]);
  else
    flag = EPDWhiteAttacks(ese.ese_ksqv[c_b]);
  return (flag);
}

/*--> EPDTestPKIC: test for passive king in check */
static siT EPDTestPKIC(void) {
  siT flag;

  if (ese.ese_actc == c_b)
    flag = EPDBlackAttacks(ese.ese_ksqv[c_w]);
  else
    flag = EPDWhiteAttacks(ese.ese_ksqv[c_b]);
  return (flag);
}

/*--> EPDCensus: calculate local census vectors */
static
void EPDCensus(void) {
  cT c;
  pT p;
  sqT sq;
  cpT cp;

/* clear census vectors */
  for (c = c_w; c <= c_b; c++) {
    count_cv[c] = 0;
    for (p = p_p; p <= p_k; p++)
      count_cpv[c][p] = 0;
  };
/* calculate census vectors */
  for (sq = sq_a1; sq <= sq_h8; sq++) {
    cp = EPDboard.rbv[sq];
    if (cp != cp_v0) {
      c = cv_c_cpv[cp];
      count_cv[c]++;
      count_cpv[c][cv_p_cpv[cp]]++;
    };
  };
  return;
}

/*--> EPDIsLegal: determine if current position is legal */
nonstatic siT EPDIsLegal(void) {
  siT flag;
  cT c;
  fileT file;
  siT apv[rcL];

/* set default return value: legal position */
  flag = 1;
/* calculate the local census vectors */
  EPDCensus();
/* calculate available promoted pawns */
  for (c = c_w; c <= c_b; c++)
    apv[c] = fileL - count_cpv[c][p_p];
/* check white pawn count */
  if (flag && (count_cpv[c_w][p_p] > fileL))
    flag = 0;
/* check black pawn count */
  if (flag && (count_cpv[c_b][p_p] > fileL))
    flag = 0;
/* check white knight count */
  if (flag && (count_cpv[c_w][p_n] > 2)) {
    apv[c_w] -= (count_cpv[c_w][p_n] - 2);
    if (apv[c_w] < 0)
      flag = 0;
  };
/* check black knight count */
  if (flag && (count_cpv[c_b][p_n] > 2)) {
    apv[c_b] -= (count_cpv[c_b][p_n] - 2);
    if (apv[c_b] < 0)
      flag = 0;
  };
/* check white bishop count */
  if (flag && (count_cpv[c_w][p_b] > 2)) {
    apv[c_w] -= (count_cpv[c_w][p_b] - 2);
    if (apv[c_w] < 0)
      flag = 0;
  };
/* check black bishop count */
  if (flag && (count_cpv[c_b][p_b] > 2)) {
    apv[c_b] -= (count_cpv[c_b][p_b] - 2);
    if (apv[c_b] < 0)
      flag = 0;
  };
/* check white rook count */
  if (flag && (count_cpv[c_w][p_r] > 2)) {
    apv[c_w] -= (count_cpv[c_w][p_r] - 2);
    if (apv[c_w] < 0)
      flag = 0;
  };
/* check black rook count */
  if (flag && (count_cpv[c_b][p_r] > 2)) {
    apv[c_b] -= (count_cpv[c_b][p_r] - 2);
    if (apv[c_b] < 0)
      flag = 0;
  };
/* check white queen count */
  if (flag && (count_cpv[c_w][p_q] > 1)) {
    apv[c_w] -= (count_cpv[c_w][p_q] - 1);
    if (apv[c_w] < 0)
      flag = 0;
  };
/* check black queen count */
  if (flag && (count_cpv[c_b][p_q] > 1)) {
    apv[c_b] -= (count_cpv[c_b][p_q] - 1);
    if (apv[c_b] < 0)
      flag = 0;
  };
/* check white king count */
  if (flag && (count_cpv[c_w][p_k] != 1))
    flag = 0;
/* check black king count */
  if (flag && (count_cpv[c_b][p_k] != 1))
    flag = 0;
/* check pawn placement */
  if (flag) {
    file = file_a;
    while (flag && (file <= file_h))
      if ((EPDboard.rbm[rank_1][file] == cp_wp) ||
          (EPDboard.rbm[rank_1][file] == cp_bp) ||
          (EPDboard.rbm[rank_8][file] == cp_wp) ||
          (EPDboard.rbm[rank_8][file] == cp_bp))
        flag = 0;
      else
        file++;
  };
/* check white kingside castling availability */
  if (flag && (ese.ese_cast & cf_wk))
    if ((EPDboard.rbv[sq_e1] != cp_wk) || (EPDboard.rbv[sq_h1] != cp_wr))
      flag = 0;
/* check white queenside castling availability */
  if (flag && (ese.ese_cast & cf_wq))
    if ((EPDboard.rbv[sq_e1] != cp_wk) || (EPDboard.rbv[sq_a1] != cp_wr))
      flag = 0;
/* check black kingside castling availability */
  if (flag && (ese.ese_cast & cf_bk))
    if ((EPDboard.rbv[sq_e8] != cp_bk) || (EPDboard.rbv[sq_h8] != cp_br))
      flag = 0;
/* check black queenside castling availability */
  if (flag && (ese.ese_cast & cf_bq))
    if ((EPDboard.rbv[sq_e8] != cp_bk) || (EPDboard.rbv[sq_a8] != cp_br))
      flag = 0;
/* check en passant target square */
  if (flag && (ese.ese_epsq != sq_nil)) {
    if (ese.ese_actc == c_w) {
      if (map_rank(ese.ese_epsq) != rank_6)
        flag = 0;
      else if (EPDboard.rbv[ese.ese_epsq + dv_3] != cp_bp)
        flag = 0;
      else if (EPDboard.rbv[ese.ese_epsq] != cp_v0)
        flag = 0;
      else if (EPDboard.rbv[ese.ese_epsq + dv_1] != cp_v0)
        flag = 0;
    } else {
      if (map_rank(ese.ese_epsq) != rank_3)
        flag = 0;
      else if (EPDboard.rbv[ese.ese_epsq + dv_1] != cp_wp)
        flag = 0;
      else if (EPDboard.rbv[ese.ese_epsq] != cp_v0)
        flag = 0;
      else if (EPDboard.rbv[ese.ese_epsq + dv_3] != cp_v0)
        flag = 0;
    }
  }
/* check for passive king in check */
  if (flag && EPDTestPKIC())
    flag = 0;
  return (flag);
}

/*--> EPDGeneratePL: generate psuedolegal moves */
static
void EPDGeneratePL(void) {
  dxT dx;
  dvT dv;
  xdvT xdv;
  xsqptrT xsqptr0, xsqptr1;
  fileT frfile;
  rankT frrank;
  mT gen_m;

/* set up the generation base */
  if (ply == 0)
    treeptr = tse.tse_base = treebaseptr;
  else
    treeptr = tse.tse_base = (tseptr - 1)->tse_base + (tseptr - 1)->tse_count;
/* test against safety margin */
  if ((treeptr - treebaseptr) >= (treeL - treemarginL))
    EPDFatal("EPDGeneratePL: move tree size safety limit exceeded");
/* set up current generation items */
  tse.tse_curr = treeptr;
  tse.tse_count = 0;
/* set the psuedoinvariant generated move template components */
  gen_m.m_scmv = scmv_reg;
  gen_m.m_flag = 0;
/* look at each origin square of the active color */
  for (gen_m.m_frsq = sq_a1; gen_m.m_frsq <= sq_h8; gen_m.m_frsq++) {
/* get origin square and moving piece */
    gen_m.m_frcp = EPDboard.rbv[gen_m.m_frsq];
/* continue if it is an active piece */
    if (cv_c_cpv[gen_m.m_frcp] == ese.ese_actc) {
/* generate moves for active color piece */
      xsqptr0 = &xb.xbv[map_xsq_sq(gen_m.m_frsq)];
      switch (cv_p_cpv[gen_m.m_frcp]) {
        case p_p:
/* pawn moves: a bit tricky; colors done separately */
          frfile = map_file(gen_m.m_frsq);
          frrank = map_rank(gen_m.m_frsq);
          if (ese.ese_actc == c_w) {
/* one square non-capture */
            gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq = gen_m.m_frsq + dv_1];
            if (gen_m.m_tocp == cp_v0) {
              if (frrank != rank_7) {
/* non-promotion */
                *treeptr++ = gen_m;
                tse.tse_count++;
              } else {
/* promotion */
                for (gen_m.m_scmv = scmv_ppn; gen_m.m_scmv <= scmv_ppq;
                    gen_m.m_scmv++) {
                  *treeptr++ = gen_m;
                  tse.tse_count++;
                }
                gen_m.m_scmv = scmv_reg;
              };
            }
/* two squares forward */
            if ((frrank == rank_2) && Vacant(gen_m.m_frsq + dv_1) &&
                Vacant(gen_m.m_frsq + (2 * dv_1))) {
              gen_m.m_tosq = gen_m.m_frsq + (2 * dv_1);
              gen_m.m_tocp = cp_v0;
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
/* capture to left */
            if (frfile != file_a) {
              gen_m.m_tosq = gen_m.m_frsq + dv_5;
              gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq];
              if (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc]) {
                if (frrank != rank_7) {
/* non-promote */
                  *treeptr++ = gen_m;
                  tse.tse_count++;
                } else {
/* promote */
                  for (gen_m.m_scmv = scmv_ppn; gen_m.m_scmv <= scmv_ppq;
                      gen_m.m_scmv++) {
                    *treeptr++ = gen_m;
                    tse.tse_count++;
                  };
                  gen_m.m_scmv = scmv_reg;
                };
              }
            };
/* capture to right */
            if (frfile != file_h) {
              gen_m.m_tosq = gen_m.m_frsq + dv_4;
              gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq];
              if (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc]) {
                if (frrank != rank_7) {
/* non-promote */
                  *treeptr++ = gen_m;
                  tse.tse_count++;
                } else {
/* promote */
                  for (gen_m.m_scmv = scmv_ppn; gen_m.m_scmv <= scmv_ppq;
                      gen_m.m_scmv++) {
                    *treeptr++ = gen_m;
                    tse.tse_count++;
                  };
                  gen_m.m_scmv = scmv_reg;
                };
              }
            };
/* en passant */
            if ((frrank == rank_5) && (ese.ese_epsq != sq_nil)) {
/* capture to left */
              if ((frfile != file_a) &&
                  ((gen_m.m_tosq = gen_m.m_frsq + dv_5) == ese.ese_epsq)) {
                gen_m.m_tocp = cp_v0;
                gen_m.m_scmv = scmv_epc;
                *treeptr++ = gen_m;
                tse.tse_count++;
                gen_m.m_scmv = scmv_reg;
              };
/* capture to right */
              if ((frfile != file_h) &&
                  ((gen_m.m_tosq = gen_m.m_frsq + dv_4) == ese.ese_epsq)) {
                gen_m.m_tocp = cp_v0;
                gen_m.m_scmv = scmv_epc;
                *treeptr++ = gen_m;
                tse.tse_count++;
                gen_m.m_scmv = scmv_reg;
              };
            };
          } else {
/* one square non-capture */
            gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq = gen_m.m_frsq + dv_3];
            if (gen_m.m_tocp == cp_v0) {
              if (frrank != rank_2) {
/* non-promotion */
                *treeptr++ = gen_m;
                tse.tse_count++;
              } else {
/* promotion */
                for (gen_m.m_scmv = scmv_ppn; gen_m.m_scmv <= scmv_ppq;
                    gen_m.m_scmv++) {
                  *treeptr++ = gen_m;
                  tse.tse_count++;
                }
                gen_m.m_scmv = scmv_reg;
              };
            }
/* two squares forward */
            if ((frrank == rank_7) && Vacant(gen_m.m_frsq + dv_3) &&
                Vacant(gen_m.m_frsq + (2 * dv_3))) {
              gen_m.m_tosq = gen_m.m_frsq + (2 * dv_3);
              gen_m.m_tocp = cp_v0;
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
/* capture to left */
            if (frfile != file_a) {
              gen_m.m_tosq = gen_m.m_frsq + dv_6;
              gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq];
              if (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc]) {
                if (frrank != rank_2) {
/* non-promote */
                  *treeptr++ = gen_m;
                  tse.tse_count++;
                } else {
/* promote */
                  for (gen_m.m_scmv = scmv_ppn; gen_m.m_scmv <= scmv_ppq;
                      gen_m.m_scmv++) {
                    *treeptr++ = gen_m;
                    tse.tse_count++;
                  };
                  gen_m.m_scmv = scmv_reg;
                };
              }
            };
/* capture to right */
            if (frfile != file_h) {
              gen_m.m_tosq = gen_m.m_frsq + dv_7;
              gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq];
              if (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc]) {
                if (frrank != rank_2) {
/* non-promote */
                  *treeptr++ = gen_m;
                  tse.tse_count++;
                } else {
/* promote */
                  for (gen_m.m_scmv = scmv_ppn; gen_m.m_scmv <= scmv_ppq;
                      gen_m.m_scmv++) {
                    *treeptr++ = gen_m;
                    tse.tse_count++;
                  };
                  gen_m.m_scmv = scmv_reg;
                };
              }
            };
/* en passant */
            if ((frrank == rank_4) && (ese.ese_epsq != sq_nil)) {
/* capture to left */
              if ((frfile != file_a) &&
                  ((gen_m.m_tosq = gen_m.m_frsq + dv_6) == ese.ese_epsq)) {
                gen_m.m_tocp = cp_v0;
                gen_m.m_scmv = scmv_epc;
                *treeptr++ = gen_m;
                tse.tse_count++;
                gen_m.m_scmv = scmv_reg;
              };
/* capture to right */
              if ((frfile != file_h) &&
                  ((gen_m.m_tosq = gen_m.m_frsq + dv_7) == ese.ese_epsq)) {
                gen_m.m_tocp = cp_v0;
                gen_m.m_scmv = scmv_epc;
                *treeptr++ = gen_m;
                tse.tse_count++;
                gen_m.m_scmv = scmv_reg;
              };
            };
          };
          break;
        case p_n:
/* knight moves: very simple */
          for (dx = dx_8; dx <= dx_f; dx++)
            if (*(xsqptr0 + xdvv[dx]) == cp_v0) {
              gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq =
                  gen_m.m_frsq + dvv[dx]];
              if (cv_c_cpv[gen_m.m_tocp] != ese.ese_actc) {
                *treeptr++ = gen_m;
                tse.tse_count++;
              };
            };
          break;
        case p_b:
/* bishop moves: diagonal sweeper */
          for (dx = dx_4; dx <= dx_7; dx++) {
            dv = dvv[dx];
            xdv = xdvv[dx];
            gen_m.m_tosq = gen_m.m_frsq;
            xsqptr1 = xsqptr0;
            while ((*(xsqptr1 += xdv) == cp_v0) &&
                ((gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq +=
                            dv]) == cp_v0)) {
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
            if ((*xsqptr1 == cp_v0) &&
                (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc])) {
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
          };
          break;
        case p_r:
/* rook moves: orthogonal sweeper */
          for (dx = dx_0; dx <= dx_3; dx++) {
            dv = dvv[dx];
            xdv = xdvv[dx];
            gen_m.m_tosq = gen_m.m_frsq;
            xsqptr1 = xsqptr0;
            while ((*(xsqptr1 += xdv) == cp_v0) &&
                ((gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq +=
                            dv]) == cp_v0)) {
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
            if ((*xsqptr1 == cp_v0) &&
                (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc])) {
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
          };
          break;
        case p_q:
/* queen moves: orthogonal and diagonal sweeper */
          for (dx = dx_0; dx <= dx_7; dx++) {
            dv = dvv[dx];
            xdv = xdvv[dx];
            gen_m.m_tosq = gen_m.m_frsq;
            xsqptr1 = xsqptr0;
            while ((*(xsqptr1 += xdv) == cp_v0) &&
                ((gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq +=
                            dv]) == cp_v0)) {
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
            if ((*xsqptr1 == cp_v0) &&
                (cv_c_cpv[gen_m.m_tocp] == inv_cv[ese.ese_actc])) {
              *treeptr++ = gen_m;
              tse.tse_count++;
            };
          };
          break;
        case p_k:
/* king moves: one square adjacent regular */
          for (dx = dx_0; dx <= dx_7; dx++)
            if (*(xsqptr0 + xdvv[dx]) == cp_v0) {
              gen_m.m_tocp = EPDboard.rbv[gen_m.m_tosq =
                  gen_m.m_frsq + dvv[dx]];
              if (cv_c_cpv[gen_m.m_tocp] != ese.ese_actc) {
                *treeptr++ = gen_m;
                tse.tse_count++;
              };
            };
/* castling; process according to active color */
          if (ese.ese_actc == c_w) {
            if ((ese.ese_cast & cf_wk) && !EPDBlackAttacks(sq_e1) &&
                Vacant(sq_f1)
                && !EPDBlackAttacks(sq_f1) && Vacant(sq_g1) &&
                !EPDBlackAttacks(sq_g1)) {
              gen_m.m_tosq = sq_g1;
              gen_m.m_tocp = cp_v0;
              gen_m.m_scmv = scmv_cks;
              *treeptr++ = gen_m;
              tse.tse_count++;
              gen_m.m_scmv = scmv_reg;
            };
            if ((ese.ese_cast & cf_wq) && !EPDBlackAttacks(sq_e1) &&
                Vacant(sq_d1)
                && !EPDBlackAttacks(sq_d1) && Vacant(sq_c1) &&
                !EPDBlackAttacks(sq_c1) && Vacant(sq_b1)) {
              gen_m.m_tosq = sq_c1;
              gen_m.m_tocp = cp_v0;
              gen_m.m_scmv = scmv_cqs;
              *treeptr++ = gen_m;
              tse.tse_count++;
              gen_m.m_scmv = scmv_reg;
            };
          } else {
            if ((ese.ese_cast & cf_bk) && !EPDWhiteAttacks(sq_e8) &&
                Vacant(sq_f8)
                && !EPDWhiteAttacks(sq_f8) && Vacant(sq_g8) &&
                !EPDWhiteAttacks(sq_g8)) {
              gen_m.m_tosq = sq_g8;
              gen_m.m_tocp = cp_v0;
              gen_m.m_scmv = scmv_cks;
              *treeptr++ = gen_m;
              tse.tse_count++;
              gen_m.m_scmv = scmv_reg;
            };
            if ((ese.ese_cast & cf_bq) && !EPDWhiteAttacks(sq_e8) &&
                Vacant(sq_d8)
                && !EPDWhiteAttacks(sq_d8) && Vacant(sq_c8) &&
                !EPDWhiteAttacks(sq_c8) && Vacant(sq_b8)) {
              gen_m.m_tosq = sq_c8;
              gen_m.m_tocp = cp_v0;
              gen_m.m_scmv = scmv_cqs;
              *treeptr++ = gen_m;
              tse.tse_count++;
              gen_m.m_scmv = scmv_reg;
            };
          };
          break;
      };
    };
  };
  return;
}

/*--> EPDSameMoveRef: check if two move references are the same move */
static siT EPDSameMoveRef(mptrT mptr0, mptrT mptr1) {
  siT flag;

  if ((mptr0->m_tosq == mptr1->m_tosq) && (mptr0->m_frsq == mptr1->m_frsq) &&
      (mptr0->m_frcp == mptr1->m_frcp) && (mptr0->m_tocp == mptr1->m_tocp) &&
      (mptr0->m_scmv == mptr1->m_scmv))
    flag = 1;
  else
    flag = 0;
  return (flag);
}

/*--> EPDFindMove: locate the move in the current generation set */
static mptrT EPDFindMove(mptrT mptr) {
  mptrT rmptr;
  siT flag;
  siT index;

  rmptr = tse.tse_base;
  flag = 0;
  index = 0;
  while (!flag && (index < tse.tse_count))
    if (EPDSameMoveRef(mptr, rmptr))
      flag = 1;
    else {
      rmptr++;
      index++;
    };
  if (!flag)
    rmptr = NULL;
  return (rmptr);
}

/*--> EPDExecute: execute the supplied move */
static
void EPDExecute(mptrT mptr) {
  sqT pcsq;
  cpT ppcp;

/* test for overflow */
  if (ply == (pmhL - 1))
    EPDFatal("EPDExecute: played move history overflow");
/* save old environment and generation records */
  *eseptr++ = ese;
  *tseptr++ = tse;
/* set the legality tested flag */
  mptr->m_flag |= mf_exec;
/* process according to move case */
  switch (mptr->m_scmv) {
    case scmv_reg:
      EPDboard.rbv[mptr->m_frsq] = cp_v0;
      EPDboard.rbv[mptr->m_tosq] = mptr->m_frcp;
      break;
    case scmv_epc:
      if (ese.ese_actc == c_w)
        pcsq = mptr->m_tosq + dv_3;
      else
        pcsq = mptr->m_tosq + dv_1;
      EPDboard.rbv[mptr->m_frsq] = cp_v0;
      EPDboard.rbv[mptr->m_tosq] = mptr->m_frcp;
      EPDboard.rbv[pcsq] = cp_v0;
      break;
    case scmv_cks:
      if (ese.ese_actc == c_w) {
        EPDboard.rbv[sq_e1] = cp_v0;
        EPDboard.rbv[sq_g1] = cp_wk;
        EPDboard.rbv[sq_h1] = cp_v0;
        EPDboard.rbv[sq_f1] = cp_wr;
      } else {
        EPDboard.rbv[sq_e8] = cp_v0;
        EPDboard.rbv[sq_g8] = cp_bk;
        EPDboard.rbv[sq_h8] = cp_v0;
        EPDboard.rbv[sq_f8] = cp_br;
      };
      break;
    case scmv_cqs:
      if (ese.ese_actc == c_w) {
        EPDboard.rbv[sq_e1] = cp_v0;
        EPDboard.rbv[sq_c1] = cp_wk;
        EPDboard.rbv[sq_a1] = cp_v0;
        EPDboard.rbv[sq_d1] = cp_wr;
      } else {
        EPDboard.rbv[sq_e8] = cp_v0;
        EPDboard.rbv[sq_c8] = cp_bk;
        EPDboard.rbv[sq_a8] = cp_v0;
        EPDboard.rbv[sq_d8] = cp_br;
      };
      break;
    case scmv_ppn:
      if (ese.ese_actc == c_w)
        ppcp = cp_wn;
      else
        ppcp = cp_bn;
      EPDboard.rbv[mptr->m_frsq] = cp_v0;
      EPDboard.rbv[mptr->m_tosq] = ppcp;
      break;
    case scmv_ppb:
      if (ese.ese_actc == c_w)
        ppcp = cp_wb;
      else
        ppcp = cp_bb;
      EPDboard.rbv[mptr->m_frsq] = cp_v0;
      EPDboard.rbv[mptr->m_tosq] = ppcp;
      break;
    case scmv_ppr:
      if (ese.ese_actc == c_w)
        ppcp = cp_wr;
      else
        ppcp = cp_br;
      EPDboard.rbv[mptr->m_frsq] = cp_v0;
      EPDboard.rbv[mptr->m_tosq] = ppcp;
      break;
    case scmv_ppq:
      if (ese.ese_actc == c_w)
        ppcp = cp_wq;
      else
        ppcp = cp_bq;
      EPDboard.rbv[mptr->m_frsq] = cp_v0;
      EPDboard.rbv[mptr->m_tosq] = ppcp;
      break;
  };
/* set values for updated environment record: active color */
  ese.ese_actc = inv_cv[ese.ese_actc];
/* set values for updated environment record: castling availablity */
  if (ese.ese_cast != 0) {
    if (ese.ese_cast & cf_wk)
      if ((mptr->m_frsq == sq_e1) || (mptr->m_frsq == sq_h1) ||
          (mptr->m_tosq == sq_h1))
        ese.ese_cast &= ~cf_wk;
    if (ese.ese_cast & cf_wq)
      if ((mptr->m_frsq == sq_e1) || (mptr->m_frsq == sq_a1) ||
          (mptr->m_tosq == sq_a1))
        ese.ese_cast &= ~cf_wq;
    if (ese.ese_cast & cf_bk)
      if ((mptr->m_frsq == sq_e8) || (mptr->m_frsq == sq_h8) ||
          (mptr->m_tosq == sq_h8))
        ese.ese_cast &= ~cf_bk;
    if (ese.ese_cast & cf_bq)
      if ((mptr->m_frsq == sq_e8) || (mptr->m_frsq == sq_a8) ||
          (mptr->m_tosq == sq_a8))
        ese.ese_cast &= ~cf_bq;
  };
/* set values for updated environment record: en passant */
  if (ese.ese_actc == c_b) {
    if ((mptr->m_frcp == cp_wp) && (map_rank(mptr->m_frsq) == rank_2) &&
        (map_rank(mptr->m_tosq) == rank_4))
      ese.ese_epsq = mptr->m_frsq + dv_1;
    else
      ese.ese_epsq = sq_nil;
  } else {
    if ((mptr->m_frcp == cp_bp) && (map_rank(mptr->m_frsq) == rank_7) &&
        (map_rank(mptr->m_tosq) == rank_5))
      ese.ese_epsq = mptr->m_frsq + dv_3;
    else
      ese.ese_epsq = sq_nil;
  }
/* set values for updated environment record: halfmove clock */
  if ((mptr->m_tocp != cp_v0) || (cv_p_cpv[mptr->m_frcp] == p_p))
    ese.ese_hmvc = 0;
  else
    ese.ese_hmvc++;
/* set values for updated environment record: fullmove number */
  if (ese.ese_actc == c_w)
    ese.ese_fmvn++;
/* set values for updated environment record: king locations */
  switch (mptr->m_frcp) {
    case cp_wk:
      ese.ese_ksqv[c_w] = mptr->m_tosq;
      break;
    case cp_bk:
      ese.ese_ksqv[c_b] = mptr->m_tosq;
      break;
    default:
      break;
  };
/* check/bust flags */
  if (EPDTestAKIC())
    mptr->m_flag |= mf_chec;
  if (EPDTestPKIC())
    mptr->m_flag |= mf_bust;
/* increment ply index */
  ply++;
  return;
}

/*--> EPDExecuteUpdate: update the current move pointer, then execute */
nonstatic void EPDExecuteUpdate(mptrT mptr) {
  tse.tse_curr = EPDFindMove(mptr);
  if (tse.tse_curr == NULL)
    EPDFatal("EPDExecuteUpdate: can't find move");
  EPDExecute(tse.tse_curr);
  return;
}

/*--> EPDRetract: retract the supplied move */
static
void EPDRetract(mptrT mptr) {
/* decrement ply */
  ply--;
/* restore the current environment and generation */
  ese = *--eseptr;
  tse = *--tseptr;
/* process by move case */
  switch (mptr->m_scmv) {
    case scmv_reg:
      EPDboard.rbv[mptr->m_tosq] = mptr->m_tocp;
      EPDboard.rbv[mptr->m_frsq] = mptr->m_frcp;
      break;
    case scmv_epc:
      EPDboard.rbv[mptr->m_tosq] = cp_v0;
      EPDboard.rbv[mptr->m_frsq] = mptr->m_frcp;
      if (ese.ese_actc == c_w)
        EPDboard.rbv[mptr->m_tosq + dv_3] = cp_bp;
      else
        EPDboard.rbv[mptr->m_tosq + dv_1] = cp_wp;
      break;
    case scmv_cks:
      if (ese.ese_actc == c_w) {
        EPDboard.rbv[sq_g1] = cp_v0;
        EPDboard.rbv[sq_e1] = cp_wk;
        EPDboard.rbv[sq_f1] = cp_v0;
        EPDboard.rbv[sq_h1] = cp_wr;
      } else {
        EPDboard.rbv[sq_g8] = cp_v0;
        EPDboard.rbv[sq_e8] = cp_bk;
        EPDboard.rbv[sq_f8] = cp_v0;
        EPDboard.rbv[sq_h8] = cp_br;
      }
      break;
    case scmv_cqs:
      if (ese.ese_actc == c_w) {
        EPDboard.rbv[sq_c1] = cp_v0;
        EPDboard.rbv[sq_e1] = cp_wk;
        EPDboard.rbv[sq_d1] = cp_v0;
        EPDboard.rbv[sq_a1] = cp_wr;
      } else {
        EPDboard.rbv[sq_c8] = cp_v0;
        EPDboard.rbv[sq_e8] = cp_bk;
        EPDboard.rbv[sq_d8] = cp_v0;
        EPDboard.rbv[sq_a8] = cp_br;
      };
      break;
    case scmv_ppn:
    case scmv_ppb:
    case scmv_ppr:
    case scmv_ppq:
      EPDboard.rbv[mptr->m_tosq] = mptr->m_tocp;
      EPDboard.rbv[mptr->m_frsq] = mptr->m_frcp;
      break;
  };
  return;
}

/*--> EPDRetractUpdate: retract last executed move */
nonstatic void EPDRetractUpdate(void) {
  mptrT mptr;

  mptr = (tseptr - 1)->tse_curr;
  EPDRetract(mptr);
  return;
}

/*--> EPDRetractAll: retract all moves in current variation */
nonstatic void EPDRetractAll(void) {
  while (ply > 0)
    EPDRetractUpdate();
  return;
}

/*--> EPDCollapse: collapse the played move stack */
nonstatic void EPDCollapse(void) {
/* process for nonzero ply */
  if (ply > 0) {
/* reset the stack pointers */
    treeptr = treebaseptr;
    eseptr = esebaseptr;
    tseptr = tsebaseptr;
/* reset the ply */
    ply = 0;
  };
  return;
}

/*--> EPDReset: collapse, then reset starting position */
nonstatic void EPDReset(void) {
  EPDCollapse();
  EPDInitArray();
  return;
}

/*--> EPDMLExec: execute the current move list */
static
void EPDMLExec(void) {
  siT i;
  mptrT mptr;

/* test and mark each move for legality and checking status */
  mptr = tse.tse_base;
  for (i = 0; i < tse.tse_count; i++) {
    tse.tse_curr = mptr;
    EPDExecute(mptr);
    EPDRetract(mptr);
    mptr++;
  };
  return;
}

/*--> EPDMLPolice: remove illegal moves the current move list */
static
void EPDMLPolice(void) {
  mptrT tptr, mptr;
  siT i, bust;
  mT t_m;

/* move illegal moves to end of list */
  mptr = tse.tse_base;
  bust = 0;
  i = 0;
  while (i < (tse.tse_count - bust))
    if (mptr->m_flag & mf_bust) {
      tptr = (tse.tse_base + (tse.tse_count - 1)) - bust;
      t_m = *mptr;
      *mptr = *tptr;
      *tptr = t_m;
      bust++;
    } else {
      mptr++;
      i++;
    };
/* shrink */
  tse.tse_count -= bust;
  return;
}

/*--> EPDMLDisambiguate: insert disambiguation flags in move list */
static
void EPDMLDisambiguate(void) {
  siT i, j, tmc, rmc, fmc;
  mptrT mptr0, mptr1;
  pT p;
  rankT rank;
  fileT file;

/* it's magic */
  mptr0 = tse.tse_base;
  for (i = 0; i < tse.tse_count; i++) {
/* the outer loop disambiguates a single move per cycle */
    p = cv_p_cpv[mptr0->m_frcp];
    if ((p != p_p) && (p != p_k)) {
      rank = map_rank(mptr0->m_frsq);
      file = map_file(mptr0->m_frsq);
      tmc = rmc = fmc = 0;
      mptr1 = tse.tse_base;
      for (j = 0; j < tse.tse_count; j++) {
/* the inner loop examines all possible sibling puns */
        if ((i != j) && (mptr0->m_frcp == mptr1->m_frcp) &&
            (mptr0->m_tosq == mptr1->m_tosq)) {
          tmc++;
          if (map_rank(mptr1->m_frsq) == rank)
            rmc++;
          if (map_file(mptr1->m_frsq) == file)
            fmc++;
        };
        mptr1++;
      };
/* check pun count for outer loop move */
      if (tmc > 0) {
/* file disambiguation has priority */
        if ((rmc > 0) || ((rmc == 0) && (fmc == 0)))
          mptr0->m_flag |= mf_sanf;
/* rank disambiguation may be needed */
        if (fmc > 0)
          mptr0->m_flag |= mf_sanr;
      };
    };
    mptr0++;
  };
  return;
}

/*--> EPDMLScanMate: scan current move list for mating moves */
static
void EPDMLScanMate(void) {
  siT i, j, mateflag, drawflag, moveflag;
  mptrT mptr0, mptr1;

/* scan */
  mptr0 = tse.tse_base;
  for (i = 0; i < tse.tse_count; i++) {
    tse.tse_curr = mptr0;
    EPDExecute(mptr0);
/* now at next higher ply, generate psuedolegal set */
    EPDGeneratePL();
/* try to find at least one legal move */
    mptr1 = tse.tse_base;
    moveflag = 0;
    j = 0;
    while (!moveflag && (j < tse.tse_count)) {
      tse.tse_curr = mptr1;
      EPDExecute(mptr1);
      EPDRetract(mptr1);
      if (!(mptr1->m_flag & mf_bust))
        moveflag = 1;
      else {
        mptr1++;
        j++;
      };
    };
/* any second level moves detected? */
    if (moveflag != 0) {
/* not a mate */
      mateflag = drawflag = 0;
    } else {
/* a mating move is detected */
      if (EPDTestAKIC()) {
        mateflag = 1;
        drawflag = 0;
      } else {
        drawflag = 1;
        mateflag = 0;
      };
    };
/* undo execution */
    EPDRetract(mptr0);
/* now back at lower ply */
    if (mateflag)
      mptr0->m_flag |= mf_chmt;
    else if (drawflag)
      mptr0->m_flag |= (mf_draw | mf_stmt);
/* next move */
    mptr0++;
  };
  return;
}

/*--> EPDGenClean: generate move list with first level processing */
static
void EPDGenClean(void) {
/* basic psuedolegal generation */
  EPDGeneratePL();
/* set legality flags, remove illegal moves, and disambiguate */
  EPDMLExec();
  EPDMLPolice();
  EPDMLDisambiguate();
  return;
}

/*--> EPDGenMoves: generate legal moves and set mate flags */
nonstatic void EPDGenMoves(void) {
/* perform basic first level generation */
  EPDGenClean();
/* handle two ply draw and checkmate detection */
  EPDMLScanMate();
  return;
}

/*--> EPDFetchMoveCount: fetch the move count */
nonstatic siT EPDFetchMoveCount(void) {
  return (tse.tse_count);
}

/*--> EPDFetchMove: fetch the nth move */
nonstatic mptrT EPDFetchMove(siT index) {
  ret_m = *(tse.tse_base + index);
  return (&ret_m);
}

/*--> EPDSetMoveFlags: set move flags from current generation set */
nonstatic void EPDSetMoveFlags(mptrT mptr) {
  mptrT rmptr;

  rmptr = EPDFindMove(mptr);
  if (rmptr != NULL)
    mptr->m_flag = rmptr->m_flag;
  return;
}

/*--> EPDSortSAN: ASCII SAN sort move list */
nonstatic void EPDSortSAN(void) {
  mptrT mptr, mptr0, mptr1;
  siT i, j, pair, pass, flag;
  sanptrT sanptr, sptr0, sptr1;
  char t_ch;
  mT t_m;

/* allocate the SAN string vector */
  sanptr = (sanptrT) EPDMemoryGrab(sizeof(sanT) * tse.tse_count);
/* construct the SAN string entries */
  mptr = tse.tse_base;
  for (i = 0; i < tse.tse_count; i++)
    EPDSANEncode(mptr++, *(sanptr + i));
/* a low tech bubble sort */
  flag = 1;
  pass = 0;
  while (flag && (pass < (tse.tse_count - 1))) {
    sptr0 = sanptr;
    sptr1 = sanptr + 1;
    mptr0 = tse.tse_base;
    mptr1 = tse.tse_base + 1;
    flag = 0;
    pair = 0;
    while (pair < (tse.tse_count - pass - 1)) {
/* case sensitive ascending order */
      if (strcmp((charptrT) sptr0, (charptrT) sptr1) > 0) {
        flag = 1;
        for (j = 0; j < sanL; j++) {
          t_ch = (*sptr0)[j];
          (*sptr0)[j] = (*sptr1)[j];
          (*sptr1)[j] = t_ch;
        };
        t_m = *mptr0;
        *mptr0 = *mptr1;
        *mptr1 = t_m;
      };
      sptr0++;
      sptr1++;
      mptr0++;
      mptr1++;
      pair++;
    };
    pass++;
  };
  EPDMemoryFree(sanptr);
  return;
}

/*--> EPDRepairMove: repair a move operation */
static
void EPDRepairMove(eopptrT eopptr) {
  eovptrT eovptr;
  mptrT mptr;
  mT m;
  sanT san;

/* repair a single move from the current position */
  eovptr = eopptr->eop_headeov;
  if (eovptr != NULL) {
    mptr = EPDSANDecodeAux(eovptr->eov_str, 0);
    if (mptr != NULL) {
      m = *mptr;
      EPDSANEncode(&m, san);
      if (strcmp(eovptr->eov_str, san) != 0)
        EPDReplaceEOVStr(eovptr, san);
    };
  };
  return;
}

/*--> EPDRepairMoveset: repair a moveset operation */
static
void EPDRepairMoveset(eopptrT eopptr) {
  eovptrT eovptr;
  mptrT mptr;
  mT m;
  sanT san;

/* check each move from the current position */
  eovptr = eopptr->eop_headeov;
  while (eovptr != NULL) {
    mptr = EPDSANDecodeAux(eovptr->eov_str, 0);
    if (mptr != NULL) {
      m = *mptr;
      EPDSANEncode(&m, san);
      if (strcmp(eovptr->eov_str, san) != 0)
        EPDReplaceEOVStr(eovptr, san);
    };
    eovptr = eovptr->eov_next;
  };
  return;
}

/*--> EPDRepairVariation: repair a variation operation */
static
void EPDRepairVariation(eopptrT eopptr) {
  eovptrT eovptr;
  mptrT mptr;
  mT m;
  sanT san;

/* play move sequence from the current position */
  eovptr = eopptr->eop_headeov;
  while (eovptr != NULL) {
    mptr = EPDSANDecodeAux(eovptr->eov_str, 0);
    if (mptr == NULL)
      eovptr = NULL;
    else {
      m = *mptr;
      EPDSANEncode(&m, san);
      if (strcmp(eovptr->eov_str, san) != 0)
        EPDReplaceEOVStr(eovptr, san);
      tse.tse_curr = EPDFindMove(mptr);
      if (tse.tse_curr == NULL)
        EPDFatal("EPDRepairVariation: can't find move");
      EPDExecute(mptr);
      EPDGenMoves();
      eovptr = eovptr->eov_next;
    }
  };
/* retract any executed moves */
  EPDRetractAll();
  return;
}

/*--> EPDPurgeOpFile: purge operation from input file to output file */
nonstatic siT EPDPurgeOpFile(charptrT opsym, charptrT fn0, charptrT fn1) {
  siT flag;
  fptrT fptr0, fptr1;
  epdptrT epdptr;
  eopptrT eopptr;
  charptrT eptr;
  char ev[epdL];

/* set default return value (success) */
  flag = 1;
/* clear the input and output file pointers */
  fptr0 = fptr1 = NULL;
/* open the input file for reading */
  if (flag) {
    fptr0 = fopen(fn0, "r");
    if (fptr0 == NULL)
      flag = 0;
  };
/* open the output file for writing */
  if (flag) {
    fptr1 = fopen(fn1, "w");
    if (fptr1 == NULL)
      flag = 0;
  };
/* scan entire file */
  while (flag && (fgets(ev, (epdL - 1), fptr0) != NULL)) {
/* decode a record */
    epdptr = EPDDecode(ev);
/* check record decode validity */
    if (epdptr == NULL)
      flag = 0;
    else {
/* locate the operation to be purged */
      eopptr = EPDLocateEOP(epdptr, opsym);
      if (eopptr != NULL) {
        EPDUnthreadEOP(epdptr, eopptr);
        EPDReleaseEOP(eopptr);
      };
/* encode the record (includes normalization) */
      eptr = EPDEncode(epdptr);
/* release EPD structure */
      EPDReleaseEPD(epdptr);
/* check result */
      if (eptr == NULL)
        flag = 0;
      else {
        fprintf(fptr1, "%s\n", eptr);
        EPDMemoryFree(eptr);
      };
    };
  };
/* close input and output files */
  if (fptr0 != NULL)
    fclose(fptr0);
  if (fptr1 != NULL)
    fclose(fptr1);
  return (flag);
}

/*--> EPDRepairEPD: repair an EPD structure */
nonstatic siT EPDRepairEPD(epdptrT epdptr) {
  siT flag;
  eopptrT eopptr;

/* set default return value: repair successful */
  flag = 1;
/* set up the position as the current position */
  EPDRealize(epdptr);
/* check legality */
  if (!EPDIsLegal())
    flag = 0;
  else {
/* generate moves and notation */
    EPDGenMoves();
/* repair moveset "am" */
    eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso_am]);
    if (eopptr != NULL)
      EPDRepairMoveset(eopptr);
/* repair moveset "bm" */
    eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso_bm]);
    if (eopptr != NULL)
      EPDRepairMoveset(eopptr);
/* repair move "pm" */
    eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso_pm]);
    if (eopptr != NULL)
      EPDRepairMove(eopptr);
/* repair variation "pv" */
    eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso_pv]);
    if (eopptr != NULL)
      EPDRepairVariation(eopptr);
/* repair move "sm" */
    eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso_sm]);
    if (eopptr != NULL)
      EPDRepairMove(eopptr);
/* repair variation "sv" */
    eopptr = EPDLocateEOP(epdptr, epdsostrv[epdso_sv]);
    if (eopptr != NULL)
      EPDRepairVariation(eopptr);
  };
  return (flag);
}

/*--> EPDRepairFile: repair input file to output file */
nonstatic siT EPDRepairFile(charptrT fn0, charptrT fn1) {
  siT flag;
  fptrT fptr0, fptr1;
  epdptrT epdptr;
  charptrT eptr;
  char ev[epdL];

/* set default return value (success) */
  flag = 1;
/* clear the input and output file pointers */
  fptr0 = fptr1 = NULL;
/* open the input file for reading */
  if (flag) {
    fptr0 = fopen(fn0, "r");
    if (fptr0 == NULL)
      flag = 0;
  };
/* open the output file for writing */
  if (flag) {
    fptr1 = fopen(fn1, "w");
    if (fptr1 == NULL)
      flag = 0;
  };
/* scan entire file */
  while (flag && (fgets(ev, (epdL - 1), fptr0) != NULL)) {
/* decode a record */
    epdptr = EPDDecode(ev);
/* check record decode validity */
    if (epdptr == NULL)
      flag = 0;
    else {
/* make repairs */
      flag = EPDRepairEPD(epdptr);
/* continue if repair okay */
      if (flag) {
/* encode the normalized record */
        eptr = EPDEncode(epdptr);
/* check result */
        if (eptr == NULL)
          flag = 0;
        else {
          fprintf(fptr1, "%s\n", eptr);
          EPDMemoryFree(eptr);
        };
      };
/* release EPD structure */
      EPDReleaseEPD(epdptr);
    };
  };
/* close input and output files */
  if (fptr0 != NULL)
    fclose(fptr0);
  if (fptr1 != NULL)
    fclose(fptr1);
  return (flag);
}

/*--> EPDNormalizeFile: normalize input file to output file */
nonstatic siT EPDNormalizeFile(charptrT fn0, charptrT fn1) {
  siT flag;
  fptrT fptr0, fptr1;
  epdptrT epdptr;
  charptrT eptr;
  char ev[epdL];

/* set default return value (success) */
  flag = 1;
/* clear the input and output file pointers */
  fptr0 = fptr1 = NULL;
/* open the input file for reading */
  if (flag) {
    fptr0 = fopen(fn0, "r");
    if (fptr0 == NULL)
      flag = 0;
  };
/* open the output file for writing */
  if (flag) {
    fptr1 = fopen(fn1, "w");
    if (fptr1 == NULL)
      flag = 0;
  };
/* scan entire file */
  while (flag && (fgets(ev, (epdL - 1), fptr0) != NULL)) {
/* decode a record */
    epdptr = EPDDecode(ev);
/* check record decode validity */
    if (epdptr == NULL)
      flag = 0;
    else {
/* encode the record (this includes normalization) */
      eptr = EPDEncode(epdptr);
/* release EPD structure */
      EPDReleaseEPD(epdptr);
/* check result */
      if (eptr == NULL)
        flag = 0;
      else {
        fprintf(fptr1, "%s\n", eptr);
        EPDMemoryFree(eptr);
      };
    };
  };
/* close input and output files */
  if (fptr0 != NULL)
    fclose(fptr0);
  if (fptr1 != NULL)
    fclose(fptr1);
  return (flag);
}

/*--> EPDScoreFile: score a benchmark file */
nonstatic siT EPDScoreFile(charptrT fn, bmsptrT bmsptr) {
  siT flag;
  siT skipflag;
  siT am_flag, bm_flag;
  siT solved = 0;
  fptrT fptr;
  epdptrT epdptr;
  eopptrT am_eopptr, bm_eopptr, acd_eopptr, acn_eopptr, acs_eopptr;
  eopptrT sm_eopptr, sv_eopptr, pm_eopptr, pv_eopptr;
  charptrT result;
  char ev[epdL];

/* set default return value (success) */
  flag = 1;
/* clear the input file pointer */
  fptr = NULL;
/* preset the summary structure */
  bmsptr->bms_acdflag = bmsptr->bms_acnflag = bmsptr->bms_acsflag = 1;
  bmsptr->bms_total = bmsptr->bms_solve = bmsptr->bms_unsol = 0;
  bmsptr->bms_total_acd = bmsptr->bms_solve_acd = bmsptr->bms_unsol_acd = 0;
  bmsptr->bms_total_acn = bmsptr->bms_solve_acn = bmsptr->bms_unsol_acn = 0;
  bmsptr->bms_total_acs = bmsptr->bms_solve_acs = bmsptr->bms_unsol_acs = 0;
/* open the input file for reading */
  if (flag) {
    fptr = fopen(fn, "r");
    if (fptr == NULL)
      flag = 0;
  };
/* scan entire file */
  while (flag && (fgets(ev, (epdL - 1), fptr) != NULL)) {
/* decode a record */
    epdptr = EPDDecode(ev);
/* check record decode validity */
    if (epdptr == NULL)
      flag = 0;
    else {
/* clear the move result pointer */
      result = NULL;
/* initialize various operation pointers */
      am_eopptr = EPDLocateEOPCode(epdptr, epdso_am);
      bm_eopptr = EPDLocateEOPCode(epdptr, epdso_bm);
      acd_eopptr = EPDLocateEOPCode(epdptr, epdso_acd);
      acn_eopptr = EPDLocateEOPCode(epdptr, epdso_acn);
      acs_eopptr = EPDLocateEOPCode(epdptr, epdso_acs);
      sm_eopptr = EPDLocateEOPCode(epdptr, epdso_sm);
      sv_eopptr = EPDLocateEOPCode(epdptr, epdso_sv);
      pm_eopptr = EPDLocateEOPCode(epdptr, epdso_pm);
      pv_eopptr = EPDLocateEOPCode(epdptr, epdso_pv);
/* test for am/bm existence */
      if ((am_eopptr == NULL) && (bm_eopptr == NULL))
        skipflag = 1;
      else
        skipflag = 0;
/* try to locate a result move (note priority) */
      if (!skipflag) {
        if (result == NULL)
          if ((pv_eopptr != NULL) && (pv_eopptr->eop_headeov != NULL))
            result = pv_eopptr->eop_headeov->eov_str;
        if (result == NULL)
          if ((pm_eopptr != NULL) && (pm_eopptr->eop_headeov != NULL))
            result = pm_eopptr->eop_headeov->eov_str;
        if (result == NULL)
          if ((sv_eopptr != NULL) && (sv_eopptr->eop_headeov != NULL))
            result = sv_eopptr->eop_headeov->eov_str;
        if (result == NULL)
          if ((sm_eopptr != NULL) && (sm_eopptr->eop_headeov != NULL))
            result = sm_eopptr->eop_headeov->eov_str;
        if (result == NULL)
          skipflag = 1;
      };
/* determine solve status */
      if (!skipflag) {
/* check for clearance with the am set */
        if ((am_eopptr == NULL) || (EPDLocateEOV(am_eopptr, result) == NULL))
          am_flag = 1;
        else
          am_flag = 0;
/* check for clearance with the bm set */
        if ((bm_eopptr == NULL) || (EPDLocateEOV(bm_eopptr, result) != NULL))
          bm_flag = 1;
        else
          bm_flag = 0;
/* set solution flag */
        solved = am_flag && bm_flag;
      };
/* update statistics block */
      if (!skipflag) {
/* clear acd flag if acd is missing */
        if ((acd_eopptr == NULL) || (acd_eopptr->eop_headeov == NULL))
          bmsptr->bms_acdflag = 0;
/* clear acn flag if acn is missing */
        if ((acn_eopptr == NULL) || (acn_eopptr->eop_headeov == NULL))
          bmsptr->bms_acnflag = 0;
/* clear acs flag if acs is missing */
        if ((acs_eopptr == NULL) || (acs_eopptr->eop_headeov == NULL))
          bmsptr->bms_acsflag = 0;
/* increment record count */
        bmsptr->bms_total++;
/* fold in acd value */
        if (bmsptr->bms_acdflag) {
          bmsptr->bms_total_acd += atoi(acd_eopptr->eop_headeov->eov_str);
          if (solved)
            bmsptr->bms_solve_acd += atoi(acd_eopptr->eop_headeov->eov_str);
          else
            bmsptr->bms_unsol_acd += atoi(acd_eopptr->eop_headeov->eov_str);
        };
/* fold in acn value */
        if (bmsptr->bms_acnflag) {
          bmsptr->bms_total_acn += atoi(acn_eopptr->eop_headeov->eov_str);
          if (solved)
            bmsptr->bms_solve_acn += atoi(acn_eopptr->eop_headeov->eov_str);
          else
            bmsptr->bms_unsol_acn += atoi(acn_eopptr->eop_headeov->eov_str);
        };
/* fold in acs value */
        if (bmsptr->bms_acsflag) {
          bmsptr->bms_total_acs += atoi(acs_eopptr->eop_headeov->eov_str);
          if (solved)
            bmsptr->bms_solve_acs += atoi(acs_eopptr->eop_headeov->eov_str);
          else
            bmsptr->bms_unsol_acs += atoi(acs_eopptr->eop_headeov->eov_str);
        };
/* update remaining items according to solved status */
        if (solved)
          bmsptr->bms_solve++;
        else
          bmsptr->bms_unsol++;
      };
/* release EPD structure */
      EPDReleaseEPD(epdptr);
    };
  };
/* close input file */
  if (fptr != NULL)
    fclose(fptr);
  return (flag);
}

/*--> EPDEnumerate: enumeration of current position */
static liT EPDEnumerate(siT depth) {
  liT total;
  mptrT mptr;
  siT i;

/* enumerate current position to the indicated depth */
  if (depth == 0)
    total = 1;
  else {
    total = 0;
    EPDGeneratePL();
    mptr = tse.tse_base;
    for (i = 0; i < tse.tse_count; i++) {
      tse.tse_curr = mptr;
      EPDExecute(mptr);
      if (!(mptr->m_flag & mf_bust))
        total += EPDEnumerate((siT) (depth - 1));
      EPDRetract(mptr);
      mptr++;
    };
  };
  return (total);
}

/*--> EPDEnumerateFile: enumerate input file to output file */
nonstatic siT EPDEnumerateFile(siT depth, charptrT fn0, charptrT fn1,
    liptrT totalptr) {
  siT flag;
  fptrT fptr0, fptr1;
  time_t start_time;
  liT acn, acs;
  epdptrT epdptr;
  charptrT eptr;
  char ev[epdL];

/* set default return value (success) */
  flag = 1;
/* clear the grand total */
  *totalptr = 0;
/* clear the input and output file pointers */
  fptr0 = fptr1 = NULL;
/* open the input file for reading */
  if (flag) {
    fptr0 = fopen(fn0, "r");
    if (fptr0 == NULL)
      flag = 0;
  };
/* open the output file for writing */
  if (flag) {
    fptr1 = fopen(fn1, "w");
    if (fptr1 == NULL)
      flag = 0;
  };
/* scan entire file */
  while (flag && (fgets(ev, (epdL - 1), fptr0) != NULL)) {
/* decode a record */
    epdptr = EPDDecode(ev);
/* check record decode validity */
    if (epdptr == NULL)
      flag = 0;
    else {
/* set up the current position */
      EPDRealize(epdptr);
/* check legality */
      if (!EPDIsLegal())
        flag = 0;
      else {
/* perform enumeration */
        start_time = time(NULL);
        acn = EPDEnumerate(depth);
        acs = time(NULL) - start_time;
/* update the grand total */
        *totalptr += acn;
/* record the updated field: acd */
        EPDAddOpInt(epdptr, epdso_acd, depth);
/* record the updated field: acn */
        EPDAddOpInt(epdptr, epdso_acn, acn);
/* record the updated field: acs */
        EPDAddOpInt(epdptr, epdso_acs, acs);
/* encode the record */
        EPDNormalize(epdptr);
        eptr = EPDEncode(epdptr);
/* check result */
        if (eptr == NULL)
          flag = 0;
        else {
          fprintf(fptr1, "%s\n", eptr);
          EPDMemoryFree(eptr);
        };
      };
/* release EPD structure */
      EPDReleaseEPD(epdptr);
    };
  };
/* close input and output files */
  if (fptr0 != NULL)
    fclose(fptr0);
  if (fptr1 != NULL)
    fclose(fptr1);
  return (flag);
}

/*--> EPDMoveList: generate a string representation of a move list */
nonstatic charptrT EPDMoveList(gamptrT gamptr) {
  charptrT s;
  charptrT b;
  siT count;
  gpmptrT gpmptr;
  mT m;
  siT sn;
  cT sc, c;
  siT pi, index, limit, length, n, column;
  sanT san;
  char tv[tL];

/* calculate upper bound on storage requirement */
  count = EPDGamePlyCount(gamptr);
  limit = (((count + 1) / 2) * 5) + 4 + (count * 8) + 8 + 1;
  b = (charptrT) EPDMemoryGrab(limit);
/* set the inital played move pointer */
  gpmptr = gamptr->gam_headgpm;
/* set up starting color and starting move number */
  if (count == 0)
    sc = c_w;
  else
    sc = gpmptr->gpm_ese.ese_actc;
  if (count == 0)
    sn = 1;
  else
    sn = gpmptr->gpm_ese.ese_fmvn;
/* more set up */
  pi = 0;
  index = 0;
  c = sc;
  n = sn;
  column = 0;
/* loop through moves */
  for (pi = 0; pi < count; pi++) {
/* handle move number indication */
    if ((c == c_w) || ((pi == 0) && (sc == c_b))) {
      sprintf(tv, "%hd.", n);
      length = strlen(tv);
      if ((column + 1 + length) >= columnL) {
        strcpy((b + index), "\n");
        index++;
        column = 0;
      };
      if (column != 0) {
        strcpy((b + index), " ");
        index++;
        column++;
      };
      strcpy((b + index), tv);
      index += length;
      column += length;
      n++;
    };
/* handle ellipsis */
    if ((pi == 0) && (sc == c_b)) {
      sprintf(tv, "...");
      length = strlen(tv);
      if ((column + 1 + length) >= columnL) {
        strcpy((b + index), "\n");
        index++;
        column = 0;
      };
      if (column != 0) {
        strcpy((b + index), " ");
        index++;
        column++;
      };
      strcpy((b + index), tv);
      index += length;
      column += length;
    };
/* handle move */
    m = gpmptr->gpm_m;
    EPDSANEncode(&m, san);
    length = strlen(san);
    if ((column + 1 + length) >= columnL) {
      strcpy((b + index), "\n");
      index++;
      column = 0;
    };
    if (column != 0) {
      strcpy((b + index), " ");
      index++;
      column++;
    };
    strcpy((b + index), san);
    index += length;
    column += length;
    gpmptr = gpmptr->gpm_next;
    c = inv_cv[c];
  };
/* append game termination marker */
  sprintf(tv, "%s", gtimstrv[gamptr->gam_gtim]);
  length = strlen(tv);
  if ((column + 1 + length) >= columnL) {
    strcpy((b + index), "\n");
    index++;
    column = 0;
  };
  if (column != 0) {
    strcpy((b + index), " ");
    index++;
    column++;
  };
  strcpy((b + index), tv);
  index += length;
  column += length;
/* closing newline */
  if (column != 0)
    strcpy((b + index), "\n");
/* allocate and copy to result */
  s = EPDStringGrab(b);
  EPDMemoryFree(b);
  return (s);
}

/*--> EPDPGNFetchTagIndex: return a PGN Seven Tag Roster tag index */
nonstatic pgnstrT EPDPGNFetchTagIndex(charptrT s) {
  pgnstrT pgnstr;
  pgnstrT rstr;

  pgnstr = pgnstr_nil;
  rstr = 0;
  while ((pgnstr == pgnstr_nil) && (rstr < pgnstrL))
    if (strcmp(s, EPDPGNFetchTagName(rstr)) == 0)
      pgnstr = rstr;
    else
      rstr++;
  return (pgnstr);
}

/*--> EPDPGNFetchTagName: return a PGN Seven Tag Roster tag name */
nonstatic charptrT EPDPGNFetchTagName(pgnstrT pgnstr) {
  return (pgnstrstrv[pgnstr]);
}

/*--> EPDPGNGetSTR: return a string from the Seven Tag Roster */
nonstatic charptrT EPDPGNGetSTR(gamptrT gamptr, pgnstrT pgnstr) {
  return (gamptr->gam_strv[pgnstr]);
}

/*--> EPDPGNPutSTR: enter a string into the Seven Tag Roster */
nonstatic void EPDPGNPutSTR(gamptrT gamptr, pgnstrT pgnstr, charptrT s) {
  if (gamptr->gam_strv[pgnstr] != NULL)
    EPDMemoryFree(gamptr->gam_strv[pgnstr]);
  gamptr->gam_strv[pgnstr] = EPDStringGrab(s);
  return;
}

/*--> EPDPGNGenSTR: return a string with the entire STR */
nonstatic charptrT EPDPGNGenSTR(gamptrT gamptr) {
  charptrT s;
  pgnstrT pgnstr;
  char tv[tL];

  s = EPDStringGrab("");
  for (pgnstr = 0; pgnstr < pgnstrL; pgnstr++) {
    sprintf(tv, "[%s \"%s\"]\n", pgnstrstrv[pgnstr],
        gamptr->gam_strv[pgnstr]);
    s = EPDStringAppendStr(s, tv);
  };
  return (s);
}

/*--> EPDPGNHistory: generate a string for PGN version of history */
nonstatic charptrT EPDPGNHistory(gamptrT gamptr) {
  charptrT s;
  charptrT ms;

  s = EPDPGNGenSTR(gamptr);
  s = EPDStringAppendChar(s, '\n');
  ms = EPDMoveList(gamptr);
  s = EPDStringAppendStr(s, ms);
  EPDMemoryFree(ms);
  s = EPDStringAppendChar(s, '\n');
  return (s);
}

/*--> EPDCopyOutPTP: copy STR from an EDP structure (ptp operation) */
nonstatic void EPDCopyOutPTP(gamptrT gamptr, epdptrT epdptr) {
  eopptrT eopptr;
  eovptrT eovptr;
  pgnstrT pgnstr;

  if (epdptr != NULL) {
    eopptr = EPDLocateEOPCode(epdptr, epdso_ptp);
    eovptr = eopptr->eop_headeov;
    while ((eovptr != NULL) && (eovptr->eov_next != NULL)) {
      pgnstr = EPDPGNFetchTagIndex(eovptr->eov_str);
      if (pgnstr != pgnstr_nil)
        EPDPGNPutSTR(gamptr, pgnstr, eovptr->eov_next->eov_str);
      eovptr = eovptr->eov_next->eov_next;
    };
  };
  return;
}

/*--> EPDFetchRefcomStr: return  pointer of indicated refcom string */
nonstatic charptrT EPDFetchRefcomStr(refcomT refcom) {
  return (refcomstrv[refcom]);
}

/*--> EPDFetchRefreqStr: return  pointer of indicated refreq string */
nonstatic charptrT EPDFetchRefreqStr(refreqT refreq) {
  return (refreqstrv[refreq]);
}

/*--> EPDFetchRefcomIndex: return a referee command index */
nonstatic refcomT EPDFetchRefcomIndex(charptrT s) {
  refcomT refcom;
  refcomT rcom;

  refcom = refcom_nil;
  rcom = 0;
  while ((refcom == refcom_nil) && (rcom < refcomL))
    if (strcmp(s, EPDFetchRefcomStr(rcom)) == 0)
      refcom = rcom;
    else
      rcom++;
  return (refcom);
}

/*--> EPDExtractRefcomIndex: extract a referee command index */
nonstatic refreqT EPDExtractRefcomIndex(epdptrT epdptr) {
  refcomT refcom;
  eopptrT eopptr;
  eovptrT eovptr;

/* set default return value */
  refcom = refreq_nil;
  if (epdptr != NULL)
    if ((eopptr = EPDLocateEOPCode(epdptr, epdso_refcom)) != NULL)
      if ((eovptr = eopptr->eop_headeov) != NULL)
        refcom = EPDFetchRefcomIndex(eovptr->eov_str);
  return (refcom);
}

/*--> EPDComm: slave to Duplex autoplay program */
nonstatic siT EPDComm(refintptrT refintptr, charptrT pipebase) {
  siT flag;
  siT done;
  siT flow;
  refcomT refcom;
  charptrT pfnv[flowL];
  fptrT pfptrv[flowL];
  epdptrT epdptr0, epdptr1;
  charptrT eptr;
  char ev[epdL];

/* set default result: success */
  flag = 1;
/* set up the EPD Kit for a new game */
  EPDInitArray();
/* generate pipe file names and clear their pointers */
  for (flow = 0; flow < flowL; flow++) {
    pfnv[flow] = EPDStringGrab(pipebase);
    pfnv[flow] = EPDStringAppendStr(pfnv[flow], ".pc");
    pfnv[flow] = EPDStringAppendChar(pfnv[flow], (char) (flow + '0'));
    pfptrv[flow] = NULL;
  };
/* pipe files already created by Duplex, attempt open */
  flow = 0;
  while (flag && (flow < flowL)) {
    pfptrv[flow] = fopen(pfnv[flow], "a+");
    if (pfptrv[flow] == NULL)
      flag = 0;
    else
      flow++;
  };
/* sign on to Duplex */
  if (flag) {
    epdptr0 = EPDGetCurrentPosition();
    EPDAddOpSym(epdptr0, epdso_refreq, refreqstrv[refreq_sign_on]);
    eptr = EPDEncode(epdptr0);
    EPDReleaseEPD(epdptr0);
    fprintf(pfptrv[0], "%s\n", eptr);
    fflush(pfptrv[0]);
    EPDMemoryFree(eptr);
  };
/* run event cycle loop */
  done = 0;
  while (flag && !done) {
/* read an incoming EPD message */
    if (fgets(ev, (epdL - 1), pfptrv[1]) == NULL)
      flag = 0;
    else {
/* decode the message */
      epdptr1 = EPDDecode(ev);
      if ((epdptr1 == NULL) ||
          ((refcom = EPDExtractRefcomIndex(epdptr1)) == refcom_nil))
        flag = 0;
      else {
/* send the message to the callback routine */
        epdptr0 = (*refintptr) (epdptr1, &flag);
/* release input storage */
        EPDReleaseEPD(epdptr1);
/* construct and transmit output string */
        if (flag && (epdptr0 != NULL)) {
          eptr = EPDEncode(epdptr0);
          fprintf(pfptrv[0], "%s\n", eptr);
          fflush(pfptrv[0]);
          EPDMemoryFree(eptr);
        };
/* release output storage */
        if (epdptr0 != NULL)
          EPDReleaseEPD(epdptr0);
/* set the completion flag on disconnect */
        if (flag && !done && (refcom == refcom_disconnect))
          done = 1;
      };
    };
  };
/* close pipes and release pipe file names */
  for (flow = 0; flow < flowL; flow++) {
    if (pfptrv[flow] != NULL)
      fclose(pfptrv[flow]);
    EPDMemoryFree(pfnv[flow]);
  };
  return (flag);
}

/*--> EPDInit: one time initialization for EPD */
nonstatic void EPDInit(void) {
  cpT cp;
  sqT sq;
  xsqT xsq;

/* this sets up the current position */
/* initialize ascii color conversion vector */
  asccv[c_w] = 'w';
  asccv[c_b] = 'b';
/* initialize ascii piece conversion vector */
  ascpv[p_p] = 'P';
  ascpv[p_n] = 'N';
  ascpv[p_b] = 'B';
  ascpv[p_r] = 'R';
  ascpv[p_q] = 'Q';
  ascpv[p_k] = 'K';
/* initialize ascii rank conversion vector */
  ascrv[rank_1] = '1';
  ascrv[rank_2] = '2';
  ascrv[rank_3] = '3';
  ascrv[rank_4] = '4';
  ascrv[rank_5] = '5';
  ascrv[rank_6] = '6';
  ascrv[rank_7] = '7';
  ascrv[rank_8] = '8';
/* initialize ascii file conversion vector */
  ascfv[file_a] = 'a';
  ascfv[file_b] = 'b';
  ascfv[file_c] = 'c';
  ascfv[file_d] = 'd';
  ascfv[file_e] = 'e';
  ascfv[file_f] = 'f';
  ascfv[file_g] = 'g';
  ascfv[file_h] = 'h';
/* initialize piece letter from special case move indicator */
  cv_p_scmvv[scmv_reg] = p_nil;
  cv_p_scmvv[scmv_epc] = p_nil;
  cv_p_scmvv[scmv_cks] = p_nil;
  cv_p_scmvv[scmv_cqs] = p_nil;
  cv_p_scmvv[scmv_ppn] = p_n;
  cv_p_scmvv[scmv_ppb] = p_b;
  cv_p_scmvv[scmv_ppr] = p_r;
  cv_p_scmvv[scmv_ppq] = p_q;
/* initialize various color piece conversion arrays */
  for (cp = cp_wp; cp <= cp_wk; cp++)
    cv_c_cpv[cp] = c_w;
  for (cp = cp_bp; cp <= cp_bk; cp++)
    cv_c_cpv[cp] = c_b;
  cv_c_cpv[cp_v0] = c_v;
  cv_c_cpv[cp_x0] = cv_c_cpv[cp_x1] = cv_c_cpv[cp_x2] = c_x;
  cv_p_cpv[cp_wp] = cv_p_cpv[cp_bp] = p_p;
  cv_p_cpv[cp_wn] = cv_p_cpv[cp_bn] = p_n;
  cv_p_cpv[cp_wb] = cv_p_cpv[cp_bb] = p_b;
  cv_p_cpv[cp_wr] = cv_p_cpv[cp_br] = p_r;
  cv_p_cpv[cp_wq] = cv_p_cpv[cp_bq] = p_q;
  cv_p_cpv[cp_wk] = cv_p_cpv[cp_bk] = p_k;
  cv_p_cpv[cp_v0] = p_v;
  cv_p_cpv[cp_x0] = cv_p_cpv[cp_x1] = cv_p_cpv[cp_x2] = p_x;
  cv_cp_c_pv[c_w][p_p] = cp_wp;
  cv_cp_c_pv[c_w][p_n] = cp_wn;
  cv_cp_c_pv[c_w][p_b] = cp_wb;
  cv_cp_c_pv[c_w][p_r] = cp_wr;
  cv_cp_c_pv[c_w][p_q] = cp_wq;
  cv_cp_c_pv[c_w][p_k] = cp_wk;
  cv_cp_c_pv[c_b][p_p] = cp_bp;
  cv_cp_c_pv[c_b][p_n] = cp_bn;
  cv_cp_c_pv[c_b][p_b] = cp_bb;
  cv_cp_c_pv[c_b][p_r] = cp_br;
  cv_cp_c_pv[c_b][p_q] = cp_bq;
  cv_cp_c_pv[c_b][p_k] = cp_bk;
  inv_cv[c_w] = c_b;
  inv_cv[c_b] = c_w;
/* initialize directional vectors */
  dvv[dx_0] = dv_0;
  dvv[dx_1] = dv_1;
  dvv[dx_2] = dv_2;
  dvv[dx_3] = dv_3;
  dvv[dx_4] = dv_4;
  dvv[dx_5] = dv_5;
  dvv[dx_6] = dv_6;
  dvv[dx_7] = dv_7;
  dvv[dx_8] = dv_8;
  dvv[dx_9] = dv_9;
  dvv[dx_a] = dv_a;
  dvv[dx_b] = dv_b;
  dvv[dx_c] = dv_c;
  dvv[dx_d] = dv_d;
  dvv[dx_e] = dv_e;
  dvv[dx_f] = dv_f;
  xdvv[dx_0] = xdv_0;
  xdvv[dx_1] = xdv_1;
  xdvv[dx_2] = xdv_2;
  xdvv[dx_3] = xdv_3;
  xdvv[dx_4] = xdv_4;
  xdvv[dx_5] = xdv_5;
  xdvv[dx_6] = xdv_6;
  xdvv[dx_7] = xdv_7;
  xdvv[dx_8] = xdv_8;
  xdvv[dx_9] = xdv_9;
  xdvv[dx_a] = xdv_a;
  xdvv[dx_b] = xdv_b;
  xdvv[dx_c] = xdv_c;
  xdvv[dx_d] = xdv_d;
  xdvv[dx_e] = xdv_e;
  xdvv[dx_f] = xdv_f;
/* initialize the extended board */
  for (xsq = 0; xsq < xsqL; xsq++)
    xb.xbv[xsq] = cp_x0;
  for (sq = sq_a1; sq <= sq_h8; sq++)
    xb.xbv[map_xsq_sq(sq)] = cp_v0;
/* initialize the standard opcode string vector */
  epdsostrv[epdso_acd] = "acd";
  epdsostrv[epdso_acn] = "acn";
  epdsostrv[epdso_acs] = "acs";
  epdsostrv[epdso_am] = "am";
  epdsostrv[epdso_bm] = "bm";
  epdsostrv[epdso_c0] = "c0";
  epdsostrv[epdso_c1] = "c1";
  epdsostrv[epdso_c2] = "c2";
  epdsostrv[epdso_c3] = "c3";
  epdsostrv[epdso_c4] = "c4";
  epdsostrv[epdso_c5] = "c5";
  epdsostrv[epdso_c6] = "c6";
  epdsostrv[epdso_c7] = "c7";
  epdsostrv[epdso_c8] = "c8";
  epdsostrv[epdso_c9] = "c9";
  epdsostrv[epdso_cc] = "cc";
  epdsostrv[epdso_ce] = "ce";
  epdsostrv[epdso_dm] = "dm";
  epdsostrv[epdso_draw_accept] = "draw_accept";
  epdsostrv[epdso_draw_claim] = "draw_claim";
  epdsostrv[epdso_draw_offer] = "draw_offer";
  epdsostrv[epdso_draw_reject] = "draw_reject";
  epdsostrv[epdso_eco] = "eco";
  epdsostrv[epdso_fmvn] = "fmvn";
  epdsostrv[epdso_hmvc] = "hmvc";
  epdsostrv[epdso_id] = "id";
  epdsostrv[epdso_nic] = "nic";
  epdsostrv[epdso_noop] = "noop";
  epdsostrv[epdso_pm] = "pm";
  epdsostrv[epdso_ptp] = "ptp";
  epdsostrv[epdso_pv] = "pv";
  epdsostrv[epdso_rc] = "rc";
  epdsostrv[epdso_refcom] = "refcom";
  epdsostrv[epdso_refreq] = "refreq";
  epdsostrv[epdso_resign] = "resign";
  epdsostrv[epdso_sm] = "sm";
  epdsostrv[epdso_sv] = "sv";
  epdsostrv[epdso_tcgs] = "tcgs";
  epdsostrv[epdso_tcri] = "tcri";
  epdsostrv[epdso_tcsi] = "tcsi";
  epdsostrv[epdso_ts] = "ts";
  epdsostrv[epdso_v0] = "v0";
  epdsostrv[epdso_v1] = "v1";
  epdsostrv[epdso_v2] = "v2";
  epdsostrv[epdso_v3] = "v3";
  epdsostrv[epdso_v4] = "v4";
  epdsostrv[epdso_v5] = "v5";
  epdsostrv[epdso_v6] = "v6";
  epdsostrv[epdso_v7] = "v7";
  epdsostrv[epdso_v8] = "v8";
  epdsostrv[epdso_v9] = "v9";
/* set the EPD refcom operand strings */
  refcomstrv[refcom_conclude] = "conclude";
  refcomstrv[refcom_disconnect] = "disconnect";
  refcomstrv[refcom_execute] = "execute";
  refcomstrv[refcom_fault] = "fault";
  refcomstrv[refcom_inform] = "inform";
  refcomstrv[refcom_respond] = "respond";
  refcomstrv[refcom_reset] = "reset";
/* set the EPD refreq operand strings */
  refreqstrv[refreq_fault] = "fault";
  refreqstrv[refreq_reply] = "reply";
  refreqstrv[refreq_sign_on] = "sign_on";
  refreqstrv[refreq_sign_off] = "sign_off";
/* set the PGN Seven Tag Roster names */
  pgnstrstrv[pgnstr_event] = "Event";
  pgnstrstrv[pgnstr_site] = "Site";
  pgnstrstrv[pgnstr_date] = "Date";
  pgnstrstrv[pgnstr_round] = "Round";
  pgnstrstrv[pgnstr_white] = "White";
  pgnstrstrv[pgnstr_black] = "Black";
  pgnstrstrv[pgnstr_result] = "Result";
/* set the game termination indication marker vector */
  gtimstrv[gtim_w] = "1-0";
  gtimstrv[gtim_b] = "0-1";
  gtimstrv[gtim_d] = "1/2-1/2";
  gtimstrv[gtim_u] = "*";
/* clear the global game chain anchor pointers */
  head_gamptr = tail_gamptr = NULL;
/* clear the token chain anchor pointers */
  head_tknptr = tail_tknptr = NULL;
/* clear the current ply */
  ply = 0;
/* allocate the move tree */
  treeptr = treebaseptr = (mptrT) EPDMemoryGrab(sizeof(mT) * treeL);
/* allocate the tree stack entry stack */
  tseptr = tsebaseptr = (tseptrT) EPDMemoryGrab(sizeof(tseT) * pmhL);
/* allocate the environment stack entry stack */
  eseptr = esebaseptr = (eseptrT) EPDMemoryGrab(sizeof(eseT) * pmhL);
/* set the current position to be the initial array */
  EPDInitArray();
/* generation */
  EPDGenMoves();
  return;
}

/*--> EPDTerm: one time termination for EPD */
nonstatic void EPDTerm(void) {
/* release any existing game chain */
  EPDReleaseGameChain();
/* release any existing token chain */
  EPDReleaseTokenChain();
/* deallocate various stacks */
  EPDMemoryFree(esebaseptr);
  EPDMemoryFree(tsebaseptr);
  EPDMemoryFree(treebaseptr);
/* "Wanna see my sprocket collection?" */
  return;
}
#endif
/*<<< epd.c: EOF */