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
//! Popperian Falsification Test Suite for Decy C-to-Rust Transpiler
//!
//! C526-C550: Compression and Encoding algorithms -- the kind of C code found
//! in zlib, brotli, lz4, base64 libraries, and data compression toolkits.
//! Tests are APPEND-ONLY per Popperian methodology.
//! Falsified tests are marked #[ignore = "FALSIFIED: reason"].
//!
//! These tests exercise real-world compression and encoding patterns commonly
//! found in zlib, liblzma, libbrotli, and similar libraries -- all expressed
//! as valid C99 with array-based representations (no malloc/free).
//!
//! Organization:
//! - C526-C530: Basic compression (RLE, Huffman, LZ77, LZW)
//! - C531-C535: Encoding and checksums (Base64, CRC32, Adler32, DEFLATE)
//! - C536-C540: Advanced coding (arithmetic, delta, BWT, MTF, bitstream)
//! - C541-C545: Variable-length codes (bitstream reader, varint, Golomb-Rice, Elias gamma, Fibonacci)
//! - C546-C550: Specialized (BPE, PPM, range encoder, Shannon entropy, JPEG quantization)
//!
//! ## Results
//! - 25 passing, 0 falsified (100.0% pass rate)

// ============================================================================
// C526-C530: Basic Compression (RLE, Huffman, LZ77, LZW)
// ============================================================================

/// C526: Run-length encoding -- compresses repeated byte sequences
#[test]
fn c526_run_length_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    uint8_t data[512];
    int len;
} rle_buf_t;

int rle_encode(const uint8_t *input, int in_len, uint8_t *output, int max_out) {
    int i = 0;
    int out_pos = 0;
    while (i < in_len && out_pos + 2 <= max_out) {
        uint8_t cur = input[i];
        int count = 1;
        while (i + count < in_len && input[i + count] == cur && count < 255) {
            count++;
        }
        output[out_pos] = (uint8_t)count;
        output[out_pos + 1] = cur;
        out_pos += 2;
        i += count;
    }
    return out_pos;
}

int rle_decode(const uint8_t *input, int in_len, uint8_t *output, int max_out) {
    int i = 0;
    int out_pos = 0;
    while (i + 1 < in_len) {
        int count = input[i];
        uint8_t val = input[i + 1];
        int j;
        for (j = 0; j < count && out_pos < max_out; j++) {
            output[out_pos] = val;
            out_pos++;
        }
        i += 2;
    }
    return out_pos;
}

int rle_roundtrip_test(void) {
    uint8_t input[16];
    uint8_t encoded[64];
    uint8_t decoded[64];
    int i;
    for (i = 0; i < 8; i++) input[i] = 0xAA;
    for (i = 8; i < 16; i++) input[i] = 0xBB;
    int enc_len = rle_encode(input, 16, encoded, 64);
    int dec_len = rle_decode(encoded, enc_len, decoded, 64);
    if (dec_len != 16) return -1;
    for (i = 0; i < 16; i++) {
        if (decoded[i] != input[i]) return -2;
    }
    return 0;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C526: Should produce output");
    assert!(rust_code.contains("fn rle_encode"), "C526: Should contain rle_encode function");
    assert!(rust_code.contains("fn rle_decode"), "C526: Should contain rle_decode function");
    Ok(())
}

/// C527: Huffman tree building -- construct frequency-based binary tree
#[test]
fn c527_huffman_tree_building() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef struct {
    int freq;
    int left;
    int right;
    int parent;
    int symbol;
} huff_node_t;

typedef struct {
    huff_node_t nodes[512];
    int count;
} huff_tree_t;

void huff_init(huff_tree_t *tree) {
    int i;
    tree->count = 0;
    for (i = 0; i < 512; i++) {
        tree->nodes[i].freq = 0;
        tree->nodes[i].left = -1;
        tree->nodes[i].right = -1;
        tree->nodes[i].parent = -1;
        tree->nodes[i].symbol = -1;
    }
}

int huff_add_leaf(huff_tree_t *tree, int symbol, int freq) {
    if (tree->count >= 512) return -1;
    int idx = tree->count;
    tree->nodes[idx].symbol = symbol;
    tree->nodes[idx].freq = freq;
    tree->nodes[idx].left = -1;
    tree->nodes[idx].right = -1;
    tree->nodes[idx].parent = -1;
    tree->count++;
    return idx;
}

int huff_find_min(const huff_tree_t *tree, int exclude) {
    int min_idx = -1;
    int min_freq = 2147483647;
    int i;
    for (i = 0; i < tree->count; i++) {
        if (i == exclude) continue;
        if (tree->nodes[i].parent != -1) continue;
        if (tree->nodes[i].freq < min_freq) {
            min_freq = tree->nodes[i].freq;
            min_idx = i;
        }
    }
    return min_idx;
}

int huff_build_step(huff_tree_t *tree) {
    int left = huff_find_min(tree, -1);
    if (left == -1) return -1;
    int right = huff_find_min(tree, left);
    if (right == -1) return -1;
    int parent = tree->count;
    if (parent >= 512) return -1;
    tree->nodes[parent].freq = tree->nodes[left].freq + tree->nodes[right].freq;
    tree->nodes[parent].left = left;
    tree->nodes[parent].right = right;
    tree->nodes[parent].symbol = -1;
    tree->nodes[parent].parent = -1;
    tree->nodes[left].parent = parent;
    tree->nodes[right].parent = parent;
    tree->count++;
    return parent;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C527: Should produce output");
    assert!(
        rust_code.contains("fn huff_build_step"),
        "C527: Should contain huff_build_step function"
    );
    Ok(())
}

/// C528: Huffman encoding -- encode data using Huffman code table
#[test]
fn c528_huffman_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

