rssn-advanced 0.1.4

This is rssn-advanced: The next generation symbolic core of rssn.
Documentation
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
//! `extern "C"` entry points for the RSSN-Advanced API.
//!
//! Exposes a flat C API, capturing panics securely at the FFI boundary to
//! avoid undefined behavior (UB).

#![allow(unsafe_code)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use super::types::RssnStatus;
use crate::dag::builder::DagBuilder;
use crate::dag::node::DagNodeId;
use crate::heuristic::{HeuristicConfig, HeuristicEngine, SearchStrategy};
use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
use std::panic::catch_unwind;
use std::time::Duration;

/// Creates a new `DagBuilder` context.
///
/// Returns a raw pointer to the builder, or NULL if creation failed or panicked.
/// The returned pointer must be freed exactly once via [`rssn_dag_free`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_new() -> *mut DagBuilder {
    let result = catch_unwind(|| Box::into_raw(Box::new(DagBuilder::new())));
    result.unwrap_or(std::ptr::null_mut())
}

/// Releases the memory of a previously allocated `DagBuilder`.
///
/// # Safety
///
/// `builder` must be a pointer previously returned by [`rssn_dag_new`], or NULL.
/// After this call the pointer is dangling and must not be used.
/// Passing a pointer not from `rssn_dag_new`, or freeing twice, is undefined behaviour.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_free(builder: *mut DagBuilder) {
    if builder.is_null() {
        return;
    }
    let _ = catch_unwind(|| {
        let _ = unsafe { Box::from_raw(builder) };
    });
}

/// Allocates a new variable node in the DAG.
///
/// Returns the index of the variable node, or `u32::MAX` if a panic
/// occurred, the builder was null, or `name` was not valid UTF-8.
///
/// **Deprecated** — use [`rssn_dag_variable_v2`] for richer error reporting.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `name` must be a valid, non-null, null-terminated C string valid for the duration of
///   this call.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_variable(builder: *mut DagBuilder, name: *const c_char) -> u32 {
    if builder.is_null() || name.is_null() {
        return u32::MAX;
    }
    let result = catch_unwind(|| -> u32 {
        let builder_ref = unsafe { &mut *builder };
        let c_str = unsafe { CStr::from_ptr(name) };
        builder_ref
            .variable_bytes(c_str.to_bytes())
            .map_or(u32::MAX, DagNodeId::value)
    });
    result.unwrap_or(u32::MAX)
}

/// Allocates a new constant node in the DAG.
///
/// Returns `u32::MAX` on error.  **Deprecated** — use [`rssn_dag_constant_v2`].
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_constant(builder: *mut DagBuilder, val: f64) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        builder_ref.constant(val).value()
    });
    result.unwrap_or(u32::MAX)
}

/// Allocates a new addition node in the DAG: `lhs + rhs`.
///
/// Returns `u32::MAX` on error.  **Deprecated** — use [`rssn_dag_add_v2`].
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_add(builder: *mut DagBuilder, lhs: u32, rhs: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        builder_ref
            .add(DagNodeId::new(lhs), DagNodeId::new(rhs))
            .value()
    });
    result.unwrap_or(u32::MAX)
}

/// Simplifies a target expression using the default heuristic engine.
///
/// Returns the new root node index, or `u32::MAX` on error.
/// **Deprecated** — use [`rssn_dag_simplify_v2`] or [`rssn_dag_simplify_with_config`].
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_simplify(builder: *mut DagBuilder, root: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);

        let config = HeuristicConfig::default();
        let mut engine = HeuristicEngine::new(config, SearchStrategy::Greedy);

        engine.simplify(builder_ref, root_id).value()
    });
    result.unwrap_or(u32::MAX)
}

/// JIT compiles a target expression and writes the native function pointer to `out_fn`.
///
/// `out_fn` can be called via `rssn_dag_execute` or cast directly as `double (*)(const double*)`.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `out_fn` must be a valid, non-null pointer to a `*mut c_void` that the function will write to.
/// - The compiled function pointer written to `*out_fn` remains valid until the `JITModule`
///   backing this compiler is dropped. Do not call it after that.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile(
    builder: *mut DagBuilder,
    root: u32,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if builder.is_null() || out_fn.is_null() {
        return RssnStatus::NullPointer;
    }

    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);

        // Reuse the process-level JIT context to amortise Cranelift init cost.
        let ctx_mutex = crate::ffi::jit_context::global_jit_ctx();
        let mut ctx = ctx_mutex
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        ctx.compiler_mut()
            .compile(&ast)
            .map_or(RssnStatus::CompilationError, |compiled_fn| {
                let ptr = compiled_fn as *mut c_void;
                unsafe { *out_fn = ptr };
                RssnStatus::Success
            })
    });

    result.unwrap_or(RssnStatus::Panic)
}

/// JIT compiles a target expression and writes the native function pointer to `out_fn`.
///
/// `out_fn` can be called via `rssn_dag_execute` or cast directly as `double (*)(const double*)`.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile(
    _builder: *mut DagBuilder,
    _root: u32,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Executes a previously compiled JIT function with the given variable input array.
///
/// Returns `0.0` on error, which is indistinguishable from a legitimate zero result.
/// **Deprecated** — use [`rssn_dag_execute_v2`] to get a distinct error status.
///
/// # Safety
///
/// - `func` must be a valid function pointer previously written by [`rssn_dag_compile`],
///   with signature `double (*)(const double*)`.
/// - `variables` must be a valid pointer to an array of at least as many `f64` values
///   as there are distinct variables in the compiled expression, ordered by `SymbolId`.
/// - Both pointers must remain valid for the duration of this call.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute(func: *const c_void, variables: *const f64) -> f64 {
    if func.is_null() || variables.is_null() {
        return 0.0;
    }
    let result = catch_unwind(|| {
        let compiled_fn: crate::jit::compiler::CompiledExprFn =
            unsafe { std::mem::transmute(func) };
        compiled_fn(variables)
    });
    result.unwrap_or(0.0)
}

/// Executes a previously compiled JIT function with the given variable input array.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute(_func: *const c_void, _variables: *const f64) -> f64 {
    0.0
}

// =========================================================================
// Status-returning surface (canonical API)
// =========================================================================
//
// Each `*_v2` function takes an `out_id: *mut u32` (or equivalent) and
// returns [`RssnStatus`].  This is the canonical API for new code.
//
// The legacy `*` (non-v2) functions below return `u32::MAX` / 0.0 on error,
// which is ambiguous and cannot distinguish between different failure modes.
// They are **deprecated**: use the `_v2` equivalents for all new consumers.
// They are retained for ABI compatibility (e.g. existing Python/C++ callers).

/// Creates a new variable node. Status-returning variant.
///
/// On `Success`, writes the new node id to `*out_id`.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `name` must be a valid, non-null, null-terminated C string valid for this call.
/// - `out_id` must be a valid, non-null, writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_variable_v2(
    builder: *mut DagBuilder,
    name: *const c_char,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || name.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let builder_ref = unsafe { &mut *builder };
        let c_str = unsafe { CStr::from_ptr(name) };
        builder_ref
            .variable_bytes(c_str.to_bytes())
            .map_or(RssnStatus::InvalidUtf8, |id| {
                unsafe { *out_id = id.value() };
                RssnStatus::Success
            })
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Creates a new constant node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null, writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_constant_v2(
    builder: *mut DagBuilder,
    val: f64,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let builder_ref = unsafe { &mut *builder };
        let id = builder_ref.constant(val);
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Creates an addition node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null, writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_add_v2(
    builder: *mut DagBuilder,
    lhs: u32,
    rhs: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if lhs == u32::MAX || rhs == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let builder_ref = unsafe { &mut *builder };
        let id = builder_ref.add(DagNodeId::new(lhs), DagNodeId::new(rhs));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Executes a previously compiled JIT function. Status-returning variant.
///
/// On `Success`, writes the result to `*out_val`.
///
/// # Safety
///
/// - `func` must be a valid function pointer previously written by [`rssn_dag_compile`].
/// - `variables` must be a valid pointer to an array of at least as many `f64` values
///   as there are variables in the compiled expression, ordered by `SymbolId`.
/// - `out_val` must be a valid, non-null, writable `f64` pointer.
/// - All pointers must remain valid for the duration of this call.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute_v2(
    func: *const c_void,
    variables: *const f64,
    out_val: *mut f64,
) -> RssnStatus {
    if func.is_null() || variables.is_null() || out_val.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| {
        let compiled_fn: crate::jit::compiler::CompiledExprFn =
            unsafe { std::mem::transmute(func) };
        compiled_fn(variables)
    });
    result.map_or(RssnStatus::Panic, |val| {
        unsafe { *out_val = val };
        RssnStatus::Success
    })
}

/// Executes a previously compiled JIT function (stub for non-JIT builds).
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute_v2(
    _func: *const c_void,
    _variables: *const f64,
    _out_val: *mut f64,
) -> RssnStatus {
    RssnStatus::CompilationError
}

// =========================================================================
// Bulk / batch evaluation — amortises FFI overhead across many rows
// =========================================================================
//
// Calling `rssn_dag_execute` from an interpreted language (Python, Julia, …)
// inside a tight loop is ~200–400 ns per call just for the FFI dispatch,
// completely swamping the 1–5 ns the JIT needs per evaluation.
//
// These three functions bring the overhead down to O(1) per batch:
//
//   rssn_dag_execute_bulk   — scalar JIT fn called in a tight *Rust* loop;
//                             ~1–5 ns amortised overhead per row.
//   rssn_dag_compile_batch  — compiles a 2-row ILP vectorised version of the
//                             expression (Cranelift SSA dual-path).
//   rssn_dag_execute_batch  — dispatches the vectorised batch fn; fastest path.
//
// Both functions use *column-major* layout for variables:
//   vars_cols[var_index]  →  pointer to an array of n_rows f64 values.
// This mirrors NumPy's column-major convention and avoids transposition.

