decy-core 2.2.0

Core transpilation pipeline for C-to-Rust conversion
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
//! Popperian Falsification Test Suite for Decy C-to-Rust Transpiler
//!
//! C1926-C1950: Resource Pooling and Management domain -- object pools,
//! connection pools, buffer pools, handle tables, and resource lifecycle.
//! Tests are APPEND-ONLY per Popperian methodology.
//! Falsified tests are marked #[ignore = "FALSIFIED: reason"].
//!
//! These tests exercise resource pooling and management patterns commonly
//! found in servers, databases, game engines, operating systems, and
//! high-performance networking -- all expressed as valid C99 without #include.
//!
//! Organization:
//! - C1926-C1930: Object pool (fixed-size pool, acquire, release, pool stats, pool exhaustion)
//! - C1931-C1935: Connection pool (connection slots, borrow, return, health check, timeout)
//! - C1936-C1940: Buffer pool (buffer allocation, buffer chain, buffer recycle, pool growth)
//! - C1941-C1945: Handle table (handle allocation, lookup, revoke, generation counter, compact)
//! - C1946-C1950: Resource lifecycle (init/deinit tracking, ref counting, leak detection, cleanup)

use decy_core::transpile;

// ============================================================================
// C1926-C1930: Object Pool
// ============================================================================

#[test]
fn c1926_fixed_size_object_pool() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_OBJ_POOL_CAP 64
#define RP_OBJ_SIZE 128

typedef struct {
    char storage[RP_OBJ_POOL_CAP * RP_OBJ_SIZE];
    int free_stack[RP_OBJ_POOL_CAP];
    int free_top;
    int active_count;
    uint32_t total_acquires;
    uint32_t total_releases;
} rp_obj_pool_t;

void rp_obj_pool_init(rp_obj_pool_t *p) {
    int i;
    for (i = 0; i < RP_OBJ_POOL_CAP; i++) {
        p->free_stack[i] = i;
    }
    p->free_top = RP_OBJ_POOL_CAP;
    p->active_count = 0;
    p->total_acquires = 0;
    p->total_releases = 0;
}

void *rp_obj_pool_acquire(rp_obj_pool_t *p) {
    if (p->free_top <= 0) return (void *)0;
    p->free_top--;
    int idx = p->free_stack[p->free_top];
    p->active_count++;
    p->total_acquires++;
    return &p->storage[idx * RP_OBJ_SIZE];
}

void rp_obj_pool_release(rp_obj_pool_t *p, void *ptr) {
    char *base = p->storage;
    int idx = (int)((char *)ptr - base) / RP_OBJ_SIZE;
    if (idx >= 0 && idx < RP_OBJ_POOL_CAP) {
        p->free_stack[p->free_top++] = idx;
        p->active_count--;
        p->total_releases++;
    }
}

int rp_obj_pool_available(rp_obj_pool_t *p) {
    return p->free_top;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1926: Fixed-size object pool should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1926: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1926: Should contain rp_ functions");
}

#[test]
fn c1927_object_pool_acquire_pattern() {
    let c_code = r##"
typedef unsigned long size_t;

#define RP_ACQ_MAX 32
#define RP_ACQ_OBJ_SIZE 64

typedef struct {
    char objects[RP_ACQ_MAX * RP_ACQ_OBJ_SIZE];
    int in_use[RP_ACQ_MAX];
    int search_hint;
    int active;
    int peak_active;
} rp_acquire_pool_t;

void rp_acquire_pool_init(rp_acquire_pool_t *p) {
    int i;
    for (i = 0; i < RP_ACQ_MAX; i++) {
        p->in_use[i] = 0;
    }
    p->search_hint = 0;
    p->active = 0;
    p->peak_active = 0;
}

int rp_acquire_get(rp_acquire_pool_t *p) {
    int i;
    int start = p->search_hint;
    for (i = 0; i < RP_ACQ_MAX; i++) {
        int idx = (start + i) % RP_ACQ_MAX;
        if (!p->in_use[idx]) {
            p->in_use[idx] = 1;
            p->active++;
            if (p->active > p->peak_active) {
                p->peak_active = p->active;
            }
            p->search_hint = (idx + 1) % RP_ACQ_MAX;
            return idx;
        }
    }
    return -1;
}

void rp_acquire_put(rp_acquire_pool_t *p, int idx) {
    if (idx >= 0 && idx < RP_ACQ_MAX && p->in_use[idx]) {
        p->in_use[idx] = 0;
        p->active--;
        p->search_hint = idx;
    }
}

int rp_acquire_peak(rp_acquire_pool_t *p) {
    return p->peak_active;
}
"##;
    let result = transpile(c_code);
    assert!(
        result.is_ok(),
        "C1927: Object pool acquire pattern should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1927: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1927: Should contain rp_ functions");
}

#[test]
fn c1928_object_pool_release_batch() {
    let c_code = r##"
typedef unsigned long size_t;

#define RP_BATCH_POOL_SIZE 48

typedef struct {
    int slots[RP_BATCH_POOL_SIZE];
    int occupied[RP_BATCH_POOL_SIZE];
    int count;
    int batch_release_count;
} rp_batch_pool_t;

void rp_batch_pool_init(rp_batch_pool_t *p) {
    int i;
    for (i = 0; i < RP_BATCH_POOL_SIZE; i++) {
        p->occupied[i] = 0;
        p->slots[i] = 0;
    }
    p->count = 0;
    p->batch_release_count = 0;
}

int rp_batch_pool_alloc(rp_batch_pool_t *p, int value) {
    int i;
    for (i = 0; i < RP_BATCH_POOL_SIZE; i++) {
        if (!p->occupied[i]) {
            p->occupied[i] = 1;
            p->slots[i] = value;
            p->count++;
            return i;
        }
    }
    return -1;
}

void rp_batch_pool_free(rp_batch_pool_t *p, int idx) {
    if (idx >= 0 && idx < RP_BATCH_POOL_SIZE && p->occupied[idx]) {
        p->occupied[idx] = 0;
        p->slots[idx] = 0;
        p->count--;
    }
}

int rp_batch_pool_release_all_matching(rp_batch_pool_t *p, int value) {
    int released = 0;
    int i;
    for (i = 0; i < RP_BATCH_POOL_SIZE; i++) {
        if (p->occupied[i] && p->slots[i] == value) {
            p->occupied[i] = 0;
            p->count--;
            released++;
        }
    }
    p->batch_release_count++;
    return released;
}

int rp_batch_pool_count(rp_batch_pool_t *p) {
    return p->count;
}
"##;
    let result = transpile(c_code);
    assert!(
        result.is_ok(),
        "C1928: Object pool release batch should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1928: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1928: Should contain rp_ functions");
}

#[test]
fn c1929_object_pool_stats() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_STATS_POOL_CAP 64

typedef struct {
    uint32_t total_acquires;
    uint32_t total_releases;
    uint32_t total_exhaustions;
    uint32_t current_active;
    uint32_t peak_active;
    uint32_t reuse_count;
} rp_pool_stats_t;

