fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
; fstat.asm — GNU-compatible 'stat' command (dev build)
; Full implementation for nasm -f elf64 + ld linking
;
; Register allocation:
;   r14d = argc, r15 = argv
;   ebx  = flags (bit 0 = -L/deref, bit 1 = -f/filesystem, bit 2 = -t/terse,
;                  bit 3 = -c/format given)
;   r12  = current file arg index
;   r13  = format string pointer (if -c/--format)

%define SYS_WRITE           1
%define SYS_STAT             4
%define SYS_LSTAT            6
%define SYS_EXIT            60
%define SYS_RT_SIGPROCMASK  14
%define SYS_READLINKAT     267
%define SYS_STATFS         137
%define SYS_STATX          332

%define STDOUT          1
%define STDERR          2
%define SIG_BLOCK       0
%define SIGPIPE        13
%define AT_FDCWD       -100
%define AT_SYMLINK_NOFOLLOW     0x100
%define AT_EMPTY_PATH           0x1000
%define STATX_BASIC_STATS       0x7FF
%define STATX_BTIME             0x800

; struct stat offsets (x86-64 Linux)
%define ST_DEV          0
%define ST_INO          8
%define ST_NLINK       16
%define ST_MODE        24
%define ST_UID         28
%define ST_GID         32
%define ST_PAD0        36
%define ST_RDEV        40
%define ST_SIZE        48
%define ST_BLKSIZE     56
%define ST_BLOCKS      64
%define ST_ATIME       72
%define ST_ATIME_NSEC  80
%define ST_MTIME       88
%define ST_MTIME_NSEC  96
%define ST_CTIME      104
%define ST_CTIME_NSEC 112

; struct statx offsets
%define STATX_MASK              0
%define STATX_BTIME_SEC         80
%define STATX_BTIME_NSEC        88

; struct statfs offsets (x86-64)
%define SF_TYPE         0
%define SF_BSIZE        8
%define SF_BLOCKS      16
%define SF_BFREE       24
%define SF_BAVAIL      32
%define SF_FILES       40
%define SF_FFREE       48
%define SF_FSID        56
%define SF_NAMELEN     64

; File type masks
%define S_IFMT   0xF000
%define S_IFSOCK 0xC000
%define S_IFLNK  0xA000
%define S_IFREG  0x8000
%define S_IFBLK  0x6000
%define S_IFDIR  0x4000
%define S_IFCHR  0x2000
%define S_IFIFO  0x1000

; Flag bits
%define FLAG_DEREF   0
%define FLAG_FS      1
%define FLAG_TERSE   2
%define FLAG_FORMAT  3

section .bss
stat_buf:    resb 144
statfs_buf:  resb 256
num_buf:     resb 64
out_buf:     resb 8192
link_buf:    resb 4096
mode_buf:    resb 16
out_pos:     resq 1
statx_buf:   resb 256

section .data

str_help:
    db "Usage: stat [OPTION]... FILE...", 10
    db "Display file or file system status.", 10, 10
    db "  -L, --dereference     follow links", 10
    db "  -f, --file-system     display file system status instead of file status", 10
    db "  -c  --format=FORMAT   use the specified FORMAT instead of the default;", 10
    db "                          output a newline after each use of FORMAT", 10
    db "      --printf=FORMAT   like --format, but interpret backslash escapes,", 10
    db "                          and do not output a mandatory trailing newline;", 10
    db "                          if you want a newline, include \n in FORMAT", 10
    db "  -t, --terse           print the information in terse form", 10
    db "      --help        display this help and exit", 10
    db "      --version     output version information and exit", 10, 10
    db "GNU coreutils online help: <https://www.gnu.org/software/coreutils/>", 10
    db "Full documentation <https://www.gnu.org/software/coreutils/stat>", 10
    db "or available locally via: info '(coreutils) stat invocation'", 10
str_help_len equ $ - str_help

str_version:
    db "stat (GNU coreutils) 9.7", 10
    db "Copyright (C) 2025 Free Software Foundation, Inc.", 10
    db "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.", 10
    db "This is free software: you are free to change and redistribute it.", 10
    db "There is NO WARRANTY, to the extent permitted by law.", 10, 10
    db "Written by Michael Meskes.", 10
str_version_len equ $ - str_version

str_prefix:      db "stat: "
str_prefix_len   equ $ - str_prefix
str_unrecog:     db "unrecognized option '"
str_unrecog_len  equ $ - str_unrecog
str_invalid:     db "invalid option -- '"
str_invalid_len  equ $ - str_invalid
str_missing:     db "missing operand", 10
str_missing_len  equ $ - str_missing
str_missing_fmt: db "option requires an argument -- 'c'", 10
str_missing_fmt_len equ $ - str_missing_fmt
str_sq_nl:       db "'", 10
str_sq:          db "'"
str_try:         db "Try 'stat --help' for more information.", 10
str_try_len      equ $ - str_try
str_cannot_stat: db "cannot stat '"
str_cannot_stat_len equ $ - str_cannot_stat

str_newline:     db 10
str_space:       db ' '
str_slash:       db '/'
str_lparen:      db '('
str_rparen:      db ')'
str_dash:        db '-'
str_zero:        db '0'
str_512:         db "512"

str_help_flag:    db "--help", 0
str_version_flag: db "--version", 0
str_deref_flag:   db "--dereference", 0
str_filesys_flag: db "--file-system", 0
str_terse_flag:   db "--terse", 0
str_format_eq:    db "--format=", 0
str_printf_eq:    db "--printf=", 0

; Default output labels
str_file_label:    db "  File: "
str_file_label_len equ $ - str_file_label
str_arrow:         db " -> "
str_arrow_len      equ $ - str_arrow
str_size_label:    db "  Size: "
str_size_label_len equ $ - str_size_label
str_blocks_label:  db 9, "Blocks: "
str_blocks_label_len equ $ - str_blocks_label
str_ioblock_label: db "  IO Block: "
str_ioblock_label_len equ $ - str_ioblock_label
str_type_sep:      db "  "
str_type_sep_len   equ $ - str_type_sep
str_device_label:  db "Device: "
str_device_label_len equ $ - str_device_label
str_inode_label:   db 9, "Inode: "
str_inode_label_len equ $ - str_inode_label
str_links_label:   db "  Links: "
str_links_label_len equ $ - str_links_label
str_access_label:  db "Access: "
str_access_label_len equ $ - str_access_label
str_uid_label:     db "  Uid: ( "
str_uid_label_len  equ $ - str_uid_label
str_uid_sep:       db "/    ?)"
str_uid_sep_len    equ $ - str_uid_sep
str_gid_label:     db "   Gid: ( "
str_gid_label_len  equ $ - str_gid_label
str_gid_sep:       db "/    ?)"
str_gid_sep_len    equ $ - str_gid_sep
str_access_ts:     db "Access: "
str_access_ts_len  equ $ - str_access_ts
str_modify_ts:     db "Modify: "
str_modify_ts_len  equ $ - str_modify_ts
str_change_ts:     db "Change: "
str_change_ts_len  equ $ - str_change_ts
str_birth_ts:      db " Birth: -"
str_birth_ts_len   equ $ - str_birth_ts
str_birth_label:   db " Birth: "
str_birth_label_len equ $ - str_birth_label