/// Evaluates a scalar JIT function over `n_rows` rows in a tight Rust loop,
/// eliminating per-row FFI overhead from the calling language.
///
/// `vars_cols` is an array of `n_vars` pointers; each pointer addresses a
/// contiguous column of `n_rows` `f64` values for the corresponding variable.
/// Columns must be ordered by **`SymbolId`**: the first variable interned into
/// the `DagBuilder` has `SymbolId` 0 and uses `vars_cols[0]`, etc.
///
/// One FFI call amortises setup cost over `n_rows` evaluations. For `n_rows`
/// ≥ 1 000, throughput is limited by memory bandwidth, not FFI overhead.
///
/// # Safety
///
/// - `func` must be a valid function pointer from [`rssn_dag_compile`].
/// - `vars_cols` must point to `n_vars` valid column pointers, each of length
///   `n_rows`.
/// - `out` must point to a writable array of `n_rows` `f64` values.
/// - All pointers must remain valid for the duration of this call.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_execute_bulk(
    func: *const c_void,
    vars_cols: *const *const f64,
    n_vars: u32,
    n_rows: usize,
    out: *mut f64,
) -> RssnStatus {
    if func.is_null() || out.is_null() {
        return RssnStatus::NullPointer;
    }
    if n_vars > 0 && vars_cols.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let compiled_fn: crate::jit::compiler::CompiledExprFn =
            unsafe { std::mem::transmute(func) };
        let nv = n_vars as usize;
        let cols: &[*const f64] = unsafe { std::slice::from_raw_parts(vars_cols, nv) };
        let out_slice: &mut [f64] = unsafe { std::slice::from_raw_parts_mut(out, n_rows) };

        // Prefetch distance: 16 rows × 8 bytes = 128 bytes ahead (2 cache lines).
        // This keeps the next rows warm in L1D while computing the current one.
        const PF: usize = 16;

        // Fixed stack buffer for the common case (≤ 8 variables).
        // Avoids heap allocation inside the hot loop.
        if nv <= 8 {
            let mut buf = [0.0f64; 8];
            for (row, out_val) in out_slice.iter_mut().enumerate() {
                // Software prefetch: hint the CPU to load the column data for
                // `row + PF` into L1D cache before we need it.
                #[cfg(target_arch = "x86_64")]
                unsafe {
                    use std::arch::x86_64::{_MM_HINT_T0, _mm_prefetch};
                    for &col in cols.iter().take(nv) {
                        let pf_ptr = col.add(row + PF).cast::<i8>();
                        if row + PF < n_rows {
                            _mm_prefetch(pf_ptr, _MM_HINT_T0);
                        }
                    }
                }
                #[cfg(target_arch = "aarch64")]
                use std::arch::asm;
                #[cfg(target_arch = "aarch64")]
                if row + PF < n_rows {
                    for &col in cols.iter().take(nv) {
                        unsafe {
                            let target_ptr = col.add(row + PF);

                            asm!(
                                "prfm pldl1keep, [{ptr}]",
                                ptr = in(reg) target_ptr,
                                options(nostack, preserves_flags, readonly)
                            );
                        }
                    }
                }
                for (vi, &col) in cols.iter().enumerate() {
                    buf[vi] = unsafe { *col.add(row) };
                }
                *out_val = compiled_fn(buf.as_ptr());
            }
        } else {
            let mut buf = vec![0.0f64; nv];
            for (row, out_val) in out_slice.iter_mut().enumerate() {
                #[cfg(target_arch = "x86_64")]
                unsafe {
                    use std::arch::x86_64::{_MM_HINT_T0, _mm_prefetch};
                    for &col in cols.iter().take(nv) {
                        if row + PF < n_rows {
                            _mm_prefetch(col.add(row + PF).cast::<i8>(), _MM_HINT_T0);
                        }
                    }
                }
                for (vi, &col) in cols.iter().enumerate() {
                    buf[vi] = unsafe { *col.add(row) };
                }
                *out_val = compiled_fn(buf.as_ptr());
            }
        }
        RssnStatus::Success
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute_bulk(
    _func: *const c_void,
    _vars_cols: *const *const f64,
    _n_vars: u32,
    _n_rows: usize,
    _out: *mut f64,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Compiles a 2-row ILP-vectorised version of the expression.
///
/// The Cranelift backend generates two independent SSA chains that evaluate
/// two rows simultaneously, keeping execution units busy across instruction
/// latency gaps. For memory-bound workloads this approaches 2× scalar
/// throughput; for compute-bound workloads the speedup is limited by
/// available instruction-level parallelism.
///
/// On success writes the batch function pointer to `*out_fn`.
/// Use [`rssn_dag_execute_batch`] to dispatch the compiled function.
///
/// Returns [`RssnStatus::CompilationError`] if the expression cannot be
/// vectorised (e.g. contains non-vectorisable operations).
///
/// # Safety
///
/// Same as [`rssn_dag_compile`].
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_compile_batch(
    builder: *mut DagBuilder,
    root: u32,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if builder.is_null() || out_fn.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);
        let ctx_mutex = crate::ffi::jit_context::global_jit_ctx();
        let mut ctx = ctx_mutex
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        match ctx.compiler_mut().compile_batch_f64x2(&ast) {
            Ok(Some(batch_fn)) => {
                unsafe { *out_fn = batch_fn as *mut c_void };
                RssnStatus::Success
            }
            _ => RssnStatus::CompilationError,
        }
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_batch(
    _builder: *mut DagBuilder,
    _root: u32,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Compiles a vectorized batch evaluation function using true F64X4 SIMD.
///
/// Same as [`rssn_dag_compile_batch`] but targets F64X4 wide vectors.
///
/// # Safety
///
/// Same as [`rssn_dag_compile`].
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_compile_batch_f64x4(
    builder: *mut DagBuilder,
    root: u32,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if builder.is_null() || out_fn.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);
        let ctx_mutex = crate::ffi::jit_context::global_jit_ctx();
        let mut ctx = ctx_mutex
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        match ctx.compiler_mut().compile_batch_f64x4(&ast) {
            Ok(Some(batch_fn)) => {
                unsafe { *out_fn = batch_fn as *mut c_void };
                RssnStatus::Success
            }
            _ => RssnStatus::CompilationError,
        }
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_batch_f64x4(
    _builder: *mut DagBuilder,
    _root: u32,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Compiles a vectorized batch evaluation function processing 8 rows per
/// loop iteration via four independent `F64X2` SIMD chains (ILP-8).
///
/// Same calling convention as [`rssn_dag_compile_batch`]; use
/// [`rssn_dag_execute_batch`] to dispatch the returned function pointer.
///
/// # Safety
///
/// Same as [`rssn_dag_compile`].
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_compile_batch_f64x8(
    builder: *mut DagBuilder,
    root: u32,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if builder.is_null() || out_fn.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);
        let ctx_mutex = crate::ffi::jit_context::global_jit_ctx();
        let mut ctx = ctx_mutex
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        match ctx.compiler_mut().compile_batch_f64x8(&ast) {
            Ok(Some(batch_fn)) => {
                unsafe { *out_fn = batch_fn as *mut c_void };
                RssnStatus::Success
            }
            _ => RssnStatus::CompilationError,
        }
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_batch_f64x8(
    _builder: *mut DagBuilder,
    _root: u32,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Dispatches a batch-compiled function over `n_rows` rows.
///
/// `vars_cols` is an array of column pointers (one per variable, each of
/// length `n_rows`).  The batch function processes two rows per cycle via
/// independent SSA chains; a scalar tail handles any odd final row.
///
/// # Safety
///
/// - `batch_fn` must be a valid function pointer from [`rssn_dag_compile_batch`].
/// - `vars_cols` must point to an array of column pointers, each of length `n_rows`.
/// - `out` must point to a writable array of `n_rows` `f64` values.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_execute_batch(
    batch_fn: *const c_void,
    vars_cols: *const *const f64,
    n_rows: usize,
    out: *mut f64,
) -> RssnStatus {
    if batch_fn.is_null() || vars_cols.is_null() || out.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let f: crate::jit::compiler::CompiledBatchFn = unsafe { std::mem::transmute(batch_fn) };
        f(vars_cols, n_rows, out);
        RssnStatus::Success
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute_batch(
    _batch_fn: *const c_void,
    _vars_cols: *const *const f64,
    _n_rows: usize,
    _out: *mut f64,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Helper wrapper to safely send raw column pointers and output pointer
/// across fiber boundaries in the parallel evaluator.
struct SendRaw {
    col_ptrs: Vec<*const f64>,
    out_ptr: *mut f64,
}

// SAFETY: The FFI caller guarantees that all pointer columns and the output
// pointer remain valid for the duration of the call, and each worker fiber
// writes to a disjoint sub-slice of `out_ptr`.
unsafe impl Send for SendRaw {}
unsafe impl Sync for SendRaw {}

/// Dispatches a batch-compiled function over `n_rows` rows using the
/// dtact fiber runtime for multi-core parallelism.
///
/// Splits the row range into `n_workers` equal chunks (default: number of
/// logical CPUs, capped at 16) and evaluates each chunk on a separate dtact
/// fiber, then joins all fibers before returning.
///
/// **When to prefer over [`rssn_dag_execute_batch`]:**
/// - `n_rows` > ~100 000 (fiber-spawn overhead amortised)
/// - Expression is compute-heavy (many operators, not trivially vectorizable)
/// - Multiple CPU cores are available and not already saturated
///
/// **Threading model:** uses `parallel_for_each` from `src/runtime` (dtact
/// fibers, lock-free pool, ABA-safe Treiber stack — no rayon, no OS threads).
///
/// # Safety
///
/// Same as [`rssn_dag_execute_batch`]. Additionally the `vars_cols` and `out`
/// pointers must remain valid until the call returns (all fibers have joined).
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_execute_batch_parallel(
    batch_fn: *const c_void,
    vars_cols: *const *const f64,
    n_vars: u32,
    n_rows: usize,
    out: *mut f64,
    n_workers: u32, // 0 = auto-detect (logical CPUs, capped at 16)
) -> RssnStatus {
    if batch_fn.is_null() || vars_cols.is_null() || out.is_null() {
        return RssnStatus::NullPointer;
    }
    if n_rows == 0 {
        return RssnStatus::Success;
    }

    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let f: crate::jit::compiler::CompiledBatchFn = unsafe { std::mem::transmute(batch_fn) };
        let nv = n_vars as usize;

        // Number of workers: auto or caller-supplied, capped at 16.
        let workers: usize = if n_workers == 0 {
            std::thread::available_parallelism()
                .map_or(4, std::num::NonZero::get)
                .min(16)
        } else {
            (n_workers as usize).min(16)
        };

        // Only parallelise if the overhead is worth it (≥8 rows per worker).
        let workers = workers.min(n_rows / 8).max(1);

        // Partition row range into contiguous slices.
        let chunk = n_rows.div_ceil(workers);

        let gate = crate::runtime::ensure_runtime();

        // Build tasks: each captures its own slice bounds and calls the JIT
        // function directly on its sub-slice of vars_cols / out.
        // SAFETY contract for the captured raw pointers:
        //   • `vars_cols` points to `nv` column pointers, each of length
        //     `n_rows` — slices stay within bounds.
        //   • `out` is `n_rows` writable f64 values — each worker writes
        //     into a unique, non-overlapping slice (row_start..row_end).
        //   • The caller promises both live until this function returns;
        //     all fibers are joined before we exit.
        let tasks: Vec<_> = (0..workers)
            .map(|w| {
                let row_start = w * chunk;
                let row_end = (row_start + chunk).min(n_rows);
                let slice_len = row_end - row_start;

                // Build offset column pointer array on the stack (up to 16 vars).
                // Each pointer is advanced by row_start so the JIT function sees
                // a 0-based sub-array of length slice_len.
                // We heap-allocate to satisfy 'static closure requirements.
                let col_ptrs: Vec<*const f64> = (0..nv)
                    .map(|vi| unsafe {
                        let col = *vars_cols.add(vi);
                        col.add(row_start)
                    })
                    .collect();
                let out_ptr: *mut f64 = unsafe { out.add(row_start) };

                let data = SendRaw { col_ptrs, out_ptr };

                move || {
                    let d = data;
                    // Mask all SSE floating point exceptions (0x1f80 sets IM, DM, ZM, OM, UM, PM mask bits)
                    // to prevent SIGFPE traps on inexact or division-by-zero results in fibers.
                    #[cfg(target_arch = "x86_64")]
                    unsafe {
                        #[allow(deprecated)]
                        use std::arch::x86_64::{_mm_getcsr, _mm_setcsr};
                        #[allow(deprecated)]
                        _mm_setcsr(_mm_getcsr() | 0x1f80);
                    }
                    let cols_ptr = d.col_ptrs.as_ptr();
                    f(cols_ptr, slice_len, d.out_ptr);
                }
            })
            .collect();

        crate::runtime::parallel_for_each(gate, tasks);
        RssnStatus::Success
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_execute_batch_parallel(
    _batch_fn: *const c_void,
    _vars_cols: *const *const f64,
    _n_vars: u32,
    _n_rows: usize,
    _out: *mut f64,
    _n_workers: u32,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Simplifies an expression. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null, writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_simplify_v2(
    builder: *mut DagBuilder,
    root: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let config = HeuristicConfig::default();
        let mut engine = HeuristicEngine::new(config, SearchStrategy::Greedy);
        let id = engine.simplify(builder_ref, root_id);
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// JIT compiles a target expression. Status-returning variant.
///
/// On `Success`, writes the compiled function pointer to `*out_fn`.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder` from [`rssn_dag_new`].
/// - `out_fn` must be a valid, non-null, writable `*mut c_void` pointer.
/// - The compiled function pointer remains valid until the `JITModule` is dropped.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_v2(
    builder: *mut DagBuilder,
    root: u32,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if builder.is_null() || out_fn.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(|| {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);
        // Reuse the process-level JIT context.
        let ctx_mutex = crate::ffi::jit_context::global_jit_ctx();
        let mut ctx = ctx_mutex
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        ctx.compiler_mut()
            .compile(&ast)
            .map_or(RssnStatus::CompilationError, |compiled_fn| {
                unsafe { *out_fn = compiled_fn as *mut c_void };
                RssnStatus::Success
            })
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// JIT compiles a target expression (stub for non-JIT builds).
///
/// Always returns [`RssnStatus::CompilationError`] when the `cranelift-jit`
/// feature is not enabled.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_v2(
    _builder: *mut DagBuilder,
    _root: u32,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// C-compatible simplification configuration.
///
/// Pass a pointer to this struct to [`rssn_dag_simplify_with_config`] to
/// override the default heuristic parameters. Pass NULL to use defaults.
#[repr(C)]
pub struct RssnSimplifyConfig {
    /// Maximum rewrite depth (default: 10).
    pub max_depth: u32,
    /// Wall-clock timeout in milliseconds (default: 500).
    pub timeout_ms: u64,
    /// Approximate-pruning aggressiveness in `[0.0, 1.0]` (default: 0.1).
    pub aggressiveness: f64,
}

/// Simplifies an expression using a caller-supplied configuration.
///
/// If `config` is NULL, behaves identically to [`rssn_dag_simplify_v2`].
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer to a `DagBuilder`.
/// - `out_id` must be a valid, non-null, writable `u32` pointer.
/// - If `config` is non-null, it must point to a valid `RssnSimplifyConfig`.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_simplify_with_config(
    builder: *mut DagBuilder,
    root: u32,
    config: *const RssnSimplifyConfig,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let (max_depth, timeout_ms, aggressiveness) = if config.is_null() {
        let def = HeuristicConfig::default();
        (
            def.max_depth,
            def.timeout.as_millis() as u64,
            def.simplification_aggressiveness,
        )
    } else {
        let c = unsafe { &*config };
        (c.max_depth as usize, c.timeout_ms, c.aggressiveness)
    };
    let result = catch_unwind(|| -> RssnStatus {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let cfg = HeuristicConfig::default()
            .max_depth(max_depth)
            .timeout(Duration::from_millis(timeout_ms))
            .simplification_aggressiveness(aggressiveness);
        let mut engine = HeuristicEngine::new(cfg, SearchStrategy::Greedy);
        let id = engine.simplify(builder_ref, root_id);
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

// =========================================================================
// Full operator surface: sub, mul, div, pow, mod, neg
// =========================================================================
//
// Each operator comes in two variants:
//   • Legacy (no suffix)  — returns u32::MAX on error. **Deprecated.**
//   • Canonical (_v2)     — returns RssnStatus; writes node id to *out_id.
//
// New code should use the _v2 forms.

/// Allocates a subtraction node: `lhs - rhs`.
///
/// Returns `u32::MAX` on error or null input.
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_sub(builder: *mut DagBuilder, lhs: u32, rhs: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        b.sub(DagNodeId::new(lhs), DagNodeId::new(rhs)).value()
    })
    .unwrap_or(u32::MAX)
}

/// Allocates a subtraction node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_sub_v2(
    builder: *mut DagBuilder,
    lhs: u32,
    rhs: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if lhs == u32::MAX || rhs == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let id = b.sub(DagNodeId::new(lhs), DagNodeId::new(rhs));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

/// Allocates a multiplication node: `lhs * rhs`.
///
/// Returns `u32::MAX` on error or null input.
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_mul(builder: *mut DagBuilder, lhs: u32, rhs: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        b.mul(DagNodeId::new(lhs), DagNodeId::new(rhs)).value()
    })
    .unwrap_or(u32::MAX)
}

/// Allocates a multiplication node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_mul_v2(
    builder: *mut DagBuilder,
    lhs: u32,
    rhs: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if lhs == u32::MAX || rhs == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let id = b.mul(DagNodeId::new(lhs), DagNodeId::new(rhs));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

/// Allocates a division node: `lhs / rhs`.
///
/// Returns `u32::MAX` on error or null input.
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_div(builder: *mut DagBuilder, lhs: u32, rhs: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        b.div(DagNodeId::new(lhs), DagNodeId::new(rhs)).value()
    })
    .unwrap_or(u32::MAX)
}

/// Allocates a division node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_div_v2(
    builder: *mut DagBuilder,
    lhs: u32,
    rhs: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if lhs == u32::MAX || rhs == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let id = b.div(DagNodeId::new(lhs), DagNodeId::new(rhs));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

/// Allocates an exponentiation node: `base ^ exp`.
///
/// Returns `u32::MAX` on error or null input.
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_pow(builder: *mut DagBuilder, base: u32, exp: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        b.pow(DagNodeId::new(base), DagNodeId::new(exp)).value()
    })
    .unwrap_or(u32::MAX)
}

/// Allocates an exponentiation node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_pow_v2(
    builder: *mut DagBuilder,
    base: u32,
    exp: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if base == u32::MAX || exp == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let id = b.pow(DagNodeId::new(base), DagNodeId::new(exp));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

/// Allocates a modulo node: `lhs % rhs`.
///
/// Returns `u32::MAX` on error or null input.
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_mod(builder: *mut DagBuilder, lhs: u32, rhs: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        b.modulo(DagNodeId::new(lhs), DagNodeId::new(rhs)).value()
    })
    .unwrap_or(u32::MAX)
}

/// Allocates a modulo node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_mod_v2(
    builder: *mut DagBuilder,
    lhs: u32,
    rhs: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if lhs == u32::MAX || rhs == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let id = b.modulo(DagNodeId::new(lhs), DagNodeId::new(rhs));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

/// Allocates a unary negation node: `-operand`.
///
/// Returns `u32::MAX` on error or null input.
///
/// # Safety
///
/// `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_neg(builder: *mut DagBuilder, operand: u32) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        b.neg(DagNodeId::new(operand)).value()
    })
    .unwrap_or(u32::MAX)
}