typedef struct {
    uint32_t code;
    int bits;
} huff_code_t;

typedef struct {
    huff_code_t table[256];
    uint8_t output[1024];
    int byte_pos;
    int bit_pos;
} huff_encoder_t;

void huff_enc_init(huff_encoder_t *enc) {
    int i;
    for (i = 0; i < 256; i++) {
        enc->table[i].code = 0;
        enc->table[i].bits = 0;
    }
    enc->byte_pos = 0;
    enc->bit_pos = 0;
    for (i = 0; i < 1024; i++) {
        enc->output[i] = 0;
    }
}

void huff_enc_set_code(huff_encoder_t *enc, int symbol, uint32_t code, int bits) {
    if (symbol >= 0 && symbol < 256) {
        enc->table[symbol].code = code;
        enc->table[symbol].bits = bits;
    }
}

void huff_enc_write_bit(huff_encoder_t *enc, int bit) {
    if (enc->byte_pos >= 1024) return;
    if (bit) {
        enc->output[enc->byte_pos] |= (uint8_t)(1 << (7 - enc->bit_pos));
    }
    enc->bit_pos++;
    if (enc->bit_pos >= 8) {
        enc->bit_pos = 0;
        enc->byte_pos++;
    }
}

void huff_enc_encode_symbol(huff_encoder_t *enc, int symbol) {
    if (symbol < 0 || symbol >= 256) return;
    huff_code_t c = enc->table[symbol];
    int i;
    for (i = 0; i < c.bits; i++) {
        int bit = (c.code >> (c.bits - 1 - i)) & 1;
        huff_enc_write_bit(enc, bit);
    }
}

int huff_enc_get_total_bits(const huff_encoder_t *enc) {
    return enc->byte_pos * 8 + enc->bit_pos;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C528: Should produce output");
    assert!(
        rust_code.contains("fn huff_enc_encode_symbol"),
        "C528: Should contain huff_enc_encode_symbol function"
    );
    Ok(())
}

/// C529: LZ77 sliding window -- find matches in a lookback buffer
#[test]
fn c529_lz77_sliding_window() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    int offset;
    int length;
    uint8_t next_char;
} lz77_match_t;

typedef struct {
    uint8_t window[4096];
    int win_size;
    int lookahead;
} lz77_ctx_t;

void lz77_init(lz77_ctx_t *ctx, int win_size, int lookahead) {
    int i;
    ctx->win_size = win_size < 4096 ? win_size : 4096;
    ctx->lookahead = lookahead;
    for (i = 0; i < 4096; i++) {
        ctx->window[i] = 0;
    }
}

lz77_match_t lz77_find_match(const uint8_t *data, int data_len, int pos, int win_start) {
    lz77_match_t best;
    int i;
    best.offset = 0;
    best.length = 0;
    best.next_char = (pos < data_len) ? data[pos] : 0;
    for (i = win_start; i < pos; i++) {
        int match_len = 0;
        while (pos + match_len < data_len && data[i + match_len] == data[pos + match_len]) {
            match_len++;
            if (match_len >= 255) break;
        }
        if (match_len > best.length) {
            best.offset = pos - i;
            best.length = match_len;
            if (pos + match_len < data_len) {
                best.next_char = data[pos + match_len];
            } else {
                best.next_char = 0;
            }
        }
    }
    return best;
}

int lz77_compress_step(const uint8_t *data, int data_len, int pos, lz77_match_t *out) {
    int win_start = pos > 4096 ? pos - 4096 : 0;
    *out = lz77_find_match(data, data_len, pos, win_start);
    if (out->length > 0) {
        return out->length + 1;
    }
    return 1;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C529: Should produce output");
    assert!(
        rust_code.contains("fn lz77_find_match"),
        "C529: Should contain lz77_find_match function"
    );
    Ok(())
}

/// C530: LZW dictionary compression -- build and use a string table
#[test]
fn c530_lzw_dictionary_compression() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;

typedef struct {
    uint16_t prefix;
    uint8_t character;
    int valid;
} lzw_entry_t;

typedef struct {
    lzw_entry_t dict[4096];
    int next_code;
    int max_code;
} lzw_dict_t;

void lzw_init(lzw_dict_t *d) {
    int i;
    d->next_code = 256;
    d->max_code = 4096;
    for (i = 0; i < 256; i++) {
        d->dict[i].prefix = 0xFFFF;
        d->dict[i].character = (uint8_t)i;
        d->dict[i].valid = 1;
    }
    for (i = 256; i < 4096; i++) {
        d->dict[i].valid = 0;
    }
}

int lzw_add_entry(lzw_dict_t *d, uint16_t prefix, uint8_t character) {
    if (d->next_code >= d->max_code) return -1;
    int code = d->next_code;
    d->dict[code].prefix = prefix;
    d->dict[code].character = character;
    d->dict[code].valid = 1;
    d->next_code++;
    return code;
}

int lzw_find_entry(const lzw_dict_t *d, uint16_t prefix, uint8_t character) {
    int i;
    for (i = 0; i < d->next_code; i++) {
        if (d->dict[i].valid &&
            d->dict[i].prefix == prefix &&
            d->dict[i].character == character) {
            return i;
        }
    }
    return -1;
}

int lzw_decode_string(const lzw_dict_t *d, int code, uint8_t *out, int max_len) {
    int pos = 0;
    uint8_t stack[256];
    int sp = 0;
    while (code != 0xFFFF && sp < 256) {
        if (code < 0 || code >= d->next_code) break;
        stack[sp] = d->dict[code].character;
        sp++;
        code = d->dict[code].prefix;
    }
    while (sp > 0 && pos < max_len) {
        sp--;
        out[pos] = stack[sp];
        pos++;
    }
    return pos;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C530: Should produce output");
    assert!(rust_code.contains("fn lzw_init"), "C530: Should contain lzw_init function");
    Ok(())
}