; File type strings
str_type_reg:      db "regular file"
str_type_reg_len   equ $ - str_type_reg
str_type_reg_empty: db "regular empty file"
str_type_reg_empty_len equ $ - str_type_reg_empty
str_type_dir:      db "directory"
str_type_dir_len   equ $ - str_type_dir
str_type_lnk:      db "symbolic link"
str_type_lnk_len   equ $ - str_type_lnk
str_type_chr:      db "character special file"
str_type_chr_len   equ $ - str_type_chr
str_type_blk:      db "block special file"
str_type_blk_len   equ $ - str_type_blk
str_type_fifo:     db "fifo"
str_type_fifo_len  equ $ - str_type_fifo
str_type_sock:     db "socket"
str_type_sock_len  equ $ - str_type_sock
str_type_unknown:  db "weird file"
str_type_unknown_len equ $ - str_type_unknown

; Filesystem labels
str_fs_file_label:    db "  File: "
str_fs_file_label_len equ $ - str_fs_file_label
str_fs_id_label:      db "    ID: "
str_fs_id_label_len   equ $ - str_fs_id_label
str_fs_namelen_label: db " Namelen: "
str_fs_namelen_label_len equ $ - str_fs_namelen_label
str_fs_type_label:    db " Type: "
str_fs_type_label_len equ $ - str_fs_type_label
str_fs_bsize_label:   db "Block size: "
str_fs_bsize_label_len equ $ - str_fs_bsize_label
str_fs_blocks_label:  db "   Blocks: Total: "
str_fs_blocks_label_len equ $ - str_fs_blocks_label
str_fs_bfree_label:   db "      Free: "
str_fs_bfree_label_len equ $ - str_fs_bfree_label
str_fs_bavail_label:  db "      Available: "
str_fs_bavail_label_len equ $ - str_fs_bavail_label
str_fs_files_label:   db "Inodes: Total: "
str_fs_files_label_len equ $ - str_fs_files_label
str_fs_ffree_label:   db "      Free: "
str_fs_ffree_label_len equ $ - str_fs_ffree_label

; Error message parts
str_err_noent:     db ": No such file or directory", 10
str_err_noent_len  equ $ - str_err_noent
str_err_acces:     db ": Permission denied", 10
str_err_acces_len  equ $ - str_err_acces
str_err_notdir:    db ": Not a directory", 10
str_err_notdir_len equ $ - str_err_notdir

section .text
global _start

_start:
    ; Block SIGPIPE
    sub     rsp, 16
    mov     qword [rsp], 0
    bts     qword [rsp], SIGPIPE
    mov     eax, SYS_RT_SIGPROCMASK
    mov     edi, SIG_BLOCK
    mov     rsi, rsp
    xor     edx, edx
    mov     r10d, 8
    syscall
    add     rsp, 16

    ; Save argc/argv
    mov     r14d, [rsp]
    lea     r15, [rsp + 8]

    ; Initialize
    xor     ebx, ebx            ; flags
    xor     r13d, r13d          ; format ptr
    mov     ecx, 1              ; arg index

; Parse options
.parse_opts:
    cmp     ecx, r14d
    jge     .err_missing_operand
    mov     rdi, [r15 + rcx*8]
    cmp     byte [rdi], '-'
    jne     .done_opts
    cmp     byte [rdi + 1], 0
    je      .done_opts
    cmp     byte [rdi + 1], '-'
    je      .check_long

    ; Short options
    inc     rdi
.short_loop:
    movzx   eax, byte [rdi]
    test    al, al
    jz      .next_opt
    cmp     al, 'L'
    je      .set_deref
    cmp     al, 'f'
    je      .set_fs
    cmp     al, 't'
    je      .set_terse
    cmp     al, 'c'
    je      .set_format
    ; Invalid short flag
    mov     r13, rdi
    mov     rsi, str_prefix
    mov     edx, str_prefix_len
    call    do_write_err
    mov     rsi, str_invalid
    mov     edx, str_invalid_len
    call    do_write_err
    mov     rsi, r13
    mov     edx, 1
    call    do_write_err
    mov     rsi, str_sq_nl
    mov     edx, 2
    call    do_write_err
    mov     rsi, str_try
    mov     edx, str_try_len
    call    do_write_err
    mov     edi, 1
    jmp     do_exit

.set_deref:
    or      bl, (1 << FLAG_DEREF)
    inc     rdi
    jmp     .short_loop

.set_fs:
    or      bl, (1 << FLAG_FS)
    inc     rdi
    jmp     .short_loop

.set_terse:
    or      bl, (1 << FLAG_TERSE)
    inc     rdi
    jmp     .short_loop

.set_format:
    or      bl, (1 << FLAG_FORMAT)
    inc     rdi
    ; Check if rest of short opt string is the format
    cmp     byte [rdi], 0
    jne     .format_inline
    ; Next arg is the format string
    inc     ecx
    cmp     ecx, r14d
    jge     .err_missing_format
    mov     r13, [r15 + rcx*8]
    inc     ecx
    jmp     .parse_opts

.format_inline:
    mov     r13, rdi
    jmp     .next_opt

.check_long:
    cmp     byte [rdi + 2], 0
    je      .double_dash
    push    rcx
    mov     r9, rdi
    ; --help
    mov     rsi, str_help_flag
    call    str_eq
    test    eax, eax
    jnz     .pop_show_help
    ; --version
    mov     rdi, r9
    mov     rsi, str_version_flag
    call    str_eq
    test    eax, eax
    jnz     .pop_show_version
    ; --dereference
    mov     rdi, r9
    mov     rsi, str_deref_flag
    call    str_eq
    test    eax, eax
    jnz     .pop_set_deref
    ; --file-system
    mov     rdi, r9
    mov     rsi, str_filesys_flag
    call    str_eq
    test    eax, eax
    jnz     .pop_set_fs
    ; --terse
    mov     rdi, r9
    mov     rsi, str_terse_flag
    call    str_eq
    test    eax, eax
    jnz     .pop_set_terse
    ; --format=...
    mov     rdi, r9
    mov     rsi, str_format_eq
    mov     edx, 9
    call    str_prefix_match
    test    eax, eax
    jnz     .pop_set_format_long
    ; --printf=...
    mov     rdi, r9
    mov     rsi, str_printf_eq
    mov     edx, 9
    call    str_prefix_match
    test    eax, eax
    jnz     .pop_set_printf_long
    ; Unrecognized
    mov     rsi, str_prefix
    mov     edx, str_prefix_len
    call    do_write_err
    mov     rsi, str_unrecog
    mov     edx, str_unrecog_len
    call    do_write_err
    mov     rdi, r9
    call    str_len
    mov     edx, eax
    mov     rsi, r9
    call    do_write_err
    mov     rsi, str_sq_nl
    mov     edx, 2
    call    do_write_err
    mov     rsi, str_try
    mov     edx, str_try_len
    call    do_write_err
    pop     rcx
    mov     edi, 1
    jmp     do_exit

.pop_show_help:
    pop     rcx
    mov     edi, STDOUT
    mov     rsi, str_help
    mov     edx, str_help_len
    call    do_write
    xor     edi, edi
    jmp     do_exit

.pop_show_version:
    pop     rcx
    mov     edi, STDOUT
    mov     rsi, str_version
    mov     edx, str_version_len
    call    do_write
    xor     edi, edi
    jmp     do_exit

.pop_set_deref:
    pop     rcx
    or      bl, (1 << FLAG_DEREF)
    inc     ecx
    jmp     .parse_opts