/// Allocates a unary negation node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_neg_v2(
    builder: *mut DagBuilder,
    operand: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if operand == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let id = b.neg(DagNodeId::new(operand));
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

// =========================================================================
// T6.4 — Parse expression from C string
// =========================================================================

/// Parses a mathematical expression from a C string into the DAG.
///
/// The expression uses the standard infix syntax: `+`, `-`, `*`, `/`,
/// `^` (exponentiation), `%` (modulo), parentheses, numeric literals,
/// and identifier names for variables.
///
/// On `Success`, writes the root node id of the parsed expression to
/// `*out_id`. On failure returns [`RssnStatus::ParseError`].
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `expr` must be a valid, non-null, null-terminated C string.
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_parse(
    builder: *mut DagBuilder,
    expr: *const c_char,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || expr.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let b = unsafe { &mut *builder };
        let c_str = unsafe { CStr::from_ptr(expr) };
        let Ok(s) = c_str.to_str() else {
            return RssnStatus::InvalidUtf8;
        };
        crate::parser::expr::parse_expression(s, b).map_or(RssnStatus::ParseError, |root_id| {
            unsafe { *out_id = root_id.value() };
            RssnStatus::Success
        })
    });
    result.unwrap_or(RssnStatus::Panic)
}

// =========================================================================
// T6.5 — Function registration and call node construction
// =========================================================================

/// Interns a function name and returns its numeric `FnId`.
///
/// The returned id can be used with [`rssn_dag_call_fn`] to build
/// function-call nodes, and with the JIT custom-function registration
/// APIs to bind native implementations.
///
/// Returns `u32::MAX` on null input or if interning fails.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `name` must be a valid, non-null, null-terminated C string.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_intern_function(builder: *mut DagBuilder, name: *const c_char) -> u32 {
    if builder.is_null() || name.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let c_str = unsafe { CStr::from_ptr(name) };
        let Ok(s) = c_str.to_str() else {
            return u32::MAX;
        };
        b.intern_function(s).0
    })
    .unwrap_or(u32::MAX)
}

/// Builds a function-call node for a previously interned function.
///
/// `args` points to an array of `n_args` node ids. The node ids must all be
/// valid (not `u32::MAX`).
///
/// Returns the new node id, or `u32::MAX` on error.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `args` must point to an array of at least `n_args` valid `u32` values,
///   or be null when `n_args == 0`.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_call_fn(
    builder: *mut DagBuilder,
    fn_id: u32,
    args: *const u32,
    n_args: u32,
) -> u32 {
    if builder.is_null() {
        return u32::MAX;
    }
    if n_args > 0 && args.is_null() {
        return u32::MAX;
    }
    catch_unwind(|| {
        let b = unsafe { &mut *builder };
        let arg_ids: Vec<DagNodeId> = if n_args == 0 {
            Vec::new()
        } else {
            let slice = unsafe { std::slice::from_raw_parts(args, n_args as usize) };
            if slice.contains(&u32::MAX) {
                return u32::MAX;
            }
            slice.iter().map(|&id| DagNodeId::new(id)).collect()
        };
        b.function_call(crate::dag::symbol::FnId(fn_id), &arg_ids)
            .value()
    })
    .unwrap_or(u32::MAX)
}

/// Builds a function-call node. Status-returning variant.
///
/// # Safety
///
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `args` must point to an array of at least `n_args` valid `u32` values,
///   or be null when `n_args == 0`.
/// - `out_id` must be a valid, non-null writable `u32` pointer.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_call_fn_v2(
    builder: *mut DagBuilder,
    fn_id: u32,
    args: *const u32,
    n_args: u32,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if n_args > 0 && args.is_null() {
        return RssnStatus::NullPointer;
    }
    catch_unwind(|| -> RssnStatus {
        let b = unsafe { &mut *builder };
        let arg_ids: Vec<DagNodeId> = if n_args == 0 {
            Vec::new()
        } else {
            let slice = unsafe { std::slice::from_raw_parts(args, n_args as usize) };
            if slice.contains(&u32::MAX) {
                return RssnStatus::InvalidNodeId;
            }
            slice.iter().map(|&id| DagNodeId::new(id)).collect()
        };
        let id = b.function_call(crate::dag::symbol::FnId(fn_id), &arg_ids);
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    })
    .unwrap_or(RssnStatus::Panic)
}

// =========================================================================
// T6.6 — JIT custom-function registration from C
// =========================================================================
//
// These functions allow C callers to register native function pointers so
// the JIT can compile call nodes that reference them. The `fn_id` must
// match the id returned by `rssn_dag_intern_function`.

/// Type for a C-callable `extern "C" fn(f64) -> f64`.
pub type RssnCustomFn1 = extern "C" fn(f64) -> f64;
/// Type for a C-callable `extern "C" fn(f64, f64) -> f64`.
pub type RssnCustomFn2 = extern "C" fn(f64, f64) -> f64;
/// Type for a C-callable `extern "C" fn(f64, f64, f64) -> f64`.
pub type RssnCustomFn3 = extern "C" fn(f64, f64, f64) -> f64;

/// Registers a one-argument native function with the persistent JIT context
/// so it can be called from compiled expressions.
///
/// The `fn_id` must have been obtained via [`rssn_dag_intern_function`].
/// The `func` pointer must remain valid for the lifetime of the JIT context.
///
/// # Safety
///
/// `func` must be a valid function pointer with the signature `double(double)`.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_jit_register_fn_1(
    ctx: *mut super::jit_context::RssnJitContext,
    fn_id: u32,
    func: Option<extern "C" fn(f64) -> f64>,
) -> RssnStatus {
    if ctx.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(func_ptr) = func else {
        return RssnStatus::NullPointer;
    };
    catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx_ref = unsafe { &mut *ctx };
        ctx_ref
            .compiler_mut()
            .register_custom_function(crate::dag::symbol::FnId(fn_id), func_ptr);
        RssnStatus::Success
    }))
    .unwrap_or(RssnStatus::Panic)
}

/// Registers a one-argument native function (stub for non-JIT builds).
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_jit_register_fn_1(
    _ctx: *mut super::jit_context::RssnJitContext,
    _fn_id: u32,
    _func: Option<extern "C" fn(f64) -> f64>,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Registers a two-argument native function with the persistent JIT context.
///
/// # Safety
///
/// `func` must be a valid function pointer with the signature `double(double, double)`.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_jit_register_fn_2(
    ctx: *mut super::jit_context::RssnJitContext,
    fn_id: u32,
    func: Option<extern "C" fn(f64, f64) -> f64>,
) -> RssnStatus {
    if ctx.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(func_ptr) = func else {
        return RssnStatus::NullPointer;
    };
    catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx_ref = unsafe { &mut *ctx };
        ctx_ref
            .compiler_mut()
            .register_custom_function_2(crate::dag::symbol::FnId(fn_id), func_ptr);
        RssnStatus::Success
    }))
    .unwrap_or(RssnStatus::Panic)
}

/// Registers a two-argument native function (stub for non-JIT builds).
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_jit_register_fn_2(
    _ctx: *mut super::jit_context::RssnJitContext,
    _fn_id: u32,
    _func: Option<extern "C" fn(f64, f64) -> f64>,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Registers a three-argument native function with the persistent JIT context.
///
/// # Safety
///
/// `func` must be a valid function pointer with the signature `double(double, double, double)`.
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_jit_register_fn_3(
    ctx: *mut super::jit_context::RssnJitContext,
    fn_id: u32,
    func: Option<extern "C" fn(f64, f64, f64) -> f64>,
) -> RssnStatus {
    if ctx.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(func_ptr) = func else {
        return RssnStatus::NullPointer;
    };
    catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx_ref = unsafe { &mut *ctx };
        ctx_ref
            .compiler_mut()
            .register_custom_function_3(crate::dag::symbol::FnId(fn_id), func_ptr);
        RssnStatus::Success
    }))
    .unwrap_or(RssnStatus::Panic)
}

/// Registers a three-argument native function (stub for non-JIT builds).
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_jit_register_fn_3(
    _ctx: *mut super::jit_context::RssnJitContext,
    _fn_id: u32,
    _func: Option<extern "C" fn(f64, f64, f64) -> f64>,
) -> RssnStatus {
    RssnStatus::CompilationError
}

// =========================================================================
// T6.7 — JIT compile with explicit optimisation configuration
// =========================================================================