// ============================================================================
// C531-C535: Encoding and Checksums (Base64, CRC32, Adler32, DEFLATE)
// ============================================================================

/// C531: Base64 encoding -- encode binary data to ASCII
#[test]
fn c531_base64_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

static const char b64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int base64_encode(const uint8_t *input, int in_len, char *output, int max_out) {
    int i = 0;
    int o = 0;
    while (i < in_len && o + 4 <= max_out) {
        uint8_t b0 = input[i];
        uint8_t b1 = (i + 1 < in_len) ? input[i + 1] : 0;
        uint8_t b2 = (i + 2 < in_len) ? input[i + 2] : 0;
        output[o]     = b64_table[(b0 >> 2) & 0x3F];
        output[o + 1] = b64_table[((b0 << 4) | (b1 >> 4)) & 0x3F];
        if (i + 1 < in_len) {
            output[o + 2] = b64_table[((b1 << 2) | (b2 >> 6)) & 0x3F];
        } else {
            output[o + 2] = '=';
        }
        if (i + 2 < in_len) {
            output[o + 3] = b64_table[b2 & 0x3F];
        } else {
            output[o + 3] = '=';
        }
        i += 3;
        o += 4;
    }
    if (o < max_out) output[o] = 0;
    return o;
}

int base64_encoded_len(int in_len) {
    return ((in_len + 2) / 3) * 4;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C531: Should produce output");
    assert!(rust_code.contains("fn base64_encode"), "C531: Should contain base64_encode function");
    Ok(())
}

/// C532: Base64 decoding -- decode ASCII back to binary
#[test]
fn c532_base64_decoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

int b64_char_to_val(char c) {
    if (c >= 'A' && c <= 'Z') return c - 'A';
    if (c >= 'a' && c <= 'z') return c - 'a' + 26;
    if (c >= '0' && c <= '9') return c - '0' + 52;
    if (c == '+') return 62;
    if (c == '/') return 63;
    return -1;
}

int base64_decode(const char *input, int in_len, uint8_t *output, int max_out) {
    int i = 0;
    int o = 0;
    while (i + 3 < in_len && o < max_out) {
        int v0 = b64_char_to_val(input[i]);
        int v1 = b64_char_to_val(input[i + 1]);
        int v2 = b64_char_to_val(input[i + 2]);
        int v3 = b64_char_to_val(input[i + 3]);
        if (v0 < 0 || v1 < 0) break;
        output[o] = (uint8_t)((v0 << 2) | (v1 >> 4));
        o++;
        if (v2 >= 0 && o < max_out) {
            output[o] = (uint8_t)((v1 << 4) | (v2 >> 2));
            o++;
        }
        if (v3 >= 0 && o < max_out) {
            output[o] = (uint8_t)((v2 << 6) | v3);
            o++;
        }
        i += 4;
    }
    return o;
}

int base64_roundtrip_check(void) {
    uint8_t data[4];
    data[0] = 0xDE; data[1] = 0xAD; data[2] = 0xBE; data[3] = 0xEF;
    return 0;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C532: Should produce output");
    assert!(rust_code.contains("fn base64_decode"), "C532: Should contain base64_decode function");
    Ok(())
}

/// C533: CRC32 computation -- compute cyclic redundancy check
#[test]
fn c533_crc32_computation() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

static uint32_t crc32_table[256];

void crc32_init_table(void) {
    uint32_t poly = 0xEDB88320;
    int i;
    for (i = 0; i < 256; i++) {
        uint32_t crc = (uint32_t)i;
        int j;
        for (j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ poly;
            } else {
                crc = crc >> 1;
            }
        }
        crc32_table[i] = crc;
    }
}

uint32_t crc32_update(uint32_t crc, const uint8_t *data, int len) {
    int i;
    crc = crc ^ 0xFFFFFFFF;
    for (i = 0; i < len; i++) {
        uint32_t idx = (crc ^ data[i]) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[idx];
    }
    return crc ^ 0xFFFFFFFF;
}

uint32_t crc32_compute(const uint8_t *data, int len) {
    return crc32_update(0, data, len);
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C533: Should produce output");
    assert!(rust_code.contains("fn crc32_update"), "C533: Should contain crc32_update function");
    Ok(())
}

/// C534: Adler32 checksum -- fast rolling checksum used in zlib
#[test]
fn c534_adler32_checksum() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

uint32_t adler32_compute(const uint8_t *data, int len) {
    uint32_t a = 1;
    uint32_t b = 0;
    int i;
    for (i = 0; i < len; i++) {
        a = (a + data[i]) % 65521;
        b = (b + a) % 65521;
    }
    return (b << 16) | a;
}

uint32_t adler32_combine(uint32_t adler1, uint32_t adler2, int len2) {
    uint32_t a1 = adler1 & 0xFFFF;
    uint32_t b1 = (adler1 >> 16) & 0xFFFF;
    uint32_t a2 = adler2 & 0xFFFF;
    uint32_t b2 = (adler2 >> 16) & 0xFFFF;
    uint32_t a = (a1 + a2 - 1) % 65521;
    uint32_t b = (b1 + b2 + a1 * (uint32_t)len2 - (uint32_t)len2) % 65521;
    return (b << 16) | a;
}

int adler32_verify(const uint8_t *data, int len, uint32_t expected) {
    uint32_t actual = adler32_compute(data, len);
    return actual == expected ? 0 : -1;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C534: Should produce output");
    assert!(
        rust_code.contains("fn adler32_compute"),
        "C534: Should contain adler32_compute function"
    );
    Ok(())
}

/// C535: DEFLATE static block -- emit fixed Huffman-coded literals
#[test]
fn c535_deflate_static_block() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;