typedef struct {
    int in_use[RP_STATS_POOL_CAP];
    int reused[RP_STATS_POOL_CAP];
    rp_pool_stats_t stats;
} rp_stats_pool_t;

void rp_stats_pool_init(rp_stats_pool_t *p) {
    int i;
    for (i = 0; i < RP_STATS_POOL_CAP; i++) {
        p->in_use[i] = 0;
        p->reused[i] = 0;
    }
    p->stats.total_acquires = 0;
    p->stats.total_releases = 0;
    p->stats.total_exhaustions = 0;
    p->stats.current_active = 0;
    p->stats.peak_active = 0;
    p->stats.reuse_count = 0;
}

int rp_stats_pool_acquire(rp_stats_pool_t *p) {
    int i;
    for (i = 0; i < RP_STATS_POOL_CAP; i++) {
        if (!p->in_use[i]) {
            p->in_use[i] = 1;
            p->stats.total_acquires++;
            p->stats.current_active++;
            if (p->reused[i]) p->stats.reuse_count++;
            if (p->stats.current_active > p->stats.peak_active) {
                p->stats.peak_active = p->stats.current_active;
            }
            return i;
        }
    }
    p->stats.total_exhaustions++;
    return -1;
}

void rp_stats_pool_release(rp_stats_pool_t *p, int idx) {
    if (idx >= 0 && idx < RP_STATS_POOL_CAP && p->in_use[idx]) {
        p->in_use[idx] = 0;
        p->reused[idx] = 1;
        p->stats.total_releases++;
        p->stats.current_active--;
    }
}

int rp_stats_pool_utilization_pct(rp_stats_pool_t *p) {
    return (int)(p->stats.current_active * 100) / RP_STATS_POOL_CAP;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1929: Object pool stats should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1929: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1929: Should contain rp_ functions");
}