/// C-compatible JIT optimisation configuration.
///
/// Fields mirror [`crate::jit::compiler::OptConfig`]. Pass a pointer to this
/// struct to [`rssn_dag_compile_with_opts`]; pass NULL to use the defaults.
#[cfg(feature = "cranelift-jit")]
#[repr(C)]
pub struct RssnOptConfig {
    /// Maximum integer exponent expanded without a `powf` call (default: 16).
    pub max_int_pow: u32,
    /// Non-zero to expand `x^0.5` to a native `sqrt` instruction (default: 1).
    pub expand_sqrt: u32,
    /// Non-zero to replace `x / C` with `x * (1/C)` (default: 0).
    pub allow_reciprocal_math: u32,
    /// Non-zero to skip divide-by-zero guards when the denominator is proven
    /// non-zero by the analysis pass (default: 1).
    pub elide_nan_guard: u32,
    /// Non-zero to reuse SSA values for repeated DAG sub-expressions (default: 1).
    pub enable_cse: u32,
}

/// Dummy C-compatible JIT optimisation configuration for non-JIT builds.
#[cfg(not(feature = "cranelift-jit"))]
#[repr(C)]
pub struct RssnOptConfig;

/// Compiles a DAG expression with explicit optimisation knobs.
///
/// If `opts` is NULL, uses [`RssnOptConfig`] defaults (equivalent to
/// [`rssn_dag_compile_v2`]).
///
/// # Safety
///
/// - `ctx` must be a valid, non-null pointer from [`rssn_jit_context_new`](crate::ffi::jit_context::rssn_jit_context_new).
/// - `builder` must be a valid, non-null pointer from [`rssn_dag_new`].
/// - `out_fn` must be a valid, non-null writable pointer.
/// - If `opts` is non-null it must point to a valid [`RssnOptConfig`].
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_with_opts(
    ctx: *mut super::jit_context::RssnJitContext,
    builder: *mut DagBuilder,
    root: u32,
    opts: *const RssnOptConfig,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if ctx.is_null() || builder.is_null() || out_fn.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let ctx_ref = unsafe { &mut *ctx };
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);

        let jit_opts = if opts.is_null() {
            crate::jit::compiler::OptConfig::default()
        } else {
            let c = unsafe { &*opts };
            crate::jit::compiler::OptConfig {
                max_int_pow: c.max_int_pow,
                expand_sqrt: c.expand_sqrt != 0,
                allow_reciprocal_math: c.allow_reciprocal_math != 0,
                elide_nan_guard: c.elide_nan_guard != 0,
                enable_cse: c.enable_cse != 0,
            }
        };

        ctx_ref
            .compiler_mut()
            .compile_with_opts(&ast, &jit_opts)
            .map_or(RssnStatus::CompilationError, |compiled_fn| {
                unsafe { *out_fn = compiled_fn as *mut c_void };
                RssnStatus::Success
            })
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// Stub for non-JIT builds: always returns `CompilationError`.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_with_opts(
    _ctx: *mut super::jit_context::RssnJitContext,
    _builder: *mut DagBuilder,
    _root: u32,
    _opts: *const RssnOptConfig,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

// =========================================================================
// T6.8 — C-side rewrite rule registration
// =========================================================================
//
// The C rule callback receives:
//   - A pointer to the builder (may call rssn_dag_* to create nodes).
//   - The node kind discriminant (see `RssnKind` below).
//   - A pointer to the child node-id array and child count.
//   - User data (an opaque void* set at registration time).
// The callback returns the replacement node id, or u32::MAX to pass.

/// Discriminant values for `SymbolKind` variants, matching the Rust enum.
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub enum RssnKind {
    /// A named variable.
    Variable = 0,
    /// A numeric constant.
    Constant = 1,
    /// `Add` operator.
    Add = 2,
    /// `Sub` operator.
    Sub = 3,
    /// `Mul` operator.
    Mul = 4,
    /// `Div` operator.
    Div = 5,
    /// `Pow` operator.
    Pow = 6,
    /// `Mod` operator.
    Mod = 7,
    /// Unary `Neg` operator.
    Neg = 8,
    /// A custom function call.
    Function = 9,
}

/// Opaque handle for a registered C rewrite rule registry.
///
/// Create with [`rssn_rule_registry_new`]; free with [`rssn_rule_registry_free`].
pub struct RssnRuleRegistry {
    inner: std::sync::Arc<crate::heuristic::rule_registry::RuleRegistry>,
}

/// C-callable rewrite rule callback.
///
/// - `builder`: pointer to the `DagBuilder`; call `rssn_dag_*` to create nodes.
/// - `kind`: node kind discriminant (see [`RssnKind`]).
/// - `children`: pointer to an array of child node ids (length `n_children`).
/// - `n_children`: number of children.
/// - `user_data`: the opaque pointer supplied at registration time.
///
/// Return the replacement node id, or `u32::MAX` to leave the node unchanged
/// (pass to the next rule).
pub type RssnRuleCallback = unsafe extern "C" fn(
    builder: *mut DagBuilder,
    kind: u8,
    children: *const u32,
    n_children: u32,
    user_data: *mut c_void,
) -> u32;

/// Creates a new, empty rule registry.
///
/// The returned pointer must be freed with [`rssn_rule_registry_free`].
/// Returns NULL if construction panics.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_rule_registry_new() -> *mut RssnRuleRegistry {
    catch_unwind(|| {
        Box::into_raw(Box::new(RssnRuleRegistry {
            inner: std::sync::Arc::new(crate::heuristic::rule_registry::RuleRegistry::new()),
        }))
    })
    .unwrap_or(std::ptr::null_mut())
}

/// Frees a rule registry previously created by [`rssn_rule_registry_new`].
///
/// Passing NULL is a safe no-op.
///
/// # Safety
///
/// `registry` must be a pointer returned by [`rssn_rule_registry_new`] or NULL.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_rule_registry_free(registry: *mut RssnRuleRegistry) {
    if registry.is_null() {
        return;
    }
    let _ = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let _ = unsafe { Box::from_raw(registry) };
    }));
}

/// Registers a C callback as a rewrite rule.
///
/// - `name`: human-readable rule name (null-terminated C string, for fingerprinting).
/// - `callback`: the rule function; called during simplification for each node.
/// - `priority`: higher values are tried first (default-priority rules use 0).
/// - `kind_filter`: if non-negative, the rule is only tried for nodes with this
///   kind discriminant (see [`RssnKind`]). Pass `-1` for a wildcard rule.
/// - `user_data`: opaque pointer forwarded to every callback invocation.
///
/// # Safety
///
/// - `registry` must be a valid, non-null pointer from [`rssn_rule_registry_new`].
/// - `name` must be a valid, non-null, null-terminated C string.
/// - `callback` must be a valid, non-null function pointer.
/// - `user_data` must remain valid for the lifetime of the registry.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_rule_register(
    registry: *mut RssnRuleRegistry,
    name: *const c_char,
    callback: Option<RssnRuleCallback>,
    priority: i32,
    kind_filter: i32,
    user_data: *mut c_void,
) -> RssnStatus {
    if registry.is_null() || name.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(cb) = callback else {
        return RssnStatus::NullPointer;
    };

    let name_str = {
        let c_str = unsafe { CStr::from_ptr(name) };
        match c_str.to_str() {
            Ok(s) => s.to_owned(),
            Err(_) => return RssnStatus::InvalidUtf8,
        }
    };

    // Transmute user_data to usize so the closure is Send + Sync.
    let user_data_addr = user_data as usize;

    let kind_opt = if kind_filter < 0 {
        None
    } else {
        use crate::dag::symbol::{OpKind, SymbolKind};
        match kind_filter as u8 {
            0 => Some(SymbolKind::Variable(crate::dag::symbol::SymbolId(0))),
            1 => Some(SymbolKind::Constant(0.0)),
            2 => Some(SymbolKind::Operator(OpKind::Add)),
            3 => Some(SymbolKind::Operator(OpKind::Sub)),
            4 => Some(SymbolKind::Operator(OpKind::Mul)),
            5 => Some(SymbolKind::Operator(OpKind::Div)),
            6 => Some(SymbolKind::Operator(OpKind::Pow)),
            7 => Some(SymbolKind::Operator(OpKind::Mod)),
            8 => Some(SymbolKind::Operator(OpKind::Neg)),
            9 => Some(SymbolKind::Function(crate::dag::symbol::FnId(0))),
            _ => return RssnStatus::InvalidNodeId,
        }
    };

    catch_unwind(std::panic::AssertUnwindSafe(|| {
        let reg_ref = unsafe { &mut *registry };
        // Get a mutable reference to the inner registry through Arc.
        // If the Arc is uniquely owned (typical during registration phase),
        // `get_mut` succeeds. After the first `clone()` by the engine we can
        // no longer add rules — callers must register before simplifying.
        let Some(inner_mut) = std::sync::Arc::get_mut(&mut reg_ref.inner) else {
            return RssnStatus::RuleConflict;
        };
        inner_mut.register_named(
            &name_str,
            move |builder, kind, children| {
                // Flatten children into a temporary u32 array.
                let child_ids: Vec<u32> = children.iter().map(|id| id.value()).collect();
                let user_data_ptr = user_data_addr as *mut c_void;
                // SAFETY: the caller guarantees the callback and user_data are valid.
                let result = unsafe {
                    cb(
                        std::ptr::from_mut::<DagBuilder>(builder),
                        kind_to_discriminant(&kind),
                        child_ids.as_ptr(),
                        child_ids.len() as u32,
                        user_data_ptr,
                    )
                };
                if result == u32::MAX {
                    None
                } else {
                    Some(DagNodeId::new(result))
                }
            },
            priority,
            kind_opt,
        );
        RssnStatus::Success
    }))
    .unwrap_or(RssnStatus::Panic)
}

/// Simplifies an expression using a caller-supplied C rule registry and configuration.
///
/// If `registry` is NULL, only the built-in heuristic patterns are applied.
/// If `config` is NULL, defaults are used.
///
/// # Safety
///
/// - `builder` and `out_id` must be valid, non-null pointers.
/// - If `registry` is non-null it must be from [`rssn_rule_registry_new`].
/// - If `config` is non-null it must point to a valid [`RssnSimplifyConfig`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_simplify_with_rules(
    builder: *mut DagBuilder,
    root: u32,
    registry: *mut RssnRuleRegistry,
    config: *const RssnSimplifyConfig,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let (max_depth, timeout_ms, aggressiveness) = if config.is_null() {
        let def = HeuristicConfig::default();
        (
            def.max_depth,
            def.timeout.as_millis() as u64,
            def.simplification_aggressiveness,
        )
    } else {
        let c = unsafe { &*config };
        (c.max_depth as usize, c.timeout_ms, c.aggressiveness)
    };

    catch_unwind(std::panic::AssertUnwindSafe(|| -> RssnStatus {
        let builder_ref = unsafe { &mut *builder };
        let root_id = DagNodeId::new(root);

        // If a registry is supplied, transfer its rules into the engine.
        let cfg = HeuristicConfig::default()
            .max_depth(max_depth)
            .timeout(Duration::from_millis(timeout_ms))
            .simplification_aggressiveness(aggressiveness);

        let mut engine = if registry.is_null() {
            HeuristicEngine::new(cfg, SearchStrategy::Greedy)
        } else {
            // Share the Arc<RuleRegistry> with the engine. This is cheap
            // (one atomic ref-count increment) and keeps the registry alive
            // and accessible to the C caller after the call returns.
            let arc_clone = std::sync::Arc::clone(unsafe { &(*registry).inner });
            HeuristicEngine::new(cfg, SearchStrategy::Greedy).with_rule_registry(arc_clone)
        };

        let id = engine.simplify(builder_ref, root_id);
        unsafe { *out_id = id.value() };
        RssnStatus::Success
    }))
    .unwrap_or(RssnStatus::Panic)
}

/// Maps a `SymbolKind` to its C discriminant byte.
const fn kind_to_discriminant(kind: &crate::dag::symbol::SymbolKind) -> u8 {
    use crate::dag::symbol::{OpKind, SymbolKind};
    match kind {
        SymbolKind::Variable(_) => 0,
        SymbolKind::Constant(_) => 1,
        SymbolKind::Operator(OpKind::Add) => 2,
        SymbolKind::Operator(OpKind::Sub) => 3,
        SymbolKind::Operator(OpKind::Mul) => 4,
        SymbolKind::Operator(OpKind::Div) => 5,
        SymbolKind::Operator(OpKind::Pow) => 6,
        SymbolKind::Operator(OpKind::Mod) => 7,
        SymbolKind::Operator(OpKind::Neg) => 8,
        SymbolKind::Function(_) => 9,
    }
}