typedef struct {
    uint8_t buf[1024];
    int byte_pos;
    int bit_pos;
} bitwriter_t;

void bw_init(bitwriter_t *bw) {
    int i;
    bw->byte_pos = 0;
    bw->bit_pos = 0;
    for (i = 0; i < 1024; i++) {
        bw->buf[i] = 0;
    }
}

void bw_write_bits(bitwriter_t *bw, uint16_t value, int nbits) {
    int i;
    for (i = 0; i < nbits; i++) {
        if (bw->byte_pos >= 1024) return;
        if (value & (1 << i)) {
            bw->buf[bw->byte_pos] |= (uint8_t)(1 << bw->bit_pos);
        }
        bw->bit_pos++;
        if (bw->bit_pos >= 8) {
            bw->bit_pos = 0;
            bw->byte_pos++;
        }
    }
}

uint16_t deflate_fixed_code(int literal) {
    if (literal <= 143) {
        return (uint16_t)(0x30 + literal);
    } else if (literal <= 255) {
        return (uint16_t)(0x190 + (literal - 144));
    } else if (literal == 256) {
        return 0;
    }
    return 0xFFFF;
}

int deflate_fixed_bits(int literal) {
    if (literal <= 143) return 8;
    if (literal <= 255) return 9;
    if (literal == 256) return 7;
    return 0;
}

void deflate_emit_literal(bitwriter_t *bw, int literal) {
    uint16_t code = deflate_fixed_code(literal);
    int bits = deflate_fixed_bits(literal);
    bw_write_bits(bw, code, bits);
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C535: Should produce output");
    assert!(
        rust_code.contains("fn deflate_emit_literal"),
        "C535: Should contain deflate_emit_literal function"
    );
    Ok(())
}

// ============================================================================
// C536-C540: Advanced Coding (Arithmetic, Delta, BWT, MTF, Bitstream)
// ============================================================================

/// C536: Arithmetic coding -- encode symbols using interval subdivision
#[test]
fn c536_arithmetic_coding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned int uint32_t;

typedef struct {
    uint32_t low;
    uint32_t high;
    uint32_t range;
    int pending_bits;
    int total_symbols;
} arith_enc_t;

void arith_init(arith_enc_t *enc) {
    enc->low = 0;
    enc->high = 0xFFFFFFFF;
    enc->range = 0xFFFFFFFF;
    enc->pending_bits = 0;
    enc->total_symbols = 0;
}

void arith_encode_symbol(arith_enc_t *enc, int cum_freq_low, int cum_freq_high, int total_freq) {
    uint32_t range = enc->high - enc->low + 1;
    enc->high = enc->low + (range * (uint32_t)cum_freq_high) / (uint32_t)total_freq - 1;
    enc->low = enc->low + (range * (uint32_t)cum_freq_low) / (uint32_t)total_freq;
    enc->total_symbols++;
}

void arith_normalize(arith_enc_t *enc) {
    while (1) {
        if ((enc->high & 0x80000000) == (enc->low & 0x80000000)) {
            enc->pending_bits++;
            enc->low = (enc->low << 1) & 0xFFFFFFFF;
            enc->high = ((enc->high << 1) | 1) & 0xFFFFFFFF;
        } else if ((enc->low & 0x40000000) && !(enc->high & 0x40000000)) {
            enc->low = (enc->low << 1) & 0x7FFFFFFF;
            enc->high = ((enc->high << 1) | 0x80000001);
            enc->pending_bits++;
        } else {
            break;
        }
    }
}

uint32_t arith_get_low(const arith_enc_t *enc) {
    return enc->low;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C536: Should produce output");
    assert!(
        rust_code.contains("fn arith_encode_symbol"),
        "C536: Should contain arith_encode_symbol function"
    );
    Ok(())
}

/// C537: Delta encoding -- store differences between consecutive values
#[test]
fn c537_delta_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef struct {
    int data[256];
    int len;
} delta_buf_t;

void delta_encode(const int *input, int len, int *output) {
    int i;
    if (len <= 0) return;
    output[0] = input[0];
    for (i = 1; i < len; i++) {
        output[i] = input[i] - input[i - 1];
    }
}

void delta_decode(const int *input, int len, int *output) {
    int i;
    if (len <= 0) return;
    output[0] = input[0];
    for (i = 1; i < len; i++) {
        output[i] = output[i - 1] + input[i];
    }
}

int delta_roundtrip(const int *data, int len) {
    int encoded[256];
    int decoded[256];
    int i;
    if (len > 256) return -1;
    delta_encode(data, len, encoded);
    delta_decode(encoded, len, decoded);
    for (i = 0; i < len; i++) {
        if (decoded[i] != data[i]) return -2;
    }
    return 0;
}

int delta_compression_ratio(const int *deltas, int len) {
    int zero_count = 0;
    int i;
    for (i = 0; i < len; i++) {
        if (deltas[i] == 0) zero_count++;
    }
    if (len == 0) return 0;
    return (zero_count * 100) / len;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C537: Should produce output");
    assert!(rust_code.contains("fn delta_encode"), "C537: Should contain delta_encode function");
    assert!(rust_code.contains("fn delta_decode"), "C537: Should contain delta_decode function");
    Ok(())
}

/// C538: Burrows-Wheeler transform -- permute input for better compression
#[test]
fn c538_burrows_wheeler_transform() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    int indices[256];
    uint8_t output[256];
    int original_idx;
    int len;
} bwt_ctx_t;

int bwt_compare(const uint8_t *data, int len, int a, int b) {
    int i;
    for (i = 0; i < len; i++) {
        int ca = data[(a + i) % len];
        int cb = data[(b + i) % len];
        if (ca < cb) return -1;
        if (ca > cb) return 1;
    }
    return 0;
}

