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
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
//! Popperian Falsification Test Suite for Decy C-to-Rust Transpiler
//!
//! C476-C500: OS Kernel and System Programming C patterns -- process
//! management, virtual memory, file systems, device drivers, schedulers,
//! and kernel infrastructure code.
//! Tests are APPEND-ONLY per Popperian methodology.
//! Falsified tests are marked #[ignore = "FALSIFIED: reason"].
//!
//! These tests exercise real-world OS kernel patterns commonly found in
//! Linux, xv6, MINIX, and similar operating system kernels -- all
//! expressed as valid C99.
//!
//! Organization:
//! - C476-C480: Process management (PCB, page fault, FD table, pipe, scheduler)
//! - C481-C485: Kernel subsystems (syscall dispatch, driver queue, FS, buffer cache, TTY)
//! - C486-C490: Kernel infrastructure (skbuff, buddy alloc, signals, mount, printk)
//! - C491-C495: Hardware and memory (interrupt controller, TLS, COW, modules, elevator)
//! - C496-C500: Networking and boot (packet filter, timekeeping, RNG, kref, ELF loader)
//!
//! Results: 25 passing, 0 falsified (100.0% pass rate)

// ============================================================================
// C476-C480: Process Management
// ============================================================================

#[test]
fn c476_process_control_block_with_context_switch() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

#define MAX_PROCS 256
#define PROC_RUNNING 1
#define PROC_READY   2
#define PROC_BLOCKED 3
#define PROC_ZOMBIE  4

struct cpu_context {
    uint32_t eax;
    uint32_t ebx;
    uint32_t ecx;
    uint32_t edx;
    uint32_t esi;
    uint32_t edi;
    uint32_t ebp;
    uint32_t esp;
    uint32_t eip;
    uint32_t eflags;
};

struct pcb {
    int pid;
    int ppid;
    int state;
    int priority;
    int time_slice;
    int exit_code;
    struct cpu_context ctx;
    uint32_t page_dir;
    int open_fds[16];
    int fd_count;
};

static struct pcb proc_table[256];
static int next_pid = 1;
static int current_pid = 0;

int pcb_alloc(int ppid, int priority) {
    if (next_pid >= MAX_PROCS) return -1;
    int pid = next_pid++;
    proc_table[pid].pid = pid;
    proc_table[pid].ppid = ppid;
    proc_table[pid].state = PROC_READY;
    proc_table[pid].priority = priority;
    proc_table[pid].time_slice = 10;
    proc_table[pid].exit_code = 0;
    proc_table[pid].fd_count = 0;
    proc_table[pid].page_dir = 0;
    return pid;
}

void pcb_save_context(int pid, const struct cpu_context *ctx) {
    proc_table[pid].ctx = *ctx;
}

void pcb_set_state(int pid, int state) {
    proc_table[pid].state = state;
}

int pcb_find_ready(void) {
    int best = -1;
    int best_prio = -1;
    for (int i = 1; i < next_pid; i++) {
        if (proc_table[i].state == PROC_READY && proc_table[i].priority > best_prio) {
            best = i;
            best_prio = proc_table[i].priority;
        }
    }
    return best;
}

void pcb_exit(int pid, int code) {
    proc_table[pid].state = PROC_ZOMBIE;
    proc_table[pid].exit_code = code;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C476: Process control block - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C476: empty output");
    assert!(code.contains("fn pcb_alloc"), "C476: Should contain pcb_alloc");
    assert!(code.contains("fn pcb_find_ready"), "C476: Should contain pcb_find_ready");
}

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

#define PAGE_SIZE 4096
#define PAGE_PRESENT  0x01
#define PAGE_WRITE    0x02
#define PAGE_USER     0x04
#define PT_ENTRIES    1024

struct page_table_entry {
    uint32_t frame;
    uint32_t flags;
};

struct page_directory {
    struct page_table_entry entries[1024];
    int mapped_count;
};

static uint32_t next_frame = 0x100000;

uint32_t alloc_frame(void) {
    uint32_t f = next_frame;
    next_frame += PAGE_SIZE;
    return f;
}

int pte_is_present(const struct page_table_entry *pte) {
    return (pte->flags & PAGE_PRESENT) != 0;
}

void pte_set(struct page_table_entry *pte, uint32_t frame, uint32_t flags) {
    pte->frame = frame;
    pte->flags = flags | PAGE_PRESENT;
}

int page_directory_lookup(struct page_directory *pd, uint32_t vaddr) {
    uint32_t idx = (vaddr / PAGE_SIZE) % PT_ENTRIES;
    return pte_is_present(&pd->entries[idx]);
}

int handle_page_fault(struct page_directory *pd, uint32_t fault_addr) {
    uint32_t idx = (fault_addr / PAGE_SIZE) % PT_ENTRIES;
    if (pte_is_present(&pd->entries[idx])) {
        return -1;
    }
    uint32_t frame = alloc_frame();
    pte_set(&pd->entries[idx], frame, PAGE_WRITE | PAGE_USER);
    pd->mapped_count++;
    return 0;
}

int count_mapped_pages(const struct page_directory *pd) {
    int count = 0;
    for (int i = 0; i < PT_ENTRIES; i++) {
        if (pte_is_present(&pd->entries[i])) {
            count++;
        }
    }
    return count;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C477: Page fault handler - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C477: empty output");
    assert!(code.contains("fn handle_page_fault"), "C477: Should contain handle_page_fault");
    assert!(code.contains("fn alloc_frame"), "C477: Should contain alloc_frame");
}

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

#define MAX_FDS 64
#define FD_FREE   0
#define FD_FILE   1
#define FD_PIPE   2
#define FD_SOCKET 3

struct fd_entry {
    int type;
    int inode;
    int offset;
    int flags;
    int ref_count;
};

struct fd_table {
    struct fd_entry fds[64];
    int count;
};

void fd_table_init(struct fd_table *ft) {
    for (int i = 0; i < MAX_FDS; i++) {
        ft->fds[i].type = FD_FREE;
        ft->fds[i].ref_count = 0;
    }
    ft->count = 0;
}

int fd_alloc(struct fd_table *ft, int type, int inode) {
    for (int i = 0; i < MAX_FDS; i++) {
        if (ft->fds[i].type == FD_FREE) {
            ft->fds[i].type = type;
            ft->fds[i].inode = inode;
            ft->fds[i].offset = 0;
            ft->fds[i].flags = 0;
            ft->fds[i].ref_count = 1;
            ft->count++;
            return i;
        }
    }
    return -1;
}

int fd_close(struct fd_table *ft, int fd) {
    if (fd < 0 || fd >= MAX_FDS) return -1;
    if (ft->fds[fd].type == FD_FREE) return -1;
    ft->fds[fd].ref_count--;
    if (ft->fds[fd].ref_count <= 0) {
        ft->fds[fd].type = FD_FREE;
        ft->count--;
    }
    return 0;
}

int fd_dup(struct fd_table *ft, int oldfd) {
    if (oldfd < 0 || oldfd >= MAX_FDS) return -1;
    if (ft->fds[oldfd].type == FD_FREE) return -1;
    int newfd = fd_alloc(ft, ft->fds[oldfd].type, ft->fds[oldfd].inode);
    if (newfd >= 0) {
        ft->fds[newfd].offset = ft->fds[oldfd].offset;
        ft->fds[newfd].flags = ft->fds[oldfd].flags;
    }
    return newfd;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C478: File descriptor table - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C478: empty output");
    assert!(code.contains("fn fd_alloc"), "C478: Should contain fd_alloc");
    assert!(code.contains("fn fd_close"), "C478: Should contain fd_close");
    assert!(code.contains("fn fd_dup"), "C478: Should contain fd_dup");
}