#[test]
fn c1930_object_pool_exhaustion() {
    let c_code = r##"
typedef unsigned long size_t;

#define RP_EXHAUST_CAP 16
#define RP_EXHAUST_WAIT_MAX 32

typedef struct {
    int occupied[RP_EXHAUST_CAP];
    int waiters[RP_EXHAUST_WAIT_MAX];
    int waiter_count;
    int active;
    int reject_count;
} rp_exhaust_pool_t;

void rp_exhaust_pool_init(rp_exhaust_pool_t *p) {
    int i;
    for (i = 0; i < RP_EXHAUST_CAP; i++) {
        p->occupied[i] = 0;
    }
    p->waiter_count = 0;
    p->active = 0;
    p->reject_count = 0;
}

int rp_exhaust_pool_try_acquire(rp_exhaust_pool_t *p) {
    int i;
    for (i = 0; i < RP_EXHAUST_CAP; i++) {
        if (!p->occupied[i]) {
            p->occupied[i] = 1;
            p->active++;
            return i;
        }
    }
    return -1;
}

int rp_exhaust_pool_acquire_or_wait(rp_exhaust_pool_t *p, int waiter_id) {
    int slot = rp_exhaust_pool_try_acquire(p);
    if (slot >= 0) return slot;
    if (p->waiter_count < RP_EXHAUST_WAIT_MAX) {
        p->waiters[p->waiter_count++] = waiter_id;
        return -2;
    }
    p->reject_count++;
    return -3;
}

int rp_exhaust_pool_release_and_notify(rp_exhaust_pool_t *p, int idx) {
    if (idx < 0 || idx >= RP_EXHAUST_CAP || !p->occupied[idx]) return -1;
    p->occupied[idx] = 0;
    p->active--;
    if (p->waiter_count > 0) {
        p->waiter_count--;
        return p->waiters[p->waiter_count];
    }
    return 0;
}

int rp_exhaust_pool_is_full(rp_exhaust_pool_t *p) {
    return p->active >= RP_EXHAUST_CAP;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1930: Object pool exhaustion should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1930: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1930: Should contain rp_ functions");
}

// ============================================================================
// C1931-C1935: Connection Pool
// ============================================================================

#[test]
fn c1931_connection_pool_slots() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_CONN_MAX 32

typedef struct {
    int state;
    uint32_t id;
    int healthy;
    int use_count;
    int last_check_time;
} rp_conn_slot_t;

typedef struct {
    rp_conn_slot_t slots[RP_CONN_MAX];
    int count;
    uint32_t next_id;
    int max_per_host;
} rp_conn_pool_t;

void rp_conn_pool_init(rp_conn_pool_t *p, int max_per_host) {
    int i;
    for (i = 0; i < RP_CONN_MAX; i++) {
        p->slots[i].state = 0;
        p->slots[i].id = 0;
        p->slots[i].healthy = 0;
        p->slots[i].use_count = 0;
        p->slots[i].last_check_time = 0;
    }
    p->count = 0;
    p->next_id = 1;
    p->max_per_host = max_per_host;
}

int rp_conn_pool_create(rp_conn_pool_t *p) {
    if (p->count >= RP_CONN_MAX) return -1;
    int i;
    for (i = 0; i < RP_CONN_MAX; i++) {
        if (p->slots[i].state == 0) {
            p->slots[i].state = 1;
            p->slots[i].id = p->next_id++;
            p->slots[i].healthy = 1;
            p->slots[i].use_count = 0;
            p->count++;
            return i;
        }
    }
    return -1;
}

void rp_conn_pool_destroy(rp_conn_pool_t *p, int idx) {
    if (idx >= 0 && idx < RP_CONN_MAX && p->slots[idx].state != 0) {
        p->slots[idx].state = 0;
        p->slots[idx].healthy = 0;
        p->count--;
    }
}

int rp_conn_pool_active_count(rp_conn_pool_t *p) {
    return p->count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1931: Connection pool slots should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1931: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1931: Should contain rp_ functions");
}

#[test]
fn c1932_connection_pool_borrow() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_BORROW_MAX 24

typedef struct {
    int available;
    int borrowed;
    uint32_t borrow_count;
    int owner_id;
} rp_borrow_slot_t;

typedef struct {
    rp_borrow_slot_t slots[RP_BORROW_MAX];
    int total_slots;
    int borrowed_count;
    uint32_t contention_count;
} rp_borrow_pool_t;

void rp_borrow_pool_init(rp_borrow_pool_t *p, int initial_count) {
    int i;
    if (initial_count > RP_BORROW_MAX) initial_count = RP_BORROW_MAX;
    for (i = 0; i < RP_BORROW_MAX; i++) {
        p->slots[i].available = (i < initial_count) ? 1 : 0;
        p->slots[i].borrowed = 0;
        p->slots[i].borrow_count = 0;
        p->slots[i].owner_id = -1;
    }
    p->total_slots = initial_count;
    p->borrowed_count = 0;
    p->contention_count = 0;
}

int rp_borrow_pool_checkout(rp_borrow_pool_t *p, int owner_id) {
    int i;
    for (i = 0; i < p->total_slots; i++) {
        if (p->slots[i].available && !p->slots[i].borrowed) {
            p->slots[i].borrowed = 1;
            p->slots[i].owner_id = owner_id;
            p->slots[i].borrow_count++;
            p->borrowed_count++;
            return i;
        }
    }
    p->contention_count++;
    return -1;
}

int rp_borrow_pool_checkin(rp_borrow_pool_t *p, int idx, int owner_id) {
    if (idx < 0 || idx >= p->total_slots) return -1;
    if (!p->slots[idx].borrowed) return -2;
    if (p->slots[idx].owner_id != owner_id) return -3;
    p->slots[idx].borrowed = 0;
    p->slots[idx].owner_id = -1;
    p->borrowed_count--;
    return 0;
}

int rp_borrow_pool_free_count(rp_borrow_pool_t *p) {
    return p->total_slots - p->borrowed_count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1932: Connection pool borrow should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1932: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1932: Should contain rp_ functions");
}

#[test]
fn c1933_connection_pool_return() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_RET_POOL_MAX 20

typedef struct {
    int active;
    int dirty;
    uint32_t use_count;
    int needs_reset;
} rp_ret_conn_t;

typedef struct {
    rp_ret_conn_t conns[RP_RET_POOL_MAX];
    int count;
    int dirty_count;
    int reset_count;
} rp_return_pool_t;

void rp_return_pool_init(rp_return_pool_t *p) {
    int i;
    for (i = 0; i < RP_RET_POOL_MAX; i++) {
        p->conns[i].active = 0;
        p->conns[i].dirty = 0;
        p->conns[i].use_count = 0;
        p->conns[i].needs_reset = 0;
    }
    p->count = 0;
    p->dirty_count = 0;
    p->reset_count = 0;
}

int rp_return_pool_open(rp_return_pool_t *p) {
    int i;
    for (i = 0; i < RP_RET_POOL_MAX; i++) {
        if (!p->conns[i].active) {
            p->conns[i].active = 1;
            p->conns[i].dirty = 0;
            p->conns[i].needs_reset = 0;
            p->count++;
            return i;
        }
    }
    return -1;
}

int rp_return_pool_give_back(rp_return_pool_t *p, int idx, int was_dirty) {
    if (idx < 0 || idx >= RP_RET_POOL_MAX) return -1;
    if (!p->conns[idx].active) return -2;
    p->conns[idx].use_count++;
    if (was_dirty) {
        p->conns[idx].dirty = 1;
        p->conns[idx].needs_reset = 1;
        p->dirty_count++;
    }
    return 0;
}

int rp_return_pool_reset_dirty(rp_return_pool_t *p) {
    int reset = 0;
    int i;
    for (i = 0; i < RP_RET_POOL_MAX; i++) {
        if (p->conns[i].active && p->conns[i].needs_reset) {
            p->conns[i].dirty = 0;
            p->conns[i].needs_reset = 0;
            reset++;
        }
    }
    p->reset_count += reset;
    return reset;
}

int rp_return_pool_dirty_count(rp_return_pool_t *p) {
    return p->dirty_count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1933: Connection pool return should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1933: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1933: Should contain rp_ functions");
}

#[test]
fn c1934_connection_pool_health_check() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_HEALTH_MAX 16
#define RP_HEALTH_INTERVAL 30
#define RP_HEALTH_MAX_FAILURES 3

typedef struct {
    int alive;
    int healthy;
    int failure_count;
    int last_check;
    int total_checks;
} rp_health_conn_t;

typedef struct {
    rp_health_conn_t conns[RP_HEALTH_MAX];
    int count;
    int evicted;
    int current_time;
} rp_health_pool_t;

void rp_health_pool_init(rp_health_pool_t *p) {
    int i;
    for (i = 0; i < RP_HEALTH_MAX; i++) {
        p->conns[i].alive = 0;
        p->conns[i].healthy = 0;
        p->conns[i].failure_count = 0;
        p->conns[i].last_check = 0;
        p->conns[i].total_checks = 0;
    }
    p->count = 0;
    p->evicted = 0;
    p->current_time = 0;
}

int rp_health_pool_add(rp_health_pool_t *p) {
    int i;
    for (i = 0; i < RP_HEALTH_MAX; i++) {
        if (!p->conns[i].alive) {
            p->conns[i].alive = 1;
            p->conns[i].healthy = 1;
            p->conns[i].failure_count = 0;
            p->conns[i].last_check = p->current_time;
            p->count++;
            return i;
        }
    }
    return -1;
}

int rp_health_pool_check(rp_health_pool_t *p, int idx, int passed) {
    if (idx < 0 || idx >= RP_HEALTH_MAX || !p->conns[idx].alive) return -1;
    p->conns[idx].last_check = p->current_time;
    p->conns[idx].total_checks++;
    if (passed) {
        p->conns[idx].healthy = 1;
        p->conns[idx].failure_count = 0;
    } else {
        p->conns[idx].failure_count++;
        if (p->conns[idx].failure_count >= RP_HEALTH_MAX_FAILURES) {
            p->conns[idx].healthy = 0;
        }
    }
    return p->conns[idx].healthy;
}

int rp_health_pool_evict_unhealthy(rp_health_pool_t *p) {
    int evicted = 0;
    int i;
    for (i = 0; i < RP_HEALTH_MAX; i++) {
        if (p->conns[i].alive && !p->conns[i].healthy) {
            p->conns[i].alive = 0;
            p->count--;
            evicted++;
        }
    }
    p->evicted += evicted;
    return evicted;
}

int rp_health_pool_healthy_count(rp_health_pool_t *p) {
    int count = 0;
    int i;
    for (i = 0; i < RP_HEALTH_MAX; i++) {
        if (p->conns[i].alive && p->conns[i].healthy) count++;
    }
    return count;
}
"##;
    let result = transpile(c_code);
    assert!(
        result.is_ok(),
        "C1934: Connection pool health check should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1934: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1934: Should contain rp_ functions");
}

#[test]
fn c1935_connection_pool_timeout() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_TIMEOUT_MAX 24
#define RP_IDLE_TIMEOUT 120
#define RP_MAX_LIFETIME 3600

typedef struct {
    int active;
    int idle;
    uint32_t created_at;
    uint32_t last_used_at;
    uint32_t use_count;
} rp_timeout_conn_t;

typedef struct {
    rp_timeout_conn_t conns[RP_TIMEOUT_MAX];
    int count;
    uint32_t current_time;
    int timeout_evictions;
    int lifetime_evictions;
} rp_timeout_pool_t;

void rp_timeout_pool_init(rp_timeout_pool_t *p) {
    int i;
    for (i = 0; i < RP_TIMEOUT_MAX; i++) {
        p->conns[i].active = 0;
        p->conns[i].idle = 0;
    }
    p->count = 0;
    p->current_time = 0;
    p->timeout_evictions = 0;
    p->lifetime_evictions = 0;
}

int rp_timeout_pool_open(rp_timeout_pool_t *p) {
    int i;
    for (i = 0; i < RP_TIMEOUT_MAX; i++) {
        if (!p->conns[i].active) {
            p->conns[i].active = 1;
            p->conns[i].idle = 1;
            p->conns[i].created_at = p->current_time;
            p->conns[i].last_used_at = p->current_time;
            p->conns[i].use_count = 0;
            p->count++;
            return i;
        }
    }
    return -1;
}