void bwt_sort_indices(const uint8_t *data, int len, int *indices) {
    int i;
    int j;
    for (i = 0; i < len; i++) {
        indices[i] = i;
    }
    for (i = 1; i < len; i++) {
        int key = indices[i];
        j = i - 1;
        while (j >= 0 && bwt_compare(data, len, indices[j], key) > 0) {
            indices[j + 1] = indices[j];
            j--;
        }
        indices[j + 1] = key;
    }
}

void bwt_transform(bwt_ctx_t *ctx, const uint8_t *data, int len) {
    int i;
    if (len > 256) len = 256;
    ctx->len = len;
    bwt_sort_indices(data, len, ctx->indices);
    ctx->original_idx = -1;
    for (i = 0; i < len; i++) {
        int rot = ctx->indices[i];
        ctx->output[i] = data[(rot + len - 1) % len];
        if (rot == 0) {
            ctx->original_idx = i;
        }
    }
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C538: Should produce output");
    assert!(rust_code.contains("fn bwt_transform"), "C538: Should contain bwt_transform function");
    Ok(())
}

/// C539: Move-to-front encoding -- adaptive byte reordering
#[test]
fn c539_move_to_front_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    uint8_t list[256];
} mtf_ctx_t;

void mtf_init(mtf_ctx_t *ctx) {
    int i;
    for (i = 0; i < 256; i++) {
        ctx->list[i] = (uint8_t)i;
    }
}

int mtf_find(const mtf_ctx_t *ctx, uint8_t value) {
    int i;
    for (i = 0; i < 256; i++) {
        if (ctx->list[i] == value) return i;
    }
    return -1;
}

void mtf_move_to_front(mtf_ctx_t *ctx, int pos) {
    uint8_t val = ctx->list[pos];
    int i;
    for (i = pos; i > 0; i--) {
        ctx->list[i] = ctx->list[i - 1];
    }
    ctx->list[0] = val;
}

void mtf_encode(mtf_ctx_t *ctx, const uint8_t *input, int len, uint8_t *output) {
    int i;
    for (i = 0; i < len; i++) {
        int pos = mtf_find(ctx, input[i]);
        output[i] = (uint8_t)pos;
        mtf_move_to_front(ctx, pos);
    }
}

void mtf_decode(mtf_ctx_t *ctx, const uint8_t *input, int len, uint8_t *output) {
    int i;
    for (i = 0; i < len; i++) {
        int pos = input[i];
        output[i] = ctx->list[pos];
        mtf_move_to_front(ctx, pos);
    }
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C539: Should produce output");
    assert!(rust_code.contains("fn mtf_encode"), "C539: Should contain mtf_encode function");
    assert!(rust_code.contains("fn mtf_decode"), "C539: Should contain mtf_decode function");
    Ok(())
}

/// C540: Bit-level I/O (bitstream writer) -- write individual bits to a byte buffer
#[test]
fn c540_bitstream_writer() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

typedef struct {
    uint8_t buffer[512];
    int byte_pos;
    int bit_pos;
    int capacity;
} bit_writer_t;

void bw_create(bit_writer_t *w, int capacity) {
    int i;
    w->byte_pos = 0;
    w->bit_pos = 0;
    w->capacity = capacity < 512 ? capacity : 512;
    for (i = 0; i < 512; i++) {
        w->buffer[i] = 0;
    }
}

int bw_put_bit(bit_writer_t *w, int bit) {
    if (w->byte_pos >= w->capacity) return -1;
    if (bit) {
        w->buffer[w->byte_pos] |= (uint8_t)(1 << (7 - w->bit_pos));
    }
    w->bit_pos++;
    if (w->bit_pos >= 8) {
        w->bit_pos = 0;
        w->byte_pos++;
    }
    return 0;
}

int bw_put_bits(bit_writer_t *w, uint32_t value, int nbits) {
    int i;
    for (i = nbits - 1; i >= 0; i--) {
        int bit = (value >> i) & 1;
        if (bw_put_bit(w, bit) < 0) return -1;
    }
    return 0;
}

int bw_total_bits(const bit_writer_t *w) {
    return w->byte_pos * 8 + w->bit_pos;
}

int bw_flush(bit_writer_t *w) {
    if (w->bit_pos > 0) {
        w->byte_pos++;
        w->bit_pos = 0;
    }
    return w->byte_pos;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C540: Should produce output");
    assert!(rust_code.contains("fn bw_put_bit"), "C540: Should contain bw_put_bit function");
    Ok(())
}

// ============================================================================
// C541-C545: Variable-Length Codes (Bitstream Reader, Varint, Golomb, Elias, Fibonacci)
// ============================================================================

/// C541: Bit-level I/O (bitstream reader) -- read individual bits from a byte buffer
#[test]
fn c541_bitstream_reader() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

typedef struct {
    const uint8_t *data;
    int len;
    int byte_pos;
    int bit_pos;
} bit_reader_t;

void br_init(bit_reader_t *r, const uint8_t *data, int len) {
    r->data = data;
    r->len = len;
    r->byte_pos = 0;
    r->bit_pos = 0;
}

int br_get_bit(bit_reader_t *r) {
    if (r->byte_pos >= r->len) return -1;
    int bit = (r->data[r->byte_pos] >> (7 - r->bit_pos)) & 1;
    r->bit_pos++;
    if (r->bit_pos >= 8) {
        r->bit_pos = 0;
        r->byte_pos++;
    }
    return bit;
}

int br_get_bits(bit_reader_t *r, int nbits, uint32_t *out) {
    uint32_t value = 0;
    int i;
    for (i = 0; i < nbits; i++) {
        int bit = br_get_bit(r);
        if (bit < 0) return -1;
        value = (value << 1) | (uint32_t)bit;
    }
    *out = value;
    return 0;
}