.pop_set_fs:
    pop     rcx
    or      bl, (1 << FLAG_FS)
    inc     ecx
    jmp     .parse_opts

.pop_set_terse:
    pop     rcx
    or      bl, (1 << FLAG_TERSE)
    inc     ecx
    jmp     .parse_opts

.pop_set_format_long:
    pop     rcx
    or      bl, (1 << FLAG_FORMAT)
    lea     r13, [r9 + 9]      ; skip "--format="
    inc     ecx
    jmp     .parse_opts

.pop_set_printf_long:
    pop     rcx
    or      bl, (1 << FLAG_FORMAT)
    lea     r13, [r9 + 9]      ; skip "--printf="
    inc     ecx
    jmp     .parse_opts

.double_dash:
    inc     ecx
    jmp     .done_opts

.next_opt:
    inc     ecx
    jmp     .parse_opts

.done_opts:
    cmp     ecx, r14d
    jge     .err_missing_operand

    ; Process files
    mov     r12d, ecx
    xor     ebp, ebp            ; exit code

.file_loop:
    cmp     r12d, r14d
    jge     .exit_done
    mov     rdi, [r15 + r12*8]

    ; Reset output buffer
    mov     qword [rel out_pos], 0

    test    bl, (1 << FLAG_FS)
    jnz     .do_statfs

    ; stat or lstat the file
    test    bl, (1 << FLAG_DEREF)
    jnz     .do_stat_deref

    ; lstat (default: don't follow symlinks)
    mov     eax, SYS_LSTAT
    lea     rsi, [rel stat_buf]
    syscall
    test    rax, rax
    js      .stat_failed
    ; rdi still has filename
    ; Call statx with AT_SYMLINK_NOFOLLOW to get birth time
    mov     rsi, rdi                ; rsi = pathname
    mov     eax, SYS_STATX
    mov     edi, AT_FDCWD           ; dirfd
    mov     edx, AT_SYMLINK_NOFOLLOW ; flags
    mov     r10d, STATX_BASIC_STATS | STATX_BTIME  ; mask
    lea     r8, [rel statx_buf]     ; statxbuf
    syscall
    ; If statx fails, zero out birth time
    test    rax, rax
    jns     .stat_ok
    mov     qword [rel statx_buf + STATX_BTIME_SEC], 0
    mov     dword [rel statx_buf + STATX_BTIME_NSEC], 0
    jmp     .stat_ok

.do_stat_deref:
    mov     eax, SYS_STAT
    lea     rsi, [rel stat_buf]
    syscall
    test    rax, rax
    js      .stat_failed
    ; rdi still has filename
    ; Call statx without AT_SYMLINK_NOFOLLOW (follow symlinks) to get birth time
    mov     rsi, rdi                ; rsi = pathname
    mov     eax, SYS_STATX
    mov     edi, AT_FDCWD           ; dirfd
    xor     edx, edx               ; flags = 0 (follow symlinks)
    mov     r10d, STATX_BASIC_STATS | STATX_BTIME  ; mask
    lea     r8, [rel statx_buf]     ; statxbuf
    syscall
    ; If statx fails, zero out birth time
    test    rax, rax
    jns     .stat_ok
    mov     qword [rel statx_buf + STATX_BTIME_SEC], 0
    mov     dword [rel statx_buf + STATX_BTIME_NSEC], 0
    jmp     .stat_ok

.do_statfs:
    mov     eax, SYS_STATFS
    lea     rsi, [rel statfs_buf]
    syscall
    test    rax, rax
    js      .stat_failed

    ; Output filesystem info
    test    bl, (1 << FLAG_FORMAT)
    jnz     .format_fs_custom
    test    bl, (1 << FLAG_TERSE)
    jnz     .terse_fs_output
    jmp     .default_fs_output

.stat_ok:
    test    bl, (1 << FLAG_FORMAT)
    jnz     .format_custom
    test    bl, (1 << FLAG_TERSE)
    jnz     .terse_output
    jmp     .default_output

.stat_failed:
    ; Print error: "stat: cannot stat 'file': No such file or directory"
    neg     rax
    mov     r9, rax             ; save errno
    mov     rsi, str_prefix
    mov     edx, str_prefix_len
    call    do_write_err
    mov     rsi, str_cannot_stat
    mov     edx, str_cannot_stat_len
    call    do_write_err
    ; Print the filename in quotes
    mov     rsi, str_sq
    mov     edx, 1
    call    do_write_err
    mov     rdi, [r15 + r12*8]
    call    str_len
    mov     edx, eax
    mov     rsi, rdi
    call    do_write_err
    mov     rsi, str_sq_nl
    mov     edx, 2
    call    do_write_err
    ; Pick error message based on errno
    cmp     r9d, 2              ; ENOENT
    je      .stat_err_noent
    cmp     r9d, 13             ; EACCES
    je      .stat_err_acces
    cmp     r9d, 20             ; ENOTDIR
    je      .stat_err_notdir
    ; Default: No such file or directory
.stat_err_noent:
    jmp     .stat_err_done
.stat_err_acces:
    jmp     .stat_err_done
.stat_err_notdir:
.stat_err_done:
    mov     ebp, 1
    inc     r12d
    jmp     .file_loop

; ============================================================
; Default output format (like GNU stat)
; ============================================================
.default_output:
    push    r12
    mov     r12, [r15 + r12*8]  ; filename ptr

    ; "  File: " + filename
    mov     rsi, str_file_label
    mov     edx, str_file_label_len
    call    buf_write
    mov     rdi, r12
    call    str_len
    mov     edx, eax
    mov     rsi, r12
    call    buf_write

    ; Check if symlink, append " -> target"
    movzx   eax, word [rel stat_buf + ST_MODE]
    and     eax, S_IFMT
    cmp     eax, S_IFLNK
    jne     .def_after_link

    mov     rsi, str_arrow
    mov     edx, str_arrow_len
    call    buf_write
    ; readlinkat to get target
    mov     eax, SYS_READLINKAT
    mov     edi, AT_FDCWD
    mov     rsi, r12
    lea     rdx, [rel link_buf]
    mov     r10d, 4095
    syscall
    test    rax, rax
    js      .def_after_link
    mov     byte [rel link_buf + rax], 0
    mov     edx, eax
    lea     rsi, [rel link_buf]
    call    buf_write

.def_after_link:
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; "  Size: N  Blocks: N  IO Block: N  type"
    mov     rsi, str_size_label
    mov     edx, str_size_label_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_SIZE]
    call    format_u64
    call    buf_write

    mov     rsi, str_blocks_label
    mov     edx, str_blocks_label_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_BLOCKS]
    call    format_u64
    call    buf_write

    mov     rsi, str_ioblock_label
    mov     edx, str_ioblock_label_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_BLKSIZE]
    call    format_u64
    call    buf_write

    ; File type
    mov     rsi, str_type_sep
    mov     edx, str_type_sep_len
    call    buf_write
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    get_file_type_str
    call    buf_write

    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; "Device: Xh/Yd  Inode: N  Links: N"
    mov     rsi, str_device_label
    mov     edx, str_device_label_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_DEV]
    call    format_dev_hex
    call    buf_write

    mov     rsi, str_inode_label
    mov     edx, str_inode_label_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_INO]
    call    format_u64
    call    buf_write

    mov     rsi, str_links_label
    mov     edx, str_links_label_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_NLINK]
    call    format_u64
    call    buf_write

    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; "Access: (OCTAL/PERMS)  Uid: ( N/ name)  Gid: ( N/ name)"
    mov     rsi, str_access_label
    mov     edx, str_access_label_len
    call    buf_write
    mov     rsi, str_lparen
    mov     edx, 1
    call    buf_write
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    format_octal_mode
    call    buf_write
    mov     rsi, str_slash
    mov     edx, 1
    call    buf_write
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    format_rwx_mode
    call    buf_write
    mov     rsi, str_rparen
    mov     edx, 1
    call    buf_write

    mov     rsi, str_uid_label
    mov     edx, str_uid_label_len
    call    buf_write
    mov     edi, dword [rel stat_buf + ST_UID]
    call    format_u64_rdi
    call    buf_write
    mov     rsi, str_uid_sep
    mov     edx, str_uid_sep_len
    call    buf_write

    mov     rsi, str_gid_label
    mov     edx, str_gid_label_len
    call    buf_write
    mov     edi, dword [rel stat_buf + ST_GID]
    call    format_u64_rdi
    call    buf_write
    mov     rsi, str_gid_sep
    mov     edx, str_gid_sep_len
    call    buf_write

    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; "Access: timestamp"
    mov     rsi, str_access_ts
    mov     edx, str_access_ts_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_ATIME]
    mov     rsi, [rel stat_buf + ST_ATIME_NSEC]
    call    format_timestamp
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; "Modify: timestamp"
    mov     rsi, str_modify_ts
    mov     edx, str_modify_ts_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_MTIME]
    mov     rsi, [rel stat_buf + ST_MTIME_NSEC]
    call    format_timestamp
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; "Change: timestamp"
    mov     rsi, str_change_ts
    mov     edx, str_change_ts_len
    call    buf_write
    mov     rdi, [rel stat_buf + ST_CTIME]
    mov     rsi, [rel stat_buf + ST_CTIME_NSEC]
    call    format_timestamp
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; " Birth: <timestamp>" or " Birth: -"
    mov     rdi, [rel statx_buf + STATX_BTIME_SEC]
    test    rdi, rdi
    jz      .def_birth_unknown
    ; Have birth time
    mov     rsi, str_birth_label
    mov     edx, str_birth_label_len
    call    buf_write
    mov     rdi, [rel statx_buf + STATX_BTIME_SEC]
    mov     esi, dword [rel statx_buf + STATX_BTIME_NSEC]
    call    format_timestamp
    call    buf_write
    jmp     .def_birth_done