void rp_timeout_pool_touch(rp_timeout_pool_t *p, int idx) {
    if (idx >= 0 && idx < RP_TIMEOUT_MAX && p->conns[idx].active) {
        p->conns[idx].last_used_at = p->current_time;
        p->conns[idx].idle = 0;
        p->conns[idx].use_count++;
    }
}

int rp_timeout_pool_reap(rp_timeout_pool_t *p) {
    int reaped = 0;
    int i;
    for (i = 0; i < RP_TIMEOUT_MAX; i++) {
        if (!p->conns[i].active) continue;
        uint32_t idle_time = p->current_time - p->conns[i].last_used_at;
        uint32_t lifetime = p->current_time - p->conns[i].created_at;
        if (idle_time > RP_IDLE_TIMEOUT) {
            p->conns[i].active = 0;
            p->count--;
            p->timeout_evictions++;
            reaped++;
        } else if (lifetime > RP_MAX_LIFETIME) {
            p->conns[i].active = 0;
            p->count--;
            p->lifetime_evictions++;
            reaped++;
        }
    }
    return reaped;
}

void rp_timeout_pool_advance_time(rp_timeout_pool_t *p, uint32_t delta) {
    p->current_time += delta;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1935: Connection pool timeout should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1935: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1935: Should contain rp_ functions");
}

// ============================================================================
// C1936-C1940: Buffer Pool
// ============================================================================

#[test]
fn c1936_buffer_pool_allocation() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_BUF_POOL_SIZE 32
#define RP_BUF_SIZE 256

typedef struct {
    char data[RP_BUF_SIZE];
    int in_use;
    size_t used_bytes;
    uint32_t alloc_id;
} rp_buffer_t;

typedef struct {
    rp_buffer_t buffers[RP_BUF_POOL_SIZE];
    int free_count;
    uint32_t next_alloc_id;
    uint32_t total_allocs;
} rp_buf_pool_t;

void rp_buf_pool_init(rp_buf_pool_t *p) {
    int i;
    for (i = 0; i < RP_BUF_POOL_SIZE; i++) {
        p->buffers[i].in_use = 0;
        p->buffers[i].used_bytes = 0;
        p->buffers[i].alloc_id = 0;
    }
    p->free_count = RP_BUF_POOL_SIZE;
    p->next_alloc_id = 1;
    p->total_allocs = 0;
}

int rp_buf_pool_alloc(rp_buf_pool_t *p) {
    int i;
    for (i = 0; i < RP_BUF_POOL_SIZE; i++) {
        if (!p->buffers[i].in_use) {
            p->buffers[i].in_use = 1;
            p->buffers[i].used_bytes = 0;
            p->buffers[i].alloc_id = p->next_alloc_id++;
            p->free_count--;
            p->total_allocs++;
            return i;
        }
    }
    return -1;
}

int rp_buf_pool_write(rp_buf_pool_t *p, int idx, size_t bytes) {
    if (idx < 0 || idx >= RP_BUF_POOL_SIZE) return -1;
    if (!p->buffers[idx].in_use) return -2;
    if (p->buffers[idx].used_bytes + bytes > RP_BUF_SIZE) return -3;
    p->buffers[idx].used_bytes += bytes;
    return 0;
}

void rp_buf_pool_free(rp_buf_pool_t *p, int idx) {
    if (idx >= 0 && idx < RP_BUF_POOL_SIZE && p->buffers[idx].in_use) {
        p->buffers[idx].in_use = 0;
        p->buffers[idx].used_bytes = 0;
        p->free_count++;
    }
}

int rp_buf_pool_free_count(rp_buf_pool_t *p) {
    return p->free_count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1936: Buffer pool allocation should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1936: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1936: Should contain rp_ functions");
}