int br_remaining_bits(const bit_reader_t *r) {
    return (r->len - r->byte_pos) * 8 - r->bit_pos;
}

int br_is_aligned(const bit_reader_t *r) {
    return r->bit_pos == 0;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C541: Should produce output");
    assert!(rust_code.contains("fn br_get_bit"), "C541: Should contain br_get_bit function");
    Ok(())
}

/// C542: Variable-length integer encoding (varint) -- protobuf-style encoding
#[test]
fn c542_varint_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

int varint_encode(uint32_t value, uint8_t *output, int max_len) {
    int pos = 0;
    while (value > 0x7F && pos < max_len) {
        output[pos] = (uint8_t)((value & 0x7F) | 0x80);
        value >>= 7;
        pos++;
    }
    if (pos < max_len) {
        output[pos] = (uint8_t)(value & 0x7F);
        pos++;
    }
    return pos;
}

int varint_decode(const uint8_t *input, int max_len, uint32_t *value) {
    uint32_t result = 0;
    int shift = 0;
    int pos = 0;
    while (pos < max_len) {
        uint32_t byte_val = input[pos];
        result |= (byte_val & 0x7F) << shift;
        pos++;
        if (!(byte_val & 0x80)) break;
        shift += 7;
        if (shift >= 35) return -1;
    }
    *value = result;
    return pos;
}

int varint_encoded_size(uint32_t value) {
    int size = 1;
    while (value > 0x7F) {
        value >>= 7;
        size++;
    }
    return size;
}

int varint_roundtrip(uint32_t input) {
    uint8_t buf[8];
    uint32_t output = 0;
    int enc_len = varint_encode(input, buf, 8);
    if (enc_len <= 0) return -1;
    int dec_len = varint_decode(buf, enc_len, &output);
    if (dec_len != enc_len) return -2;
    if (output != input) return -3;
    return 0;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C542: Should produce output");
    assert!(rust_code.contains("fn varint_encode"), "C542: Should contain varint_encode function");
    assert!(rust_code.contains("fn varint_decode"), "C542: Should contain varint_decode function");
    Ok(())
}

/// C543: Golomb-Rice coding -- encode integers with tunable parameter
#[test]
fn c543_golomb_rice_coding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned int uint32_t;

typedef struct {
    int quotient;
    uint32_t remainder;
    int m_bits;
} golomb_code_t;

golomb_code_t golomb_rice_encode(uint32_t value, int k) {
    golomb_code_t code;
    uint32_t m = (uint32_t)1 << k;
    code.quotient = (int)(value / m);
    code.remainder = value % m;
    code.m_bits = k;
    return code;
}

uint32_t golomb_rice_decode(int quotient, uint32_t remainder, int k) {
    uint32_t m = (uint32_t)1 << k;
    return (uint32_t)quotient * m + remainder;
}

int golomb_rice_total_bits(golomb_code_t code) {
    return code.quotient + 1 + code.m_bits;
}

int golomb_rice_roundtrip(uint32_t value, int k) {
    golomb_code_t code = golomb_rice_encode(value, k);
    uint32_t decoded = golomb_rice_decode(code.quotient, code.remainder, k);
    return (decoded == value) ? 0 : -1;
}

int golomb_rice_optimal_k(const uint32_t *data, int len) {
    uint32_t sum = 0;
    int i;
    for (i = 0; i < len; i++) {
        sum += data[i];
    }
    if (len == 0) return 0;
    uint32_t mean = sum / (uint32_t)len;
    int k = 0;
    while ((uint32_t)(1 << k) < mean && k < 16) {
        k++;
    }
    return k > 0 ? k - 1 : 0;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C543: Should produce output");
    assert!(
        rust_code.contains("fn golomb_rice_encode"),
        "C543: Should contain golomb_rice_encode function"
    );
    Ok(())
}

/// C544: Elias gamma coding -- encode positive integers using prefix-free code
#[test]
fn c544_elias_gamma_coding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned int uint32_t;

int elias_floor_log2(uint32_t n) {
    int log = 0;
    while (n > 1) {
        n >>= 1;
        log++;
    }
    return log;
}

int elias_gamma_bits(uint32_t value) {
    if (value == 0) return 1;
    int n = elias_floor_log2(value);
    return 2 * n + 1;
}

int elias_gamma_encode_to_array(uint32_t value, int *bits, int max_bits) {
    if (value == 0) {
        if (max_bits < 1) return -1;
        bits[0] = 0;
        return 1;
    }
    int n = elias_floor_log2(value);
    int total = 2 * n + 1;
    if (total > max_bits) return -1;
    int i;
    for (i = 0; i < n; i++) {
        bits[i] = 0;
    }
    for (i = 0; i <= n; i++) {
        bits[n + i] = (value >> (n - i)) & 1;
    }
    return total;
}

uint32_t elias_gamma_decode_from_array(const int *bits, int len, int *consumed) {
    int zeros = 0;
    int i;
    for (i = 0; i < len; i++) {
        if (bits[i] == 0) {
            zeros++;
        } else {
            break;
        }
    }
    uint32_t value = 0;
    for (i = 0; i <= zeros; i++) {
        if (zeros + i >= len) break;
        value = (value << 1) | (uint32_t)bits[zeros + i];
    }
    *consumed = 2 * zeros + 1;
    return value;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C544: Should produce output");
    assert!(
        rust_code.contains("fn elias_gamma_bits"),
        "C544: Should contain elias_gamma_bits function"
    );
    Ok(())
}

/// C545: Fibonacci coding -- represent integers using Fibonacci number sums
#[test]
fn c545_fibonacci_coding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned int uint32_t;