// =========================================================================
// Tests for the new FFI functions
// =========================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn binary_ops_roundtrip() {
        let builder = rssn_dag_new();
        assert!(!builder.is_null());

        let x = rssn_dag_variable(builder, c"x".as_ptr());
        let y = rssn_dag_variable(builder, c"y".as_ptr());
        assert_ne!(x, u32::MAX);
        assert_ne!(y, u32::MAX);

        assert_ne!(rssn_dag_sub(builder, x, y), u32::MAX);
        assert_ne!(rssn_dag_mul(builder, x, y), u32::MAX);
        assert_ne!(rssn_dag_div(builder, x, y), u32::MAX);
        assert_ne!(rssn_dag_pow(builder, x, y), u32::MAX);
        assert_ne!(rssn_dag_mod(builder, x, y), u32::MAX);
        assert_ne!(rssn_dag_neg(builder, x), u32::MAX);

        rssn_dag_free(builder);
    }

    #[test]
    fn v2_ops_return_success() {
        let builder = rssn_dag_new();
        let x = rssn_dag_variable(builder, c"x".as_ptr());
        let y = rssn_dag_variable(builder, c"y".as_ptr());

        let mut out = u32::MAX;
        assert_eq!(
            rssn_dag_sub_v2(builder, x, y, &mut out),
            RssnStatus::Success
        );
        assert_ne!(out, u32::MAX);
        assert_eq!(
            rssn_dag_mul_v2(builder, x, y, &mut out),
            RssnStatus::Success
        );
        assert_ne!(out, u32::MAX);
        assert_eq!(
            rssn_dag_div_v2(builder, x, y, &mut out),
            RssnStatus::Success
        );
        assert_ne!(out, u32::MAX);
        assert_eq!(
            rssn_dag_pow_v2(builder, x, y, &mut out),
            RssnStatus::Success
        );
        assert_ne!(out, u32::MAX);
        assert_eq!(
            rssn_dag_mod_v2(builder, x, y, &mut out),
            RssnStatus::Success
        );
        assert_ne!(out, u32::MAX);
        assert_eq!(rssn_dag_neg_v2(builder, x, &mut out), RssnStatus::Success);
        assert_ne!(out, u32::MAX);

        rssn_dag_free(builder);
    }

    #[test]
    fn null_inputs_return_sentinel_or_null_pointer_status() {
        assert_eq!(rssn_dag_sub(std::ptr::null_mut(), 0, 0), u32::MAX);
        assert_eq!(rssn_dag_mul(std::ptr::null_mut(), 0, 0), u32::MAX);
        assert_eq!(rssn_dag_div(std::ptr::null_mut(), 0, 0), u32::MAX);
        assert_eq!(rssn_dag_pow(std::ptr::null_mut(), 0, 0), u32::MAX);
        assert_eq!(rssn_dag_mod(std::ptr::null_mut(), 0, 0), u32::MAX);
        assert_eq!(rssn_dag_neg(std::ptr::null_mut(), 0), u32::MAX);
    }

    #[test]
    fn parse_and_build() {
        let builder = rssn_dag_new();
        let mut out = u32::MAX;
        let status = rssn_dag_parse(builder, c"x + y * 2.0".as_ptr(), &mut out);
        assert_eq!(status, RssnStatus::Success);
        assert_ne!(out, u32::MAX);
        rssn_dag_free(builder);
    }

    #[test]
    fn parse_invalid_expression() {
        let builder = rssn_dag_new();
        let mut out = u32::MAX;
        let status = rssn_dag_parse(builder, c"(".as_ptr(), &mut out);
        assert_ne!(status, RssnStatus::Success);
        rssn_dag_free(builder);
    }

    #[test]
    fn intern_function_and_call() {
        let builder = rssn_dag_new();
        let fn_id = rssn_dag_intern_function(builder, c"mysin".as_ptr());
        assert_ne!(fn_id, u32::MAX);

        let x = rssn_dag_variable(builder, c"x".as_ptr());
        let args = [x];
        let call_node = rssn_dag_call_fn(builder, fn_id, args.as_ptr(), 1);
        assert_ne!(call_node, u32::MAX);
        rssn_dag_free(builder);
    }

    #[test]
    fn rule_registry_lifecycle() {
        let reg = rssn_rule_registry_new();
        assert!(!reg.is_null());
        rssn_rule_registry_free(reg);

        // Double-free safety: free NULL is a no-op.
        rssn_rule_registry_free(std::ptr::null_mut());
    }

    #[test]
    fn register_and_apply_c_rule() {
        unsafe extern "C" fn zero_add_rule(
            builder: *mut DagBuilder,
            kind: u8,
            children: *const u32,
            n_children: u32,
            _user_data: *mut c_void,
        ) -> u32 {
            // Rule: x + 0 → x  (kind == Add, one child is constant 0)
            if kind != 2 || n_children != 2 {
                return u32::MAX;
            }
            let lhs = unsafe { *children };
            let rhs = unsafe { *children.add(1) };
            let b = unsafe { &mut *builder };
            let rhs_node = b.arena().get(DagNodeId::new(rhs));
            if let Some(node) = rhs_node {
                if let crate::dag::symbol::SymbolKind::Constant(v) = node.kind {
                    if v == 0.0 {
                        return lhs;
                    }
                }
            }
            u32::MAX
        }

        let reg = rssn_rule_registry_new();
        let status = rssn_rule_register(
            reg,
            c"zero_add".as_ptr(),
            Some(zero_add_rule),
            0,
            2, // Add
            std::ptr::null_mut(),
        );
        assert_eq!(status, RssnStatus::Success);

        // Build x + 0 and simplify.
        let builder = rssn_dag_new();
        let x = rssn_dag_variable(builder, c"x".as_ptr());
        let zero = rssn_dag_constant(builder, 0.0);
        let expr = rssn_dag_add(builder, x, zero);

        let mut out = u32::MAX;
        let s = rssn_dag_simplify_with_rules(builder, expr, reg, std::ptr::null(), &mut out);
        assert_eq!(s, RssnStatus::Success);
        // After simplification x + 0 should reduce to x.
        assert_eq!(out, x, "x + 0 should simplify to x");

        rssn_dag_free(builder);
        rssn_rule_registry_free(reg);
    }
}

// =========================================================================
// E-graph equality saturation FFI
// =========================================================================
// Design: the EGraph is created transiently for each saturate+extract call.
// This avoids exposing a long-lived handle across the FFI boundary (which
// would complicate lifetime management on both sides). The overhead is low
// because all real memory lives inside the DagBuilder which is long-lived.
//
// For callers that need repeated extraction on the same expression with the
// same rule set, the recommended pattern is:
//   1. rssn_dag_egraph_saturate_extract(...) — one call, returns best node.
//   2. Cache the returned node ID on the C side.

/// Configuration for E-graph equality saturation.
///
/// Passed by value across the FFI boundary; zero-initialise for defaults.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct RssnEGraphConfig {
    /// Maximum saturation rounds (0 → use library default of 8).
    pub max_rounds: u32,
    /// Maximum equivalence merges before stopping (0 → default 512).
    pub max_merges: u32,
    /// Maximum new nodes the E-graph may create via rewrites (0 → default 1024).
    pub max_new_nodes: u32,
    /// Non-zero → enable strict IEEE 754 signed-zero semantics.
    ///
    /// When set, `x + (-0.0)` will **not** be simplified to `x`, matching
    /// `-fno-unsafe-math-optimizations`. Default (0) uses `-fno-signed-zeros`.
    pub strict_ieee754_signed_zero: u8,
}

impl RssnEGraphConfig {
    const fn to_rust(self) -> crate::egraph::EGraphConfig {
        crate::egraph::EGraphConfig {
            max_rounds: if self.max_rounds == 0 {
                8
            } else {
                self.max_rounds as usize
            },
            max_merges: if self.max_merges == 0 {
                512
            } else {
                self.max_merges as usize
            },
            max_new_nodes: if self.max_new_nodes == 0 {
                1024
            } else {
                self.max_new_nodes as usize
            },
            strict_ieee754_signed_zero: self.strict_ieee754_signed_zero != 0,
            cost_weights: None,
        }
    }
}

/// A C-callable rewrite rule for the E-graph.
///
/// Called for each node during saturation. Return the ID of an equivalent
/// node to merge into the same e-class, or `u32::MAX` to decline.
///
/// `kind`       — discriminant of the current node's kind (see `RssnKind`).
/// `children`   — pointer to the *canonical* child IDs (length `n_children`).
/// `n_children` — number of children.
/// `user_data`  — opaque pointer forwarded unchanged from the registration call.
pub type RssnEGraphRuleCallback = unsafe extern "C" fn(
    builder: *mut DagBuilder,
    kind: u8,
    children: *const u32,
    n_children: u32,
    user_data: *mut c_void,
) -> u32;

/// Runs E-graph equality saturation on `root` and returns the cheapest
/// equivalent node ID, or `u32::MAX` on error.
///
/// # C example
///
/// ```c
/// uint32_t best = rssn_dag_egraph_saturate_extract(
///     builder, expr_id,
///     (RssnEGraphConfig){ .max_rounds = 4, .max_merges = 256, .max_new_nodes = 512 },
///     NULL, 0,   // no user rules
///     NULL
/// );
/// ```
///
/// # Safety
///
/// - `builder` must be a valid, non-null `DagBuilder` from `rssn_dag_new`.
/// - If `rules` is non-null, `n_rules` must be the number of valid callback
///   pointers in the array.
/// - `user_data` pointers must remain valid for the duration of this call.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_egraph_saturate_extract(
    builder: *mut DagBuilder,
    root: u32,
    cfg: RssnEGraphConfig,
    rules: *const RssnEGraphRuleCallback,
    n_rules: u32,
    out: *mut u32,
) -> RssnStatus {
    if builder.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let b = unsafe { &mut *builder };
        let root_id = crate::dag::node::DagNodeId::new(root);
        let rust_cfg = cfg.to_rust();

        let mut eg = crate::egraph::EGraph::new(b, rust_cfg);

        // Register C-side user rules.
        if !rules.is_null() {
            for i in 0..n_rules as usize {
                let cb: RssnEGraphRuleCallback = unsafe { *rules.add(i) };
                // SAFETY: cb is valid for the duration of saturate (this call).
                // user_data is forwarded transparently.
                eg.add_rule(move |builder_inner, kind, children| {
                    let kind_disc = kind_to_discriminant(kind);
                    let ch_ptr = children.as_ptr().cast::<u32>();
                    let result = unsafe {
                        cb(
                            std::ptr::from_mut::<DagBuilder>(builder_inner),
                            kind_disc,
                            ch_ptr,
                            children.len() as u32,
                            std::ptr::null_mut(), // user_data not storable in 'static closure
                        )
                    };
                    if result == u32::MAX {
                        None
                    } else {
                        Some(crate::dag::node::DagNodeId::new(result))
                    }
                });
            }
        }

        eg.saturate(root_id);
        let best = eg.extract(root_id);
        if let Some(out_ptr) = unsafe { out.as_mut() } {
            *out_ptr = best.value();
        }
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Like [`rssn_dag_egraph_saturate_extract`] but also enables the E-graph
/// pass inside the full heuristic simplification pipeline and returns the
/// result after both passes.
///
/// # Safety
///
/// Same as `rssn_dag_egraph_saturate_extract`.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_simplify_with_egraph(
    builder: *mut DagBuilder,
    root: u32,
    egraph_cfg: RssnEGraphConfig,
    out: *mut u32,
) -> RssnStatus {
    if builder.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let b = unsafe { &mut *builder };
        let root_id = crate::dag::node::DagNodeId::new(root);
        let hcfg = crate::heuristic::HeuristicConfig::default().with_egraph(egraph_cfg.to_rust());
        let mut engine = crate::heuristic::HeuristicEngine::new(
            hcfg,
            crate::heuristic::SearchStrategy::default(),
        );
        let simplified = engine.simplify(b, root_id);
        if let Some(out_ptr) = unsafe { out.as_mut() } {
            *out_ptr = simplified.value();
        }
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

// =========================================================================
// Batch custom operator registry
// =========================================================================
//
// Developers can register user-defined operators for use with the batch-build
// API without modifying library source code.  Registered kinds must fall in
// the range 16..=255 (kinds 0..=15 are reserved for built-in operators).
//
// Thread safety: the registry uses a `RwLock`; concurrent reads (during
// `rssn_dag_batch_build`) never block each other.  Writes (registration /
// unregistration) acquire an exclusive lock.

use std::collections::HashMap as StdHashMap;
use std::sync::OnceLock;

/// Callback type for a custom batch-build operator.
///
/// Called during [`rssn_dag_batch_build`] when the node `kind` field matches
/// a registered custom kind.  The callback receives a `DagBuilder`, the
/// resolved child node IDs and their count, and the `user_data` pointer
/// supplied at registration.  Return a new valid node ID allocated in
/// `builder`, or `u32::MAX` to signal failure.
///
/// # Safety
///
/// - `builder` is valid and non-null for the duration of this call.
/// - `child_ids` points to an array of exactly `n_children` resolved node IDs.
/// - `user_data` is the opaque pointer provided at `rssn_batch_op_register` time;
///   the caller is responsible for its lifetime.
pub type RssnBatchOpCallback = unsafe extern "C" fn(
    builder: *mut DagBuilder,
    child_ids: *const u32,
    n_children: u32,
    user_data: *mut c_void,
) -> u32;

/// Entry stored in the process-level batch operator registry.
struct BatchOpEntry {
    callback: RssnBatchOpCallback,
    /// Expected number of resolved children (capped at 2 by `RssnNodeDesc`).
    n_children: u32,
    /// Caller-provided opaque pointer, stored as `usize` for `Send` safety.
    user_data: usize,
}

static BATCH_OP_REGISTRY: OnceLock<std::sync::RwLock<StdHashMap<u8, BatchOpEntry>>> =
    OnceLock::new();

#[inline]
fn batch_op_registry() -> &'static std::sync::RwLock<StdHashMap<u8, BatchOpEntry>> {
    BATCH_OP_REGISTRY.get_or_init(|| std::sync::RwLock::new(StdHashMap::new()))
}