.def_birth_unknown:
    mov     rsi, str_birth_ts
    mov     edx, str_birth_ts_len
    call    buf_write
.def_birth_done:
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; Flush output buffer
    call    buf_flush

    pop     r12
    inc     r12d
    jmp     .file_loop

; ============================================================
; Terse output: %n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o
; ============================================================
.terse_output:
    push    r12
    mov     r12, [r15 + r12*8]

    ; name
    mov     rdi, r12
    call    str_len
    mov     edx, eax
    mov     rsi, r12
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; size
    mov     rdi, [rel stat_buf + ST_SIZE]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; blocks
    mov     rdi, [rel stat_buf + ST_BLOCKS]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; raw mode hex
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    format_hex_lower
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; uid
    mov     edi, dword [rel stat_buf + ST_UID]
    call    format_u64_rdi
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; gid
    mov     edi, dword [rel stat_buf + ST_GID]
    call    format_u64_rdi
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; device hex
    mov     rdi, [rel stat_buf + ST_DEV]
    call    format_hex_lower_full
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; inode
    mov     rdi, [rel stat_buf + ST_INO]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; links
    mov     rdi, [rel stat_buf + ST_NLINK]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; major hex
    mov     rdi, [rel stat_buf + ST_RDEV]
    shr     rdi, 8
    and     edi, 0xFFF
    call    format_hex_lower
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; minor hex
    mov     rdi, [rel stat_buf + ST_RDEV]
    mov     rax, rdi
    and     edi, 0xFF
    shr     rax, 12
    and     eax, 0xFFF00
    or      edi, eax
    call    format_hex_lower
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; atime epoch
    mov     rdi, [rel stat_buf + ST_ATIME]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; mtime epoch
    mov     rdi, [rel stat_buf + ST_MTIME]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; ctime epoch
    mov     rdi, [rel stat_buf + ST_CTIME]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; birth epoch (from statx, 0 if unavailable)
    mov     rdi, [rel statx_buf + STATX_BTIME_SEC]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; IO block size
    mov     rdi, [rel stat_buf + ST_BLKSIZE]
    call    format_u64
    call    buf_write

    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write
    call    buf_flush

    pop     r12
    inc     r12d
    jmp     .file_loop

; ============================================================
; Custom format output (-c FORMAT)
; ============================================================
.format_custom:
    push    r12
    mov     r12, [r15 + r12*8]  ; filename
    mov     rdi, r13            ; format string

.fc_loop:
    movzx   eax, byte [rdi]
    test    al, al
    jz      .fc_done
    cmp     al, '%'
    je      .fc_percent
    cmp     al, '\'
    je      .fc_escape
    ; Regular char
    lea     rcx, [rel num_buf]
    mov     byte [rcx], al
    push    rdi
    mov     rsi, rcx
    mov     edx, 1
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_escape:
    inc     rdi
    movzx   eax, byte [rdi]
    test    al, al
    jz      .fc_done
    cmp     al, 'n'
    je      .fc_esc_nl
    cmp     al, 't'
    je      .fc_esc_tab
    cmp     al, '\'
    je      .fc_esc_bslash
    ; Unknown escape - output literally
    lea     rcx, [rel num_buf]
    mov     byte [rcx], '\'
    push    rdi
    mov     rsi, rcx
    mov     edx, 1
    call    buf_write
    pop     rdi
    jmp     .fc_loop