#[test]
fn c1937_buffer_chain() {
    let c_code = r##"
typedef unsigned long size_t;

#define RP_CHAIN_MAX_BUFS 16
#define RP_CHAIN_BUF_SIZE 512

typedef struct {
    char data[RP_CHAIN_BUF_SIZE];
    size_t used;
    int next;
} rp_chain_buf_t;

typedef struct {
    rp_chain_buf_t bufs[RP_CHAIN_MAX_BUFS];
    int allocated[RP_CHAIN_MAX_BUFS];
    int head;
    int tail;
    int chain_length;
    size_t total_bytes;
} rp_buf_chain_t;

void rp_buf_chain_init(rp_buf_chain_t *c) {
    int i;
    for (i = 0; i < RP_CHAIN_MAX_BUFS; i++) {
        c->bufs[i].used = 0;
        c->bufs[i].next = -1;
        c->allocated[i] = 0;
    }
    c->head = -1;
    c->tail = -1;
    c->chain_length = 0;
    c->total_bytes = 0;
}

int rp_buf_chain_alloc_buf(rp_buf_chain_t *c) {
    int i;
    for (i = 0; i < RP_CHAIN_MAX_BUFS; i++) {
        if (!c->allocated[i]) {
            c->allocated[i] = 1;
            c->bufs[i].used = 0;
            c->bufs[i].next = -1;
            return i;
        }
    }
    return -1;
}

int rp_buf_chain_append(rp_buf_chain_t *c, size_t bytes) {
    int idx = rp_buf_chain_alloc_buf(c);
    if (idx < 0) return -1;
    c->bufs[idx].used = (bytes > RP_CHAIN_BUF_SIZE) ? RP_CHAIN_BUF_SIZE : bytes;
    if (c->tail >= 0) {
        c->bufs[c->tail].next = idx;
    }
    c->tail = idx;
    if (c->head < 0) c->head = idx;
    c->chain_length++;
    c->total_bytes += c->bufs[idx].used;
    return idx;
}

void rp_buf_chain_pop_front(rp_buf_chain_t *c) {
    if (c->head < 0) return;
    int old_head = c->head;
    c->total_bytes -= c->bufs[old_head].used;
    c->head = c->bufs[old_head].next;
    c->allocated[old_head] = 0;
    c->chain_length--;
    if (c->head < 0) c->tail = -1;
}

size_t rp_buf_chain_total(rp_buf_chain_t *c) {
    return c->total_bytes;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1937: Buffer chain should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1937: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1937: Should contain rp_ functions");
}

#[test]
fn c1938_buffer_recycle() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_RECYCLE_MAX 24

typedef struct {
    int in_use;
    size_t capacity;
    size_t used;
    uint32_t recycle_count;
} rp_recycle_buf_t;

typedef struct {
    rp_recycle_buf_t bufs[RP_RECYCLE_MAX];
    int free_stack[RP_RECYCLE_MAX];
    int free_top;
    uint32_t total_recycled;
    uint32_t total_fresh;
} rp_recycle_pool_t;

void rp_recycle_pool_init(rp_recycle_pool_t *p, size_t buf_cap) {
    int i;
    for (i = 0; i < RP_RECYCLE_MAX; i++) {
        p->bufs[i].in_use = 0;
        p->bufs[i].capacity = buf_cap;
        p->bufs[i].used = 0;
        p->bufs[i].recycle_count = 0;
        p->free_stack[i] = i;
    }
    p->free_top = RP_RECYCLE_MAX;
    p->total_recycled = 0;
    p->total_fresh = 0;
}

int rp_recycle_pool_get(rp_recycle_pool_t *p) {
    if (p->free_top <= 0) return -1;
    p->free_top--;
    int idx = p->free_stack[p->free_top];
    p->bufs[idx].in_use = 1;
    p->bufs[idx].used = 0;
    if (p->bufs[idx].recycle_count > 0) {
        p->total_recycled++;
    } else {
        p->total_fresh++;
    }
    return idx;
}

void rp_recycle_pool_put(rp_recycle_pool_t *p, int idx) {
    if (idx < 0 || idx >= RP_RECYCLE_MAX || !p->bufs[idx].in_use) return;
    p->bufs[idx].in_use = 0;
    p->bufs[idx].used = 0;
    p->bufs[idx].recycle_count++;
    p->free_stack[p->free_top++] = idx;
}

int rp_recycle_pool_recycle_ratio(rp_recycle_pool_t *p) {
    uint32_t total = p->total_recycled + p->total_fresh;
    if (total == 0) return 0;
    return (int)((p->total_recycled * 100) / total);
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1938: Buffer recycle should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1938: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1938: Should contain rp_ functions");
}

#[test]
fn c1939_buffer_pool_growth() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_GROW_INITIAL 8
#define RP_GROW_MAX 64
#define RP_GROW_THRESHOLD_PCT 75

typedef struct {
    int active[RP_GROW_MAX];
    int capacity;
    int used;
    int grow_events;
    int shrink_events;
    uint32_t high_watermark;
} rp_growable_pool_t;

void rp_growable_pool_init(rp_growable_pool_t *p) {
    int i;
    for (i = 0; i < RP_GROW_MAX; i++) {
        p->active[i] = 0;
    }
    p->capacity = RP_GROW_INITIAL;
    p->used = 0;
    p->grow_events = 0;
    p->shrink_events = 0;
    p->high_watermark = 0;
}

int rp_growable_pool_should_grow(rp_growable_pool_t *p) {
    return (p->used * 100 / p->capacity) >= RP_GROW_THRESHOLD_PCT;
}

int rp_growable_pool_grow(rp_growable_pool_t *p) {
    int new_cap = p->capacity * 2;
    if (new_cap > RP_GROW_MAX) new_cap = RP_GROW_MAX;
    if (new_cap <= p->capacity) return 0;
    p->capacity = new_cap;
    p->grow_events++;
    return new_cap;
}

int rp_growable_pool_alloc(rp_growable_pool_t *p) {
    if (p->used >= p->capacity) {
        if (rp_growable_pool_grow(p) == 0) return -1;
    }
    int i;
    for (i = 0; i < p->capacity; i++) {
        if (!p->active[i]) {
            p->active[i] = 1;
            p->used++;
            if (p->used > (int)p->high_watermark) {
                p->high_watermark = (uint32_t)p->used;
            }
            return i;
        }
    }
    return -1;
}

void rp_growable_pool_free(rp_growable_pool_t *p, int idx) {
    if (idx >= 0 && idx < p->capacity && p->active[idx]) {
        p->active[idx] = 0;
        p->used--;
    }
}

int rp_growable_pool_capacity(rp_growable_pool_t *p) {
    return p->capacity;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1939: Buffer pool growth should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1939: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1939: Should contain rp_ functions");
}

#[test]
fn c1940_buffer_pool_size_classes() {
    let c_code = r##"
typedef unsigned long size_t;

#define RP_SIZE_CLASSES 4
#define RP_PER_CLASS 8

typedef struct {
    int free_count;
    size_t buf_size;
    int alloc_count;
    int miss_count;
} rp_size_class_t;

typedef struct {
    rp_size_class_t classes[RP_SIZE_CLASSES];
    int total_allocs;
    int total_misses;
} rp_sized_buf_pool_t;

void rp_sized_buf_pool_init(rp_sized_buf_pool_t *p) {
    size_t sizes[4] = {64, 256, 1024, 4096};
    int i;
    for (i = 0; i < RP_SIZE_CLASSES; i++) {
        p->classes[i].buf_size = sizes[i];
        p->classes[i].free_count = RP_PER_CLASS;
        p->classes[i].alloc_count = 0;
        p->classes[i].miss_count = 0;
    }
    p->total_allocs = 0;
    p->total_misses = 0;
}

int rp_sized_buf_pool_find_class(rp_sized_buf_pool_t *p, size_t needed) {
    int i;
    for (i = 0; i < RP_SIZE_CLASSES; i++) {
        if (needed <= p->classes[i].buf_size) return i;
    }
    return -1;
}

int rp_sized_buf_pool_alloc(rp_sized_buf_pool_t *p, size_t needed) {
    int cls = rp_sized_buf_pool_find_class(p, needed);
    if (cls < 0) {
        p->total_misses++;
        return -1;
    }
    if (p->classes[cls].free_count <= 0) {
        p->classes[cls].miss_count++;
        p->total_misses++;
        return -2;
    }
    p->classes[cls].free_count--;
    p->classes[cls].alloc_count++;
    p->total_allocs++;
    return cls;
}

void rp_sized_buf_pool_free(rp_sized_buf_pool_t *p, int cls) {
    if (cls >= 0 && cls < RP_SIZE_CLASSES) {
        if (p->classes[cls].free_count < RP_PER_CLASS) {
            p->classes[cls].free_count++;
        }
    }
}

int rp_sized_buf_pool_total_free(rp_sized_buf_pool_t *p) {
    int total = 0;
    int i;
    for (i = 0; i < RP_SIZE_CLASSES; i++) {
        total += p->classes[i].free_count;
    }
    return total;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1940: Buffer pool size classes should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1940: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1940: Should contain rp_ functions");
}

// ============================================================================
// C1941-C1945: Handle Table
// ============================================================================

#[test]
fn c1941_handle_allocation() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_HANDLE_MAX 128

typedef struct {
    int active;
    uint32_t generation;
    int data_index;
} rp_handle_entry_t;

typedef struct {
    rp_handle_entry_t entries[RP_HANDLE_MAX];
    int free_list[RP_HANDLE_MAX];
    int free_top;
    int count;
} rp_handle_table_t;

void rp_handle_table_init(rp_handle_table_t *t) {
    int i;
    for (i = 0; i < RP_HANDLE_MAX; i++) {
        t->entries[i].active = 0;
        t->entries[i].generation = 0;
        t->entries[i].data_index = -1;
        t->free_list[i] = i;
    }
    t->free_top = RP_HANDLE_MAX;
    t->count = 0;
}

uint32_t rp_handle_table_alloc(rp_handle_table_t *t, int data_index) {
    if (t->free_top <= 0) return 0;
    t->free_top--;
    int idx = t->free_list[t->free_top];
    t->entries[idx].active = 1;
    t->entries[idx].generation++;
    t->entries[idx].data_index = data_index;
    t->count++;
    return (t->entries[idx].generation << 16) | (uint32_t)idx;
}

void rp_handle_table_free(rp_handle_table_t *t, int idx) {
    if (idx >= 0 && idx < RP_HANDLE_MAX && t->entries[idx].active) {
        t->entries[idx].active = 0;
        t->entries[idx].data_index = -1;
        t->free_list[t->free_top++] = idx;
        t->count--;
    }
}

int rp_handle_table_count(rp_handle_table_t *t) {
    return t->count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1941: Handle allocation should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1941: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1941: Should contain rp_ functions");
}

#[test]
fn c1942_handle_lookup() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_LOOKUP_MAX 64

typedef struct {
    int active;
    uint32_t generation;
    int value;
    int access_count;
} rp_lookup_entry_t;

typedef struct {
    rp_lookup_entry_t entries[RP_LOOKUP_MAX];
    int count;
    int miss_count;
    int stale_count;
} rp_lookup_table_t;

void rp_lookup_table_init(rp_lookup_table_t *t) {
    int i;
    for (i = 0; i < RP_LOOKUP_MAX; i++) {
        t->entries[i].active = 0;
        t->entries[i].generation = 0;
        t->entries[i].value = 0;
        t->entries[i].access_count = 0;
    }
    t->count = 0;
    t->miss_count = 0;
    t->stale_count = 0;
}

int rp_lookup_table_insert(rp_lookup_table_t *t, int value) {
    int i;
    for (i = 0; i < RP_LOOKUP_MAX; i++) {
        if (!t->entries[i].active) {
            t->entries[i].active = 1;
            t->entries[i].generation++;
            t->entries[i].value = value;
            t->entries[i].access_count = 0;
            t->count++;
            return i;
        }
    }
    return -1;
}

int rp_lookup_table_get(rp_lookup_table_t *t, int idx, uint32_t expected_gen) {
    if (idx < 0 || idx >= RP_LOOKUP_MAX) {
        t->miss_count++;
        return -1;
    }
    if (!t->entries[idx].active) {
        t->miss_count++;
        return -2;
    }
    if (t->entries[idx].generation != expected_gen) {
        t->stale_count++;
        return -3;
    }
    t->entries[idx].access_count++;
    return t->entries[idx].value;
}

void rp_lookup_table_remove(rp_lookup_table_t *t, int idx) {
    if (idx >= 0 && idx < RP_LOOKUP_MAX && t->entries[idx].active) {
        t->entries[idx].active = 0;
        t->count--;
    }
}

int rp_lookup_table_miss_rate(rp_lookup_table_t *t) {
    int total = t->miss_count + t->stale_count + t->count;
    if (total == 0) return 0;
    return (t->miss_count * 100) / total;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1942: Handle lookup should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1942: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1942: Should contain rp_ functions");
}

#[test]
fn c1943_handle_revoke() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_REVOKE_MAX 48

typedef struct {
    int active;
    uint32_t generation;
    int owner;
    int revoked;
} rp_revoke_entry_t;

typedef struct {
    rp_revoke_entry_t entries[RP_REVOKE_MAX];
    int count;
    int revoke_count;
    int total_revoked;
} rp_revoke_table_t;

void rp_revoke_table_init(rp_revoke_table_t *t) {
    int i;
    for (i = 0; i < RP_REVOKE_MAX; i++) {
        t->entries[i].active = 0;
        t->entries[i].generation = 0;
        t->entries[i].owner = -1;
        t->entries[i].revoked = 0;
    }
    t->count = 0;
    t->revoke_count = 0;
    t->total_revoked = 0;
}

int rp_revoke_table_grant(rp_revoke_table_t *t, int owner) {
    int i;
    for (i = 0; i < RP_REVOKE_MAX; i++) {
        if (!t->entries[i].active) {
            t->entries[i].active = 1;
            t->entries[i].generation++;
            t->entries[i].owner = owner;
            t->entries[i].revoked = 0;
            t->count++;
            return i;
        }
    }
    return -1;
}

int rp_revoke_table_revoke(rp_revoke_table_t *t, int idx) {
    if (idx < 0 || idx >= RP_REVOKE_MAX) return -1;
    if (!t->entries[idx].active) return -2;
    if (t->entries[idx].revoked) return -3;
    t->entries[idx].revoked = 1;
    t->entries[idx].generation++;
    t->revoke_count++;
    t->total_revoked++;
    return 0;
}

int rp_revoke_table_is_valid(rp_revoke_table_t *t, int idx, uint32_t gen) {
    if (idx < 0 || idx >= RP_REVOKE_MAX) return 0;
    if (!t->entries[idx].active) return 0;
    if (t->entries[idx].revoked) return 0;
    return t->entries[idx].generation == gen;
}

int rp_revoke_table_revoke_by_owner(rp_revoke_table_t *t, int owner) {
    int revoked = 0;
    int i;
    for (i = 0; i < RP_REVOKE_MAX; i++) {
        if (t->entries[i].active && !t->entries[i].revoked &&
            t->entries[i].owner == owner) {
            t->entries[i].revoked = 1;
            t->entries[i].generation++;
            revoked++;
        }
    }
    t->total_revoked += revoked;
    return revoked;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1943: Handle revoke should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1943: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1943: Should contain rp_ functions");
}

#[test]
fn c1944_handle_generation_counter() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_GEN_MAX 32

typedef struct {
    uint32_t generation;
    int active;
    int data;
} rp_gen_slot_t;

typedef struct {
    rp_gen_slot_t slots[RP_GEN_MAX];
    int free_list[RP_GEN_MAX];
    int free_top;
    uint32_t global_gen;
    int stale_access_count;
} rp_gen_table_t;

void rp_gen_table_init(rp_gen_table_t *t) {
    int i;
    for (i = 0; i < RP_GEN_MAX; i++) {
        t->slots[i].generation = 0;
        t->slots[i].active = 0;
        t->slots[i].data = 0;
        t->free_list[i] = i;
    }
    t->free_top = RP_GEN_MAX;
    t->global_gen = 1;
    t->stale_access_count = 0;
}

uint32_t rp_gen_table_create(rp_gen_table_t *t, int data) {
    if (t->free_top <= 0) return 0;
    t->free_top--;
    int idx = t->free_list[t->free_top];
    t->slots[idx].generation = t->global_gen++;
    t->slots[idx].active = 1;
    t->slots[idx].data = data;
    return (t->slots[idx].generation << 8) | (uint32_t)idx;
}

int rp_gen_table_resolve(rp_gen_table_t *t, uint32_t handle) {
    int idx = (int)(handle & 0xFF);
    uint32_t gen = handle >> 8;
    if (idx < 0 || idx >= RP_GEN_MAX) return -1;
    if (!t->slots[idx].active) return -2;
    if (t->slots[idx].generation != gen) {
        t->stale_access_count++;
        return -3;
    }
    return t->slots[idx].data;
}

void rp_gen_table_destroy(rp_gen_table_t *t, int idx) {
    if (idx >= 0 && idx < RP_GEN_MAX && t->slots[idx].active) {
        t->slots[idx].active = 0;
        t->free_list[t->free_top++] = idx;
    }
}

int rp_gen_table_stale_count(rp_gen_table_t *t) {
    return t->stale_access_count;
}
"##;
    let result = transpile(c_code);
    assert!(
        result.is_ok(),
        "C1944: Handle generation counter should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1944: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1944: Should contain rp_ functions");
}

#[test]
fn c1945_handle_table_compact() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_COMPACT_MAX 64

typedef struct {
    int active;
    int data;
    int original_index;
} rp_compact_entry_t;

typedef struct {
    rp_compact_entry_t entries[RP_COMPACT_MAX];
    int count;
    int active_count;
    int compact_ops;
    int holes;
} rp_compact_table_t;

void rp_compact_table_init(rp_compact_table_t *t) {
    int i;
    for (i = 0; i < RP_COMPACT_MAX; i++) {
        t->entries[i].active = 0;
        t->entries[i].data = 0;
        t->entries[i].original_index = i;
    }
    t->count = 0;
    t->active_count = 0;
    t->compact_ops = 0;
    t->holes = 0;
}

int rp_compact_table_add(rp_compact_table_t *t, int data) {
    if (t->count >= RP_COMPACT_MAX) return -1;
    int idx = t->count++;
    t->entries[idx].active = 1;
    t->entries[idx].data = data;
    t->entries[idx].original_index = idx;
    t->active_count++;
    return idx;
}

void rp_compact_table_remove(rp_compact_table_t *t, int idx) {
    if (idx >= 0 && idx < t->count && t->entries[idx].active) {
        t->entries[idx].active = 0;
        t->active_count--;
        t->holes++;
    }
}

int rp_compact_table_compact(rp_compact_table_t *t) {
    int write_pos = 0;
    int moved = 0;
    int i;
    for (i = 0; i < t->count; i++) {
        if (t->entries[i].active) {
            if (i != write_pos) {
                t->entries[write_pos] = t->entries[i];
                t->entries[i].active = 0;
                moved++;
            }
            write_pos++;
        }
    }
    t->count = write_pos;
    t->holes = 0;
    t->compact_ops++;
    return moved;
}

int rp_compact_table_fragmentation_pct(rp_compact_table_t *t) {
    if (t->count == 0) return 0;
    return (t->holes * 100) / t->count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1945: Handle table compact should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1945: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1945: Should contain rp_ functions");
}

// ============================================================================
// C1946-C1950: Resource Lifecycle
// ============================================================================

#[test]
fn c1946_init_deinit_tracking() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_LIFECYCLE_MAX 32

typedef struct {
    int initialized;
    int finalized;
    uint32_t init_order;
    uint32_t fini_order;
    int resource_type;
} rp_lifecycle_entry_t;

typedef struct {
    rp_lifecycle_entry_t entries[RP_LIFECYCLE_MAX];
    int count;
    uint32_t next_init_order;
    uint32_t next_fini_order;
    int init_errors;
    int fini_errors;
} rp_lifecycle_tracker_t;

void rp_lifecycle_tracker_init(rp_lifecycle_tracker_t *t) {
    int i;
    for (i = 0; i < RP_LIFECYCLE_MAX; i++) {
        t->entries[i].initialized = 0;
        t->entries[i].finalized = 0;
        t->entries[i].init_order = 0;
        t->entries[i].fini_order = 0;
        t->entries[i].resource_type = 0;
    }
    t->count = 0;
    t->next_init_order = 1;
    t->next_fini_order = 1;
    t->init_errors = 0;
    t->fini_errors = 0;
}

int rp_lifecycle_tracker_register_init(rp_lifecycle_tracker_t *t, int rtype) {
    if (t->count >= RP_LIFECYCLE_MAX) return -1;
    int idx = t->count++;
    t->entries[idx].initialized = 1;
    t->entries[idx].finalized = 0;
    t->entries[idx].init_order = t->next_init_order++;
    t->entries[idx].resource_type = rtype;
    return idx;
}

int rp_lifecycle_tracker_register_fini(rp_lifecycle_tracker_t *t, int idx) {
    if (idx < 0 || idx >= t->count) return -1;
    if (!t->entries[idx].initialized) {
        t->fini_errors++;
        return -2;
    }
    if (t->entries[idx].finalized) {
        t->fini_errors++;
        return -3;
    }
    t->entries[idx].finalized = 1;
    t->entries[idx].fini_order = t->next_fini_order++;
    return 0;
}

int rp_lifecycle_tracker_check_leaks(rp_lifecycle_tracker_t *t) {
    int leaks = 0;
    int i;
    for (i = 0; i < t->count; i++) {
        if (t->entries[i].initialized && !t->entries[i].finalized) {
            leaks++;
        }
    }
    return leaks;
}

int rp_lifecycle_tracker_total(rp_lifecycle_tracker_t *t) {
    return t->count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1946: Init/deinit tracking should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1946: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1946: Should contain rp_ functions");
}

#[test]
fn c1947_reference_counting() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_REFCOUNT_MAX 48

typedef struct {
    int active;
    uint32_t ref_count;
    int data;
    int destroy_count;
} rp_refcounted_t;

typedef struct {
    rp_refcounted_t objects[RP_REFCOUNT_MAX];
    int count;
    int total_created;
    int total_destroyed;
} rp_refcount_pool_t;

void rp_refcount_pool_init(rp_refcount_pool_t *p) {
    int i;
    for (i = 0; i < RP_REFCOUNT_MAX; i++) {
        p->objects[i].active = 0;
        p->objects[i].ref_count = 0;
        p->objects[i].data = 0;
        p->objects[i].destroy_count = 0;
    }
    p->count = 0;
    p->total_created = 0;
    p->total_destroyed = 0;
}

int rp_refcount_pool_create(rp_refcount_pool_t *p, int data) {
    int i;
    for (i = 0; i < RP_REFCOUNT_MAX; i++) {
        if (!p->objects[i].active) {
            p->objects[i].active = 1;
            p->objects[i].ref_count = 1;
            p->objects[i].data = data;
            p->count++;
            p->total_created++;
            return i;
        }
    }
    return -1;
}

int rp_refcount_pool_retain(rp_refcount_pool_t *p, int idx) {
    if (idx < 0 || idx >= RP_REFCOUNT_MAX) return -1;
    if (!p->objects[idx].active) return -2;
    p->objects[idx].ref_count++;
    return (int)p->objects[idx].ref_count;
}

int rp_refcount_pool_release(rp_refcount_pool_t *p, int idx) {
    if (idx < 0 || idx >= RP_REFCOUNT_MAX) return -1;
    if (!p->objects[idx].active) return -2;
    if (p->objects[idx].ref_count == 0) return -3;
    p->objects[idx].ref_count--;
    if (p->objects[idx].ref_count == 0) {
        p->objects[idx].active = 0;
        p->objects[idx].destroy_count++;
        p->count--;
        p->total_destroyed++;
        return 0;
    }
    return (int)p->objects[idx].ref_count;
}

uint32_t rp_refcount_pool_get_count(rp_refcount_pool_t *p, int idx) {
    if (idx < 0 || idx >= RP_REFCOUNT_MAX || !p->objects[idx].active) return 0;
    return p->objects[idx].ref_count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1947: Reference counting should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1947: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1947: Should contain rp_ functions");
}

#[test]
fn c1948_resource_leak_detection() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_LEAK_DET_MAX 64

typedef struct {
    int allocated;
    int freed;
    size_t size;
    int tag;
    uint32_t alloc_time;
} rp_leak_record_t;

typedef struct {
    rp_leak_record_t records[RP_LEAK_DET_MAX];
    int count;
    uint32_t current_time;
    int detected_leaks;
    size_t leaked_bytes;
} rp_leak_detector_t;

void rp_leak_detector_init(rp_leak_detector_t *d) {
    int i;
    for (i = 0; i < RP_LEAK_DET_MAX; i++) {
        d->records[i].allocated = 0;
        d->records[i].freed = 0;
    }
    d->count = 0;
    d->current_time = 0;
    d->detected_leaks = 0;
    d->leaked_bytes = 0;
}

int rp_leak_detector_track_alloc(rp_leak_detector_t *d, size_t size, int tag) {
    if (d->count >= RP_LEAK_DET_MAX) return -1;
    int idx = d->count++;
    d->records[idx].allocated = 1;
    d->records[idx].freed = 0;
    d->records[idx].size = size;
    d->records[idx].tag = tag;
    d->records[idx].alloc_time = d->current_time;
    return idx;
}

int rp_leak_detector_track_free(rp_leak_detector_t *d, int idx) {
    if (idx < 0 || idx >= d->count) return -1;
    if (!d->records[idx].allocated) return -2;
    if (d->records[idx].freed) return -3;
    d->records[idx].freed = 1;
    return 0;
}

int rp_leak_detector_scan(rp_leak_detector_t *d) {
    int leaks = 0;
    size_t bytes = 0;
    int i;
    for (i = 0; i < d->count; i++) {
        if (d->records[i].allocated && !d->records[i].freed) {
            leaks++;
            bytes += d->records[i].size;
        }
    }
    d->detected_leaks = leaks;
    d->leaked_bytes = bytes;
    return leaks;
}

size_t rp_leak_detector_leaked_bytes(rp_leak_detector_t *d) {
    return d->leaked_bytes;
}

int rp_leak_detector_leaks_by_tag(rp_leak_detector_t *d, int tag) {
    int leaks = 0;
    int i;
    for (i = 0; i < d->count; i++) {
        if (d->records[i].allocated && !d->records[i].freed &&
            d->records[i].tag == tag) {
            leaks++;
        }
    }
    return leaks;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1948: Resource leak detection should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1948: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1948: Should contain rp_ functions");
}

#[test]
fn c1949_resource_cleanup_stack() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_CLEANUP_MAX 32

typedef struct {
    int resource_id;
    int resource_type;
    int cleaned;
} rp_cleanup_entry_t;

typedef struct {
    rp_cleanup_entry_t stack[RP_CLEANUP_MAX];
    int top;
    int total_cleaned;
    int total_registered;
    int cleanup_errors;
} rp_cleanup_stack_t;

void rp_cleanup_stack_init(rp_cleanup_stack_t *s) {
    s->top = 0;
    s->total_cleaned = 0;
    s->total_registered = 0;
    s->cleanup_errors = 0;
}

int rp_cleanup_stack_push(rp_cleanup_stack_t *s, int res_id, int res_type) {
    if (s->top >= RP_CLEANUP_MAX) return -1;
    s->stack[s->top].resource_id = res_id;
    s->stack[s->top].resource_type = res_type;
    s->stack[s->top].cleaned = 0;
    s->top++;
    s->total_registered++;
    return s->top - 1;
}

int rp_cleanup_stack_pop_and_clean(rp_cleanup_stack_t *s) {
    if (s->top <= 0) return -1;
    s->top--;
    if (s->stack[s->top].cleaned) {
        s->cleanup_errors++;
        return -2;
    }
    s->stack[s->top].cleaned = 1;
    s->total_cleaned++;
    return s->stack[s->top].resource_id;
}

int rp_cleanup_stack_unwind_all(rp_cleanup_stack_t *s) {
    int cleaned = 0;
    while (s->top > 0) {
        s->top--;
        if (!s->stack[s->top].cleaned) {
            s->stack[s->top].cleaned = 1;
            cleaned++;
        }
    }
    s->total_cleaned += cleaned;
    return cleaned;
}

int rp_cleanup_stack_pending(rp_cleanup_stack_t *s) {
    int pending = 0;
    int i;
    for (i = 0; i < s->top; i++) {
        if (!s->stack[i].cleaned) pending++;
    }
    return pending;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1949: Resource cleanup stack should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1949: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1949: Should contain rp_ functions");
}

#[test]
fn c1950_resource_scope_guard() {
    let c_code = r##"
typedef unsigned long size_t;
typedef unsigned int uint32_t;

#define RP_SCOPE_MAX_DEPTH 8
#define RP_SCOPE_MAX_RESOURCES 16

typedef struct {
    int resource_ids[RP_SCOPE_MAX_RESOURCES];
    int resource_count;
    int scope_id;
} rp_scope_t;

typedef struct {
    rp_scope_t scopes[RP_SCOPE_MAX_DEPTH];
    int depth;
    int next_scope_id;
    int total_resources_guarded;
    int total_scopes_exited;
} rp_scope_guard_t;

void rp_scope_guard_init(rp_scope_guard_t *g) {
    g->depth = 0;
    g->next_scope_id = 1;
    g->total_resources_guarded = 0;
    g->total_scopes_exited = 0;
}

int rp_scope_guard_enter(rp_scope_guard_t *g) {
    if (g->depth >= RP_SCOPE_MAX_DEPTH) return -1;
    int idx = g->depth;
    g->scopes[idx].resource_count = 0;
    g->scopes[idx].scope_id = g->next_scope_id++;
    g->depth++;
    return g->scopes[idx].scope_id;
}

int rp_scope_guard_register(rp_scope_guard_t *g, int resource_id) {
    if (g->depth <= 0) return -1;
    int scope_idx = g->depth - 1;
    if (g->scopes[scope_idx].resource_count >= RP_SCOPE_MAX_RESOURCES) return -2;
    int res_idx = g->scopes[scope_idx].resource_count;
    g->scopes[scope_idx].resource_ids[res_idx] = resource_id;
    g->scopes[scope_idx].resource_count++;
    g->total_resources_guarded++;
    return 0;
}

int rp_scope_guard_exit(rp_scope_guard_t *g) {
    if (g->depth <= 0) return -1;
    g->depth--;
    int released = g->scopes[g->depth].resource_count;
    g->scopes[g->depth].resource_count = 0;
    g->total_scopes_exited++;
    return released;
}

int rp_scope_guard_depth(rp_scope_guard_t *g) {
    return g->depth;
}

int rp_scope_guard_current_resource_count(rp_scope_guard_t *g) {
    if (g->depth <= 0) return 0;
    return g->scopes[g->depth - 1].resource_count;
}
"##;
    let result = transpile(c_code);
    assert!(result.is_ok(), "C1950: Resource scope guard should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C1950: Output should not be empty");
    assert!(code.contains("fn rp_"), "C1950: Should contain rp_ functions");
}