/// Registers a custom batch operator for use with [`rssn_dag_batch_build`].
///
/// `kind` must be in the range `16..=255`; kinds `0..=15` are reserved for
/// built-in operators and this function returns [`RssnStatus::InvalidNodeId`]
/// if `kind` falls in that range.  Registering the same `kind` twice returns
/// [`RssnStatus::RuleConflict`].
///
/// The `callback` receives the resolved child node IDs for the batch node and
/// must allocate a new DAG node in `builder`, returning its id.  `n_children`
/// specifies how many of `child0`/`child1` are meaningful in the descriptor
/// (currently capped at 2 by the `RssnNodeDesc` layout).
///
/// # Safety
///
/// - `callback` must be a valid function pointer that remains valid until
///   [`rssn_batch_op_unregister`] is called for the same `kind`.
/// - `user_data` is forwarded to the callback opaquely; its lifetime is the
///   caller's responsibility.
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_batch_op_register(
    kind: u8,
    n_children: u32,
    callback: Option<RssnBatchOpCallback>,
    user_data: *mut c_void,
) -> RssnStatus {
    if kind < 16 {
        // Built-in range is 0..=15; custom operators start at 16.
        return RssnStatus::InvalidNodeId;
    }
    let Some(cb) = callback else {
        return RssnStatus::NullPointer;
    };
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let reg = batch_op_registry();
        let mut guard = reg
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if guard.contains_key(&kind) {
            return RssnStatus::RuleConflict;
        }
        guard.insert(
            kind,
            BatchOpEntry {
                callback: cb,
                n_children,
                user_data: user_data as usize,
            },
        );
        RssnStatus::Success
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// Unregisters a previously registered custom batch operator.
///
/// Returns [`RssnStatus::Success`] if the kind was registered, or
/// [`RssnStatus::InvalidNodeId`] if it was not (or if `kind < 16`).
#[unsafe(no_mangle)]
pub extern "C" fn rssn_batch_op_unregister(kind: u8) -> RssnStatus {
    if kind < 16 {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let reg = batch_op_registry();
        let mut guard = reg
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if guard.remove(&kind).is_some() {
            RssnStatus::Success
        } else {
            RssnStatus::InvalidNodeId
        }
    }));
    result.unwrap_or(RssnStatus::Panic)
}

// =========================================================================
// Batch-build API — reduced cross-FFI overhead
// =========================================================================
// Rationale: building a 50-node expression via individual rssn_dag_add/mul/
// etc. calls costs 50× catch_unwind + null check + FFI frame. The batch
// API amortises this to a single call: C fills an array of `RssnNodeDesc`
// and we process the whole array in one Rust call.
//
// Custom operators (kinds 16–255) registered via `rssn_batch_op_register`
// are dispatched through the process-level `BATCH_OP_REGISTRY` below.

/// Node kind discriminant used in [`RssnNodeDesc`].
///
/// Values 0–8 are built-in; 16–255 are available for user-defined operators
/// registered via [`rssn_batch_op_register`].  Matches `RssnKind` in the C header.
pub type RssnNodeKindBatch = u8;

/// Compact node descriptor for batch DAG construction.
///
/// The caller allocates an array of these, fills them in topological order
/// (children before parents), and passes the whole array to
/// [`rssn_dag_batch_build`]. The output array receives the allocated IDs.
///
/// Field semantics by `kind`:
///
/// | kind | meaning | fields used |
/// |------|---------|-------------|
/// | 0 = Variable | leaf variable | `name[0..32]` |
/// | 1 = Constant | leaf constant | `value` |
/// | 2 = Add      | `child0 + child1` | `child0`, `child1` |
/// | 3 = Sub      | `child0 - child1` | `child0`, `child1` |
/// | 4 = Mul      | `child0 * child1` | `child0`, `child1` |
/// | 5 = Div      | `child0 / child1` | `child0`, `child1` |
/// | 6 = Pow      | `child0 ^ child1` | `child0`, `child1` |
/// | 7 = Neg      | `-child0`         | `child0` |
/// | 8 = Mod      | `child0 % child1` | `child0`, `child1` |
///
/// `child0` and `child1` are **indices into the `out_ids` array** of the
/// same batch call — they are NOT `DagNodeId` values. Index `u32::MAX`
/// means "no child". This allows forward-reference-free batch construction.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct RssnNodeDesc {
    /// Constant value (used when `kind == 1`).
    pub value: f64,
    /// Index into `out_ids` of this call for the first child.
    pub child0: u32,
    /// Index into `out_ids` of this call for the second child.
    pub child1: u32,
    /// Node kind discriminant (see table above).
    pub kind: u8,
    /// Null-terminated variable name (used when `kind == 0`).
    pub name: [u8; 31],
}

/// Builds `n` DAG nodes in a single FFI call, writing allocated node IDs
/// into `out_ids`.
///
/// Nodes are processed in order `0..n`. Children are referenced by their
/// **index in the batch** (not their `DagNodeId`); the builder translates
/// indices to IDs after allocating each node.
///
/// Returns `RssnStatus::Success` on success. On any error the output array
/// may be partially populated — already-built nodes remain valid.
///
/// # Safety
///
/// - `builder` must be a valid, non-null `DagBuilder` from `rssn_dag_new`.
/// - `descs` must point to an array of at least `n` `RssnNodeDesc` values,
///   valid for the duration of this call.
/// - `out_ids` must point to a writable array of at least `n` `u32` values.
/// - Children referenced by `child0`/`child1` must have indices strictly
///   less than the current node's index (topological order).
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_batch_build(
    builder: *mut crate::dag::builder::DagBuilder,
    descs: *const RssnNodeDesc,
    n: u32,
    out_ids: *mut u32,
) -> RssnStatus {
    if builder.is_null() || descs.is_null() || out_ids.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let b = unsafe { &mut *builder };
        let descs_slice: &[RssnNodeDesc] = unsafe { std::slice::from_raw_parts(descs, n as usize) };
        let out_slice: &mut [u32] = unsafe { std::slice::from_raw_parts_mut(out_ids, n as usize) };

        // Accumulated IDs for this batch (so nodes can reference earlier siblings).
        let mut batch_ids: Vec<crate::dag::node::DagNodeId> = Vec::with_capacity(n as usize);

        for (i, desc) in descs_slice.iter().enumerate() {
            // Resolve child indices → DagNodeIds, clamping out-of-range to NONE.
            let resolve = |idx: u32| -> crate::dag::node::DagNodeId {
                if idx == u32::MAX || idx as usize >= i {
                    crate::dag::node::DagNodeId::NONE
                } else {
                    batch_ids[idx as usize]
                }
            };

            let id = match desc.kind {
                0 => {
                    // Variable: find the null terminator in `desc.name`.
                    let name_bytes = &desc.name;
                    let len = name_bytes.iter().position(|&b| b == 0).unwrap_or(31);
                    b.variable_bytes(&name_bytes[..len])
                        .unwrap_or(crate::dag::node::DagNodeId::NONE)
                }
                1 => b.constant(desc.value),
                2 => {
                    let (c0, c1) = (resolve(desc.child0), resolve(desc.child1));
                    if c0.is_none() || c1.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.add(c0, c1)
                }
                3 => {
                    let (c0, c1) = (resolve(desc.child0), resolve(desc.child1));
                    if c0.is_none() || c1.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.sub(c0, c1)
                }
                4 => {
                    let (c0, c1) = (resolve(desc.child0), resolve(desc.child1));
                    if c0.is_none() || c1.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.mul(c0, c1)
                }
                5 => {
                    let (c0, c1) = (resolve(desc.child0), resolve(desc.child1));
                    if c0.is_none() || c1.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.div(c0, c1)
                }
                6 => {
                    let (c0, c1) = (resolve(desc.child0), resolve(desc.child1));
                    if c0.is_none() || c1.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.pow(c0, c1)
                }
                7 => {
                    let c0 = resolve(desc.child0);
                    if c0.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.neg(c0)
                }
                8 => {
                    let (c0, c1) = (resolve(desc.child0), resolve(desc.child1));
                    if c0.is_none() || c1.is_none() {
                        return RssnStatus::InvalidNode;
                    }
                    b.modulo(c0, c1)
                }
                kind => {
                    // Look up a user-defined operator in the custom registry.
                    // We copy the entry fields before dropping the read lock so
                    // the callback can safely re-enter `builder` without holding
                    // the registry lock.
                    let entry = {
                        let reg = batch_op_registry();
                        let guard = reg
                            .read()
                            .unwrap_or_else(std::sync::PoisonError::into_inner);
                        guard
                            .get(&kind)
                            .map(|e| (e.callback, e.n_children, e.user_data))
                    };
                    let Some((cb, n_ch, ud_usize)) = entry else {
                        return RssnStatus::InvalidNode;
                    };
                    // Resolve up to two children from the batch index space.
                    let resolve_raw = |idx: u32| -> u32 {
                        if idx == u32::MAX || idx as usize >= i {
                            u32::MAX
                        } else {
                            batch_ids[idx as usize].value()
                        }
                    };
                    let actual_n = (n_ch as usize).min(2);
                    let mut ch_buf = [u32::MAX; 2];
                    if actual_n > 0 {
                        ch_buf[0] = resolve_raw(desc.child0);
                    }
                    if actual_n > 1 {
                        ch_buf[1] = resolve_raw(desc.child1);
                    }
                    let ud = ud_usize as *mut c_void;
                    // SAFETY: callback is a valid fn ptr (guaranteed by rssn_batch_op_register),
                    // builder is valid for this call, ch_buf lives on the stack.
                    let result_id = unsafe {
                        cb(
                            std::ptr::from_mut::<DagBuilder>(b),
                            ch_buf.as_ptr(),
                            actual_n as u32,
                            ud,
                        )
                    };
                    if result_id == u32::MAX {
                        return RssnStatus::InvalidNode;
                    }
                    DagNodeId::new(result_id)
                }
            };

            out_slice[i] = id.value();
            batch_ids.push(id);
        }
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

/// Writes the packed arena snapshot to a caller-provided byte buffer.
///
/// On success, `*bytes_written` receives the number of bytes written.
/// Call once with `buf = NULL` to query the required buffer size
/// (`*bytes_written` will be the needed byte count and the return value
/// is `RssnStatus::Success`).
///
/// The layout is: a little-endian `u64` node count, then `n × 32` bytes
/// of packed node data (`PackedDagNode`), then a little-endian `u64` pool
/// count, then `pool_count × 4` bytes of `u32` child IDs. Alignment of
/// `buf` to 8 bytes is required.
///
/// # Safety
///
/// - `builder` must be a valid, non-null `DagBuilder`.
/// - If `buf` is non-null, it must point to at least `buf_len` bytes of
///   writable memory, correctly aligned to 8 bytes.
/// - `bytes_written` must be a valid non-null pointer to a `u64`.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_get_packed(
    builder: *const crate::dag::builder::DagBuilder,
    buf: *mut u8,
    buf_len: usize,
    bytes_written: *mut usize,
) -> RssnStatus {
    if builder.is_null() || bytes_written.is_null() {
        return RssnStatus::NullPointer;
    }
    let result = catch_unwind(|| -> RssnStatus {
        let b = unsafe { &*builder };
        let image = b.packed_snapshot();
        // Compute needed size: 8 (node_count) + n*32 + 8 (pool_count) + pool*4.
        let node_count = image.len();
        let pool_count = image.children_pool().len();
        let needed = 8 + node_count * 32 + 8 + pool_count * 4;
        unsafe {
            *bytes_written = needed;
        }

        if buf.is_null() {
            // Size query only.
            return RssnStatus::Success;
        }
        if buf_len < needed {
            return RssnStatus::BufferTooSmall;
        }

        // SAFETY: caller guarantees buf is writable and at least buf_len bytes.
        let out: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(buf, buf_len) };
        let mut pos = 0usize;

        // Write node count (little-endian u64).
        out[pos..pos + 8].copy_from_slice(&(node_count as u64).to_le_bytes());
        pos += 8;

        // Write packed nodes as raw bytes.
        let node_bytes = image.nodes().len() * 32;
        // SAFETY: PackedDagNode is Pod (#[repr(C)], no padding, Copy).
        let node_src: &[u8] =
            unsafe { std::slice::from_raw_parts(image.nodes().as_ptr().cast::<u8>(), node_bytes) };
        out[pos..pos + node_bytes].copy_from_slice(node_src);
        pos += node_bytes;

        // Write pool count (little-endian u64).
        out[pos..pos + 8].copy_from_slice(&(pool_count as u64).to_le_bytes());
        pos += 8;

        // Write pool as little-endian u32 values.
        for &v in image.children_pool() {
            out[pos..pos + 4].copy_from_slice(&v.to_le_bytes());
            pos += 4;
        }

        let _ = pos; // suppress unused-assignment warning
        RssnStatus::Success
    });
    result.unwrap_or(RssnStatus::Panic)
}