void fib_generate(uint32_t *fibs, int max_count) {
    if (max_count < 2) return;
    fibs[0] = 1;
    fibs[1] = 2;
    int i;
    for (i = 2; i < max_count; i++) {
        fibs[i] = fibs[i - 1] + fibs[i - 2];
        if (fibs[i] > 0x7FFFFFFF) {
            fibs[i] = 0;
            break;
        }
    }
}

int fib_encode(uint32_t value, int *bits, int max_bits) {
    uint32_t fibs[47];
    int bit_count = 0;
    int i;
    fib_generate(fibs, 47);
    int highest = 0;
    for (i = 0; i < 47; i++) {
        if (fibs[i] == 0) break;
        if (fibs[i] <= value) highest = i;
    }
    if (highest + 2 > max_bits) return -1;
    for (i = 0; i <= highest; i++) {
        bits[i] = 0;
    }
    for (i = highest; i >= 0; i--) {
        if (fibs[i] <= value) {
            bits[i] = 1;
            value -= fibs[i];
        }
    }
    bit_count = highest + 1;
    bits[bit_count] = 1;
    bit_count++;
    return bit_count;
}

int fib_code_length(uint32_t value) {
    int bits[64];
    return fib_encode(value, bits, 64);
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C545: Should produce output");
    assert!(rust_code.contains("fn fib_encode"), "C545: Should contain fib_encode function");
    Ok(())
}

// ============================================================================
// C546-C550: Specialized (BPE, PPM, Range Encoder, Shannon Entropy, JPEG Quant)
// ============================================================================

/// C546: Byte-pair encoding -- iteratively merge most frequent byte pairs
#[test]
fn c546_byte_pair_encoding() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    uint8_t first;
    uint8_t second;
    int count;
} bpe_pair_t;

typedef struct {
    bpe_pair_t pairs[256];
    int num_pairs;
    uint8_t merge_table[256];
    int next_symbol;
} bpe_ctx_t;

void bpe_init(bpe_ctx_t *ctx) {
    int i;
    ctx->num_pairs = 0;
    ctx->next_symbol = 128;
    for (i = 0; i < 256; i++) {
        ctx->merge_table[i] = (uint8_t)i;
    }
}

void bpe_count_pairs(const uint8_t *data, int len, bpe_pair_t *pairs, int *num_pairs) {
    int i;
    *num_pairs = 0;
    for (i = 0; i + 1 < len; i++) {
        int j;
        int found = 0;
        for (j = 0; j < *num_pairs; j++) {
            if (pairs[j].first == data[i] && pairs[j].second == data[i + 1]) {
                pairs[j].count++;
                found = 1;
                break;
            }
        }
        if (!found && *num_pairs < 256) {
            pairs[*num_pairs].first = data[i];
            pairs[*num_pairs].second = data[i + 1];
            pairs[*num_pairs].count = 1;
            (*num_pairs)++;
        }
    }
}

int bpe_find_best_pair(const bpe_pair_t *pairs, int num_pairs) {
    int best = -1;
    int best_count = 0;
    int i;
    for (i = 0; i < num_pairs; i++) {
        if (pairs[i].count > best_count) {
            best_count = pairs[i].count;
            best = i;
        }
    }
    return best;
}

int bpe_merge_pair(uint8_t *data, int len, uint8_t first, uint8_t second, uint8_t replacement) {
    int new_len = 0;
    int i = 0;
    while (i < len) {
        if (i + 1 < len && data[i] == first && data[i + 1] == second) {
            data[new_len] = replacement;
            new_len++;
            i += 2;
        } else {
            data[new_len] = data[i];
            new_len++;
            i++;
        }
    }
    return new_len;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C546: Should produce output");
    assert!(
        rust_code.contains("fn bpe_merge_pair"),
        "C546: Should contain bpe_merge_pair function"
    );
    Ok(())
}

/// C547: Prediction by partial matching (PPM context) -- context modeling for compression
#[test]
fn c547_ppm_context() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    uint8_t context[4];
    int ctx_len;
    int counts[256];
    int total;
} ppm_ctx_t;

void ppm_init(ppm_ctx_t *ctx) {
    int i;
    ctx->ctx_len = 0;
    ctx->total = 0;
    for (i = 0; i < 4; i++) {
        ctx->context[i] = 0;
    }
    for (i = 0; i < 256; i++) {
        ctx->counts[i] = 0;
    }
}

void ppm_update(ppm_ctx_t *ctx, uint8_t symbol) {
    ctx->counts[symbol]++;
    ctx->total++;
    int i;
    for (i = 0; i < 3; i++) {
        ctx->context[i] = ctx->context[i + 1];
    }
    ctx->context[3] = symbol;
    if (ctx->ctx_len < 4) ctx->ctx_len++;
}

int ppm_predict(const ppm_ctx_t *ctx) {
    int best = -1;
    int best_count = 0;
    int i;
    for (i = 0; i < 256; i++) {
        if (ctx->counts[i] > best_count) {
            best_count = ctx->counts[i];
            best = i;
        }
    }
    return best;
}

int ppm_probability(const ppm_ctx_t *ctx, uint8_t symbol) {
    if (ctx->total == 0) return 0;
    return (ctx->counts[symbol] * 1000) / ctx->total;
}

int ppm_escape_probability(const ppm_ctx_t *ctx) {
    int unique = 0;
    int i;
    for (i = 0; i < 256; i++) {
        if (ctx->counts[i] > 0) unique++;
    }
    if (ctx->total == 0) return 1000;
    return (unique * 1000) / (ctx->total + unique);
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C547: Should produce output");
    assert!(rust_code.contains("fn ppm_predict"), "C547: Should contain ppm_predict function");
    Ok(())
}

/// C548: Range encoder -- encode symbols using range subdivision
#[test]
fn c548_range_encoder() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;