.fc_esc_nl:
    push    rdi
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_esc_tab:
    push    rdi
    lea     rcx, [rel num_buf]
    mov     byte [rcx], 9
    mov     rsi, rcx
    mov     edx, 1
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_esc_bslash:
    push    rdi
    lea     rcx, [rel num_buf]
    mov     byte [rcx], '\'
    mov     rsi, rcx
    mov     edx, 1
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_percent:
    inc     rdi
    movzx   eax, byte [rdi]
    cmp     al, '%'
    je      .fc_pct_literal
    cmp     al, 'a'
    je      .fc_pct_a
    cmp     al, 'A'
    je      .fc_pct_A
    cmp     al, 'b'
    je      .fc_pct_b
    cmp     al, 'B'
    je      .fc_pct_B
    cmp     al, 'd'
    je      .fc_pct_d
    cmp     al, 'D'
    je      .fc_pct_D
    cmp     al, 'f'
    je      .fc_pct_f
    cmp     al, 'F'
    je      .fc_pct_F
    cmp     al, 'g'
    je      .fc_pct_g
    cmp     al, 'G'
    je      .fc_pct_G
    cmp     al, 'h'
    je      .fc_pct_h
    cmp     al, 'i'
    je      .fc_pct_i
    cmp     al, 'n'
    je      .fc_pct_n
    cmp     al, 'N'
    je      .fc_pct_N
    cmp     al, 'o'
    je      .fc_pct_o
    cmp     al, 's'
    je      .fc_pct_s
    cmp     al, 't'
    je      .fc_pct_t
    cmp     al, 'T'
    je      .fc_pct_T
    cmp     al, 'u'
    je      .fc_pct_u
    cmp     al, 'U'
    je      .fc_pct_U
    cmp     al, 'w'
    je      .fc_pct_w
    cmp     al, 'W'
    je      .fc_pct_W
    cmp     al, 'x'
    je      .fc_pct_x
    cmp     al, 'X'
    je      .fc_pct_X
    cmp     al, 'y'
    je      .fc_pct_y
    cmp     al, 'Y'
    je      .fc_pct_Y
    cmp     al, 'z'
    je      .fc_pct_z
    cmp     al, 'Z'
    je      .fc_pct_Z
    ; Unknown %spec - output %X literally
    lea     rcx, [rel num_buf]
    mov     byte [rcx], '%'
    mov     byte [rcx + 1], al
    push    rdi
    mov     rsi, rcx
    mov     edx, 2
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_literal:
    push    rdi
    lea     rcx, [rel num_buf]
    mov     byte [rcx], '%'
    mov     rsi, rcx
    mov     edx, 1
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_a:  ; octal mode
    push    rdi
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    format_octal_mode
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_A:  ; rwx mode
    push    rdi
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    format_rwx_mode
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_b:  ; blocks
    push    rdi
    mov     rdi, [rel stat_buf + ST_BLOCKS]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_B:  ; block size (512)
    push    rdi
    mov     rsi, str_512
    mov     edx, 3
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_d:  ; device decimal
    push    rdi
    mov     rdi, [rel stat_buf + ST_DEV]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_D:  ; device hex
    push    rdi
    mov     rdi, [rel stat_buf + ST_DEV]
    call    format_hex_lower_full
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_f:  ; raw mode hex
    push    rdi
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    format_hex_lower
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_F:  ; file type
    push    rdi
    movzx   edi, word [rel stat_buf + ST_MODE]
    call    get_file_type_str
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_g:  ; gid
    push    rdi
    mov     edi, dword [rel stat_buf + ST_GID]
    call    format_u64_rdi
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_G:  ; group name (just numeric for assembly)
    push    rdi
    mov     edi, dword [rel stat_buf + ST_GID]
    call    format_u64_rdi
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_h:  ; nlinks
    push    rdi
    mov     rdi, [rel stat_buf + ST_NLINK]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_i:  ; inode
    push    rdi
    mov     rdi, [rel stat_buf + ST_INO]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_n:  ; name
    push    rdi
    mov     rdi, r12
    call    str_len
    mov     edx, eax
    mov     rsi, r12
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_N:  ; quoted name (with link target for symlinks)
    push    rdi
    ; Print quoted name
    mov     rsi, str_sq
    mov     edx, 1
    call    buf_write
    mov     rdi, r12
    call    str_len
    mov     edx, eax
    mov     rsi, r12
    call    buf_write
    mov     rsi, str_sq
    mov     edx, 1
    call    buf_write
    ; Check if symlink
    movzx   eax, word [rel stat_buf + ST_MODE]
    and     eax, S_IFMT
    cmp     eax, S_IFLNK
    jne     .fc_pct_N_done
    mov     rsi, str_arrow
    mov     edx, str_arrow_len
    call    buf_write
    mov     rsi, str_sq
    mov     edx, 1
    call    buf_write
    ; readlinkat
    mov     eax, SYS_READLINKAT
    mov     edi, AT_FDCWD
    mov     rsi, r12
    lea     rdx, [rel link_buf]
    mov     r10d, 4095
    syscall
    test    rax, rax
    js      .fc_pct_N_done
    mov     byte [rel link_buf + rax], 0
    mov     edx, eax
    lea     rsi, [rel link_buf]
    call    buf_write
    mov     rsi, str_sq
    mov     edx, 1
    call    buf_write
.fc_pct_N_done:
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_o:  ; IO block
    push    rdi
    mov     rdi, [rel stat_buf + ST_BLKSIZE]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_s:  ; size
    push    rdi
    mov     rdi, [rel stat_buf + ST_SIZE]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_t:  ; major hex
    push    rdi
    mov     rdi, [rel stat_buf + ST_RDEV]
    shr     rdi, 8
    and     edi, 0xFFF
    call    format_hex_lower
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_T:  ; minor hex
    push    rdi
    mov     rdi, [rel stat_buf + ST_RDEV]
    mov     rax, rdi
    and     edi, 0xFF
    shr     rax, 12
    and     eax, 0xFFF00
    or      edi, eax
    call    format_hex_lower
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_u:  ; uid
    push    rdi
    mov     edi, dword [rel stat_buf + ST_UID]
    call    format_u64_rdi
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_U:  ; user name (just numeric)
    push    rdi
    mov     edi, dword [rel stat_buf + ST_UID]
    call    format_u64_rdi
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_w:  ; birth time (formatted timestamp or "-")
    push    rdi
    mov     rdi, [rel statx_buf + STATX_BTIME_SEC]
    test    rdi, rdi
    jz      .fc_pct_w_unknown
    mov     esi, dword [rel statx_buf + STATX_BTIME_NSEC]
    call    format_timestamp
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop
.fc_pct_w_unknown:
    mov     rsi, str_dash
    mov     edx, 1
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_W:  ; birth epoch (seconds or 0)
    push    rdi
    mov     rdi, [rel statx_buf + STATX_BTIME_SEC]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_x:  ; access time
    push    rdi
    mov     rdi, [rel stat_buf + ST_ATIME]
    mov     rsi, [rel stat_buf + ST_ATIME_NSEC]
    call    format_timestamp
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_X:  ; access epoch
    push    rdi
    mov     rdi, [rel stat_buf + ST_ATIME]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_y:  ; modify time
    push    rdi
    mov     rdi, [rel stat_buf + ST_MTIME]
    mov     rsi, [rel stat_buf + ST_MTIME_NSEC]
    call    format_timestamp
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_Y:  ; modify epoch
    push    rdi
    mov     rdi, [rel stat_buf + ST_MTIME]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_z:  ; change time
    push    rdi
    mov     rdi, [rel stat_buf + ST_CTIME]
    mov     rsi, [rel stat_buf + ST_CTIME_NSEC]
    call    format_timestamp
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_pct_Z:  ; change epoch
    push    rdi
    mov     rdi, [rel stat_buf + ST_CTIME]
    call    format_u64
    call    buf_write
    pop     rdi
    inc     rdi
    jmp     .fc_loop

.fc_done:
    ; Flush and add newline for -c (not --printf)
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write
    call    buf_flush
    pop     r12
    inc     r12d
    jmp     .file_loop