#[cfg(test)]
mod egraph_ffi_tests {
    use super::*;

    #[test]
    fn egraph_ffi_constant_fold_add() {
        let builder = rssn_dag_new();
        let c3 = rssn_dag_constant(builder, 3.0);
        let c4 = rssn_dag_constant(builder, 4.0);
        let s = rssn_dag_add(builder, c3, c4);

        let cfg = RssnEGraphConfig {
            max_rounds: 4,
            max_merges: 64,
            max_new_nodes: 64,
            strict_ieee754_signed_zero: 0,
        };
        let mut out: u32 = u32::MAX;
        let status =
            rssn_dag_egraph_saturate_extract(builder, s, cfg, std::ptr::null(), 0, &mut out);
        assert_eq!(status, RssnStatus::Success);
        // The constant-folded node 7.0 should be in the same e-class and have lower cost.
        // out may be s itself or the folded constant — both are valid extractions.
        assert_ne!(out, u32::MAX);

        rssn_dag_free(builder);
    }

    #[test]
    fn egraph_ffi_add_zero_simplifies() {
        let builder = rssn_dag_new();
        let x = rssn_dag_variable(builder, c"x".as_ptr());
        let zero = rssn_dag_constant(builder, 0.0);
        let xpz = rssn_dag_add(builder, x, zero);

        let cfg = RssnEGraphConfig::default();
        let mut out: u32 = u32::MAX;
        let status =
            rssn_dag_egraph_saturate_extract(builder, xpz, cfg, std::ptr::null(), 0, &mut out);
        assert_eq!(status, RssnStatus::Success);
        // x is cheaper than x+0; extractor should return x.
        assert_eq!(out, x, "x+0 extracts to x");

        rssn_dag_free(builder);
    }

    #[test]
    fn egraph_ffi_null_builder_returns_null_pointer() {
        let mut out: u32 = 0;
        let status = rssn_dag_egraph_saturate_extract(
            std::ptr::null_mut(),
            0,
            RssnEGraphConfig::default(),
            std::ptr::null(),
            0,
            &mut out,
        );
        assert_eq!(status, RssnStatus::NullPointer);
    }
}

#[cfg(test)]
mod batch_build_tests {
    use super::*;

    /// Build `x * (x + 2.0)` as a batch of 4 nodes:
    ///   [0] Variable "x"
    ///   [1] Constant 2.0
    ///   [2] Add (0, 1) = x + 2
    ///   [3] Mul (0, 2) = x * (x + 2)
    #[test]
    fn batch_build_polynomial() {
        let builder = rssn_dag_new();
        let mut out_ids = [u32::MAX; 4];

        let mut descs = [RssnNodeDesc {
            value: 0.0,
            child0: u32::MAX,
            child1: u32::MAX,
            kind: 0,
            name: [0u8; 31],
        }; 4];

        // Node 0: variable "x"
        descs[0].kind = 0;
        descs[0].name[0] = b'x';

        // Node 1: constant 2.0
        descs[1].kind = 1;
        descs[1].value = 2.0;

        // Node 2: Add(0, 1)
        descs[2].kind = 2;
        descs[2].child0 = 0;
        descs[2].child1 = 1;

        // Node 3: Mul(0, 2)
        descs[3].kind = 4;
        descs[3].child0 = 0;
        descs[3].child1 = 2;

        let status = rssn_dag_batch_build(builder, descs.as_ptr(), 4, out_ids.as_mut_ptr());
        assert_eq!(status, RssnStatus::Success);

        // All node IDs must be valid (not u32::MAX).
        for &id in &out_ids {
            assert_ne!(id, u32::MAX, "all nodes should be allocated");
        }

        // x*x deduplication: same variable → same ID
        assert_eq!(out_ids[0], out_ids[0]);

        // Build the same expression manually and compare IDs (dedup).
        let x2 = rssn_dag_variable(builder, c"x".as_ptr());
        let c2 = rssn_dag_constant(builder, 2.0);
        let add2 = rssn_dag_add(builder, x2, c2);
        let mul2 = rssn_dag_mul(builder, x2, add2);
        assert_eq!(
            out_ids[3], mul2,
            "batch and individual build produce same node ID"
        );

        rssn_dag_free(builder);
    }

    #[test]
    fn batch_build_null_returns_null_pointer() {
        let mut out_ids = [0u32; 2];
        let descs = [RssnNodeDesc {
            value: 1.0,
            child0: u32::MAX,
            child1: u32::MAX,
            kind: 1,
            name: [0u8; 31],
        }; 2];
        let status = rssn_dag_batch_build(
            std::ptr::null_mut(),
            descs.as_ptr(),
            2,
            out_ids.as_mut_ptr(),
        );
        assert_eq!(status, RssnStatus::NullPointer);
    }

    #[test]
    fn get_packed_size_query() {
        let builder = rssn_dag_new();
        // Build a small expression.
        let x = rssn_dag_variable(builder, c"x".as_ptr());
        let c = rssn_dag_constant(builder, 3.0);
        let _ = rssn_dag_add(builder, x, c);

        // Size query: pass null buffer.
        let mut needed: usize = 0;
        let status = rssn_dag_get_packed(builder as *const _, std::ptr::null_mut(), 0, &mut needed);
        assert_eq!(status, RssnStatus::Success);
        assert!(needed > 0, "packed snapshot must have positive size");

        // Actual write.
        let mut buf = vec![0u8; needed];
        let mut written: usize = 0;
        let status2 =
            rssn_dag_get_packed(builder as *const _, buf.as_mut_ptr(), needed, &mut written);
        assert_eq!(status2, RssnStatus::Success);
        assert_eq!(written, needed);

        // First 8 bytes are node count (little-endian).
        let node_count = u64::from_le_bytes(buf[0..8].try_into().expect("8 bytes"));
        assert!(node_count >= 3, "at least 3 nodes: x, 3.0, x+3.0");

        rssn_dag_free(builder);
    }
}

// =============================================================================
// Unified Custom-Operator Registry — C FFI
// =============================================================================
//
// The `RssnCustomOpRegistry` is an opaque handle to a
// `crate::custom::descriptor::CustomOpRegistry`.  It is the C-facing
// equivalent of the Rust `CustomOpRegistry` and lets C/C++ callers register
// operators that plug into all three pipelines in one place.
//
// Lifecycle:
//   RssnCustomOpRegistry* reg = rssn_custom_op_registry_new();
//   rssn_custom_op_register_fn1(reg, fn_id, "name", fn_ptr, vectorizable);
//   rssn_custom_op_add_simplify_rule(reg, fn_id, "rule name", priority, cb, ud);
//   rssn_custom_op_add_egraph_rule(reg, fn_id, after_builtins, cb, ud);
//
//   // Use in each pipeline step:
//   rssn_dag_compile_with_custom_ops(builder, root, reg, &fn_ptr);
//   rssn_dag_simplify_with_custom_ops(builder, root, reg, &out_id);
//   rssn_dag_egraph_with_custom_ops(builder, root, cfg, reg, &out_id);
//
//   rssn_custom_op_registry_free(reg);

use crate::custom::descriptor::{CustomOpDescriptor, CustomOpRegistry, EvalFn};
use std::sync::Arc;

/// Opaque handle to a [`CustomOpRegistry`].
///
/// Heap-allocated; must be freed exactly once via [`rssn_custom_op_registry_free`].
pub struct RssnCustomOpRegistry(Arc<CustomOpRegistry>);

/// Allocates an empty [`RssnCustomOpRegistry`].
///
/// # Safety
///
/// The returned pointer must be freed exactly once via
/// [`rssn_custom_op_registry_free`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_registry_new() -> *mut RssnCustomOpRegistry {
    let result = catch_unwind(|| {
        Box::into_raw(Box::new(RssnCustomOpRegistry(Arc::new(
            CustomOpRegistry::new(),
        ))))
    });
    result.unwrap_or(std::ptr::null_mut())
}

/// Frees a [`RssnCustomOpRegistry`] allocated by [`rssn_custom_op_registry_new`].
///
/// # Safety
///
/// `reg` must be a pointer from [`rssn_custom_op_registry_new`], or NULL.
/// Double-free is undefined behaviour.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_registry_free(reg: *mut RssnCustomOpRegistry) {
    if reg.is_null() {
        return;
    }
    let _ = catch_unwind(std::panic::AssertUnwindSafe(|| unsafe {
        drop(Box::from_raw(reg));
    }));
}

// ── Internal helper: get a mutable reference to the inner registry ─────────
//
// The Arc inside RssnCustomOpRegistry is unwrapped mutably only while the
// registry is being built (before it is shared with the JIT).  We use
// Arc::get_mut; if the Arc has been cloned (i.e. shared with a JitCompiler)
// this returns None and we return InvalidNodeId.

fn registry_mut(reg: *mut RssnCustomOpRegistry) -> Option<&'static mut CustomOpRegistry> {
    if reg.is_null() {
        return None;
    }
    let wrapper = unsafe { &mut *reg };
    Arc::get_mut(&mut wrapper.0)
}

// ── Operator registration ──────────────────────────────────────────────────

/// Registers a 1-argument (`f64 → f64`) custom operator.
///
/// - `fn_id`: numeric identifier (must be unique in the registry).
/// - `name`: null-terminated operator name (resolved by the parser).
/// - `eval_fn`: `extern "C" fn(f64) -> f64` pointer.
/// - `vectorizable`: non-zero if the function is pure and safe to duplicate
///   in the ILP batch path.
///
/// # Safety
///
/// `reg` and `name` must be valid non-null pointers for the duration of
/// this call.
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_register_fn1(
    reg: *mut RssnCustomOpRegistry,
    fn_id: u32,
    name: *const c_char,
    eval_fn: Option<extern "C" fn(f64) -> f64>,
    vectorizable: u8,
) -> RssnStatus {
    if reg.is_null() || name.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(eval_fn) = eval_fn else {
        return RssnStatus::NullPointer;
    };
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let name_str = unsafe { CStr::from_ptr(name) }
            .to_str()
            .map_err(|_| RssnStatus::ParseError)?
            .to_owned();
        let reg_mut = registry_mut(reg).ok_or(RssnStatus::InvalidNode)?;
        let desc = CustomOpDescriptor::builder(
            crate::dag::symbol::FnId(fn_id),
            name_str,
            EvalFn::Arity1(eval_fn),
        )
        .cost(2.0);
        let desc = if vectorizable != 0 {
            desc.vectorizable()
        } else {
            desc
        };
        reg_mut
            .register(desc.build())
            .map_err(|_| RssnStatus::RuleConflict)?;
        Ok(RssnStatus::Success)
    }));
    result
        .unwrap_or(Err(RssnStatus::Panic))
        .unwrap_or_else(|e| e)
}