typedef struct {
    uint32_t low;
    uint32_t range;
    uint8_t output[1024];
    int out_pos;
    int symbols_encoded;
} range_enc_t;

void range_enc_init(range_enc_t *enc) {
    int i;
    enc->low = 0;
    enc->range = 0xFFFFFFFF;
    enc->out_pos = 0;
    enc->symbols_encoded = 0;
    for (i = 0; i < 1024; i++) {
        enc->output[i] = 0;
    }
}

void range_enc_emit_byte(range_enc_t *enc) {
    if (enc->out_pos < 1024) {
        enc->output[enc->out_pos] = (uint8_t)(enc->low >> 24);
        enc->out_pos++;
    }
    enc->low <<= 8;
    enc->range <<= 8;
}

void range_enc_encode(range_enc_t *enc, uint32_t cum_freq, uint32_t freq, uint32_t total) {
    enc->range = enc->range / total;
    enc->low += cum_freq * enc->range;
    enc->range = freq * enc->range;
    while (enc->range < 0x01000000) {
        range_enc_emit_byte(enc);
    }
    enc->symbols_encoded++;
}

void range_enc_flush(range_enc_t *enc) {
    int i;
    for (i = 0; i < 4; i++) {
        range_enc_emit_byte(enc);
    }
}

int range_enc_output_size(const range_enc_t *enc) {
    return enc->out_pos;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C548: Should produce output");
    assert!(
        rust_code.contains("fn range_enc_encode"),
        "C548: Should contain range_enc_encode function"
    );
    Ok(())
}

/// C549: Entropy calculation (Shannon entropy) -- measure information content
#[test]
fn c549_shannon_entropy() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef unsigned char uint8_t;

typedef struct {
    int counts[256];
    int total;
} entropy_ctx_t;

void entropy_init(entropy_ctx_t *ctx) {
    int i;
    ctx->total = 0;
    for (i = 0; i < 256; i++) {
        ctx->counts[i] = 0;
    }
}

void entropy_add_data(entropy_ctx_t *ctx, const uint8_t *data, int len) {
    int i;
    for (i = 0; i < len; i++) {
        ctx->counts[data[i]]++;
        ctx->total++;
    }
}

int entropy_unique_symbols(const entropy_ctx_t *ctx) {
    int count = 0;
    int i;
    for (i = 0; i < 256; i++) {
        if (ctx->counts[i] > 0) count++;
    }
    return count;
}

int entropy_most_frequent(const entropy_ctx_t *ctx) {
    int best = 0;
    int best_count = 0;
    int i;
    for (i = 0; i < 256; i++) {
        if (ctx->counts[i] > best_count) {
            best_count = ctx->counts[i];
            best = i;
        }
    }
    return best;
}

int entropy_compute_scaled(const entropy_ctx_t *ctx) {
    int result = 0;
    int i;
    if (ctx->total == 0) return 0;
    for (i = 0; i < 256; i++) {
        if (ctx->counts[i] == 0) continue;
        int freq_pct = (ctx->counts[i] * 1000) / ctx->total;
        if (freq_pct <= 0) continue;
        int log_approx = 0;
        int tmp = freq_pct;
        while (tmp > 1) {
            tmp >>= 1;
            log_approx++;
        }
        result += freq_pct * log_approx;
    }
    return result;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C549: Should produce output");
    assert!(
        rust_code.contains("fn entropy_compute_scaled"),
        "C549: Should contain entropy_compute_scaled function"
    );
    Ok(())
}

/// C550: JPEG quantization matrix -- apply DCT coefficient quantization
#[test]
fn c550_jpeg_quantization_matrix() -> Result<(), Box<dyn std::error::Error>> {
    let c_code = r#"
typedef struct {
    int table[64];
    int quality;
} jpeg_qtable_t;

static const int jpeg_std_luma_qt[64] = {
    16, 11, 10, 16, 24, 40, 51, 61,
    12, 12, 14, 19, 26, 58, 60, 55,
    14, 13, 16, 24, 40, 57, 69, 56,
    14, 17, 22, 29, 51, 87, 80, 62,
    18, 22, 37, 56, 68,109,103, 77,
    24, 35, 55, 64, 81,104,113, 92,
    49, 64, 78, 87,103,121,120,101,
    72, 92, 95, 98,112,100,103, 99
};

void jpeg_qt_init(jpeg_qtable_t *qt, int quality) {
    int scale;
    int i;
    if (quality < 1) quality = 1;
    if (quality > 100) quality = 100;
    if (quality < 50) {
        scale = 5000 / quality;
    } else {
        scale = 200 - quality * 2;
    }
    qt->quality = quality;
    for (i = 0; i < 64; i++) {
        int val = (jpeg_std_luma_qt[i] * scale + 50) / 100;
        if (val < 1) val = 1;
        if (val > 255) val = 255;
        qt->table[i] = val;
    }
}

int jpeg_qt_quantize(const jpeg_qtable_t *qt, int coeff, int index) {
    if (index < 0 || index >= 64) return 0;
    int q = qt->table[index];
    if (q == 0) return 0;
    return (coeff + (q / 2)) / q;
}

int jpeg_qt_dequantize(const jpeg_qtable_t *qt, int qcoeff, int index) {
    if (index < 0 || index >= 64) return 0;
    return qcoeff * qt->table[index];
}

int jpeg_qt_count_zeros(const int *quantized, int len) {
    int count = 0;
    int i;
    for (i = 0; i < len && i < 64; i++) {
        if (quantized[i] == 0) count++;
    }
    return count;
}
"#;
    let rust_code = decy_core::transpile(c_code)?;
    assert!(!rust_code.is_empty(), "C550: Should produce output");
    assert!(rust_code.contains("fn jpeg_qt_init"), "C550: Should contain jpeg_qt_init function");
    Ok(())
}