; ============================================================
; Default filesystem output
; ============================================================
.default_fs_output:
    push    r12
    mov     r12, [r15 + r12*8]

    mov     rsi, str_fs_file_label
    mov     edx, str_fs_file_label_len
    call    buf_write
    mov     rdi, r12
    call    str_len
    mov     edx, eax
    mov     rsi, r12
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; ID / Namelen / Type
    mov     rsi, str_fs_id_label
    mov     edx, str_fs_id_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_FSID]
    call    format_hex_lower_full
    call    buf_write
    mov     rsi, str_fs_namelen_label
    mov     edx, str_fs_namelen_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_NAMELEN]
    call    format_u64
    call    buf_write
    mov     rsi, str_fs_type_label
    mov     edx, str_fs_type_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_TYPE]
    call    format_hex_lower
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; Block size / Blocks / Free / Available
    mov     rsi, str_fs_bsize_label
    mov     edx, str_fs_bsize_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_BSIZE]
    call    format_u64
    call    buf_write
    mov     rsi, str_fs_blocks_label
    mov     edx, str_fs_blocks_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_BLOCKS]
    call    format_u64
    call    buf_write
    mov     rsi, str_fs_bfree_label
    mov     edx, str_fs_bfree_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_BFREE]
    call    format_u64
    call    buf_write
    mov     rsi, str_fs_bavail_label
    mov     edx, str_fs_bavail_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_BAVAIL]
    call    format_u64
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    ; Inodes / Free inodes
    mov     rsi, str_fs_files_label
    mov     edx, str_fs_files_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_FILES]
    call    format_u64
    call    buf_write
    mov     rsi, str_fs_ffree_label
    mov     edx, str_fs_ffree_label_len
    call    buf_write
    mov     rdi, [rel statfs_buf + SF_FFREE]
    call    format_u64
    call    buf_write
    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write

    call    buf_flush
    pop     r12
    inc     r12d
    jmp     .file_loop

; ============================================================
; Terse filesystem output
; ============================================================
.terse_fs_output:
    push    r12
    mov     r12, [r15 + r12*8]

    ; name
    mov     rdi, r12
    call    str_len
    mov     edx, eax
    mov     rsi, r12
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; fs id hex
    mov     rdi, [rel statfs_buf + SF_FSID]
    call    format_hex_lower_full
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; namelen
    mov     rdi, [rel statfs_buf + SF_NAMELEN]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; type hex
    mov     rdi, [rel statfs_buf + SF_TYPE]
    call    format_hex_lower
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; bsize
    mov     rdi, [rel statfs_buf + SF_BSIZE]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; blocks
    mov     rdi, [rel statfs_buf + SF_BLOCKS]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; bfree
    mov     rdi, [rel statfs_buf + SF_BFREE]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; bavail
    mov     rdi, [rel statfs_buf + SF_BAVAIL]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; files
    mov     rdi, [rel statfs_buf + SF_FILES]
    call    format_u64
    call    buf_write
    mov     rsi, str_space
    mov     edx, 1
    call    buf_write

    ; ffree
    mov     rdi, [rel statfs_buf + SF_FFREE]
    call    format_u64
    call    buf_write

    mov     rsi, str_newline
    mov     edx, 1
    call    buf_write
    call    buf_flush

    pop     r12
    inc     r12d
    jmp     .file_loop

; Filesystem custom format stub
.format_fs_custom:
    jmp     .terse_fs_output

.exit_done:
    mov     edi, ebp
    jmp     do_exit

.err_missing_operand:
    mov     rsi, str_prefix
    mov     edx, str_prefix_len
    call    do_write_err
    mov     rsi, str_missing
    mov     edx, str_missing_len
    call    do_write_err
    mov     rsi, str_try
    mov     edx, str_try_len
    call    do_write_err
    mov     edi, 1
    jmp     do_exit

.err_missing_format:
    mov     rsi, str_prefix
    mov     edx, str_prefix_len
    call    do_write_err
    mov     rsi, str_missing_fmt
    mov     edx, str_missing_fmt_len
    call    do_write_err
    mov     edi, 1
    jmp     do_exit

; ============================================================
; Output buffer routines
; ============================================================
buf_write:
    ; rsi = data, edx = length
    push    rdi
    push    rcx
    mov     rcx, [rel out_pos]
    ; Copy to out_buf + rcx
    xor     eax, eax
.bw_loop:
    cmp     eax, edx
    jge     .bw_done
    movzx   r8d, byte [rsi + rax]
    lea     r9, [rel out_buf]
    mov     byte [r9 + rcx], r8b
    inc     rcx
    inc     eax
    ; Flush if buffer near full
    cmp     ecx, 8000
    jl      .bw_loop
    mov     [rel out_pos], rcx
    push    rsi
    push    rdx
    push    rax
    call    buf_flush
    pop     rax
    pop     rdx
    pop     rsi
    xor     ecx, ecx
    jmp     .bw_loop
.bw_done:
    mov     [rel out_pos], rcx
    pop     rcx
    pop     rdi
    ret

buf_flush:
    mov     rcx, [rel out_pos]
    test    rcx, rcx
    jz      .bf_done
    mov     edi, STDOUT
    lea     rsi, [rel out_buf]
    mov     edx, ecx
    call    do_write
    mov     qword [rel out_pos], 0
.bf_done:
    ret

; ============================================================
; Number formatting
; ============================================================

; format_u64: format rdi as unsigned decimal
; Returns: rsi = start, edx = length
format_u64:
    mov     rax, rdi
    lea     rcx, [rel num_buf + 63]
    mov     byte [rcx], 0
    mov     r8d, 10
.fu_loop:
    xor     edx, edx
    div     r8
    add     dl, '0'
    dec     rcx
    mov     byte [rcx], dl
    test    rax, rax
    jnz     .fu_loop
    mov     rsi, rcx
    lea     rax, [rel num_buf + 63]
    sub     rax, rcx
    mov     edx, eax
    ret

; format_u64_rdi: same but for 32-bit values zero-extended
format_u64_rdi:
    mov     edi, edi            ; zero-extend edi to rdi
    jmp     format_u64

; format_hex_lower: format edi as lowercase hex (no leading zeros, no 0x prefix)
format_hex_lower:
    mov     eax, edi
    lea     rcx, [rel num_buf + 63]
    mov     byte [rcx], 0
    test    eax, eax
    jnz     .fhl_loop
    dec     rcx
    mov     byte [rcx], '0'
    jmp     .fhl_done
.fhl_loop:
    test    eax, eax
    jz      .fhl_done
    mov     edx, eax
    and     edx, 0xF
    cmp     edx, 10
    jl      .fhl_digit
    add     dl, ('a' - 10)
    jmp     .fhl_store
.fhl_digit:
    add     dl, '0'
.fhl_store:
    dec     rcx
    mov     byte [rcx], dl
    shr     eax, 4
    jmp     .fhl_loop
.fhl_done:
    mov     rsi, rcx
    lea     rax, [rel num_buf + 63]
    sub     rax, rcx
    mov     edx, eax
    ret

; format_hex_lower_full: format rdi as lowercase hex (no leading zeros)
format_hex_lower_full:
    mov     rax, rdi
    lea     rcx, [rel num_buf + 63]
    mov     byte [rcx], 0
    test    rax, rax
    jnz     .fhlf_loop
    dec     rcx
    mov     byte [rcx], '0'
    jmp     .fhlf_done
.fhlf_loop:
    test    rax, rax
    jz      .fhlf_done
    mov     edx, eax
    and     edx, 0xF
    cmp     edx, 10
    jl      .fhlf_digit
    add     dl, ('a' - 10)
    jmp     .fhlf_store
.fhlf_digit:
    add     dl, '0'
.fhlf_store:
    dec     rcx
    mov     byte [rcx], dl
    shr     rax, 4
    jmp     .fhlf_loop
.fhlf_done:
    mov     rsi, rcx
    lea     rax, [rel num_buf + 63]
    sub     rax, rcx
    mov     edx, eax
    ret