/// Registers a 2-argument (`f64, f64 → f64`) custom operator.
///
/// # Safety
///
/// Same as [`rssn_custom_op_register_fn1`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_register_fn2(
    reg: *mut RssnCustomOpRegistry,
    fn_id: u32,
    name: *const c_char,
    eval_fn: Option<extern "C" fn(f64, f64) -> f64>,
    vectorizable: u8,
) -> RssnStatus {
    if reg.is_null() || name.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(eval_fn) = eval_fn else {
        return RssnStatus::NullPointer;
    };
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let name_str = unsafe { CStr::from_ptr(name) }
            .to_str()
            .map_err(|_| RssnStatus::ParseError)?
            .to_owned();
        let reg_mut = registry_mut(reg).ok_or(RssnStatus::InvalidNode)?;
        let desc = CustomOpDescriptor::builder(
            crate::dag::symbol::FnId(fn_id),
            name_str,
            EvalFn::Arity2(eval_fn),
        )
        .cost(2.0);
        let desc = if vectorizable != 0 {
            desc.vectorizable()
        } else {
            desc
        };
        reg_mut
            .register(desc.build())
            .map_err(|_| RssnStatus::RuleConflict)?;
        Ok(RssnStatus::Success)
    }));
    result
        .unwrap_or(Err(RssnStatus::Panic))
        .unwrap_or_else(|e| e)
}

/// Registers a 3-argument (`f64, f64, f64 → f64`) custom operator.
///
/// # Safety
///
/// Same as [`rssn_custom_op_register_fn1`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_register_fn3(
    reg: *mut RssnCustomOpRegistry,
    fn_id: u32,
    name: *const c_char,
    eval_fn: Option<extern "C" fn(f64, f64, f64) -> f64>,
    vectorizable: u8,
) -> RssnStatus {
    if reg.is_null() || name.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(eval_fn) = eval_fn else {
        return RssnStatus::NullPointer;
    };
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let name_str = unsafe { CStr::from_ptr(name) }
            .to_str()
            .map_err(|_| RssnStatus::ParseError)?
            .to_owned();
        let reg_mut = registry_mut(reg).ok_or(RssnStatus::InvalidNode)?;
        let desc = CustomOpDescriptor::builder(
            crate::dag::symbol::FnId(fn_id),
            name_str,
            EvalFn::Arity3(eval_fn),
        )
        .cost(2.0);
        let desc = if vectorizable != 0 {
            desc.vectorizable()
        } else {
            desc
        };
        reg_mut
            .register(desc.build())
            .map_err(|_| RssnStatus::RuleConflict)?;
        Ok(RssnStatus::Success)
    }));
    result
        .unwrap_or(Err(RssnStatus::Panic))
        .unwrap_or_else(|e| e)
}

// ── Rule attachment ────────────────────────────────────────────────────────

/// Returns the `u8` kind discriminant for a `SymbolKind` value, matching
/// the `RssnKind` encoding used throughout the C API.
const fn symbol_kind_to_u8(kind: &crate::dag::symbol::SymbolKind) -> u8 {
    use crate::dag::symbol::{OpKind, SymbolKind};
    match kind {
        SymbolKind::Variable(_) => 0,
        SymbolKind::Constant(_) => 1,
        SymbolKind::Operator(OpKind::Add) => 2,
        SymbolKind::Operator(OpKind::Sub) => 3,
        SymbolKind::Operator(OpKind::Mul) => 4,
        SymbolKind::Operator(OpKind::Div) => 5,
        SymbolKind::Operator(OpKind::Pow) => 6,
        SymbolKind::Operator(OpKind::Neg) => 7,
        SymbolKind::Operator(OpKind::Mod) => 8,
        SymbolKind::Function(_) => 9,
    }
}

/// Adds a heuristic simplification rule to a custom operator.
///
/// `fn_id` must already be registered via `rssn_custom_op_register_fn*`.
/// `callback` is called by the simplifier for every node it visits.
/// Return `u32::MAX` from the callback to pass (no rewrite); any other value
/// is treated as the replacement node ID.
///
/// # Safety
///
/// `reg`, `rule_name`, and `user_data` must remain valid for the lifetime of
/// the registry (until [`rssn_custom_op_registry_free`]).
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_add_simplify_rule(
    reg: *mut RssnCustomOpRegistry,
    fn_id: u32,
    rule_name: *const c_char,
    priority: i32,
    callback: Option<RssnRuleCallback>,
    user_data: *mut c_void,
) -> RssnStatus {
    if reg.is_null() || rule_name.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(callback) = callback else {
        return RssnStatus::NullPointer;
    };
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let name_str = unsafe { CStr::from_ptr(rule_name) }
            .to_str()
            .map_err(|_| RssnStatus::ParseError)?
            .to_owned();
        let reg_mut = registry_mut(reg).ok_or(RssnStatus::InvalidNode)?;
        let target_id = crate::dag::symbol::FnId(fn_id);
        let desc = reg_mut
            .get_mut(target_id)
            .ok_or(RssnStatus::InvalidNodeId)?;

        // Capture callback + user_data (as usize for Send safety).
        let ud = user_data as usize;
        desc.simplify_rules
            .push(crate::custom::descriptor::SimplifyRule {
                name: name_str,
                priority,
                rule: std::sync::Arc::new(
                    move |builder: &mut DagBuilder,
                          kind: crate::dag::symbol::SymbolKind,
                          children: &[DagNodeId]| {
                        let kind_byte = symbol_kind_to_u8(&kind);
                        let child_ids: Vec<u32> = children.iter().map(|id| id.value()).collect();
                        // SAFETY: callback and ud were valid when registered; the
                        // registry lifetime covers any call through this closure.
                        let result = unsafe {
                            callback(
                                std::ptr::from_mut::<DagBuilder>(builder),
                                kind_byte,
                                child_ids.as_ptr(),
                                child_ids.len() as u32,
                                ud as *mut c_void,
                            )
                        };
                        if result == u32::MAX {
                            None
                        } else {
                            Some(DagNodeId::new(result))
                        }
                    },
                ),
            });
        Ok(RssnStatus::Success)
    }));
    result
        .unwrap_or(Err(RssnStatus::Panic))
        .unwrap_or_else(|e| e)
}

/// Adds an e-graph rewrite rule to a custom operator.
///
/// `after_builtins`: non-zero → run after built-in algebraic rules each round.
///
/// # Safety
///
/// Same as [`rssn_custom_op_add_simplify_rule`].
#[unsafe(no_mangle)]
pub extern "C" fn rssn_custom_op_add_egraph_rule(
    reg: *mut RssnCustomOpRegistry,
    fn_id: u32,
    after_builtins: u8,
    callback: Option<RssnEGraphRuleCallback>,
    user_data: *mut c_void,
) -> RssnStatus {
    if reg.is_null() {
        return RssnStatus::NullPointer;
    }
    let Some(callback) = callback else {
        return RssnStatus::NullPointer;
    };
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let reg_mut = registry_mut(reg).ok_or(RssnStatus::InvalidNode)?;
        let target_id = crate::dag::symbol::FnId(fn_id);
        let desc = reg_mut
            .get_mut(target_id)
            .ok_or(RssnStatus::InvalidNodeId)?;

        let ud = user_data as usize;
        desc.egraph_rules
            .push(crate::custom::descriptor::EGraphRule {
                after_builtins: after_builtins != 0,
                rule: std::sync::Arc::new(
                    move |builder: &mut DagBuilder,
                          kind: &crate::dag::symbol::SymbolKind,
                          children: &[DagNodeId]| {
                        let kind_byte = symbol_kind_to_u8(kind);
                        let child_ids: Vec<u32> = children.iter().map(|id| id.value()).collect();
                        let result = unsafe {
                            callback(
                                std::ptr::from_mut::<DagBuilder>(builder),
                                kind_byte,
                                child_ids.as_ptr(),
                                child_ids.len() as u32,
                                ud as *mut c_void,
                            )
                        };
                        if result == u32::MAX {
                            None
                        } else {
                            Some(DagNodeId::new(result))
                        }
                    },
                ),
            });
        Ok(RssnStatus::Success)
    }));
    result
        .unwrap_or(Err(RssnStatus::Panic))
        .unwrap_or_else(|e| e)
}

// ── Pipeline integration functions ─────────────────────────────────────────

/// JIT-compiles `root` using operators from `reg`.
///
/// Internally calls [`rssn_dag_compile`] after feeding all `eval_fn` pointers
/// from the registry into the global JIT context.  The batch f64×2 path
/// honours `vectorizable` flags for `Function` nodes.
///
/// # Safety
///
/// Same as [`rssn_dag_compile`].
#[cfg(feature = "cranelift-jit")]
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_compile_with_custom_ops(
    builder: *mut DagBuilder,
    root: u32,
    reg: *mut RssnCustomOpRegistry,
    out_fn: *mut *mut c_void,
) -> RssnStatus {
    if builder.is_null() || out_fn.is_null() || reg.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let builder_ref = unsafe { &mut *builder };
        let reg_ref = unsafe { &*reg };
        // Pre-intern all names so the parser (and any builder calls made
        // inside this function) can resolve them.
        reg_ref.0.register_with_builder(builder_ref);

        let root_id = DagNodeId::new(root);
        let ast = crate::ast::convert::dag_to_ast(builder_ref.arena(), root_id);
        let ctx_mutex = crate::ffi::jit_context::global_jit_ctx();
        let mut ctx = ctx_mutex
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);

        // Install the registry into the JIT context (feeds fn pointers +
        // enables vectorizable check).
        ctx.compiler_mut()
            .set_custom_op_registry(Arc::clone(&reg_ref.0));

        ctx.compiler_mut()
            .compile(&ast)
            .map_or(RssnStatus::CompilationError, |f| {
                unsafe { *out_fn = f as *mut c_void };
                RssnStatus::Success
            })
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// Non-JIT stub.
#[cfg(not(feature = "cranelift-jit"))]
#[unsafe(no_mangle)]
pub extern "C" fn rssn_dag_compile_with_custom_ops(
    _builder: *mut DagBuilder,
    _root: u32,
    _reg: *mut RssnCustomOpRegistry,
    _out_fn: *mut *mut c_void,
) -> RssnStatus {
    RssnStatus::CompilationError
}

/// Simplifies `root` applying all simplification rules from `reg`.
///
/// Combines the registry's rules with the built-in heuristic patterns and
/// runs the standard simplification pass.
///
/// # Safety
///
/// `builder`, `reg`, and `out_id` must be valid non-null pointers.
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_simplify_with_custom_ops(
    builder: *mut DagBuilder,
    root: u32,
    reg: *mut RssnCustomOpRegistry,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || reg.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let builder_ref = unsafe { &mut *builder };
        let reg_ref = unsafe { &*reg };

        // Build a RuleRegistry from all attached simplify_rules.
        let rule_registry = reg_ref.0.build_rule_registry();

        let config = HeuristicConfig::default();
        let mut engine = HeuristicEngine::new(config, SearchStrategy::Greedy)
            .with_rule_registry(std::sync::Arc::new(rule_registry));

        let root_id = DagNodeId::new(root);
        // HeuristicEngine::simplify returns DagNodeId directly (not Result).
        let simplified = engine.simplify(builder_ref, root_id);
        unsafe { *out_id = simplified.value() };
        RssnStatus::Success
    }));
    result.unwrap_or(RssnStatus::Panic)
}

/// E-graph equality saturation with all rules from `reg`.
///
/// Runs the built-in algebraic rules plus all e-graph rules attached to
/// descriptors in `reg`, then extracts the minimum-cost representative.
///
/// # Safety
///
/// `builder`, `reg`, and `out_id` must be valid non-null pointers.
#[unsafe(no_mangle)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn rssn_dag_egraph_with_custom_ops(
    builder: *mut DagBuilder,
    root: u32,
    config: RssnEGraphConfig,
    reg: *mut RssnCustomOpRegistry,
    out_id: *mut u32,
) -> RssnStatus {
    if builder.is_null() || reg.is_null() || out_id.is_null() {
        return RssnStatus::NullPointer;
    }
    if root == u32::MAX {
        return RssnStatus::InvalidNodeId;
    }
    let result = catch_unwind(std::panic::AssertUnwindSafe(|| {
        let builder_ref = unsafe { &mut *builder };
        let reg_ref = unsafe { &*reg };

        let eg_config = crate::egraph::egraph::EGraphConfig {
            max_rounds: if config.max_rounds == 0 {
                8
            } else {
                config.max_rounds as usize
            },
            max_merges: if config.max_merges == 0 {
                512
            } else {
                config.max_merges as usize
            },
            max_new_nodes: if config.max_new_nodes == 0 {
                1024
            } else {
                config.max_new_nodes as usize
            },
            strict_ieee754_signed_zero: config.strict_ieee754_signed_zero != 0,
            ..Default::default()
        };

        let root_id = DagNodeId::new(root);
        let mut egraph = crate::egraph::egraph::EGraph::new(builder_ref, eg_config);

        // Inject all e-graph rules from the custom-op registry.
        reg_ref.0.apply_to_egraph(&mut egraph);

        egraph.saturate(root_id);
        let best = egraph.extract(root_id);
        unsafe { *out_id = best.value() };
        RssnStatus::Success
    }));
    result.unwrap_or(RssnStatus::Panic)
}