#[test]
fn c479_pipe_buffer_reader_writer() {
    let c_code = r#"
typedef unsigned char uint8_t;

#define PIPE_BUF_SIZE 512

struct pipe_buf {
    uint8_t data[512];
    int read_pos;
    int write_pos;
    int count;
    int readers;
    int writers;
    int closed;
};

void pipe_init(struct pipe_buf *p) {
    p->read_pos = 0;
    p->write_pos = 0;
    p->count = 0;
    p->readers = 1;
    p->writers = 1;
    p->closed = 0;
}

int pipe_is_full(const struct pipe_buf *p) {
    return p->count >= PIPE_BUF_SIZE;
}

int pipe_is_empty(const struct pipe_buf *p) {
    return p->count == 0;
}

int pipe_write(struct pipe_buf *p, uint8_t byte) {
    if (p->closed || p->readers <= 0) return -1;
    if (pipe_is_full(p)) return 0;
    p->data[p->write_pos] = byte;
    p->write_pos = (p->write_pos + 1) % PIPE_BUF_SIZE;
    p->count++;
    return 1;
}

int pipe_read(struct pipe_buf *p, uint8_t *out) {
    if (pipe_is_empty(p)) {
        if (p->writers <= 0) return -1;
        return 0;
    }
    *out = p->data[p->read_pos];
    p->read_pos = (p->read_pos + 1) % PIPE_BUF_SIZE;
    p->count--;
    return 1;
}

void pipe_close_writer(struct pipe_buf *p) {
    p->writers--;
    if (p->writers <= 0 && p->readers <= 0) {
        p->closed = 1;
    }
}

void pipe_close_reader(struct pipe_buf *p) {
    p->readers--;
    if (p->writers <= 0 && p->readers <= 0) {
        p->closed = 1;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C479: Pipe buffer - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C479: empty output");
    assert!(code.contains("fn pipe_write"), "C479: Should contain pipe_write");
    assert!(code.contains("fn pipe_read"), "C479: Should contain pipe_read");
}

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

#define MAX_TASKS 32
#define QUANTUM 10
#define TASK_IDLE    0
#define TASK_READY   1
#define TASK_RUNNING 2
#define TASK_BLOCKED 3

struct task {
    int tid;
    int state;
    int remaining;
    int total_time;
    int priority;
};

struct scheduler {
    struct task tasks[32];
    int task_count;
    int current;
    int ticks;
};

void sched_init(struct scheduler *s) {
    s->task_count = 0;
    s->current = -1;
    s->ticks = 0;
}

int sched_add_task(struct scheduler *s, int priority) {
    if (s->task_count >= MAX_TASKS) return -1;
    int tid = s->task_count;
    s->tasks[tid].tid = tid;
    s->tasks[tid].state = TASK_READY;
    s->tasks[tid].remaining = QUANTUM;
    s->tasks[tid].total_time = 0;
    s->tasks[tid].priority = priority;
    s->task_count++;
    return tid;
}

int sched_pick_next(struct scheduler *s) {
    int start = (s->current + 1) % s->task_count;
    int i = start;
    do {
        if (s->tasks[i].state == TASK_READY) {
            return i;
        }
        i = (i + 1) % s->task_count;
    } while (i != start);
    return -1;
}

void sched_tick(struct scheduler *s) {
    s->ticks++;
    if (s->current < 0) return;
    s->tasks[s->current].remaining--;
    s->tasks[s->current].total_time++;
    if (s->tasks[s->current].remaining <= 0) {
        s->tasks[s->current].state = TASK_READY;
        s->tasks[s->current].remaining = QUANTUM;
        int next = sched_pick_next(s);
        if (next >= 0) {
            s->current = next;
            s->tasks[next].state = TASK_RUNNING;
        }
    }
}

void sched_block(struct scheduler *s, int tid) {
    s->tasks[tid].state = TASK_BLOCKED;
    if (tid == s->current) {
        int next = sched_pick_next(s);
        if (next >= 0) {
            s->current = next;
            s->tasks[next].state = TASK_RUNNING;
        }
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C480: Round-robin scheduler - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C480: empty output");
    assert!(code.contains("fn sched_tick"), "C480: Should contain sched_tick");
    assert!(code.contains("fn sched_pick_next"), "C480: Should contain sched_pick_next");
}

// ============================================================================
// C481-C485: Kernel Subsystems
// ============================================================================

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

#define SYS_READ   0
#define SYS_WRITE  1
#define SYS_OPEN   2
#define SYS_CLOSE  3
#define SYS_FORK   4
#define SYS_EXIT   5
#define SYS_WAIT   6
#define SYS_EXEC   7
#define NR_SYSCALLS 8

struct syscall_args {
    uint32_t arg0;
    uint32_t arg1;
    uint32_t arg2;
    uint32_t arg3;
};

static int syscall_counts[8];

int sys_read_handler(struct syscall_args *args) {
    syscall_counts[SYS_READ]++;
    return (int)args->arg2;
}

int sys_write_handler(struct syscall_args *args) {
    syscall_counts[SYS_WRITE]++;
    return (int)args->arg2;
}

int sys_open_handler(struct syscall_args *args) {
    syscall_counts[SYS_OPEN]++;
    return 3;
}

int sys_close_handler(struct syscall_args *args) {
    syscall_counts[SYS_CLOSE]++;
    return 0;
}

int syscall_dispatch(int nr, struct syscall_args *args) {
    if (nr < 0 || nr >= NR_SYSCALLS) return -1;
    switch (nr) {
        case SYS_READ:  return sys_read_handler(args);
        case SYS_WRITE: return sys_write_handler(args);
        case SYS_OPEN:  return sys_open_handler(args);
        case SYS_CLOSE: return sys_close_handler(args);
        default: return -1;
    }
}

int get_syscall_count(int nr) {
    if (nr < 0 || nr >= NR_SYSCALLS) return -1;
    return syscall_counts[nr];
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C481: Syscall dispatch - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C481: empty output");
    assert!(code.contains("fn syscall_dispatch"), "C481: Should contain syscall_dispatch");
}

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

#define MAX_REQUESTS 128
#define REQ_READ  1
#define REQ_WRITE 2
#define REQ_IOCTL 3
#define REQ_PENDING  0
#define REQ_ACTIVE   1
#define REQ_DONE     2
#define REQ_ERROR    3

struct dev_request {
    int type;
    int status;
    uint64_t sector;
    uint32_t count;
    int priority;
    int result;
};

struct request_queue {
    struct dev_request reqs[128];
    int head;
    int tail;
    int count;
    int active;
};

void rq_init(struct request_queue *rq) {
    rq->head = 0;
    rq->tail = 0;
    rq->count = 0;
    rq->active = -1;
}

int rq_submit(struct request_queue *rq, int type, uint64_t sector, uint32_t cnt) {
    if (rq->count >= MAX_REQUESTS) return -1;
    int idx = rq->tail;
    rq->reqs[idx].type = type;
    rq->reqs[idx].status = REQ_PENDING;
    rq->reqs[idx].sector = sector;
    rq->reqs[idx].count = cnt;
    rq->reqs[idx].priority = 0;
    rq->reqs[idx].result = 0;
    rq->tail = (rq->tail + 1) % MAX_REQUESTS;
    rq->count++;
    return idx;
}

int rq_dequeue(struct request_queue *rq) {
    if (rq->count == 0) return -1;
    int idx = rq->head;
    rq->reqs[idx].status = REQ_ACTIVE;
    rq->active = idx;
    rq->head = (rq->head + 1) % MAX_REQUESTS;
    rq->count--;
    return idx;
}

void rq_complete(struct request_queue *rq, int idx, int result) {
    rq->reqs[idx].status = REQ_DONE;
    rq->reqs[idx].result = result;
    if (rq->active == idx) {
        rq->active = -1;
    }
}

int rq_pending_count(const struct request_queue *rq) {
    return rq->count;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C482: Device driver request queue - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C482: empty output");
    assert!(code.contains("fn rq_submit"), "C482: Should contain rq_submit");
    assert!(code.contains("fn rq_dequeue"), "C482: Should contain rq_dequeue");
}

#[test]
fn c483_filesystem_superblock_and_inode() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;

#define MAX_INODES 1024
#define BLOCKS_PER_INODE 12
#define INODE_FREE  0
#define INODE_FILE  1
#define INODE_DIR   2

struct inode {
    uint32_t ino;
    uint16_t type;
    uint16_t permissions;
    uint32_t size;
    uint32_t blocks[12];
    uint32_t block_count;
    uint32_t link_count;
    uint32_t uid;
    uint32_t gid;
};

struct superblock {
    uint32_t magic;
    uint32_t total_blocks;
    uint32_t free_blocks;
    uint32_t total_inodes;
    uint32_t free_inodes;
    uint32_t block_size;
    struct inode inodes[1024];
};

void sb_init(struct superblock *sb, uint32_t total_blocks) {
    sb->magic = 0xDECF5000;
    sb->total_blocks = total_blocks;
    sb->free_blocks = total_blocks;
    sb->total_inodes = MAX_INODES;
    sb->free_inodes = MAX_INODES;
    sb->block_size = 4096;
    for (int i = 0; i < MAX_INODES; i++) {
        sb->inodes[i].ino = (uint32_t)i;
        sb->inodes[i].type = INODE_FREE;
        sb->inodes[i].size = 0;
        sb->inodes[i].block_count = 0;
        sb->inodes[i].link_count = 0;
    }
}

int sb_alloc_inode(struct superblock *sb, uint16_t type) {
    for (int i = 1; i < MAX_INODES; i++) {
        if (sb->inodes[i].type == INODE_FREE) {
            sb->inodes[i].type = type;
            sb->inodes[i].link_count = 1;
            sb->free_inodes--;
            return i;
        }
    }
    return -1;
}

int sb_free_inode(struct superblock *sb, uint32_t ino) {
    if (ino == 0 || ino >= MAX_INODES) return -1;
    if (sb->inodes[ino].type == INODE_FREE) return -1;
    sb->inodes[ino].type = INODE_FREE;
    sb->inodes[ino].size = 0;
    sb->inodes[ino].block_count = 0;
    sb->free_inodes++;
    return 0;
}

uint32_t sb_inode_size(const struct superblock *sb, uint32_t ino) {
    if (ino >= MAX_INODES) return 0;
    return sb->inodes[ino].size;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C483: Filesystem superblock - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C483: empty output");
    assert!(code.contains("fn sb_init"), "C483: Should contain sb_init");
    assert!(code.contains("fn sb_alloc_inode"), "C483: Should contain sb_alloc_inode");
}

#[test]
fn c484_block_device_buffer_cache() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;

#define CACHE_SIZE 64
#define BLOCK_SIZE 512
#define BUF_CLEAN 0
#define BUF_DIRTY 1
#define BUF_EMPTY 2

struct buf_entry {
    uint32_t block_no;
    int state;
    int ref_count;
    int lru_time;
    uint8_t data[512];
};

struct buffer_cache {
    struct buf_entry bufs[64];
    int clock;
};

void bcache_init(struct buffer_cache *bc) {
    bc->clock = 0;
    for (int i = 0; i < CACHE_SIZE; i++) {
        bc->bufs[i].block_no = 0;
        bc->bufs[i].state = BUF_EMPTY;
        bc->bufs[i].ref_count = 0;
        bc->bufs[i].lru_time = 0;
    }
}

int bcache_find(struct buffer_cache *bc, uint32_t block_no) {
    for (int i = 0; i < CACHE_SIZE; i++) {
        if (bc->bufs[i].state != BUF_EMPTY && bc->bufs[i].block_no == block_no) {
            bc->bufs[i].ref_count++;
            bc->bufs[i].lru_time = bc->clock++;
            return i;
        }
    }
    return -1;
}

int bcache_evict(struct buffer_cache *bc) {
    int oldest = -1;
    int oldest_time = 0x7FFFFFFF;
    for (int i = 0; i < CACHE_SIZE; i++) {
        if (bc->bufs[i].ref_count == 0 && bc->bufs[i].lru_time < oldest_time) {
            oldest = i;
            oldest_time = bc->bufs[i].lru_time;
        }
    }
    if (oldest >= 0 && bc->bufs[oldest].state == BUF_DIRTY) {
        bc->bufs[oldest].state = BUF_CLEAN;
    }
    return oldest;
}

int bcache_get(struct buffer_cache *bc, uint32_t block_no) {
    int idx = bcache_find(bc, block_no);
    if (idx >= 0) return idx;
    idx = bcache_evict(bc);
    if (idx < 0) return -1;
    bc->bufs[idx].block_no = block_no;
    bc->bufs[idx].state = BUF_CLEAN;
    bc->bufs[idx].ref_count = 1;
    bc->bufs[idx].lru_time = bc->clock++;
    return idx;
}

void bcache_mark_dirty(struct buffer_cache *bc, int idx) {
    if (idx >= 0 && idx < CACHE_SIZE) {
        bc->bufs[idx].state = BUF_DIRTY;
    }
}

void bcache_release(struct buffer_cache *bc, int idx) {
    if (idx >= 0 && idx < CACHE_SIZE && bc->bufs[idx].ref_count > 0) {
        bc->bufs[idx].ref_count--;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C484: Buffer cache - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C484: empty output");
    assert!(code.contains("fn bcache_get"), "C484: Should contain bcache_get");
    assert!(code.contains("fn bcache_evict"), "C484: Should contain bcache_evict");
}

#[test]
fn c485_terminal_line_discipline() {
    let c_code = r#"
typedef unsigned char uint8_t;

#define LINE_BUF_SIZE 256
#define CHAR_BS    0x08
#define CHAR_DEL   0x7F
#define CHAR_CR    0x0D
#define CHAR_LF    0x0A
#define CHAR_EOT   0x04
#define CHAR_KILL  0x15

struct line_disc {
    uint8_t buf[256];
    int pos;
    int len;
    int canonical;
    int echo;
    int complete;
};

void ld_init(struct line_disc *ld, int canonical, int echo) {
    ld->pos = 0;
    ld->len = 0;
    ld->canonical = canonical;
    ld->echo = echo;
    ld->complete = 0;
}

void ld_reset(struct line_disc *ld) {
    ld->pos = 0;
    ld->len = 0;
    ld->complete = 0;
}

int ld_input_char(struct line_disc *ld, uint8_t ch) {
    if (!ld->canonical) {
        if (ld->len < LINE_BUF_SIZE) {
            ld->buf[ld->len++] = ch;
            return 1;
        }
        return 0;
    }
    if (ch == CHAR_BS || ch == CHAR_DEL) {
        if (ld->len > 0) {
            ld->len--;
            return 1;
        }
        return 0;
    }
    if (ch == CHAR_KILL) {
        ld->len = 0;
        return 1;
    }
    if (ch == CHAR_CR || ch == CHAR_LF) {
        ld->complete = 1;
        return 1;
    }
    if (ch == CHAR_EOT) {
        ld->complete = 1;
        return -1;
    }
    if (ld->len < LINE_BUF_SIZE - 1) {
        ld->buf[ld->len++] = ch;
        return 1;
    }
    return 0;
}

int ld_is_complete(const struct line_disc *ld) {
    return ld->complete;
}

int ld_get_line_length(const struct line_disc *ld) {
    return ld->len;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C485: Terminal line discipline - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C485: empty output");
    assert!(code.contains("fn ld_input_char"), "C485: Should contain ld_input_char");
    assert!(code.contains("fn ld_is_complete"), "C485: Should contain ld_is_complete");
}

// ============================================================================
// C486-C490: Kernel Infrastructure
// ============================================================================

#[test]
fn c486_network_socket_buffer_chain() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

#define SKB_POOL_SIZE 64
#define SKB_DATA_SIZE 1500
#define SKB_FREE  0
#define SKB_ALLOC 1

struct sk_buff {
    uint8_t data[1500];
    uint16_t len;
    uint16_t protocol;
    int next;
    int state;
};

static struct sk_buff skb_pool[64];
static int skb_free_head = 0;

void skb_pool_init(void) {
    for (int i = 0; i < SKB_POOL_SIZE - 1; i++) {
        skb_pool[i].state = SKB_FREE;
        skb_pool[i].next = i + 1;
        skb_pool[i].len = 0;
    }
    skb_pool[SKB_POOL_SIZE - 1].state = SKB_FREE;
    skb_pool[SKB_POOL_SIZE - 1].next = -1;
    skb_free_head = 0;
}

int skb_alloc(void) {
    if (skb_free_head < 0) return -1;
    int idx = skb_free_head;
    skb_free_head = skb_pool[idx].next;
    skb_pool[idx].state = SKB_ALLOC;
    skb_pool[idx].next = -1;
    skb_pool[idx].len = 0;
    return idx;
}

void skb_free(int idx) {
    if (idx < 0 || idx >= SKB_POOL_SIZE) return;
    skb_pool[idx].state = SKB_FREE;
    skb_pool[idx].next = skb_free_head;
    skb_free_head = idx;
}

int skb_set_data(int idx, const uint8_t *src, uint16_t len) {
    if (idx < 0 || idx >= SKB_POOL_SIZE) return -1;
    if (len > SKB_DATA_SIZE) return -1;
    for (int i = 0; i < len; i++) {
        skb_pool[idx].data[i] = src[i];
    }
    skb_pool[idx].len = len;
    return 0;
}

uint16_t skb_get_len(int idx) {
    if (idx < 0 || idx >= SKB_POOL_SIZE) return 0;
    return skb_pool[idx].len;
}

int skb_chain(int head, int tail) {
    if (head < 0 || tail < 0) return -1;
    skb_pool[head].next = tail;
    return 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C486: Socket buffer chain - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C486: empty output");
    assert!(code.contains("fn skb_alloc"), "C486: Should contain skb_alloc");
    assert!(code.contains("fn skb_free"), "C486: Should contain skb_free");
}

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

#define MAX_ORDER 10
#define BUDDY_FREE 0
#define BUDDY_USED 1
#define POOL_SIZE 1024

struct buddy_block {
    int order;
    int state;
    int next_free;
};

struct buddy_alloc {
    struct buddy_block blocks[1024];
    int free_lists[11];
    int total_free;
};

void buddy_init(struct buddy_alloc *ba) {
    ba->total_free = POOL_SIZE;
    for (int i = 0; i <= MAX_ORDER; i++) {
        ba->free_lists[i] = -1;
    }
    for (int i = 0; i < POOL_SIZE; i++) {
        ba->blocks[i].order = 0;
        ba->blocks[i].state = BUDDY_FREE;
        ba->blocks[i].next_free = -1;
    }
    ba->blocks[0].order = MAX_ORDER;
    ba->blocks[0].next_free = -1;
    ba->free_lists[MAX_ORDER] = 0;
}

int buddy_find_block(struct buddy_alloc *ba, int order) {
    int o = order;
    while (o <= MAX_ORDER) {
        if (ba->free_lists[o] >= 0) {
            return o;
        }
        o++;
    }
    return -1;
}

int buddy_split(struct buddy_alloc *ba, int idx, int from_order, int to_order) {
    int current_order = from_order;
    while (current_order > to_order) {
        current_order--;
        int buddy_size = 1 << current_order;
        int buddy_idx = idx + buddy_size;
        if (buddy_idx < POOL_SIZE) {
            ba->blocks[buddy_idx].order = current_order;
            ba->blocks[buddy_idx].state = BUDDY_FREE;
            ba->blocks[buddy_idx].next_free = ba->free_lists[current_order];
            ba->free_lists[current_order] = buddy_idx;
        }
    }
    ba->blocks[idx].order = to_order;
    ba->blocks[idx].state = BUDDY_USED;
    return idx;
}

int buddy_alloc_order(struct buddy_alloc *ba, int order) {
    int found = buddy_find_block(ba, order);
    if (found < 0) return -1;
    int idx = ba->free_lists[found];
    ba->free_lists[found] = ba->blocks[idx].next_free;
    int result = buddy_split(ba, idx, found, order);
    ba->total_free -= (1 << order);
    return result;
}

void buddy_free_block(struct buddy_alloc *ba, int idx) {
    if (idx < 0 || idx >= POOL_SIZE) return;
    ba->blocks[idx].state = BUDDY_FREE;
    int order = ba->blocks[idx].order;
    ba->blocks[idx].next_free = ba->free_lists[order];
    ba->free_lists[order] = idx;
    ba->total_free += (1 << order);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C487: Buddy allocator - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C487: empty output");
    assert!(code.contains("fn buddy_alloc_order"), "C487: Should contain buddy_alloc_order");
    assert!(code.contains("fn buddy_free_block"), "C487: Should contain buddy_free_block");
}

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

#define MAX_SIGNALS 32
#define SIG_PENDING 0
#define SIG_HANDLED 1
#define SIG_IGNORED 2

#define SIGTERM  15
#define SIGKILL  9
#define SIGUSR1  10
#define SIGUSR2  12
#define SIGCHLD  17

struct signal_entry {
    int signo;
    int state;
    int sender_pid;
    uint32_t timestamp;
};

struct signal_queue {
    struct signal_entry queue[32];
    int count;
    uint32_t mask;
    uint32_t pending_set;
};

void sigq_init(struct signal_queue *sq) {
    sq->count = 0;
    sq->mask = 0;
    sq->pending_set = 0;
}

int sigq_send(struct signal_queue *sq, int signo, int sender) {
    if (sq->count >= MAX_SIGNALS) return -1;
    if (signo < 1 || signo > 31) return -1;
    if ((sq->mask & (1U << signo)) && signo != SIGKILL) {
        return 0;
    }
    sq->queue[sq->count].signo = signo;
    sq->queue[sq->count].state = SIG_PENDING;
    sq->queue[sq->count].sender_pid = sender;
    sq->queue[sq->count].timestamp = 0;
    sq->count++;
    sq->pending_set |= (1U << signo);
    return 1;
}

int sigq_dequeue(struct signal_queue *sq) {
    for (int i = 0; i < sq->count; i++) {
        if (sq->queue[i].state == SIG_PENDING) {
            sq->queue[i].state = SIG_HANDLED;
            return sq->queue[i].signo;
        }
    }
    return 0;
}

void sigq_mask(struct signal_queue *sq, int signo) {
    if (signo != SIGKILL) {
        sq->mask |= (1U << signo);
    }
}

void sigq_unmask(struct signal_queue *sq, int signo) {
    sq->mask &= ~(1U << signo);
}

int sigq_is_pending(const struct signal_queue *sq, int signo) {
    return (sq->pending_set & (1U << signo)) != 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C488: Signal delivery queue - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C488: empty output");
    assert!(code.contains("fn sigq_send"), "C488: Should contain sigq_send");
    assert!(code.contains("fn sigq_dequeue"), "C488: Should contain sigq_dequeue");
}

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

#define MAX_MOUNTS 16
#define FS_EXT4  1
#define FS_TMPFS 2
#define FS_PROC  3
#define FS_DEVFS 4
#define MNT_RDONLY   0x01
#define MNT_NOSUID   0x02
#define MNT_NOEXEC   0x04

struct mount_entry {
    int fs_type;
    uint32_t flags;
    int device_id;
    int root_inode;
    int active;
    int ref_count;
};

struct mount_table {
    struct mount_entry mounts[16];
    int count;
};

void mt_init(struct mount_table *mt) {
    mt->count = 0;
    for (int i = 0; i < MAX_MOUNTS; i++) {
        mt->mounts[i].active = 0;
        mt->mounts[i].ref_count = 0;
    }
}

int mt_mount(struct mount_table *mt, int fs_type, int dev_id, uint32_t flags) {
    if (mt->count >= MAX_MOUNTS) return -1;
    for (int i = 0; i < MAX_MOUNTS; i++) {
        if (!mt->mounts[i].active) {
            mt->mounts[i].fs_type = fs_type;
            mt->mounts[i].device_id = dev_id;
            mt->mounts[i].flags = flags;
            mt->mounts[i].root_inode = 2;
            mt->mounts[i].active = 1;
            mt->mounts[i].ref_count = 1;
            mt->count++;
            return i;
        }
    }
    return -1;
}

int mt_unmount(struct mount_table *mt, int idx) {
    if (idx < 0 || idx >= MAX_MOUNTS) return -1;
    if (!mt->mounts[idx].active) return -1;
    if (mt->mounts[idx].ref_count > 1) return -1;
    mt->mounts[idx].active = 0;
    mt->count--;
    return 0;
}

int mt_is_readonly(const struct mount_table *mt, int idx) {
    if (idx < 0 || idx >= MAX_MOUNTS) return 1;
    return (mt->mounts[idx].flags & MNT_RDONLY) != 0;
}

int mt_find_by_device(const struct mount_table *mt, int dev_id) {
    for (int i = 0; i < MAX_MOUNTS; i++) {
        if (mt->mounts[i].active && mt->mounts[i].device_id == dev_id) {
            return i;
        }
    }
    return -1;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C489: Mount table - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C489: empty output");
    assert!(code.contains("fn mt_mount"), "C489: Should contain mt_mount");
    assert!(code.contains("fn mt_unmount"), "C489: Should contain mt_unmount");
}

#[test]
fn c490_kernel_log_ring_buffer() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;

#define LOG_BUF_SIZE 4096
#define LOG_EMERG  0
#define LOG_ERR    3
#define LOG_WARN   4
#define LOG_INFO   6
#define LOG_DEBUG  7
#define MSG_MAX_LEN 128

struct log_entry {
    uint32_t seq;
    uint8_t level;
    uint8_t len;
    char msg[128];
};

struct log_ring {
    struct log_entry entries[32];
    int head;
    int tail;
    int count;
    uint32_t next_seq;
    int max_entries;
};

void logr_init(struct log_ring *lr) {
    lr->head = 0;
    lr->tail = 0;
    lr->count = 0;
    lr->next_seq = 1;
    lr->max_entries = 32;
}

int logr_write(struct log_ring *lr, uint8_t level, const char *msg, int len) {
    if (len > MSG_MAX_LEN) len = MSG_MAX_LEN;
    int idx = lr->tail;
    lr->entries[idx].seq = lr->next_seq++;
    lr->entries[idx].level = level;
    lr->entries[idx].len = (uint8_t)len;
    for (int i = 0; i < len; i++) {
        lr->entries[idx].msg[i] = msg[i];
    }
    lr->tail = (lr->tail + 1) % lr->max_entries;
    if (lr->count < lr->max_entries) {
        lr->count++;
    } else {
        lr->head = (lr->head + 1) % lr->max_entries;
    }
    return idx;
}

int logr_read(struct log_ring *lr, uint32_t from_seq, struct log_entry *out) {
    for (int i = 0; i < lr->count; i++) {
        int idx = (lr->head + i) % lr->max_entries;
        if (lr->entries[idx].seq >= from_seq) {
            *out = lr->entries[idx];
            return 1;
        }
    }
    return 0;
}

int logr_count_by_level(const struct log_ring *lr, uint8_t level) {
    int c = 0;
    for (int i = 0; i < lr->count; i++) {
        int idx = (lr->head + i) % lr->max_entries;
        if (lr->entries[idx].level == level) {
            c++;
        }
    }
    return c;
}

void logr_clear(struct log_ring *lr) {
    lr->head = 0;
    lr->tail = 0;
    lr->count = 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C490: Kernel log ring buffer - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C490: empty output");
    assert!(code.contains("fn logr_write"), "C490: Should contain logr_write");
    assert!(code.contains("fn logr_read"), "C490: Should contain logr_read");
}

// ============================================================================
// C491-C495: Hardware and Memory Management
// ============================================================================

#[test]
fn c491_interrupt_controller_simulation() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

#define MAX_IRQS 16
#define IRQ_DISABLED 0
#define IRQ_ENABLED  1
#define IRQ_PENDING  2

struct irq_desc {
    int state;
    int priority;
    uint32_t count;
    int masked;
};

struct irq_controller {
    struct irq_desc irqs[16];
    uint16_t mask_reg;
    uint16_t pending_reg;
    uint16_t in_service;
    int nested_depth;
};

void irqc_init(struct irq_controller *ic) {
    ic->mask_reg = 0xFFFF;
    ic->pending_reg = 0;
    ic->in_service = 0;
    ic->nested_depth = 0;
    for (int i = 0; i < MAX_IRQS; i++) {
        ic->irqs[i].state = IRQ_DISABLED;
        ic->irqs[i].priority = i;
        ic->irqs[i].count = 0;
        ic->irqs[i].masked = 1;
    }
}

void irqc_enable(struct irq_controller *ic, int irq) {
    if (irq < 0 || irq >= MAX_IRQS) return;
    ic->irqs[irq].state = IRQ_ENABLED;
    ic->irqs[irq].masked = 0;
    ic->mask_reg &= ~(1U << irq);
}

void irqc_disable(struct irq_controller *ic, int irq) {
    if (irq < 0 || irq >= MAX_IRQS) return;
    ic->irqs[irq].state = IRQ_DISABLED;
    ic->irqs[irq].masked = 1;
    ic->mask_reg |= (1U << irq);
}

void irqc_raise(struct irq_controller *ic, int irq) {
    if (irq < 0 || irq >= MAX_IRQS) return;
    if (!ic->irqs[irq].masked) {
        ic->pending_reg |= (1U << irq);
        ic->irqs[irq].state = IRQ_PENDING;
    }
}

int irqc_get_highest(struct irq_controller *ic) {
    int best = -1;
    int best_prio = 0x7FFFFFFF;
    for (int i = 0; i < MAX_IRQS; i++) {
        if ((ic->pending_reg & (1U << i)) && ic->irqs[i].priority < best_prio) {
            best = i;
            best_prio = ic->irqs[i].priority;
        }
    }
    return best;
}

void irqc_ack(struct irq_controller *ic, int irq) {
    if (irq < 0 || irq >= MAX_IRQS) return;
    ic->pending_reg &= ~(1U << irq);
    ic->in_service |= (1U << irq);
    ic->irqs[irq].count++;
    ic->irqs[irq].state = IRQ_ENABLED;
    ic->nested_depth++;
}

void irqc_eoi(struct irq_controller *ic, int irq) {
    if (irq < 0 || irq >= MAX_IRQS) return;
    ic->in_service &= ~(1U << irq);
    if (ic->nested_depth > 0) ic->nested_depth--;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C491: Interrupt controller - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C491: empty output");
    assert!(code.contains("fn irqc_raise"), "C491: Should contain irqc_raise");
    assert!(code.contains("fn irqc_get_highest"), "C491: Should contain irqc_get_highest");
}

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

#define MAX_TLS_KEYS 64
#define MAX_THREADS 32
#define TLS_FREE  0
#define TLS_USED  1

struct tls_key {
    int state;
    uint32_t default_val;
};

struct tls_store {
    struct tls_key keys[64];
    uint32_t values[32][64];
    int key_count;
};

void tls_init(struct tls_store *ts) {
    ts->key_count = 0;
    for (int i = 0; i < MAX_TLS_KEYS; i++) {
        ts->keys[i].state = TLS_FREE;
        ts->keys[i].default_val = 0;
    }
    for (int t = 0; t < MAX_THREADS; t++) {
        for (int k = 0; k < MAX_TLS_KEYS; k++) {
            ts->values[t][k] = 0;
        }
    }
}

int tls_create_key(struct tls_store *ts, uint32_t default_val) {
    for (int i = 0; i < MAX_TLS_KEYS; i++) {
        if (ts->keys[i].state == TLS_FREE) {
            ts->keys[i].state = TLS_USED;
            ts->keys[i].default_val = default_val;
            ts->key_count++;
            for (int t = 0; t < MAX_THREADS; t++) {
                ts->values[t][i] = default_val;
            }
            return i;
        }
    }
    return -1;
}

int tls_delete_key(struct tls_store *ts, int key) {
    if (key < 0 || key >= MAX_TLS_KEYS) return -1;
    if (ts->keys[key].state == TLS_FREE) return -1;
    ts->keys[key].state = TLS_FREE;
    ts->key_count--;
    return 0;
}

uint32_t tls_get(const struct tls_store *ts, int thread, int key) {
    if (thread < 0 || thread >= MAX_THREADS) return 0;
    if (key < 0 || key >= MAX_TLS_KEYS) return 0;
    return ts->values[thread][key];
}

int tls_set(struct tls_store *ts, int thread, int key, uint32_t val) {
    if (thread < 0 || thread >= MAX_THREADS) return -1;
    if (key < 0 || key >= MAX_TLS_KEYS) return -1;
    if (ts->keys[key].state == TLS_FREE) return -1;
    ts->values[thread][key] = val;
    return 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C492: Thread-local storage - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C492: empty output");
    assert!(code.contains("fn tls_create_key"), "C492: Should contain tls_create_key");
    assert!(code.contains("fn tls_get"), "C492: Should contain tls_get");
    assert!(code.contains("fn tls_set"), "C492: Should contain tls_set");
}

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

#define MAX_PAGES 256
#define PAGE_COW     0x01
#define PAGE_SHARED  0x02
#define PAGE_DIRTY   0x04
#define PAGE_FREE    0

struct page_info {
    uint32_t flags;
    int ref_count;
    int owner;
    uint32_t phys_addr;
};

struct cow_manager {
    struct page_info pages[256];
    int total_pages;
    int shared_count;
    int cow_faults;
};

void cow_init(struct cow_manager *cm) {
    cm->total_pages = MAX_PAGES;
    cm->shared_count = 0;
    cm->cow_faults = 0;
    for (int i = 0; i < MAX_PAGES; i++) {
        cm->pages[i].flags = PAGE_FREE;
        cm->pages[i].ref_count = 0;
        cm->pages[i].owner = -1;
        cm->pages[i].phys_addr = (uint32_t)(i * 4096);
    }
}

int cow_alloc_page(struct cow_manager *cm, int owner) {
    for (int i = 0; i < MAX_PAGES; i++) {
        if (cm->pages[i].flags == PAGE_FREE) {
            cm->pages[i].flags = 0;
            cm->pages[i].ref_count = 1;
            cm->pages[i].owner = owner;
            return i;
        }
    }
    return -1;
}

int cow_share_page(struct cow_manager *cm, int page_idx, int new_owner) {
    if (page_idx < 0 || page_idx >= MAX_PAGES) return -1;
    if (cm->pages[page_idx].flags == PAGE_FREE) return -1;
    cm->pages[page_idx].ref_count++;
    cm->pages[page_idx].flags |= PAGE_COW | PAGE_SHARED;
    cm->shared_count++;
    return 0;
}

int cow_handle_fault(struct cow_manager *cm, int page_idx, int pid) {
    if (page_idx < 0 || page_idx >= MAX_PAGES) return -1;
    if (!(cm->pages[page_idx].flags & PAGE_COW)) return -1;
    cm->cow_faults++;
    if (cm->pages[page_idx].ref_count == 1) {
        cm->pages[page_idx].flags &= ~PAGE_COW;
        cm->pages[page_idx].flags &= ~PAGE_SHARED;
        return page_idx;
    }
    int new_page = cow_alloc_page(cm, pid);
    if (new_page < 0) return -1;
    cm->pages[page_idx].ref_count--;
    if (cm->pages[page_idx].ref_count == 1) {
        cm->pages[page_idx].flags &= ~(PAGE_COW | PAGE_SHARED);
    }
    return new_page;
}

void cow_free_page(struct cow_manager *cm, int page_idx) {
    if (page_idx < 0 || page_idx >= MAX_PAGES) return;
    cm->pages[page_idx].ref_count--;
    if (cm->pages[page_idx].ref_count <= 0) {
        cm->pages[page_idx].flags = PAGE_FREE;
        cm->pages[page_idx].owner = -1;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C493: Copy-on-write pages - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C493: empty output");
    assert!(code.contains("fn cow_handle_fault"), "C493: Should contain cow_handle_fault");
    assert!(code.contains("fn cow_share_page"), "C493: Should contain cow_share_page");
}

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

#define MAX_MODULES 32
#define MAX_DEPS 8
#define MOD_UNLOADED 0
#define MOD_LOADED   1
#define MOD_LOADING  2
#define MOD_ERROR    3

struct kmodule {
    int id;
    int state;
    int deps[8];
    int dep_count;
    int ref_count;
    uint32_t size;
};

struct mod_registry {
    struct kmodule modules[32];
    int count;
};

void modr_init(struct mod_registry *mr) {
    mr->count = 0;
    for (int i = 0; i < MAX_MODULES; i++) {
        mr->modules[i].id = i;
        mr->modules[i].state = MOD_UNLOADED;
        mr->modules[i].dep_count = 0;
        mr->modules[i].ref_count = 0;
        mr->modules[i].size = 0;
    }
}

int modr_register(struct mod_registry *mr, uint32_t size) {
    if (mr->count >= MAX_MODULES) return -1;
    int id = mr->count;
    mr->modules[id].state = MOD_UNLOADED;
    mr->modules[id].size = size;
    mr->modules[id].dep_count = 0;
    mr->modules[id].ref_count = 0;
    mr->count++;
    return id;
}

int modr_add_dep(struct mod_registry *mr, int mod_id, int dep_id) {
    if (mod_id < 0 || mod_id >= mr->count) return -1;
    if (dep_id < 0 || dep_id >= mr->count) return -1;
    if (mr->modules[mod_id].dep_count >= MAX_DEPS) return -1;
    mr->modules[mod_id].deps[mr->modules[mod_id].dep_count] = dep_id;
    mr->modules[mod_id].dep_count++;
    return 0;
}

int modr_can_load(const struct mod_registry *mr, int mod_id) {
    if (mod_id < 0 || mod_id >= mr->count) return 0;
    const struct kmodule *m = &mr->modules[mod_id];
    for (int i = 0; i < m->dep_count; i++) {
        if (mr->modules[m->deps[i]].state != MOD_LOADED) {
            return 0;
        }
    }
    return 1;
}

int modr_load(struct mod_registry *mr, int mod_id) {
    if (!modr_can_load(mr, mod_id)) return -1;
    mr->modules[mod_id].state = MOD_LOADED;
    for (int i = 0; i < mr->modules[mod_id].dep_count; i++) {
        mr->modules[mr->modules[mod_id].deps[i]].ref_count++;
    }
    return 0;
}

int modr_can_unload(const struct mod_registry *mr, int mod_id) {
    if (mod_id < 0 || mod_id >= mr->count) return 0;
    return mr->modules[mod_id].ref_count == 0 &&
           mr->modules[mod_id].state == MOD_LOADED;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C494: Kernel module deps - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C494: empty output");
    assert!(code.contains("fn modr_load"), "C494: Should contain modr_load");
    assert!(code.contains("fn modr_can_load"), "C494: Should contain modr_can_load");
}

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

#define ELEV_QUEUE_SIZE 128
#define DIR_UP   1
#define DIR_DOWN 0
#define IO_READ  0
#define IO_WRITE 1

struct io_request {
    uint64_t sector;
    uint32_t count;
    int type;
    int served;
};

struct elevator {
    struct io_request queue[128];
    int req_count;
    uint64_t head_pos;
    int direction;
    int served_count;
};

void elev_init(struct elevator *e) {
    e->req_count = 0;
    e->head_pos = 0;
    e->direction = DIR_UP;
    e->served_count = 0;
}

int elev_add_request(struct elevator *e, uint64_t sector, uint32_t count, int type) {
    if (e->req_count >= ELEV_QUEUE_SIZE) return -1;
    int idx = e->req_count;
    e->queue[idx].sector = sector;
    e->queue[idx].count = count;
    e->queue[idx].type = type;
    e->queue[idx].served = 0;
    e->req_count++;
    return idx;
}

int elev_scan_next(struct elevator *e) {
    int best = -1;
    uint64_t best_dist = 0xFFFFFFFFFFFFFFFFULL;
    if (e->direction == DIR_UP) {
        for (int i = 0; i < e->req_count; i++) {
            if (!e->queue[i].served && e->queue[i].sector >= e->head_pos) {
                uint64_t dist = e->queue[i].sector - e->head_pos;
                if (dist < best_dist) {
                    best_dist = dist;
                    best = i;
                }
            }
        }
        if (best < 0) {
            e->direction = DIR_DOWN;
            return elev_scan_next(e);
        }
    } else {
        for (int i = 0; i < e->req_count; i++) {
            if (!e->queue[i].served && e->queue[i].sector <= e->head_pos) {
                uint64_t dist = e->head_pos - e->queue[i].sector;
                if (dist < best_dist) {
                    best_dist = dist;
                    best = i;
                }
            }
        }
        if (best < 0) {
            e->direction = DIR_UP;
            return -1;
        }
    }
    return best;
}

void elev_serve(struct elevator *e, int idx) {
    if (idx < 0 || idx >= e->req_count) return;
    e->queue[idx].served = 1;
    e->head_pos = e->queue[idx].sector;
    e->served_count++;
}

int elev_pending(const struct elevator *e) {
    int count = 0;
    for (int i = 0; i < e->req_count; i++) {
        if (!e->queue[i].served) count++;
    }
    return count;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C495: Elevator scheduler - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C495: empty output");
    assert!(code.contains("fn elev_scan_next"), "C495: Should contain elev_scan_next");
    assert!(code.contains("fn elev_serve"), "C495: Should contain elev_serve");
}

// ============================================================================
// C496-C500: Networking, Timekeeping, and Boot
// ============================================================================

#[test]
fn c496_network_packet_filter_rules() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

#define MAX_RULES 64
#define ACTION_ACCEPT 1
#define ACTION_DROP   2
#define ACTION_LOG    3
#define PROTO_TCP 6
#define PROTO_UDP 17
#define PROTO_ICMP 1

struct filter_rule {
    uint32_t src_ip;
    uint32_t src_mask;
    uint32_t dst_ip;
    uint32_t dst_mask;
    uint16_t src_port;
    uint16_t dst_port;
    uint8_t protocol;
    uint8_t action;
    int enabled;
    uint32_t match_count;
};

struct packet_filter {
    struct filter_rule rules[64];
    int rule_count;
    uint32_t default_action;
    uint32_t total_packets;
    uint32_t dropped;
    uint32_t accepted;
};

void pf_init(struct packet_filter *pf) {
    pf->rule_count = 0;
    pf->default_action = ACTION_ACCEPT;
    pf->total_packets = 0;
    pf->dropped = 0;
    pf->accepted = 0;
}

int pf_add_rule(struct packet_filter *pf, uint32_t src, uint32_t smask,
                uint32_t dst, uint32_t dmask, uint8_t proto, uint8_t action) {
    if (pf->rule_count >= MAX_RULES) return -1;
    int idx = pf->rule_count;
    pf->rules[idx].src_ip = src;
    pf->rules[idx].src_mask = smask;
    pf->rules[idx].dst_ip = dst;
    pf->rules[idx].dst_mask = dmask;
    pf->rules[idx].protocol = proto;
    pf->rules[idx].action = action;
    pf->rules[idx].src_port = 0;
    pf->rules[idx].dst_port = 0;
    pf->rules[idx].enabled = 1;
    pf->rules[idx].match_count = 0;
    pf->rule_count++;
    return idx;
}

int pf_match_packet(struct packet_filter *pf, uint32_t src, uint32_t dst,
                    uint8_t proto) {
    pf->total_packets++;
    for (int i = 0; i < pf->rule_count; i++) {
        if (!pf->rules[i].enabled) continue;
        if ((src & pf->rules[i].src_mask) != (pf->rules[i].src_ip & pf->rules[i].src_mask))
            continue;
        if ((dst & pf->rules[i].dst_mask) != (pf->rules[i].dst_ip & pf->rules[i].dst_mask))
            continue;
        if (pf->rules[i].protocol != 0 && pf->rules[i].protocol != proto)
            continue;
        pf->rules[i].match_count++;
        if (pf->rules[i].action == ACTION_DROP) {
            pf->dropped++;
        } else {
            pf->accepted++;
        }
        return pf->rules[i].action;
    }
    if (pf->default_action == ACTION_DROP) {
        pf->dropped++;
    } else {
        pf->accepted++;
    }
    return pf->default_action;
}

void pf_disable_rule(struct packet_filter *pf, int idx) {
    if (idx >= 0 && idx < pf->rule_count) {
        pf->rules[idx].enabled = 0;
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C496: Packet filter - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C496: empty output");
    assert!(code.contains("fn pf_match_packet"), "C496: Should contain pf_match_packet");
    assert!(code.contains("fn pf_add_rule"), "C496: Should contain pf_add_rule");
}

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

#define HZ 1000
#define NSEC_PER_SEC  1000000000UL
#define NSEC_PER_MSEC 1000000UL
#define NSEC_PER_USEC 1000UL

struct timespec {
    uint64_t tv_sec;
    uint32_t tv_nsec;
};

struct kernel_clock {
    uint64_t jiffies;
    struct timespec wall_time;
    struct timespec boot_time;
    uint64_t uptime_ms;
    uint32_t tick_nsec;
};

void kclock_init(struct kernel_clock *kc) {
    kc->jiffies = 0;
    kc->wall_time.tv_sec = 0;
    kc->wall_time.tv_nsec = 0;
    kc->boot_time.tv_sec = 0;
    kc->boot_time.tv_nsec = 0;
    kc->uptime_ms = 0;
    kc->tick_nsec = NSEC_PER_SEC / HZ;
}

void kclock_tick(struct kernel_clock *kc) {
    kc->jiffies++;
    kc->wall_time.tv_nsec += kc->tick_nsec;
    if (kc->wall_time.tv_nsec >= NSEC_PER_SEC) {
        kc->wall_time.tv_sec++;
        kc->wall_time.tv_nsec -= (uint32_t)NSEC_PER_SEC;
    }
    kc->uptime_ms = kc->jiffies * 1000 / HZ;
}

uint64_t kclock_get_jiffies(const struct kernel_clock *kc) {
    return kc->jiffies;
}

uint64_t kclock_uptime_seconds(const struct kernel_clock *kc) {
    return kc->uptime_ms / 1000;
}

uint64_t jiffies_to_msec(uint64_t j) {
    return j * 1000 / HZ;
}

uint64_t msec_to_jiffies(uint64_t ms) {
    return ms * HZ / 1000;
}

int timespec_compare(const struct timespec *a, const struct timespec *b) {
    if (a->tv_sec < b->tv_sec) return -1;
    if (a->tv_sec > b->tv_sec) return 1;
    if (a->tv_nsec < b->tv_nsec) return -1;
    if (a->tv_nsec > b->tv_nsec) return 1;
    return 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C497: Kernel timekeeping - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C497: empty output");
    assert!(code.contains("fn kclock_tick"), "C497: Should contain kclock_tick");
    assert!(code.contains("fn jiffies_to_msec"), "C497: Should contain jiffies_to_msec");
}

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

struct kernel_rng {
    uint64_t state[4];
    uint32_t entropy_count;
    int initialized;
};

static uint64_t rotl(uint64_t x, int k) {
    return (x << k) | (x >> (64 - k));
}

void krng_init(struct kernel_rng *rng, uint64_t seed) {
    rng->state[0] = seed;
    rng->state[1] = seed ^ 0x6A09E667F3BCC908ULL;
    rng->state[2] = seed ^ 0xBB67AE8584CAA73BULL;
    rng->state[3] = seed ^ 0x3C6EF372FE94F82BULL;
    rng->entropy_count = 64;
    rng->initialized = 1;
}

uint64_t krng_next(struct kernel_rng *rng) {
    uint64_t result = rotl(rng->state[0] + rng->state[3], 23) + rng->state[0];
    uint64_t t = rng->state[1] << 17;
    rng->state[2] ^= rng->state[0];
    rng->state[3] ^= rng->state[1];
    rng->state[1] ^= rng->state[2];
    rng->state[0] ^= rng->state[3];
    rng->state[2] ^= t;
    rng->state[3] = rotl(rng->state[3], 45);
    return result;
}

uint32_t krng_next_u32(struct kernel_rng *rng) {
    return (uint32_t)(krng_next(rng) >> 32);
}

uint32_t krng_bounded(struct kernel_rng *rng, uint32_t bound) {
    if (bound == 0) return 0;
    return krng_next_u32(rng) % bound;
}

void krng_add_entropy(struct kernel_rng *rng, uint64_t noise) {
    rng->state[0] ^= noise;
    rng->entropy_count += 8;
    if (rng->entropy_count > 256) {
        rng->entropy_count = 256;
    }
    krng_next(rng);
}

int krng_has_entropy(const struct kernel_rng *rng) {
    return rng->entropy_count >= 64;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C498: Kernel RNG - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C498: empty output");
    assert!(code.contains("fn krng_next"), "C498: Should contain krng_next");
    assert!(code.contains("fn krng_add_entropy"), "C498: Should contain krng_add_entropy");
}

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

#define MAX_KOBJS 128
#define KOBJ_FREE    0
#define KOBJ_ACTIVE  1
#define KOBJ_DYING   2

struct kref {
    int count;
};

struct kobject {
    int id;
    int state;
    struct kref ref;
    int parent;
    int type;
    uint32_t flags;
};

struct kobj_pool {
    struct kobject objects[128];
    int count;
};

void kref_init(struct kref *kr) {
    kr->count = 1;
}

void kref_get(struct kref *kr) {
    kr->count++;
}

int kref_put(struct kref *kr) {
    kr->count--;
    return kr->count == 0;
}

void kpool_init(struct kobj_pool *kp) {
    kp->count = 0;
    for (int i = 0; i < MAX_KOBJS; i++) {
        kp->objects[i].id = i;
        kp->objects[i].state = KOBJ_FREE;
        kp->objects[i].ref.count = 0;
        kp->objects[i].parent = -1;
        kp->objects[i].type = 0;
        kp->objects[i].flags = 0;
    }
}

int kpool_alloc(struct kobj_pool *kp, int parent, int type) {
    for (int i = 0; i < MAX_KOBJS; i++) {
        if (kp->objects[i].state == KOBJ_FREE) {
            kp->objects[i].state = KOBJ_ACTIVE;
            kp->objects[i].parent = parent;
            kp->objects[i].type = type;
            kref_init(&kp->objects[i].ref);
            kp->count++;
            if (parent >= 0 && parent < MAX_KOBJS) {
                kref_get(&kp->objects[parent].ref);
            }
            return i;
        }
    }
    return -1;
}

int kpool_release(struct kobj_pool *kp, int id) {
    if (id < 0 || id >= MAX_KOBJS) return -1;
    if (kp->objects[id].state != KOBJ_ACTIVE) return -1;
    if (kref_put(&kp->objects[id].ref)) {
        kp->objects[id].state = KOBJ_FREE;
        kp->count--;
        int parent = kp->objects[id].parent;
        if (parent >= 0) {
            kpool_release(kp, parent);
        }
        return 1;
    }
    return 0;
}

int kpool_ref_count(const struct kobj_pool *kp, int id) {
    if (id < 0 || id >= MAX_KOBJS) return 0;
    return kp->objects[id].ref.count;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C499: Kernel object refcounting - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C499: empty output");
    assert!(code.contains("fn kref_get"), "C499: Should contain kref_get");
    assert!(code.contains("fn kpool_release"), "C499: Should contain kpool_release");
}

#[test]
fn c500_elf_binary_header_parser() {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
typedef unsigned long uint64_t;

#define ELF_MAGIC 0x464C457F
#define ET_EXEC 2
#define ET_DYN  3
#define EM_386    3
#define EM_X86_64 62
#define PT_LOAD 1
#define PT_NOTE 4
#define MAX_PHDRS 16

struct elf_header {
    uint32_t magic;
    uint8_t elf_class;
    uint8_t data_encoding;
    uint8_t version;
    uint8_t os_abi;
    uint16_t type;
    uint16_t machine;
    uint32_t entry;
    uint32_t phoff;
    uint32_t shoff;
    uint16_t phnum;
    uint16_t shnum;
};

struct program_header {
    uint32_t type;
    uint32_t offset;
    uint32_t vaddr;
    uint32_t paddr;
    uint32_t filesz;
    uint32_t memsz;
    uint32_t flags;
    uint32_t align;
};

struct elf_info {
    struct elf_header hdr;
    struct program_header phdrs[16];
    int valid;
    int loadable_count;
    uint32_t total_memsz;
};

int elf_validate(struct elf_info *ei) {
    if (ei->hdr.magic != ELF_MAGIC) {
        ei->valid = 0;
        return -1;
    }
    if (ei->hdr.type != ET_EXEC && ei->hdr.type != ET_DYN) {
        ei->valid = 0;
        return -2;
    }
    ei->valid = 1;
    return 0;
}

int elf_count_loadable(struct elf_info *ei) {
    int count = 0;
    int n = ei->hdr.phnum;
    if (n > MAX_PHDRS) n = MAX_PHDRS;
    for (int i = 0; i < n; i++) {
        if (ei->phdrs[i].type == PT_LOAD) {
            count++;
        }
    }
    ei->loadable_count = count;
    return count;
}

uint32_t elf_total_memory(struct elf_info *ei) {
    uint32_t total = 0;
    int n = ei->hdr.phnum;
    if (n > MAX_PHDRS) n = MAX_PHDRS;
    for (int i = 0; i < n; i++) {
        if (ei->phdrs[i].type == PT_LOAD) {
            total += ei->phdrs[i].memsz;
        }
    }
    ei->total_memsz = total;
    return total;
}

int elf_is_64bit(const struct elf_info *ei) {
    return ei->hdr.elf_class == 2;
}

int elf_is_executable(const struct elf_info *ei) {
    return ei->hdr.type == ET_EXEC;
}

uint32_t elf_entry_point(const struct elf_info *ei) {
    return ei->hdr.entry;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C500: ELF header parser - failed: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C500: empty output");
    assert!(code.contains("fn elf_validate"), "C500: Should contain elf_validate");
    assert!(code.contains("fn elf_count_loadable"), "C500: Should contain elf_count_loadable");
    assert!(code.contains("fn elf_entry_point"), "C500: Should contain elf_entry_point");
}