; format_octal_mode: format lower 12 bits of edi as octal (no leading zeros)
; Returns: rsi = start, edx = length
format_octal_mode:
    and     edi, 0xFFF
    mov     eax, edi
    lea     rcx, [rel num_buf + 10]
    mov     byte [rcx], 0
    ; Handle zero specially
    test    eax, eax
    jnz     .fom_loop
    dec     rcx
    mov     byte [rcx], '0'
    jmp     .fom_done
.fom_loop:
    test    eax, eax
    jz      .fom_done
    mov     edx, eax
    and     edx, 7
    add     dl, '0'
    dec     rcx
    mov     byte [rcx], dl
    shr     eax, 3
    jmp     .fom_loop
.fom_done:
    mov     rsi, rcx
    lea     rax, [rel num_buf + 10]
    sub     rax, rcx
    mov     edx, eax
    ret

; format_rwx_mode: format mode as drwxrwxrwx string (10 chars)
; Input: edi = mode, Returns: rsi = mode_buf, edx = 10
format_rwx_mode:
    push    rdi
    ; First char: file type
    mov     eax, edi
    and     eax, S_IFMT
    lea     r8, [rel mode_buf]
    cmp     eax, S_IFDIR
    je      .frm_dir
    cmp     eax, S_IFLNK
    je      .frm_lnk
    cmp     eax, S_IFCHR
    je      .frm_chr
    cmp     eax, S_IFBLK
    je      .frm_blk
    cmp     eax, S_IFIFO
    je      .frm_fifo
    cmp     eax, S_IFSOCK
    je      .frm_sock
    mov     byte [r8], '-'
    jmp     .frm_perms
.frm_dir:
    mov     byte [r8], 'd'
    jmp     .frm_perms
.frm_lnk:
    mov     byte [r8], 'l'
    jmp     .frm_perms
.frm_chr:
    mov     byte [r8], 'c'
    jmp     .frm_perms
.frm_blk:
    mov     byte [r8], 'b'
    jmp     .frm_perms
.frm_fifo:
    mov     byte [r8], 'p'
    jmp     .frm_perms
.frm_sock:
    mov     byte [r8], 's'
    jmp     .frm_perms

.frm_perms:
    pop     rdi
    lea     r8, [rel mode_buf]
    ; Owner rwx
    mov     byte [r8 + 1], '-'
    test    edi, 0x100
    jz      .frm_p2
    mov     byte [r8 + 1], 'r'
.frm_p2:
    mov     byte [r8 + 2], '-'
    test    edi, 0x80
    jz      .frm_p3
    mov     byte [r8 + 2], 'w'
.frm_p3:
    mov     byte [r8 + 3], '-'
    test    edi, 0x40
    jz      .frm_p3s
    mov     byte [r8 + 3], 'x'
.frm_p3s:
    test    edi, 0x800           ; setuid
    jz      .frm_p4
    test    edi, 0x40
    jz      .frm_p3S
    mov     byte [r8 + 3], 's'
    jmp     .frm_p4
.frm_p3S:
    mov     byte [r8 + 3], 'S'

.frm_p4:
    ; Group rwx
    mov     byte [r8 + 4], '-'
    test    edi, 0x20
    jz      .frm_p5
    mov     byte [r8 + 4], 'r'
.frm_p5:
    mov     byte [r8 + 5], '-'
    test    edi, 0x10
    jz      .frm_p6
    mov     byte [r8 + 5], 'w'
.frm_p6:
    mov     byte [r8 + 6], '-'
    test    edi, 0x8
    jz      .frm_p6s
    mov     byte [r8 + 6], 'x'
.frm_p6s:
    test    edi, 0x400           ; setgid
    jz      .frm_p7
    test    edi, 0x8
    jz      .frm_p6S
    mov     byte [r8 + 6], 's'
    jmp     .frm_p7
.frm_p6S:
    mov     byte [r8 + 6], 'S'

.frm_p7:
    ; Other rwx
    mov     byte [r8 + 7], '-'
    test    edi, 0x4
    jz      .frm_p8
    mov     byte [r8 + 7], 'r'
.frm_p8:
    mov     byte [r8 + 8], '-'
    test    edi, 0x2
    jz      .frm_p9
    mov     byte [r8 + 8], 'w'
.frm_p9:
    mov     byte [r8 + 9], '-'
    test    edi, 0x1
    jz      .frm_p9t
    mov     byte [r8 + 9], 'x'
.frm_p9t:
    test    edi, 0x200           ; sticky
    jz      .frm_done
    test    edi, 0x1
    jz      .frm_p9T
    mov     byte [r8 + 9], 't'
    jmp     .frm_done
.frm_p9T:
    mov     byte [r8 + 9], 'T'

.frm_done:
    lea     rsi, [rel mode_buf]
    mov     edx, 10
    ret

; get_file_type_str: return file type string
; Input: edi = mode, Returns: rsi = string, edx = length
get_file_type_str:
    and     edi, S_IFMT
    cmp     edi, S_IFREG
    je      .gft_reg
    cmp     edi, S_IFDIR
    je      .gft_dir
    cmp     edi, S_IFLNK
    je      .gft_lnk
    cmp     edi, S_IFCHR
    je      .gft_chr
    cmp     edi, S_IFBLK
    je      .gft_blk
    cmp     edi, S_IFIFO
    je      .gft_fifo
    cmp     edi, S_IFSOCK
    je      .gft_sock
    mov     rsi, str_type_unknown
    mov     edx, str_type_unknown_len
    ret
.gft_reg:
    ; Check if file size is 0 -> "regular empty file"
    cmp     qword [rel stat_buf + ST_SIZE], 0
    jne     .gft_reg_nonempty
    mov     rsi, str_type_reg_empty
    mov     edx, str_type_reg_empty_len
    ret
.gft_reg_nonempty:
    mov     rsi, str_type_reg
    mov     edx, str_type_reg_len
    ret
.gft_dir:
    mov     rsi, str_type_dir
    mov     edx, str_type_dir_len
    ret
.gft_lnk:
    mov     rsi, str_type_lnk
    mov     edx, str_type_lnk_len
    ret
.gft_chr:
    mov     rsi, str_type_chr
    mov     edx, str_type_chr_len
    ret
.gft_blk:
    mov     rsi, str_type_blk
    mov     edx, str_type_blk_len
    ret
.gft_fifo:
    mov     rsi, str_type_fifo
    mov     edx, str_type_fifo_len
    ret
.gft_sock:
    mov     rsi, str_type_sock
    mov     edx, str_type_sock_len
    ret

; format_dev_hex: format device number as "Xh/Yd"
; Input: rdi = st_dev, Returns: rsi, edx
format_dev_hex:
    push    rdi
    call    format_hex_lower_full
    push    rsi
    push    rdx
    ; Write hex part to num_buf+20 area
    lea     rdi, [rel num_buf + 20]
    pop     rcx                 ; hex len
    pop     rsi                 ; hex ptr
    xor     eax, eax
.fdh_copy:
    cmp     eax, ecx
    jge     .fdh_hex_done
    movzx   r8d, byte [rsi + rax]
    mov     byte [rdi + rax], r8b
    inc     eax
    jmp     .fdh_copy
.fdh_hex_done:
    mov     byte [rdi + rax], 'h'
    inc     eax
    mov     rsi, rdi
    mov     edx, eax
    pop     rdi
    ret

; format_timestamp: format epoch time as "YYYY-MM-DD HH:MM:SS.NNNNNNNNN +0000"
; Input: rdi = epoch seconds, rsi = nanoseconds
; Returns: rsi = start, edx = length
format_timestamp:
    push    rbp
    push    rbx
    push    r12
    push    r13
    push    r14
    mov     r12, rdi            ; epoch
    mov     r13, rsi            ; nsec

    ; Compute date from epoch (days since 1970-01-01)
    mov     rax, r12
    xor     edx, edx
    mov     rcx, 86400
    div     rcx
    mov     rbx, rax            ; days since epoch
    mov     r14, rdx            ; remaining seconds

    ; Civil date from days (algorithm from Howard Hinnant)
    add     rax, 719468
    mov     rbp, rax

    ; era = floor(days / 146097)
    xor     edx, edx
    mov     rcx, 146097
    cmp     rbp, 0
    jge     .ts_pos_era
    sub     rbp, 146096
.ts_pos_era:
    mov     rax, rbp
    xor     edx, edx
    div     rcx
    mov     r8, rax             ; era
    ; doe = days - era * 146097
    imul    rax, rcx
    mov     r9, rbp
    sub     r9, rax             ; doe

    ; yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365
    mov     rax, r9
    xor     edx, edx
    mov     rcx, 1460
    div     rcx
    mov     r10, rax

    mov     rax, r9
    xor     edx, edx
    mov     rcx, 36524
    div     rcx
    mov     r11, rax

    mov     rax, r9
    xor     edx, edx
    mov     rcx, 146096
    div     rcx

    mov     rcx, r9
    sub     rcx, r10
    add     rcx, r11
    sub     rcx, rax
    mov     rax, rcx
    xor     edx, edx
    mov     rcx, 365
    div     rcx
    mov     r10, rax            ; yoe

    ; year = yoe + era * 400
    imul    r8, 400
    add     r10, r8             ; year (March-based)

    ; doy = doe - (365*yoe + yoe/4 - yoe/100)
    mov     rax, r10
    sub     rax, r8             ; back to yoe
    push    rax
    imul    rax, 365
    mov     r11, rax
    pop     rax
    push    rax
    xor     edx, edx
    mov     rcx, 4
    div     rcx
    add     r11, rax
    pop     rax
    xor     edx, edx
    mov     rcx, 100
    div     rcx
    sub     r11, rax
    mov     rax, r9
    sub     rax, r11            ; doy

    ; mp = (5*doy + 2) / 153
    imul    rcx, rax, 5
    add     rcx, 2
    push    rax
    mov     rax, rcx
    xor     edx, edx
    mov     rcx, 153
    div     rcx
    mov     r8, rax             ; mp
    pop     rax

    ; day = doy - (153*mp+2)/5 + 1
    imul    rcx, r8, 153
    add     rcx, 2
    push    rax
    mov     rax, rcx
    xor     edx, edx
    mov     rcx, 5
    div     rcx
    mov     r9, rax
    pop     rax
    sub     eax, r9d
    inc     eax
    mov     r9d, eax            ; day (1-31)

    ; month = mp + (mp < 10 ? 3 : -9)
    cmp     r8d, 10
    jl      .ts_m_lt10
    sub     r8d, 9
    jmp     .ts_m_done
.ts_m_lt10:
    add     r8d, 3
.ts_m_done:
    ; If month <= 2, year++
    cmp     r8d, 2
    jg      .ts_y_done
    inc     r10
.ts_y_done:

    ; Now format: YYYY-MM-DD HH:MM:SS.NNNNNNNNN +0000
    lea     rdi, [rel num_buf]
    ; Year (4 digits)
    mov     eax, r10d
    mov     ecx, 1000
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi], al
    mov     eax, edx
    mov     ecx, 100
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+1], al
    mov     eax, edx
    mov     ecx, 10
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+2], al
    add     dl, '0'
    mov     byte [rdi+3], dl
    mov     byte [rdi+4], '-'
    ; Month (2 digits)
    mov     eax, r8d
    mov     ecx, 10
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+5], al
    add     dl, '0'
    mov     byte [rdi+6], dl
    mov     byte [rdi+7], '-'
    ; Day (2 digits)
    mov     eax, r9d
    mov     ecx, 10
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+8], al
    add     dl, '0'
    mov     byte [rdi+9], dl
    mov     byte [rdi+10], ' '

    ; Time from remaining seconds
    mov     rax, r14
    xor     edx, edx
    mov     ecx, 3600
    div     ecx
    ; al = hour
    push    rdx
    mov     ecx, 10
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+11], al
    add     dl, '0'
    mov     byte [rdi+12], dl
    mov     byte [rdi+13], ':'
    pop     rax
    xor     edx, edx
    mov     ecx, 60
    div     ecx
    ; al = minute
    push    rdx
    mov     ecx, 10
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+14], al
    add     dl, '0'
    mov     byte [rdi+15], dl
    mov     byte [rdi+16], ':'
    pop     rax
    ; al = second
    mov     ecx, 10
    xor     edx, edx
    div     ecx
    add     al, '0'
    mov     byte [rdi+17], al
    add     dl, '0'
    mov     byte [rdi+18], dl
    mov     byte [rdi+19], '.'

    ; Nanoseconds (9 digits)
    mov     rax, r13
    mov     ecx, 9
    lea     r8, [rdi + 29]
.ts_ns_loop:
    xor     edx, edx
    push    rcx
    mov     rcx, 10
    div     rcx
    pop     rcx
    add     dl, '0'
    dec     r8
    mov     byte [r8], dl
    dec     ecx
    jnz     .ts_ns_loop

    ; " +0000"
    mov     byte [rdi+29], ' '
    mov     byte [rdi+30], '+'
    mov     byte [rdi+31], '0'
    mov     byte [rdi+32], '0'
    mov     byte [rdi+33], '0'
    mov     byte [rdi+34], '0'
    mov     byte [rdi+35], 0

    mov     rsi, rdi
    mov     edx, 35

    pop     r14
    pop     r13
    pop     r12
    pop     rbx
    pop     rbp
    ret

; ============================================================
; Utility functions
; ============================================================
do_write:
    mov     eax, SYS_WRITE
    syscall
    cmp     rax, -4
    je      do_write
    ret

do_write_err:
    mov     edi, STDERR
    jmp     do_write

do_exit:
    mov     eax, SYS_EXIT
    syscall

str_len:
    xor     eax, eax
.sl_loop:
    cmp     byte [rdi + rax], 0
    je      .sl_done
    inc     eax
    jmp     .sl_loop
.sl_done:
    ret

str_eq:
    xor     r8d, r8d
.se_loop:
    movzx   eax, byte [rdi + r8]
    movzx   edx, byte [rsi + r8]
    cmp     al, dl
    jne     .se_ne
    test    al, al
    jz      .se_eq
    inc     r8d
    jmp     .se_loop
.se_eq:
    mov     eax, 1
    ret
.se_ne:
    xor     eax, eax
    ret

str_prefix_match:
    xor     r8d, r8d
.sp_loop:
    cmp     r8d, edx
    jge     .sp_match
    movzx   eax, byte [rdi + r8]
    cmp     al, byte [rsi + r8]
    jne     .sp_nomatch
    inc     r8d
    jmp     .sp_loop
.sp_match:
    mov     eax, 1
    ret
.sp_nomatch:
    xor     eax, eax
    ret