{"name":"std/zstd.nu","source":"// stdlib/std/zstd.nu — Zstandard (RFC 8878) in pure NURL.\n//\n// This is the real format, not a subset: frames with or without a\n// declared content size, raw / RLE / compressed blocks, Huffman-coded\n// literals in one or four bitstreams (with a tree that is itself FSE-\n// compressed, or repeated from the previous block), FSE-coded sequences\n// in all four table modes, the three repeat offsets with their\n// literals-length-zero exception, skippable frames, multi-frame\n// concatenation, and the XXH64 content checksum. Output of `zstd -1`\n// through `zstd -19 --ultra` decodes here byte for byte.\n//\n// Why in the stdlib rather than behind an FFI: `ext/compress.nu` used to\n// reach libzstd, which made zstd unavailable wherever that library is\n// not — a freestanding build, a unikernel, wasm, or any machine without\n// libzstd-dev — and made the *whole* module fail to compile there. A\n// format is not a dependency.\n//\n//   ( zstd_decode       ( Vec u ) src )            → !( Vec u ) ZstdErr\n//   ( zstd_decode_limit ( Vec u ) src i max_out )  → !( Vec u ) ZstdErr\n//   ( zstd_content_size ( Vec u ) src )            → ?i\n//   ( zstd_err_name     ZstdErr e )                → s\n//\n// `zstd_decode` decodes every frame in `src` and concatenates the\n// results, which is what the `zstd` CLI does with a concatenated file.\n// `zstd_decode_limit` refuses at `max_out` bytes — use it on input you\n// did not produce, because a 100 kB frame can legally declare gigabytes.\n// `zstd_content_size` reads the first frame's declared size without\n// decoding anything; it is absent when the producer streamed the frame.\n//\n// Inputs are BORROWED. The returned Vec is OWNED — the caller's, dropped\n// with its owner.\n//\n// Not supported: dictionaries (`ZstdUnsupported` — a frame that names a\n// dictionary ID cannot be decoded without it, and silently producing\n// wrong bytes is worse than refusing).\n//\n// The compressor lives in the same module (see `zstd_encode`), so a\n// round trip needs nothing else.\n\n$ `stdlib/core/vec.nu`\n$ `stdlib/std/bytes.nu`\n$ `stdlib/std/hash_xxh64.nu`\n\n: | ZstdErr {\n    ZstdMagic  // not a Zstandard frame\n    ZstdCorrupt  // malformed or self-inconsistent data\n    ZstdTruncated  // input ended inside a frame\n    ZstdChecksum  // content checksum mismatch\n    ZstdUnsupported  // valid zstd this decoder cannot do (dictionaries)\n    ZstdTooLarge  // output would exceed the caller's limit\n}\n\n@ zstd_err_name ZstdErr e → s {\n    ^ ?? e {\n        ZstdMagic → `ZstdMagic`\n        ZstdCorrupt → `ZstdCorrupt`\n        ZstdTruncated → `ZstdTruncated`\n        ZstdChecksum → `ZstdChecksum`\n        ZstdUnsupported → `ZstdUnsupported`\n        ZstdTooLarge → `ZstdTooLarge`\n    }\n}\n\n// Internal error codes, mapped to ZstdErr on the way out.\n: i ZSE_CORRUPT 1\n: i ZSE_TRUNC 2\n: i ZSE_CKSUM 3\n: i ZSE_UNSUP 4\n: i ZSE_LARGE 5\n: i ZSE_MAGIC 6\n\n// Decode-side sequence statistics, for studying what an encoder chose:\n// zeroed by zstd_decode, read back with zstd_seq_stats. Scalars because\n// the language has no compound-typed globals.\n: ~ i g_zs_nseq 0\n: ~ i g_zs_sumll 0\n: ~ i g_zs_summl 0\n: ~ i g_zs_nrep 0\n\n// [0]=sequences [1]=literal bytes in sequences [2]=match bytes\n// [3]=sequences that rode a repeat offset (offbase 1..3)\n@ zstd_seq_stats → ( Vec i ) {\n    : ( Vec i ) v ( vec_new [i] )\n    ( vec_push [i] v g_zs_nseq )\n    ( vec_push [i] v g_zs_sumll )\n    ( vec_push [i] v g_zs_summl )\n    ( vec_push [i] v g_zs_nrep )\n    ^ v\n}\n\n: i ZS_MAGIC 0xFD2FB528\n: i ZS_SKIP_LO 0x184D2A50\n: i ZS_SKIP_HI 0x184D2A5F\n: i ZS_MAX_HUF_BITS 11  // Huffman codes are at most 11 bits\n: i ZS_MAX_SYMS 256\n\n@ __zs_err i code → ZstdErr {\n    ^ ? == code ZSE_TRUNC # ZstdErr ZstdTruncated\n    ? == code ZSE_CKSUM # ZstdErr ZstdChecksum\n    ? == code ZSE_UNSUP # ZstdErr ZstdUnsupported\n    ? == code ZSE_LARGE # ZstdErr ZstdTooLarge\n    ? == code ZSE_MAGIC # ZstdErr ZstdMagic\n    # ZstdErr ZstdCorrupt\n}\n\n// Index of the highest set bit, -1 for zero. floor(log2 x) for x > 0.\n@ __zs_hbit i x → i {\n    ? <= x 0 { ^ -1 } {}\n    ^ - 63 # i ( nurl_clz # u64 x )\n}\n\n@ __zs_zeros i n → ( Vec i ) {\n    : ( Vec i ) v ( vec_with_cap [i] ? > n 0 n 1 )\n    : ~ i k 0\n    ~ < k n { ( vec_push [i] v 0 ) = k + k 1 }\n    ^ v\n}\n\n// ── Decoder state ───────────────────────────────────────────────────\n//\n// One struct, a local of zstd_decode_limit that the helpers advance in\n// place (`inout`: NURL structs pass by value, an inout parameter is the\n// caller's storage). It owns its buffers and goes with that local.\n//\n// The entropy tables are allocated once at their maximum size and\n// rewritten in place: FSE accuracy is at most 9 bits (512 entries) and\n// a Huffman table at most 11 (2048), so a block that repeats the\n// previous tables costs no allocation at all.\n\n: ZsDec {\n    * u src  // input bytes (borrowed)\n    i srclen\n    i pos  // byte cursor into src\n    ( Vec u ) out  // output buffer, len kept at 0 until the end\n    * u op  // out's data pointer, refreshed on every grow\n    i olen  // bytes written so far\n    i ocap\n    i limit\n    i framestart  // olen at the start of the current frame\n    ( Vec u ) lit  // literals of the current block\n    i litlen\n    i litcap\n    i litpos  // read cursor while executing sequences\n    ( Vec i ) huf  // packed Huffman entries: sym | nbits<<8\n    i hufbits  // 0 = no table decoded yet\n    ( Vec i ) llt  // packed FSE entries: sym | nbits<<8 | newstate<<16\n    i lllog  // -1 = no table\n    ( Vec i ) oft\n    i oflog\n    ( Vec i ) mlt\n    i mllog\n    ( Vec i ) sc1  // scratch: normalized counts / weights\n    ( Vec i ) sc2  // scratch: per-symbol state descriptor / code lengths\n    ( Vec i ) sc3  // scratch: Huffman weight FSE table\n    i rep0\n    i rep1\n    i rep2\n    i window\n    i err\n}\n\n// A forward, little-endian bit reader — headers and FSE distributions.\n: ZsFwd {\n    * u src\n    i len\n    i bit  // bit cursor from the start of src\n    i err\n}\n\n// A backward bit reader — every FSE and Huffman payload. `off` is a bit\n// cursor that walks down to zero and is allowed to go negative: the\n// last few bits of a stream are defined to read as zeros.\n: ZsBk {\n    * u src\n    i len\n    i off\n}\n\n// Read `n` bits little-endian starting at bit `off`. The caller\n// guarantees the range is inside the buffer.\n@ __zs_rd_le * u src i n i off → i {\n    : ~ i bytepos >> off 3\n    : ~ i bitoff & off 7\n    : ~ i res 0\n    : ~ i shift 0\n    : ~ i left n\n    ~ > left 0 {\n        : i mask ? >= left 8 255 - << 1 left 1\n        : i byte # i . src bytepos\n        = res + res << & >> byte bitoff mask shift\n        = shift + shift - 8 bitoff\n        = left - left - 8 bitoff\n        = bitoff 0\n        = bytepos + bytepos 1\n    }\n    ^ res\n}\n\n@ __zs_fw_take inout ZsFwd f i n → i {\n    ? == n 0 { ^ 0 } {}\n    ? > + . f bit n * . f len 8 { = . f err 1 ^ 0 } {}\n    : i v ( __zs_rd_le . f src n . f bit )\n    = . f bit + . f bit n\n    ^ v\n}\n\n@ __zs_fw_rewind inout ZsFwd f i n → v { = . f bit - . f bit n }\n\n@ __zs_fw_align inout ZsFwd f → v { = . f bit << >> + . f bit 7 3 3 }\n\n@ __zs_fw_bytepos inout ZsFwd f → i { ^ >> . f bit 3 }\n\n// Read `n` bits from the end of a backward stream. Bits before the\n// start of the buffer read as zero, as the format requires.\n// The same read as `__zs_bk_take`, but WITHOUT the cursor struct: the\n// caller keeps `off` in a local and passes it in. That is the whole\n// optimisation — a `* ZsBk` field is memory the compiler must reload\n// after every call, and this bit cursor is touched six to eight times\n// per sequence. `off` may be negative; the bits before the start of the\n// stream read as zero, as the format requires.\n// The rare halves of `__zs_bits`, moved out of it: the last few bytes\n// of a stream, where an eight-byte load would run off the end, and the\n// bits before its start, which the format defines as zeros. Splitting\n// them out keeps the hot function small enough to be inlined, which is\n// worth more than either branch costs.\n@ __zs_bits_edge * u src i len i off i n → i {\n    ? >= off 0 { ^ ( __zs_rd_le src n off ) } {}\n    : i have + n off\n    : ~ i r 0\n    ? > have 0 { = r ( __zs_rd_le src have 0 ) } {}\n    : i sh - 0 off\n    ^ ? >= sh 64 0 << r sh\n}\n\n// The same read as `__zs_bk_take`, but WITHOUT the cursor struct: the\n// caller keeps `off` in a local and passes it in. That is the whole\n// optimisation — a `* ZsBk` field is memory the compiler must reload\n// after every call, and this bit cursor is touched six to eight times\n// per sequence. `off` may be negative; the bits before the start of the\n// stream read as zero, as the format requires.\n//\n// Eight consecutive byte loads with constant shifts is a pattern LLVM\n// folds into ONE unaligned 64-bit load — the instruction this wants,\n// and one the language has no way to spell directly. Reads are at most\n// 32 bits, so 64 bits minus a 7-bit misalignment always covers them.\n@ __zs_bits * u src i len i off i n → i {\n    : i bytepos >> off 3\n    ? & >= off 0 <= + bytepos 8 len {\n        : i lo | | | # i . src bytepos\n        << # i . src + bytepos 1 8\n        << # i . src + bytepos 2 16\n        << # i . src + bytepos 3 24\n        : i hi | | | # i . src + bytepos 4\n        << # i . src + bytepos 5 8\n        << # i . src + bytepos 6 16\n        << # i . src + bytepos 7 24\n        : i word | lo << hi 32\n        ^ & >> # u64 word & off 7 - << 1 n 1\n    } {}\n    ? == n 0 { ^ 0 } {}\n    ^ ( __zs_bits_edge src len off n )\n}\n\n// The cursor-carrying form, for the paths where a struct is clearer\n// than threading an offset through: the Huffman streams and the weight\n// decoder, which read far fewer bits per call than the sequence loop.\n@ __zs_bk_take inout ZsBk b i n → i {\n    ? == n 0 { ^ 0 } {}\n    = . b off - . b off n\n    ^ ( __zs_bits . b src . b len . b off n )\n}\n\n// Position a backward reader at the last information bit: the final\n// byte carries a 1 bit marking the end, above 0..7 bits of padding.\n// A final byte of zero is therefore malformed.\n@ __zs_bk_start inout ZsBk b → i {\n    ? <= . b len 0 { ^ ZSE_CORRUPT } {}\n    : *u sp . b src\n    : i last # i . sp - . b len 1\n    ? == last 0 { ^ ZSE_CORRUPT } {}\n    : i padding - 8 ( __zs_hbit last )\n    = . b off - * . b len 8 padding\n    ^ 0\n}\n\n// ── FSE ─────────────────────────────────────────────────────────────\n\n// Read a normalized distribution. Returns the number of symbols, or -1.\n// The counts land in `freq`; a count of -1 means \"less than one\", a\n// symbol that still gets a cell.\n@ __zs_fse_header inout ZsFwd f ( Vec i ) freq i maxlog → i {\n    : i log + 5 ( __zs_fw_take f 4 )\n    ? > log maxlog { ^ -1 } {}\n    : *i fp ( vec_data [i] freq )\n    : ~ i remaining << 1 log\n    : ~ i symb 0\n    ~ & > remaining 0 < symb ZS_MAX_SYMS {\n        // The field is only as wide as the probability space left.\n        : i bits + 1 ( __zs_hbit + remaining 1 )\n        : i val ( __zs_fw_take f bits )\n        : i lower_mask - << 1 - bits 1 1\n        : i threshold - - << 1 bits 1 + remaining 1\n        : ~ i v val\n        ? < & val lower_mask threshold {\n            // Small values spend one bit less; give the top bit back.\n            ( __zs_fw_rewind f 1 )\n            = v & val lower_mask\n        } { ? > val lower_mask { = v - val threshold } {} }\n        : i proba - v 1\n        = remaining - remaining ? < proba 0 - 0 proba proba\n        = . fp symb proba\n        = symb + symb 1\n        ? == proba 0 {\n            // A zero count is followed by a 2-bit run length, chained\n            // while it reads 3.\n            : ~ i repeat ( __zs_fw_take f 2 )\n            : ~ b more T\n            ~ more {\n                : ~ i k 0\n                ~ & < k repeat < symb ZS_MAX_SYMS {\n                    = . fp symb 0\n                    = symb + symb 1\n                    = k + k 1\n                }\n                ? == repeat 3 { = repeat ( __zs_fw_take f 2 ) } { = more F }\n            }\n        } {}\n    }\n    ( __zs_fw_align f )\n    ? != . f err 0 { ^ -1 } {}\n    ? != remaining 0 { ^ -1 } {}\n    ? >= symb ZS_MAX_SYMS { ^ -1 } {}\n    ^ | symb << log 16\n}\n\n// Turn a normalized distribution into a decoding table. `tab` holds\n// 1<<log packed entries. Returns 0, or ZSE_CORRUPT.\n@ __zs_fse_build ( Vec i ) tab ( Vec i ) freq ( Vec i ) sd0 i nsym i log → i {\n    : i size << 1 log\n    : *i tp ( vec_data [i] tab )\n    : *i fp ( vec_data [i] freq )\n    : *i sd ( vec_data [i] sd0 )\n    // \"Less than one\" symbols take one cell each from the top.\n    : ~ i high size\n    : ~ i s 0\n    ~ < s nsym {\n        ? == # i . fp s -1 {\n            = high - high 1\n            = . tp high s\n            = . sd s 1\n        } {}\n        = s + s 1\n    }\n    ? < high 0 { ^ ZSE_CORRUPT } {}\n    // Everything else is spread with a step coprime to the table size,\n    // skipping the cells already taken.\n    : i step + + >> size 1 >> size 3 3\n    : i mask - size 1\n    : ~ i pos 0\n    = s 0\n    ~ < s nsym {\n        : i c # i . fp s\n        ? > c 0 {\n            = . sd s c\n            : ~ i k 0\n            ~ < k c {\n                = . tp pos s\n                = pos & + pos step mask\n                ~ >= pos high { = pos & + pos step mask }\n                = k + k 1\n            }\n        } {}\n        = s + s 1\n    }\n    // The walk visits every cell exactly once, so it must come home.\n    ? != pos 0 { ^ ZSE_CORRUPT } {}\n    : ~ i u 0\n    ~ < u size {\n        : i sym # i . tp u\n        : i nx # i . sd sym\n        = . sd sym + nx 1\n        : i nb - log ( __zs_hbit nx )\n        : i ns - << nx nb size\n        = . tp u | | sym << nb 8 << ns 16\n        = u + u 1\n    }\n    ^ 0\n}\n\n// A table that always decodes one symbol and never reads a bit.\n@ __zs_fse_rle ( Vec i ) tab i sym → v {\n    : *i tp ( vec_data [i] tab )\n    = . tp 0 sym\n}\n\n// ── Huffman ─────────────────────────────────────────────────────────\n\n// Build a decoding table from per-symbol code lengths. Entries are\n// indexed by the next `max_bits` bits of the stream.\n@ __zs_huf_build ( Vec i ) tab ( Vec i ) bits0 i nsym → i {\n    : *i bp ( vec_data [i] bits0 )\n    : ~ i maxb 0\n    : ~ i s 0\n    // rank_count in a fixed 12-slot window; code lengths cannot exceed 11.\n    : ( Vec i ) rc ( __zs_zeros 16 )\n    : *i rcp ( vec_data [i] rc )\n    ~ < s nsym {\n        : i b # i . bp s\n        ? > b ZS_MAX_HUF_BITS { ^ -1 } {}\n        ? > b maxb { = maxb b } {}\n        = . rcp b + # i . rcp b 1\n        = s + s 1\n    }\n    ? == maxb 0 { ^ -1 } {}\n    : i size << 1 maxb\n    // Codes are handed out shortest-last: the longest codes (weight 1)\n    // sit at the bottom of the table.\n    : ( Vec i ) ri ( __zs_zeros 16 )\n    : *i rip ( vec_data [i] ri )\n    = . rip maxb 0\n    : ~ i i maxb\n    ~ >= i 1 {\n        = . rip - i 1 + # i . rip i * # i . rcp i << 1 - maxb i\n        = i - i 1\n    }\n    : b full == # i . rip 0 size\n    ? ! full { ^ -1 } {}\n    : *i tp ( vec_data [i] tab )\n    = s 0\n    ~ < s nsym {\n        : i b # i . bp s\n        ? != b 0 {\n            : i code # i . rip b\n            : i len << 1 - maxb b\n            : i packed | s << b 8\n            : ~ i k 0\n            ~ < k len { = . tp + code k packed = k + k 1 }\n            = . rip b + code len\n        } {}\n        = s + s 1\n    }\n    ^ maxb\n}\n\n// Weights → code lengths → table. The last symbol's weight is not\n// transmitted: it is whatever completes the next power of two.\n@ __zs_huf_from_weights ( Vec i ) tab ( Vec i ) wts ( Vec i ) bits0 i nsym → i {\n    : *i wp ( vec_data [i] wts )\n    : *i bp ( vec_data [i] bits0 )\n    : ~ i sum 0\n    : ~ i s 0\n    ~ < s nsym {\n        : i w # i . wp s\n        ? > w ZS_MAX_HUF_BITS { ^ -1 } {}\n        ? > w 0 { = sum + sum << 1 - w 1 } {}\n        = s + s 1\n    }\n    ? <= sum 0 { ^ -1 } {}\n    : i maxb + ( __zs_hbit sum ) 1\n    ? > maxb ZS_MAX_HUF_BITS { ^ -1 } {}\n    : i left - << 1 maxb sum\n    ? == left 0 { ^ -1 } {}\n    ? != & left - left 1 0 { ^ -1 } {}\n    : i last + ( __zs_hbit left ) 1\n    = s 0\n    ~ < s nsym {\n        : i w # i . wp s\n        = . bp s ? > w 0 - + maxb 1 w 0\n        = s + s 1\n    }\n    = . bp nsym - + maxb 1 last\n    ^ ( __zs_huf_build tab bits0 + nsym 1 )\n}\n\n// ── Output buffer ───────────────────────────────────────────────────\n\n@ __zs_need inout ZsDec d i n → v {\n    ? != . d err 0 { ^ v } {}\n    : i want + . d olen n\n    ? > want . d limit { = . d err ZSE_LARGE ^ v } {}\n    ? > want . d ocap {\n        : i twice * . d ocap 2\n        : i newcap ? > twice want twice want\n        ( vec_reserve [u] . d out newcap )\n        = . d op ( vec_data [u] . d out )\n        = . d ocap ( vec_cap [u] . d out )\n        ? < . d ocap want { = . d err ZSE_CORRUPT } {}\n    } {}\n}\n\n// ── Literals ────────────────────────────────────────────────────────\n\n@ __zs_lit_need inout ZsDec d i n → v {\n    ? > n . d litcap {\n        // The buffer's length stays 0 (writes go through its data\n        // pointer), so reserving `n` is growing it to `n`.\n        ( vec_reserve [u] . d lit n )\n        = . d litcap ( vec_cap [u] . d lit )\n        ? < . d litcap n { = . d err ZSE_CORRUPT } {}\n    } {}\n}\n\n// Decode one Huffman-coded bitstream into the literals buffer at\n// `at`. Returns the number of symbols, or -1.\n@ __zs_huf_stream inout ZsDec d i off i len i at i cap → i {\n    ? <= len 0 { ^ -1 } {}\n    : *u bsrc # *u + # i . d src off\n    : ~ ZsBk b @ ZsBk { bsrc len 0 }\n    ? != ( __zs_bk_start b ) 0 { ^ -1 } {}\n    : i maxb . d hufbits\n    : i mask - << 1 maxb 1\n    : *i tp ( vec_data [i] . d huf )\n    : *u lp ( vec_data [u] . d lit )\n    : ~ i state ( __zs_bk_take b maxb )\n    : ~ i n 0\n    : ~ b bad F\n    ~ & ! bad > . b off - 0 maxb {\n        : i e # i . tp state\n        ? >= + at n cap { = bad T } {\n            = . lp + at n # u & e 255\n            = n + n 1\n            : i nb & >> e 8 255\n            : i rest ( __zs_bk_take b nb )\n            = state & + << state nb rest mask\n        }\n    }\n    // A stream must land exactly on its own beginning.\n    ? != . b off - 0 maxb { = bad T } {}\n    ^ ? bad -1 n\n}\n\n// Decode the Huffman tree description. Returns bytes consumed, or -1.\n@ __zs_huf_table inout ZsDec d i off i len → i {\n    ? <= len 0 { ^ -1 } {}\n    : *u sp . d src\n    : i header # i . sp off\n    : *i wp ( vec_data [i] . d sc1 )\n    : ~ i nsym 0\n    : ~ i used 1\n    ? >= header 128 {\n        // Weights written straight out, two to a byte, high nibble first.\n        = nsym - header 127\n        : i nbytes >> + nsym 1 1\n        ? > + 1 nbytes len { ^ -1 } {}\n        : ~ i k 0\n        ~ < k nsym {\n            : i byte # i . sp + + off 1 >> k 1\n            = . wp k ? == & k 1 0 >> byte 4 & byte 15\n            = k + k 1\n        }\n        = used + 1 nbytes\n    } {\n        // Weights are themselves FSE-coded in the next `header` bytes.\n        ? > + 1 header len { ^ -1 } {}\n        : *u fsrc # *u + # i . d src + off 1\n        : ~ ZsFwd f @ ZsFwd { fsrc header 0 0 }\n        : i hdr ( __zs_fse_header f . d sc2 7 )\n        ? < hdr 0 { ^ -1 } {}\n        : i wsym & hdr 65535\n        : i wlog >> hdr 16\n        ? != ( __zs_fse_build . d sc3 . d sc2 . d sc1 wsym wlog ) 0 {\n            ^ -1\n        } {}\n        : i bp ( __zs_fw_bytepos f )\n        : i rem - header bp\n        ? <= rem 0 { ^ -1 } {}\n        // Two interleaved states share one table: state1 takes the even\n        // weights, state2 the odd ones.\n        : *u bsrc # *u + # i . d src + + off 1 bp\n        : ~ ZsBk b @ ZsBk { bsrc rem 0 }\n        ? != ( __zs_bk_start b ) 0 { ^ -1 } {}\n        : *i tp ( vec_data [i] . d sc3 )\n        : ~ i s1 ( __zs_bk_take b wlog )\n        : ~ i s2 ( __zs_bk_take b wlog )\n        : ~ i n 0\n        : ~ b done F\n        : ~ b bad F\n        ~ ! done {\n            ? >= + n 2 ZS_MAX_SYMS { = bad T = done T } {\n                : i e1 # i . tp s1\n                = . wp n & e1 255\n                = n + n 1\n                = s1 + >> e1 16 ( __zs_bk_take b & >> e1 8 255 )\n                ? < . b off 0 {\n                    = . wp n & # i . tp s2 255\n                    = n + n 1\n                    = done T\n                } {\n                    : i e2 # i . tp s2\n                    = . wp n & e2 255\n                    = n + n 1\n                    = s2 + >> e2 16 ( __zs_bk_take b & >> e2 8 255 )\n                    ? < . b off 0 {\n                        = . wp n & # i . tp s1 255\n                        = n + n 1\n                        = done T\n                    } {}\n                }\n            }\n        }\n        ? bad { ^ -1 } {}\n        = nsym n\n        = used + 1 header\n    }\n    ? | <= nsym 0 >= nsym ZS_MAX_SYMS { ^ -1 } {}\n    : i maxb ( __zs_huf_from_weights . d huf . d sc1 . d sc2 nsym )\n    ? < maxb 0 { ^ -1 } {}\n    = . d hufbits maxb\n    ^ used\n}\n\n// Literals section. Leaves the reader positioned after it.\n@ __zs_literals inout ZsDec d inout ZsFwd f → v {\n    : *u fsp . f src\n    : *u dsp . d src\n    : i btype ( __zs_fw_take f 2 )\n    : i sfmt ( __zs_fw_take f 2 )\n    ? != . f err 0 { = . d err ZSE_CORRUPT ^ v } {}\n    ? <= btype 1 {\n        // Raw or RLE: one size field, whose width the format bit selects.\n        : ~ i size 0\n        ? == sfmt 1 { = size ( __zs_fw_take f 12 ) }\n        { ? == sfmt 3 { = size ( __zs_fw_take f 20 ) }\n            { ( __zs_fw_rewind f 1 ) = size ( __zs_fw_take f 5 ) } }\n        ? != . f err 0 { = . d err ZSE_CORRUPT ^ v } {}\n        ( __zs_lit_need d ? > size 0 size 1 )\n        ? != . d err 0 { ^ v } {}\n        ( __zs_fw_align f )\n        : i at ( __zs_fw_bytepos f )\n        : *u lp ( vec_data [u] . d lit )\n        ? == btype 0 {\n            ? > + at size . f len { = . d err ZSE_CORRUPT ^ v } {}\n            ? > size 0 {\n                ( nurl_memcpy # s lp # s # *u + # i . f src at size )\n            } {}\n            = . f bit << + at size 3\n        } {\n            ? >= at . f len { = . d err ZSE_CORRUPT ^ v } {}\n            : i byte # i . fsp at\n            : ~ i k 0\n            ~ < k size { = . lp k # u byte = k + k 1 }\n            = . f bit << + at 1 3\n        }\n        = . d litlen size\n        ^ v\n    } {}\n    // Huffman-coded, with a fresh tree (2) or the previous one (3).\n    : ~ i nstreams 4\n    : ~ i regen 0\n    : ~ i csize 0\n    ? == sfmt 0 {\n        = nstreams 1\n        = regen ( __zs_fw_take f 10 )\n        = csize ( __zs_fw_take f 10 )\n    } { ? == sfmt 1 {\n            = regen ( __zs_fw_take f 10 )\n            = csize ( __zs_fw_take f 10 )\n        } { ? == sfmt 2 {\n                = regen ( __zs_fw_take f 14 )\n                = csize ( __zs_fw_take f 14 )\n            } {\n                = regen ( __zs_fw_take f 18 )\n                = csize ( __zs_fw_take f 18 )\n            } } }\n    ? != . f err 0 { = . d err ZSE_CORRUPT ^ v } {}\n    ( __zs_fw_align f )\n    : i at ( __zs_fw_bytepos f )\n    ? > + at csize . f len { = . d err ZSE_CORRUPT ^ v } {}\n    ( __zs_lit_need d ? > regen 0 regen 1 )\n    ? != . d err 0 { ^ v } {}\n    // The sub-stream is expressed as an absolute offset into src so the\n    // Huffman helpers can work without another reader.\n    : i base + . d pos at\n    : ~ i hoff base\n    : ~ i hlen csize\n    ? == btype 2 {\n        : i used ( __zs_huf_table d hoff hlen )\n        ? < used 0 { = . d err ZSE_CORRUPT ^ v } {}\n        = hoff + hoff used\n        = hlen - hlen used\n    } { ? == . d hufbits 0 { = . d err ZSE_CORRUPT ^ v } {} }\n    ? <= hlen 0 { = . d err ZSE_CORRUPT ^ v } {}\n    : ~ i total 0\n    ? == nstreams 1 {\n        = total ( __zs_huf_stream d hoff hlen 0 regen )\n    } {\n        ? < hlen 6 { = . d err ZSE_CORRUPT ^ v } {}\n        : i c1 | # i . dsp hoff << # i . dsp + hoff 1 8\n        : i c2 | # i . dsp + hoff 2 << # i . dsp + hoff 3 8\n        : i c3 | # i . dsp + hoff 4 << # i . dsp + hoff 5 8\n        : i c4 - - - - hlen 6 c1 c2 c3\n        ? <= c4 0 { = . d err ZSE_CORRUPT ^ v } {}\n        : i s1 ( __zs_huf_stream d + hoff 6 c1 0 regen )\n        ? < s1 0 { = . d err ZSE_CORRUPT ^ v } {}\n        : i s2 ( __zs_huf_stream d + + hoff 6 c1 c2 s1 regen )\n        ? < s2 0 { = . d err ZSE_CORRUPT ^ v } {}\n        : i s3 ( __zs_huf_stream d + + + hoff 6 c1 c2 c3 + s1 s2 regen )\n        ? < s3 0 { = . d err ZSE_CORRUPT ^ v } {}\n        : i s4 ( __zs_huf_stream d + + + + hoff 6 c1 c2 c3 c4 + + s1 s2 s3 regen )\n        ? < s4 0 { = . d err ZSE_CORRUPT ^ v } {}\n        = total + + + s1 s2 s3 s4\n    }\n    ? != total regen { = . d err ZSE_CORRUPT ^ v } {}\n    = . d litlen regen\n    = . f bit << + at csize 3\n}\n\n// ── Sequences ───────────────────────────────────────────────────────\n//\n// Only the extra-bit counts are tabulated: every baseline is the\n// previous baseline plus 1<<extra, which is how the format was built.\n\n@ __zs_pl * i p i at i a i b i c i d i e i f i g i h → i {\n    = . p at a\n    = . p + at 1 b\n    = . p + at 2 c\n    = . p + at 3 d\n    = . p + at 4 e\n    = . p + at 5 f\n    = . p + at 6 g\n    = . p + at 7 h\n    ^ + at 8\n}\n\n// Literals-length codes 0..35: 16 direct values, then widening fields.\n@ __zs_ll_extra → ( Vec i ) {\n    : ( Vec i ) v ( __zs_zeros 36 )\n    : *i p ( vec_data [i] v )\n    : ~ i at 16\n    = at ( __zs_pl p at 1 1 1 1 2 2 3 3 )\n    = at ( __zs_pl p at 4 6 7 8 9 10 11 12 )\n    = at ( __zs_pl p at 13 14 15 16 0 0 0 0 )\n    ^ v\n}\n\n// Match-length codes 0..52: 32 direct values (baseline code+3), then\n// the same widening shape.\n@ __zs_ml_extra → ( Vec i ) {\n    : ( Vec i ) v ( __zs_zeros 53 )\n    : *i p ( vec_data [i] v )\n    : ~ i at 32\n    = at ( __zs_pl p at 1 1 1 1 2 2 3 3 )\n    = at ( __zs_pl p at 4 4 5 7 8 9 10 11 )\n    = at ( __zs_pl p at 12 13 14 15 16 0 0 0 )\n    ^ v\n}\n\n@ __zs_ll_base ( Vec i ) extra → ( Vec i ) {\n    : ( Vec i ) v ( __zs_zeros 36 )\n    : *i p ( vec_data [i] v )\n    : *i e ( vec_data [i] extra )\n    : ~ i c 0\n    ~ < c 16 { = . p c c = c + c 1 }\n    ~ < c 36 {\n        = . p c + # i . p - c 1 << 1 # i . e - c 1\n        = c + c 1\n    }\n    ^ v\n}\n\n@ __zs_ml_base ( Vec i ) extra → ( Vec i ) {\n    : ( Vec i ) v ( __zs_zeros 53 )\n    : *i p ( vec_data [i] v )\n    : *i e ( vec_data [i] extra )\n    : ~ i c 0\n    ~ < c 32 { = . p c + c 3 = c + c 1 }\n    ~ < c 53 {\n        = . p c + # i . p - c 1 << 1 # i . e - c 1\n        = c + c 1\n    }\n    ^ v\n}\n\n@ __zs_ll_default ( Vec i ) freq → i {\n    : *i p ( vec_data [i] freq )\n    : ~ i at 0\n    = at ( __zs_pl p at 4 3 2 2 2 2 2 2 )\n    = at ( __zs_pl p at 2 2 2 2 2 1 1 1 )\n    = at ( __zs_pl p at 2 2 2 2 2 2 2 2 )\n    = at ( __zs_pl p at 2 3 2 1 1 1 1 1 )\n    = at ( __zs_pl p at -1 -1 -1 -1 0 0 0 0 )\n    ^ 36\n}\n\n@ __zs_of_default ( Vec i ) freq → i {\n    : *i p ( vec_data [i] freq )\n    : ~ i at 0\n    = at ( __zs_pl p at 1 1 1 1 1 1 2 2 )\n    = at ( __zs_pl p at 2 1 1 1 1 1 1 1 )\n    = at ( __zs_pl p at 1 1 1 1 1 1 1 1 )\n    = at ( __zs_pl p at -1 -1 -1 -1 -1 0 0 0 )\n    ^ 29\n}\n\n@ __zs_ml_default ( Vec i ) freq → i {\n    : *i p ( vec_data [i] freq )\n    : ~ i at 0\n    = at ( __zs_pl p at 1 4 3 2 2 2 2 2 )  // 0..7\n    = at ( __zs_pl p at 2 1 1 1 1 1 1 1 )  // 8..15\n    = at ( __zs_pl p at 1 1 1 1 1 1 1 1 )  // 16..23\n    = at ( __zs_pl p at 1 1 1 1 1 1 1 1 )  // 24..31\n    = at ( __zs_pl p at 1 1 1 1 1 1 1 1 )  // 32..39\n    = at ( __zs_pl p at 1 1 1 1 1 1 -1 -1 )  // 40..47\n    = at ( __zs_pl p at -1 -1 -1 -1 -1 0 0 0 )  // 48..52 (+ pad)\n    ^ 53\n}\n\n// One of the three sequence tables. `which`: 0 = literals lengths,\n// 1 = offsets, 2 = match lengths.\n@ __zs_seq_table inout ZsDec d inout ZsFwd f i which i mode → v {\n    : ( Vec i ) tab ? == which 0 . d llt ? == which 1 . d oft . d mlt\n    : i maxlog ? == which 1 8 9\n    : ~ i log -1\n    ? == mode 0 {\n        : i n ? == which 0 ( __zs_ll_default . d sc1 )\n        ? == which 1 ( __zs_of_default . d sc1 )\n        ( __zs_ml_default . d sc1 )\n        = log ? == which 1 5 6\n        ? != ( __zs_fse_build tab . d sc1 . d sc2 n log ) 0 {\n            = . d err ZSE_CORRUPT\n            ^ v\n        } {}\n    } { ? == mode 1 {\n            ( __zs_fw_align f )\n            : i at ( __zs_fw_bytepos f )\n            ? >= at . f len { = . d err ZSE_CORRUPT ^ v } {}\n            : *u fsp . f src\n            ( __zs_fse_rle tab # i . fsp at )\n            = . f bit << + at 1 3\n            = log 0\n        } { ? == mode 2 {\n                : i hdr ( __zs_fse_header f . d sc1 maxlog )\n                ? < hdr 0 { = . d err ZSE_CORRUPT ^ v } {}\n                : i n & hdr 65535\n                = log >> hdr 16\n                ? != ( __zs_fse_build tab . d sc1 . d sc2 n log ) 0 {\n                    = . d err ZSE_CORRUPT\n                    ^ v\n                } {}\n            } {\n                // Repeat: keep the table from the previous block.\n                : i prev ? == which 0 . d lllog ? == which 1 . d oflog . d mllog\n                ? < prev 0 { = . d err ZSE_CORRUPT ^ v } {}\n                = log prev\n            } } }\n    ? == which 0 { = . d lllog log }\n    { ? == which 1 { = . d oflog log } { = . d mllog log } }\n}\n\n// Copy `n` literals to the output, then a match of `ml` bytes from\n// `offset` back. Overlapping copies are byte by byte on purpose: an\n// offset of 1 is the format's run-length encoding.\n@ __zs_emit inout ZsDec d i ll i ml i offset → v {\n    ? > + . d litpos ll . d litlen { = . d err ZSE_CORRUPT ^ v } {}\n    ( __zs_need d + ll ml )\n    ? != . d err 0 { ^ v } {}\n    : *u op . d op\n    : *u lp ( vec_data [u] . d lit )\n    : ~ i o . d olen\n    ? > ll 0 {\n        ( nurl_memcpy # s # *u + # i op o\n        # s # *u + # i lp . d litpos ll )\n        = o + o ll\n        = . d litpos + . d litpos ll\n    } {}\n    // An offset may not reach behind this frame's own output, and may\n    // not exceed the window the frame's header promised — a decoder\n    // that keeps the whole frame in memory could serve the second case\n    // anyway, and would then accept a frame the format calls invalid\n    // and quietly hand back whatever those bytes happened to be.\n    ? | <= offset 0 > offset - o . d framestart { = . d err ZSE_CORRUPT ^ v } {}\n    ? & > . d window 0 > offset . d window { = . d err ZSE_CORRUPT ^ v } {}\n    // The match copy, in chunks of `offset` bytes. Each chunk reads\n    // only bytes written before it started, so no single memcpy ever\n    // overlaps — while the SEQUENCE of them still reproduces the\n    // format's run semantics, where an offset of 1 means \"repeat this\n    // byte\". A byte-at-a-time loop was a fifth of decode time.\n    : ~ i left ml\n    : ~ i dst o\n    ~ > left 0 {\n        : i chunk ? > offset left left offset\n        ( nurl_memcpy # s # *u + # i op dst\n        # s # *u + # i op - dst offset chunk )\n        = dst + dst chunk\n        = left - left chunk\n    }\n    = . d olen + o ml\n}\n\n// The offset code carries three recent offsets in slots 1..3; a\n// sequence with no literals shifts the meaning of each slot by one.\n@ __zs_offset inout ZsDec d i code i ll → i {\n    ? > code 3 {\n        : i off - code 3\n        = . d rep2 . d rep1\n        = . d rep1 . d rep0\n        = . d rep0 off\n        ^ off\n    } {}\n    : i idx ? == ll 0 code - code 1\n    ? == idx 0 { ^ . d rep0 } {}\n    : i off ? == idx 1 . d rep1 ? == idx 2 . d rep2 - . d rep0 1\n    ? > idx 1 { = . d rep2 . d rep1 } {}\n    = . d rep1 . d rep0\n    = . d rep0 off\n    ^ off\n}\n\n@ __zs_sequences inout ZsDec d inout ZsFwd f i blocklen → v {\n    : ~ i nseq ( __zs_fw_take f 8 )\n    ? >= nseq 128 {\n        ? == nseq 255 {\n            = nseq + ( __zs_fw_take f 16 ) 0x7F00\n        } {\n            = nseq + << - nseq 128 8 ( __zs_fw_take f 8 )\n        }\n    } {}\n    ? != . f err 0 { = . d err ZSE_CORRUPT ^ v } {}\n    ? == nseq 0 {\n        // No sequences: the block is its literals, and nothing else may\n        // follow them.\n        ( __zs_emit d . d litlen 0 1 )\n        = . d litpos 0\n        ^ v\n    } {}\n    : i modes ( __zs_fw_take f 8 )\n    ? != . f err 0 { = . d err ZSE_CORRUPT ^ v } {}\n    ? != & modes 3 0 { = . d err ZSE_CORRUPT ^ v } {}\n    ( __zs_seq_table d f 0 & >> modes 6 3 )\n    ( __zs_seq_table d f 1 & >> modes 4 3 )\n    ( __zs_seq_table d f 2 & >> modes 2 3 )\n    ? != . d err 0 { ^ v } {}\n    ( __zs_fw_align f )\n    : i at ( __zs_fw_bytepos f )\n    ? >= at blocklen { = . d err ZSE_CORRUPT ^ v } {}\n    : *u bsrc0 # *u + # i . f src at\n    : ~ ZsBk b @ ZsBk { bsrc0 - blocklen at 0 }\n    ? != ( __zs_bk_start b ) 0 {\n        = . d err ZSE_CORRUPT\n        ^ v\n    } {}\n    : ( Vec i ) llx ( __zs_ll_extra )\n    : ( Vec i ) mlx ( __zs_ml_extra )\n    : ( Vec i ) llb ( __zs_ll_base llx )\n    : ( Vec i ) mlb ( __zs_ml_base mlx )\n    : *i llxp ( vec_data [i] llx )\n    : *i mlxp ( vec_data [i] mlx )\n    : *i llbp ( vec_data [i] llb )\n    : *i mlbp ( vec_data [i] mlb )\n    : *i lltp ( vec_data [i] . d llt )\n    : *i oftp ( vec_data [i] . d oft )\n    : *i mltp ( vec_data [i] . d mlt )\n    // From here the bit cursor is a LOCAL. It is read six to eight times\n    // per sequence, and as a field of the reader struct every one of\n    // those was a load and a store the compiler could not eliminate.\n    : *u bsp . b src\n    : i blen . b len\n    : ~ i boff . b off\n    // States are initialized literals-length, offset, match-length.\n    : i lllog . d lllog\n    : i oflog . d oflog\n    : i mllog . d mllog\n    = boff - boff lllog\n    : ~ i lls ( __zs_bits bsp blen boff lllog )\n    = boff - boff oflog\n    : ~ i ofs ( __zs_bits bsp blen boff oflog )\n    = boff - boff mllog\n    : ~ i mls ( __zs_bits bsp blen boff mllog )\n    : ~ i n 0\n    ~ & < n nseq == . d err 0 {\n        : i ofe # i . oftp ofs\n        : i lle # i . lltp lls\n        : i mle # i . mltp mls\n        : i ofc & ofe 255\n        : i llc & lle 255\n        : i mlc & mle 255\n        ? | | > llc 35 > mlc 52 > ofc 63 { = . d err ZSE_CORRUPT } {\n            // Values are read offset first, then match, then literals.\n            = boff - boff ofc\n            : i offcode + << 1 ofc ( __zs_bits bsp blen boff ofc )\n            : i mlx # i . mlxp mlc\n            = boff - boff mlx\n            : i ml + # i . mlbp mlc ( __zs_bits bsp blen boff mlx )\n            : i llx # i . llxp llc\n            = boff - boff llx\n            : i ll + # i . llbp llc ( __zs_bits bsp blen boff llx )\n            : i off ( __zs_offset d offcode ll )\n            = g_zs_nseq + g_zs_nseq 1\n            = g_zs_sumll + g_zs_sumll ll\n            = g_zs_summl + g_zs_summl ml\n            ? <= offcode 3 { = g_zs_nrep + g_zs_nrep 1 } {}\n            ( __zs_emit d ll ml off )\n            ? < + n 1 nseq {\n                : i lnb & >> lle 8 255\n                = boff - boff lnb\n                = lls + >> lle 16 ( __zs_bits bsp blen boff lnb )\n                : i mnb & >> mle 8 255\n                = boff - boff mnb\n                = mls + >> mle 16 ( __zs_bits bsp blen boff mnb )\n                : i onb & >> ofe 8 255\n                = boff - boff onb\n                = ofs + >> ofe 16 ( __zs_bits bsp blen boff onb )\n            } {}\n        }\n        = n + n 1\n    }\n    // The stream must be consumed exactly.\n    ? & == . d err 0 != boff 0 { = . d err ZSE_CORRUPT } {}\n    ? != . d err 0 { ^ v } {}\n    // Whatever literals the sequences did not consume close the block.\n    ? < . d litpos . d litlen {\n        ( __zs_emit d - . d litlen . d litpos 0 1 )\n    } {}\n    = . d litpos 0\n}\n\n// ── Blocks and frames ───────────────────────────────────────────────\n\n@ __zs_block inout ZsDec d i blocklen → v {\n    : *u fsrc # *u + # i . d src . d pos\n    : ~ ZsFwd f @ ZsFwd { fsrc blocklen 0 0 }\n    = . d litpos 0\n    ( __zs_literals d f )\n    ? == . d err 0 { ( __zs_sequences d f blocklen ) } {}\n}\n\n@ __zs_u32 inout ZsDec d i off → i {\n    : *u sp . d src\n    ^ | | | # i . sp off\n    << # i . sp + off 1 8\n    << # i . sp + off 2 16\n    << # i . sp + off 3 24\n}\n\n@ __zs_frame inout ZsDec d → v {\n    : *u sp . d src\n    // Frame_Header_Descriptor\n    ? >= . d pos . d srclen { = . d err ZSE_TRUNC ^ v } {}\n    : i desc # i . sp . d pos\n    = . d pos + . d pos 1\n    : i fcs_flag >> desc 6\n    : i single & >> desc 5 1\n    ? != & >> desc 3 1 0 { = . d err ZSE_CORRUPT ^ v } {}\n    : i checksum & >> desc 2 1\n    : i did_flag & desc 3\n    : ~ i window 0\n    ? == single 0 {\n        ? >= . d pos . d srclen { = . d err ZSE_TRUNC ^ v } {}\n        : i wd # i . sp . d pos\n        = . d pos + . d pos 1\n        : i base << 1 + 10 >> wd 3\n        = window + base * >> base 3 & wd 7\n    } {}\n    ? != did_flag 0 {\n        // A dictionary is named: without it the output would be wrong.\n        = . d err ZSE_UNSUP\n        ^ v\n    } {}\n    : ~ i fcs 0\n    ? | != single 0 != fcs_flag 0 {\n        : i nbytes ? == fcs_flag 0 1 ? == fcs_flag 1 2 ? == fcs_flag 2 4 8\n        ? > + . d pos nbytes . d srclen { = . d err ZSE_TRUNC ^ v } {}\n        : ~ i k 0\n        ~ < k nbytes {\n            = fcs | fcs << # i . sp + . d pos k * k 8\n            = k + k 1\n        }\n        ? == nbytes 2 { = fcs + fcs 256 } {}\n        = . d pos + . d pos nbytes\n    } {}\n    ? != single 0 { = window fcs } {}\n    = . d window window\n    = . d framestart . d olen\n    = . d rep0 1\n    = . d rep1 4\n    = . d rep2 8\n    = . d hufbits 0\n    = . d lllog -1\n    = . d oflog -1\n    = . d mllog -1\n    // Reserve the declared size up front — one allocation for the frame.\n    ? > fcs 0 { ( __zs_need d fcs ) } {}\n    ? != . d err 0 { ^ v } {}\n    : ~ b last F\n    ~ & ! last == . d err 0 {\n        ? > + . d pos 3 . d srclen { = . d err ZSE_TRUNC ^ v } {}\n        : i hdr | | # i . sp . d pos\n        << # i . sp + . d pos 1 8\n        << # i . sp + . d pos 2 16\n        = . d pos + . d pos 3\n        = last != & hdr 1 0\n        : i btype & >> hdr 1 3\n        : i blen >> hdr 3\n        // An RLE block's Block_Size is what it REGENERATES; it occupies\n        // exactly one byte of input. Bounding the read by blen would\n        // reject every large run.\n        : i inbytes ? == btype 1 1 blen\n        ? > + . d pos inbytes . d srclen { = . d err ZSE_TRUNC ^ v } {}\n        ? == btype 0 {\n            ( __zs_need d blen )\n            ? == . d err 0 {\n                ? > blen 0 {\n                    ( nurl_memcpy # s # *u + # i . d op . d olen\n                    # s # *u + # i . d src . d pos blen )\n                } {}\n                = . d olen + . d olen blen\n            } {}\n            = . d pos + . d pos blen\n        } { ? == btype 1 {\n                ? != blen 0 {\n                    ? == . d pos . d srclen { = . d err ZSE_TRUNC ^ v } {}\n                    : i byte # i . sp . d pos\n                    ( __zs_need d blen )\n                    ? == . d err 0 {\n                        : ~ i k 0\n                        ~ < k blen { = . . d op + . d olen k # u byte = k + k 1 }\n                        = . d olen + . d olen blen\n                    } {}\n                } {}\n                = . d pos + . d pos 1\n            } { ? == btype 2 {\n                    ( __zs_block d blen )\n                    = . d pos + . d pos blen\n                } {\n                    = . d err ZSE_CORRUPT\n                } } }\n    }\n    ? != . d err 0 { ^ v } {}\n    ? > fcs 0 {\n        ? != - . d olen . d framestart fcs { = . d err ZSE_CORRUPT ^ v } {}\n    } {}\n    ? != checksum 0 {\n        ? > + . d pos 4 . d srclen { = . d err ZSE_TRUNC ^ v } {}\n        : i want ( __zs_u32 d . d pos )\n        = . d pos + . d pos 4\n        : i got & ( xxh64_ptr # *u + # i . d op . d framestart\n        - . d olen . d framestart 0 ) 0xFFFFFFFF\n        ? != got & want 0xFFFFFFFF { = . d err ZSE_CKSUM ^ v } {}\n    } {}\n}\n\n// ── Public API ──────────────────────────────────────────────────────\n\n// A decoder over `src`: its buffers are its own (dropped with it); `out`\n// grows as frames are decoded.\n@ __zs_new ( Vec u ) src i limit → ZsDec {\n    : ( Vec u ) out ( vec_new [u] )\n    : *u op ( vec_data [u] out )\n    : ( Vec u ) lit ( vec_with_cap [u] 1024 )\n    : i litcap ( vec_cap [u] lit )\n    ^ @ ZsDec {\n        ( vec_data [u] src ) ( vec_len [u] src ) 0\n        out op 0 0 limit 0\n        lit 0 litcap 0\n        ( __zs_zeros 2048 ) 0\n        ( __zs_zeros 512 ) -1\n        ( __zs_zeros 256 ) -1\n        ( __zs_zeros 512 ) -1\n        ( __zs_zeros 300 ) ( __zs_zeros 300 ) ( __zs_zeros 128 )\n        1 4 8 0 0\n    }\n}\n\n@ zstd_decode_limit ( Vec u ) src i max_out → !( Vec u ) ZstdErr {\n    = g_zs_nseq 0\n    = g_zs_sumll 0\n    = g_zs_summl 0\n    = g_zs_nrep 0\n    : i n ( vec_len [u] src )\n    ? == n 0 {\n        ^ @ !( Vec u ) ZstdErr { T ( vec_new [u] ) }\n    } {}\n    : ~ ZsDec d ( __zs_new src max_out )\n    ~ & == . d err 0 < . d pos . d srclen {\n        ? > + . d pos 4 . d srclen { = . d err ZSE_TRUNC } {\n            : i magic ( __zs_u32 d . d pos )\n            ? == magic ZS_MAGIC {\n                = . d pos + . d pos 4\n                ( __zs_frame d )\n            } { ? & >= magic ZS_SKIP_LO <= magic ZS_SKIP_HI {\n                    // A skippable frame carries a payload for someone else.\n                    ? > + . d pos 8 . d srclen { = . d err ZSE_TRUNC } {\n                        : i skip ( __zs_u32 d + . d pos 4 )\n                        ? > + + . d pos 8 skip . d srclen { = . d err ZSE_TRUNC }\n                        { = . d pos + + . d pos 8 skip }\n                    }\n                } { = . d err ZSE_MAGIC } }\n        }\n    }\n    ? != . d err 0 {\n        ^ @ !( Vec u ) ZstdErr { F ( __zs_err . d err ) }\n    } {}\n    ? ! ( vec_set_len [u] . d out . d olen ) {\n        ^ @ !( Vec u ) ZstdErr { F # ZstdErr ZstdCorrupt }\n    } {}\n    // The output leaves the decoder (moved, not copied); its tables go\n    // with it.\n    : ( Vec u ) out . d out\n    ^ @ !( Vec u ) ZstdErr { T out }\n}\n\n@ zstd_decode ( Vec u ) src → !( Vec u ) ZstdErr {\n    ^ ( zstd_decode_limit src 0x7FFFFFFFFFFFFFFF )\n}\n\n// ── Encoder ─────────────────────────────────────────────────────────\n//\n// A Lempel-Ziv match finder feeding FSE-coded sequences — the same\n// shape as the reference compressor, minus the parts that trade code\n// for the last few percent. Matches come from a hash table with a\n// collision chain whose search depth is what `level` selects. The three\n// sequence tables are the format's PREDEFINED distributions: they cost\n// zero header bytes, which is the right trade until a block is large\n// enough to pay for a custom table.\n//\n// The sequence bitstream is written so that reading it BACKWARD yields\n// the format's order, which is why sequences are encoded last-to-first\n// and each state is flushed at the end.\n\n: i ZS_BLOCK_MAX 131072  // the format's largest block\n: i ZS_MIN_MATCH 4  // the hash indexes 4 bytes, so matches start there\n// What a literal costs, in bits, when the match finder is deciding\n// whether a longer-but-further match is worth its offset. Literals are\n// Huffman-coded, so they are NOT eight bits each; pricing them at eight\n// overvalues distance and makes a deeper search compress WORSE — which\n// is exactly what level 12 did against level 1 before this was a price.\n: i ZS_LIT_BITS 5\n\n@ __zs_pu32 ( Vec u ) v i x → v {\n    ( vec_push [u] v # u & x 255 )\n    ( vec_push [u] v # u & >> x 8 255 )\n    ( vec_push [u] v # u & >> x 16 255 )\n    ( vec_push [u] v # u & >> x 24 255 )\n}\n\n// ── A bit writer whose output is read from the end ──────────────────\n\n: ZsBw {\n    ( Vec u ) buf\n    i acc  // pending bits, LSB first\n    i bits  // how many are pending\n}\n\n@ __zs_bw_add inout ZsBw w i val i n → v {\n    ? <= n 0 { ^ v } {}\n    = . w acc | . w acc << & val - << 1 n 1 . w bits\n    = . w bits + . w bits n\n    ~ >= . w bits 8 {\n        ( vec_push [u] . w buf # u & . w acc 255 )\n        = . w acc >> # u64 . w acc 8\n        = . w bits - . w bits 8\n    }\n}\n\n// Close the stream: a single 1 bit marks the last information bit, and\n// the rest of the byte is zero padding the decoder skips.\n@ __zs_bw_close inout ZsBw w → v {\n    ( __zs_bw_add w 1 1 )\n    ? > . w bits 0 {\n        ( vec_push [u] . w buf # u & . w acc 255 )\n        = . w acc 0\n        = . w bits 0\n    } {}\n}\n\n// ── FSE encoding table ──────────────────────────────────────────────\n\n: ZsCt {\n    ( Vec i ) next  // next-state table in cumulative symbol order\n    ( Vec i ) dnb  // per symbol: delta applied to derive the bit count\n    ( Vec i ) dfs  // per symbol: offset into `next`\n    i log\n}\n\n// Mirror of `__zs_fse_build`: the same spread, read the other way.\n@ __zs_ct_build ( Vec i ) freq i nsym i log → ZsCt {\n    : i size << 1 log\n    : ( Vec i ) next ( __zs_zeros size )\n    : ( Vec i ) dnb ( __zs_zeros ? > nsym 0 nsym 1 )\n    : ( Vec i ) dfs ( __zs_zeros ? > nsym 0 nsym 1 )\n    : ( Vec i ) symbol ( __zs_zeros size )\n    : ( Vec i ) cumul ( __zs_zeros + nsym 2 )\n    : *i fp ( vec_data [i] freq )\n    : *i np ( vec_data [i] next )\n    : *i dnbp ( vec_data [i] dnb )\n    : *i dfsp ( vec_data [i] dfs )\n    : *i sp ( vec_data [i] symbol )\n    : *i cp ( vec_data [i] cumul )\n    // Start positions per symbol; a \"less than one\" symbol takes a\n    // single cell from the top of the table.\n    : ~ i high size\n    : ~ i s 0\n    ~ < s nsym {\n        : i c # i . fp s\n        ? == c -1 {\n            = . cp + s 1 + # i . cp s 1\n            = high - high 1\n            = . sp high s\n        } {\n            = . cp + s 1 + # i . cp s c\n        }\n        = s + s 1\n    }\n    : i step + + >> size 1 >> size 3 3\n    : i mask - size 1\n    : ~ i pos 0\n    = s 0\n    ~ < s nsym {\n        : i c # i . fp s\n        ? > c 0 {\n            : ~ i k 0\n            ~ < k c {\n                = . sp pos s\n                = pos & + pos step mask\n                ~ >= pos high { = pos & + pos step mask }\n                = k + k 1\n            }\n        } {}\n        = s + s 1\n    }\n    // Each cell records the state a symbol lands in, in symbol order.\n    : ~ i u 0\n    ~ < u size {\n        : i sym # i . sp u\n        : i at # i . cp sym\n        = . cp sym + at 1\n        = . np at + size u\n        = u + u 1\n    }\n    // Per-symbol transform: how many bits leave the state, and where in\n    // `next` this symbol's cells begin.\n    : ~ i total 0\n    = s 0\n    ~ < s nsym {\n        : i c # i . fp s\n        ? | == c 1 == c -1 {\n            = . dnbp s - << log 16 size\n            = . dfsp s - total 1\n            = total + total 1\n        } { ? > c 1 {\n                : i maxbits - log ( __zs_hbit - c 1 )\n                = . dnbp s - << maxbits 16 << c maxbits\n                = . dfsp s - total c\n                = total + total c\n            } {} }\n        = s + s 1\n    }\n    ^ @ ZsCt { next dnb dfs log }\n}\n\n@ __zs_ct_init ZsCt ct i sym → i {\n    : *i dnbp ( vec_data [i] . ct dnb )\n    : *i dfsp ( vec_data [i] . ct dfs )\n    : *i np ( vec_data [i] . ct next )\n    : i dnb # i . dnbp sym\n    : i nb >> + dnb 32768 16\n    : i v - << nb 16 dnb\n    ^ # i . np + >> v nb # i . dfsp sym\n}\n\n@ __zs_ct_encode inout ZsBw w ZsCt ct i state i sym → i {\n    : *i dnbp ( vec_data [i] . ct dnb )\n    : *i dfsp ( vec_data [i] . ct dfs )\n    : *i np ( vec_data [i] . ct next )\n    : i nb >> + state # i . dnbp sym 16\n    ( __zs_bw_add w state nb )\n    ^ # i . np + >> state nb # i . dfsp sym\n}\n\n@ __zs_ct_flush inout ZsBw w ZsCt ct i state → v {\n    ( __zs_bw_add w state . ct log )\n}\n\n// ── Code assignment ─────────────────────────────────────────────────\n\n@ __zs_ll_code ( Vec i ) base i ll → i {\n    ? < ll 16 { ^ ll } {}\n    : *i p ( vec_data [i] base )\n    : ~ i c 16\n    ~ & < c 35 <= # i . p + c 1 ll { = c + c 1 }\n    ^ c\n}\n\n@ __zs_ml_code ( Vec i ) base i ml → i {\n    ? < ml 35 { ^ - ml 3 } {}\n    : *i p ( vec_data [i] base )\n    : ~ i c 32\n    ~ & < c 52 <= # i . p + c 1 ml { = c + c 1 }\n    ^ c\n}\n\n// ── Match finder ────────────────────────────────────────────────────\n\n@ __zs_hash4 * u p i at i hlog → i {\n    : i v | | | # i . p at << # i . p + at 1 8\n    << # i . p + at 2 16 << # i . p + at 3 24\n    ^ & >> # u64 * v 2654435761 - 32 hlog - << 1 hlog 1\n}\n\n@ __zs_matchlen * u p i a i b i limit → i {\n    : ~ i k 0\n    ~ & < + b k limit == # i . p + a k # i . p + b k { = k + k 1 }\n    ^ k\n}\n\n// ── Cross-block writer state: repeat and treeless modes ─────────────\n//\n// The format lets a block reuse the previous block's entropy tables —\n// mode 3 for a sequence table, block type 3 (treeless) for the Huffman\n// tree — and on any multi-block input that reuse is worth real bytes:\n// three FSE descriptions and a tree, per block, on data whose shape\n// barely changed. The writer keeps what the DECODER would be holding\n// (the last tables it built) and, per block, prices sending a new\n// table against reusing the standing one against the predefined one,\n// in bits, and takes the cheapest. The decoder mirror of this state is\n// ZsDec's hufbits / lllog / oflog / mllog.\n: ZsWst {\n    ( Vec i ) hlen  // previous Huffman code lengths; empty until hvalid\n    i hvalid\n    ( Vec i ) lldist  // previous normalized distributions\n    ( Vec i ) ofdist\n    ( Vec i ) mldist\n    i lllg  // their accuracy logs\n    i oflg\n    i mllg\n    i llok  // 1 = a table is standing (sent or predefined)\n    i ofok\n    i mlok\n    ( Vec i ) shadow  // save/restore snapshot: 256+3*64 dists + 8 scalars\n}\n\n// A fresh writer state: nothing standing yet. The encoder keeps it in a\n// local the block writer advances in place (`inout`).\n@ __zs_wst_new → ZsWst {\n    ^ @ ZsWst {\n        ( __zs_zeros 256 ) 0\n        ( __zs_zeros 64 ) ( __zs_zeros 64 ) ( __zs_zeros 64 )\n        0 0 0\n        0 0 0\n        ( __zs_zeros 912 )\n    }\n}\n\n// Writing a block ADVANCES this state (a sent table becomes the\n// standing one). Trying several parses of the same block therefore\n// needs the state as it stood before each trial: save it, and either\n// commit the advance (the trial won) or put it back (it lost).\n@ __zs_wst_save inout ZsWst st i slot → v {\n    : *i sh0 ( vec_data [i] . st shadow )\n    : *i sh # *i + # i sh0 * slot 3648\n    : *i hl ( vec_data [i] . st hlen )\n    : *i l1 ( vec_data [i] . st lldist )\n    : *i l2 ( vec_data [i] . st ofdist )\n    : *i l3 ( vec_data [i] . st mldist )\n    : ~ i t 0\n    ~ < t 256 { = . sh t # i . hl t = t + t 1 }\n    ~ < t 320 { = . sh t # i . l1 - t 256 = t + t 1 }\n    ~ < t 384 { = . sh t # i . l2 - t 320 = t + t 1 }\n    ~ < t 448 { = . sh t # i . l3 - t 384 = t + t 1 }\n    = . sh 448 . st hvalid\n    = . sh 449 . st lllg\n    = . sh 450 . st oflg\n    = . sh 451 . st mllg\n    = . sh 452 . st llok\n    = . sh 453 . st ofok\n    = . sh 454 . st mlok\n}\n\n@ __zs_wst_restore inout ZsWst st i slot → v {\n    : *i sh0 ( vec_data [i] . st shadow )\n    : *i sh # *i + # i sh0 * slot 3648\n    : *i hl ( vec_data [i] . st hlen )\n    : *i l1 ( vec_data [i] . st lldist )\n    : *i l2 ( vec_data [i] . st ofdist )\n    : *i l3 ( vec_data [i] . st mldist )\n    : ~ i t 0\n    ~ < t 256 { = . hl t # i . sh t = t + t 1 }\n    ~ < t 320 { = . l1 - t 256 # i . sh t = t + t 1 }\n    ~ < t 384 { = . l2 - t 320 # i . sh t = t + t 1 }\n    ~ < t 448 { = . l3 - t 384 # i . sh t = t + t 1 }\n    = . st hvalid # i . sh 448\n    = . st lllg # i . sh 449\n    = . st oflg # i . sh 450\n    = . st mllg # i . sh 451\n    = . st llok # i . sh 452\n    = . st ofok # i . sh 453\n    = . st mlok # i . sh 454\n}\n\n// The cost, in 1/16 bits, of coding `counts` with the distribution\n// `dist` (normalized to 1<<log). -1 when some present symbol has no\n// cell — that table cannot code this block at all.\n@ __zs_dist_cost * i cnt i nsym * i dist i ndist i log → i {\n    : ~ i totalc 0\n    : ~ i s 0\n    ~ < s nsym {\n        : i c # i . cnt s\n        ? > c 0 {\n            ? | >= s ndist == # i . dist s 0 { ^ -1 } {}\n            : i share0 # i . dist s\n            // A \"less than one\" (-1) cell prices as one cell.\n            : i share ? < share0 0 1 share0\n            = totalc + totalc * c - << log 4 ( __zs_log2_16 share )\n        } {}\n        = s + s 1\n    }\n    ^ totalc\n}\n\n// ── Huffman-coded literals ──────────────────────────────────────────\n//\n// Literals are half the output of a text block, so coding them is the\n// difference between \"a valid frame\" and \"a competitive one\".\n//\n// Code lengths come from an ordinary Huffman merge over the block's\n// byte counts. The format caps a code at 11 bits; when the merge\n// produces a deeper tree the counts are halved and the merge repeats,\n// which converges quickly and costs a fraction of a percent against a\n// length-limited construction.\n\n@ __zs_huf_merge ( Vec i ) counts ( Vec i ) lens i nsym → i {\n    : ( Vec i ) cnt ( __zs_zeros * 2 ZS_MAX_SYMS )\n    : ( Vec i ) par ( __zs_zeros * 2 ZS_MAX_SYMS )\n    : ( Vec i ) live ( __zs_zeros * 2 ZS_MAX_SYMS )\n    : *i cp ( vec_data [i] counts )\n    : *i lp ( vec_data [i] lens )\n    : *i c ( vec_data [i] cnt )\n    : *i pa ( vec_data [i] par )\n    : *i lv ( vec_data [i] live )\n    : ~ i nodes 0\n    : ~ i present 0\n    : ~ i s 0\n    ~ < s nsym {\n        = . lp s 0\n        ? > # i . cp s 0 {\n            = . c s # i . cp s\n            = . lv s 1\n            = present + present 1\n        } {}\n        = s + s 1\n    }\n    = nodes nsym\n    ? == present 0 { ^ 0 } {}\n    ? == present 1 {\n        // A single distinct byte still needs a one-bit code.\n        = s 0\n        ~ < s nsym { ? > # i . cp s 0 { = . lp s 1 } {} = s + s 1 }\n        ^ 1\n    } {}\n    : ~ i remaining present\n    ~ > remaining 1 {\n        // Two lightest live nodes become one.\n        : ~ i a -1\n        : ~ i b -1\n        : ~ i k 0\n        ~ < k nodes {\n            ? != # i . lv k 0 {\n                ? | == a -1 < # i . c k # i . c a { = b a = a k }\n                { ? | == b -1 < # i . c k # i . c b { = b k } {} }\n            } {}\n            = k + k 1\n        }\n        = . lv a 0\n        = . lv b 0\n        : i n nodes\n        = nodes + nodes 1\n        = . c n + # i . c a # i . c b\n        = . lv n 1\n        = . pa a n\n        = . pa b n\n        = . pa n -1\n        = remaining - remaining 1\n    }\n    // Depth of each leaf is its code length.\n    : ~ i maxb 0\n    = s 0\n    ~ < s nsym {\n        ? > # i . cp s 0 {\n            : ~ i d 0\n            : ~ i k s\n            ~ != # i . pa k -1 { = k # i . pa k = d + d 1 }\n            = . lp s d\n            ? > d maxb { = maxb d } {}\n        } {}\n        = s + s 1\n    }\n    ^ maxb\n}\n\n// Code lengths for the block's literals, capped at 11 bits.\n@ __zs_huf_lengths ( Vec i ) counts ( Vec i ) lens i nsym → i {\n    : ~ i maxb ( __zs_huf_merge counts lens nsym )\n    : *i cp ( vec_data [i] counts )\n    : ~ i guard 0\n    ~ & > maxb ZS_MAX_HUF_BITS < guard 32 {\n        // Flatten the distribution and try again: a tree over halved\n        // counts is shallower, and the coding loss is negligible.\n        : ~ i s 0\n        ~ < s nsym {\n            : i c # i . cp s\n            ? > c 0 { = . cp s ? > >> c 1 1 >> c 1 1 } {}\n            = s + s 1\n        }\n        = maxb ( __zs_huf_merge counts lens nsym )\n        = guard + guard 1\n    }\n    ^ maxb\n}\n\n// Canonical codes, assigned longest-first so they agree with the way a\n// decoder lays its table out.\n@ __zs_huf_codes ( Vec i ) lens ( Vec i ) codes i nsym i maxb → v {\n    : ( Vec i ) rank ( __zs_zeros 16 )\n    : ( Vec i ) val ( __zs_zeros 16 )\n    : *i lp ( vec_data [i] lens )\n    : *i cop ( vec_data [i] codes )\n    : *i rp ( vec_data [i] rank )\n    : *i vp ( vec_data [i] val )\n    : ~ i s 0\n    ~ < s nsym {\n        : i L # i . lp s\n        ? > L 0 { = . rp L + # i . rp L 1 } {}\n        = s + s 1\n    }\n    : ~ i min 0\n    : ~ i n maxb\n    ~ > n 0 {\n        = . vp n min\n        = min >> + min # i . rp n 1\n        = n - n 1\n    }\n    = s 0\n    ~ < s nsym {\n        : i L # i . lp s\n        ? > L 0 {\n            = . cop s # i . vp L\n            = . vp L + # i . vp L 1\n        } { = . cop s 0 }\n        = s + s 1\n    }\n}\n\n// Flush a forward bitstream: no end marker, just zero padding. Header\n// bitstreams are read forward and their length is known, so the 1-bit\n// terminator a backward stream needs would be one bit of corruption.\n@ __zs_bw_close_fwd inout ZsBw w → v {\n    ? > . w bits 0 {\n        ( vec_push [u] . w buf # u & . w acc 255 )\n        = . w acc 0\n        = . w bits 0\n    } {}\n}\n\n// Scale counts onto a table of 1<<log cells. Every symbol that occurs\n// keeps at least one cell; the rounding error lands on the largest.\n// Returns the highest symbol with a cell, or -1.\n@ __zs_normalize ( Vec i ) counts ( Vec i ) norm i nsym i log → i {\n    : *i cp ( vec_data [i] counts )\n    : *i np ( vec_data [i] norm )\n    : i scale << 1 log\n    : ~ i total 0\n    : ~ i s 0\n    ~ < s nsym { = total + total # i . cp s = s + s 1 }\n    ? <= total 0 { ^ -1 } {}\n    : ~ i sum 0\n    : ~ i big 0\n    = s 0\n    ~ < s nsym {\n        : i c # i . cp s\n        : ~ i q 0\n        ? > c 0 {\n            = q / * c scale total\n            ? < q 1 { = q 1 } {}\n        } {}\n        = . np s q\n        = sum + sum q\n        ? > q # i . np big { = big s } {}\n        = s + s 1\n    }\n    // Reconcile by largest remainder: each leftover cell goes to the\n    // symbol whose share was rounded down the hardest. Dumping the\n    // whole difference on the biggest symbol — the previous rule —\n    // distorts exactly the code the data uses most.\n    ~ < sum scale {\n        : ~ i pick -1\n        : ~ i pickrem -1\n        : ~ i t 0\n        ~ < t nsym {\n            ? > # i . cp t 0 {\n                : i rem % * # i . cp t scale total\n                ? > rem pickrem { = pickrem rem = pick t } {}\n            } {}\n            = t + t 1\n        }\n        ? < pick 0 { = pick big } {}\n        = . np pick + # i . np pick 1\n        = sum + sum 1\n    }\n    ~ != sum scale {\n        ? < sum scale {\n            = . np big + # i . np big - scale sum\n            = sum scale\n        } {\n            : i over - sum scale\n            : i can - # i . np big 1\n            ? >= can over {\n                = . np big - # i . np big over\n                = sum scale\n            } {\n                = . np big 1\n                = sum - sum can\n                = s 0\n                ~ & < s nsym > sum scale {\n                    ? > # i . np s 1 {\n                        : i take ? > - # i . np s 1 - sum scale - sum scale - # i . np s 1\n                        = . np s - # i . np s take\n                        = sum - sum take\n                    } {}\n                    = s + s 1\n                }\n                ? != sum scale { ^ -1 } {}\n            }\n        }\n    }\n    : ~ i maxsym -1\n    = s 0\n    ~ < s nsym { ? > # i . np s 0 { = maxsym s } {} = s + s 1 }\n    ^ maxsym\n}\n\n// Write a normalized distribution — the exact inverse of the reader in\n// `__zs_fse_header`, sharing its shrinking field width and its 2-bit\n// runs of zero-probability symbols.\n@ __zs_write_ncount inout ZsBw w ( Vec i ) norm i nsym i log → v {\n    : *i np ( vec_data [i] norm )\n    ( __zs_bw_add w - log 5 4 )\n    : i size << 1 log\n    : ~ i remaining + size 1\n    : ~ i threshold size\n    : ~ i nbits + log 1\n    : ~ i s 0\n    : ~ b prev0 F\n    ~ & < s nsym > remaining 1 {\n        ? prev0 {\n            // Skip the run of zeros and spell its length in 2-bit\n            // groups, chaining on 3.\n            : ~ i start s\n            ~ & < s nsym == # i . np s 0 { = s + s 1 }\n            ? >= s nsym { = prev0 F } {\n                ~ >= s + start 3 {\n                    = start + start 3\n                    ( __zs_bw_add w 3 2 )\n                }\n                ( __zs_bw_add w - s start 2 )\n                = prev0 F\n            }\n        } {\n            : i c # i . np s\n            = s + s 1\n            : i max - - * 2 threshold 1 remaining\n            = remaining - remaining ? < c 0 - 0 c c\n            : ~ i v + c 1\n            ? >= v threshold { = v + v max } {}\n            ( __zs_bw_add w v ? < v max - nbits 1 nbits )\n            = prev0 == v 1\n            ~ < remaining threshold {\n                = nbits - nbits 1\n                = threshold >> threshold 1\n            }\n        }\n    }\n}\n\n// The Huffman tree description in its FSE-coded form: needed whenever\n// the block uses a literal byte above 128, since the direct form's\n// header byte cannot count that far.\n@ __zs_tree_fse ( Vec u ) tree ( Vec i ) wts i nw i maxb → b {\n    ? < nw 4 { ^ F } {}\n    : i alpha + maxb 1\n    : ( Vec i ) counts ( __zs_zeros 16 )\n    : ( Vec i ) norm ( __zs_zeros 16 )\n    : *i cp ( vec_data [i] counts )\n    : *i wp ( vec_data [i] wts )\n    : ~ i k 0\n    ~ < k nw { : i x # i . wp k = . cp x + # i . cp x 1 = k + k 1 }\n    : i log 6\n    : i maxsym ( __zs_normalize counts norm alpha log )\n    ? < maxsym 1 { ^ F } {}\n    : ZsCt ct ( __zs_ct_build norm + maxsym 1 log )\n    : ~ ZsBw w @ ZsBw { ( vec_new [u] ) 0 0 }\n    ( __zs_write_ncount w norm + maxsym 1 log )\n    ( __zs_bw_close_fwd w )\n    // Two states, even weights on the first, odd on the second, encoded\n    // from the end so a backward reader replays them in order.\n    : ~ i ip nw\n    : ~ i s1 0\n    : ~ i s2 0\n    ? != & nw 1 0 {\n        = ip - ip 1\n        = s1 ( __zs_ct_init ct # i . wp ip )\n        = ip - ip 1\n        = s2 ( __zs_ct_init ct # i . wp ip )\n        = ip - ip 1\n        = s1 ( __zs_ct_encode w ct s1 # i . wp ip )\n    } {\n        = ip - ip 1\n        = s2 ( __zs_ct_init ct # i . wp ip )\n        = ip - ip 1\n        = s1 ( __zs_ct_init ct # i . wp ip )\n    }\n    ~ > ip 0 {\n        = ip - ip 1\n        = s2 ( __zs_ct_encode w ct s2 # i . wp ip )\n        = ip - ip 1\n        = s1 ( __zs_ct_encode w ct s1 # i . wp ip )\n    }\n    ( __zs_ct_flush w ct s2 )\n    ( __zs_ct_flush w ct s1 )\n    ( __zs_bw_close w )\n    : i n ( vec_len [u] . w buf )\n    : ~ b ok F\n    ? & > n 0 < n 128 {\n        ( vec_push [u] tree # u n )\n        ( vec_extend [u] tree . w buf )\n        = ok T\n    } {}\n    ^ ok\n}\n\n// One Huffman bitstream over lits[from, to), written so that reading it\n// backward replays the symbols in order.\n@ __zs_huf_stream_enc ( Vec u ) dst ( Vec u ) lits i from i to\n( Vec i ) lens ( Vec i ) codes → i {\n    : ~ ZsBw w @ ZsBw { ( vec_new [u] ) 0 0 }\n    : *u lp ( vec_data [u] lits )\n    : *i lnp ( vec_data [i] lens )\n    : *i cop ( vec_data [i] codes )\n    : ~ i k to\n    ~ > k from {\n        : i sym # i . lp - k 1\n        ( __zs_bw_add w # i . cop sym # i . lnp sym )\n        = k - k 1\n    }\n    ( __zs_bw_close w )\n    : i n ( vec_len [u] . w buf )\n    ( vec_extend [u] dst . w buf )\n    ^ n\n}\n\n// Write the literals section. Returns T when it went out Huffman-coded,\n// F when raw literals were smaller (or the tree could not be sent).\n@ __zs_lit_compress ( Vec u ) body ( Vec u ) lits inout ZsWst wst → b {\n    : i n ( vec_len [u] lits )\n    ? < n 64 { ^ F } {}\n    : ( Vec i ) counts ( __zs_zeros ZS_MAX_SYMS )\n    : *i cnp ( vec_data [i] counts )\n    : *u lp ( vec_data [u] lits )\n    : ~ i k 0\n    ~ < k n { : i c # i . lp k = . cnp c + # i . cnp c 1 = k + k 1 }\n    // The tree description is 4 bits per symbol up to the last one\n    // present, and its header byte only reaches 128 symbols.\n    : ~ i last 0\n    : ~ i s 0\n    ~ < s ZS_MAX_SYMS { ? > # i . cnp s 0 { = last s } {} = s + s 1 }\n    ? == last 0 { ^ F } {}\n    : ( Vec i ) lens ( __zs_zeros ZS_MAX_SYMS )\n    : ( Vec i ) codes ( __zs_zeros ZS_MAX_SYMS )\n    : ~ i maxb ( __zs_huf_lengths counts lens ZS_MAX_SYMS )\n    ? | <= maxb 0 > maxb ZS_MAX_HUF_BITS {\n        ^ F\n    } {}\n    ( __zs_huf_codes lens codes ZS_MAX_SYMS maxb )\n    // The standing tree — the one the decoder still holds from the\n    // previous block — codes this block for zero header bytes if it\n    // covers every byte present. Weigh that against a fresh tree's\n    // bits-plus-description; the winner is decided in whole bits\n    // before anything is built.\n    : ~ b treeless F\n    ? == . wst hvalid 1 {\n        : *i hlp ( vec_data [i] . wst hlen )\n        : ~ i newbits 0\n        : ~ i prevbits 0\n        : ~ b covered T\n        : *i lnp0 ( vec_data [i] lens )\n        : ~ i t 0\n        ~ < t ZS_MAX_SYMS {\n            : i c # i . cnp t\n            ? > c 0 {\n                = newbits + newbits * c # i . lnp0 t\n                ? > # i . hlp t 0 { = prevbits + prevbits * c # i . hlp t }\n                { = covered F }\n            } {}\n            = t + t 1\n        }\n        // A fresh description costs 40-120 bytes; charge a conservative\n        // 40 against the fresh tree and let ties keep the old one.\n        ? & covered <= prevbits + newbits 320 {\n            = treeless T\n            = t 0\n            ~ < t ZS_MAX_SYMS { = . lnp0 t # i . hlp t = t + t 1 }\n            : ~ i mb 0\n            = t 0\n            ~ < t ZS_MAX_SYMS {\n                ? > # i . lnp0 t mb { = mb # i . lnp0 t } {}\n                = t + t 1\n            }\n            = maxb mb\n            ( __zs_huf_codes lens codes ZS_MAX_SYMS maxb )\n        } {}\n    } {}\n    // Tree description: weight = maxbits + 1 - length. The last\n    // symbol's weight is what completes the power of two, so it is\n    // never transmitted.\n    : ( Vec i ) wts ( __zs_zeros ? > last 0 last 1 )\n    : *i lnp ( vec_data [i] lens )\n    : *i wtp ( vec_data [i] wts )\n    = s 0\n    ~ < s last {\n        : i L # i . lnp s\n        = . wtp s ? > L 0 - + maxb 1 L 0\n        = s + s 1\n    }\n    // Two ways to send it: four bits per weight, or the weights\n    // themselves FSE-coded. The direct form cannot count past 128\n    // symbols, so a block with a high literal byte has only one option.\n    : ( Vec u ) tdir ( vec_new [u] )\n    ? <= last 128 {\n        ( vec_push [u] tdir # u + 127 last )\n        : ~ i acc 0\n        = s 0\n        ~ < s last {\n            : i wgt # i . wtp s\n            ? == & s 1 0 { = acc << wgt 4 } { ( vec_push [u] tdir # u | acc wgt ) }\n            = s + s 1\n        }\n        ? != & last 1 0 { ( vec_push [u] tdir # u acc ) } {}\n    } {}\n    : ( Vec u ) tfse ( vec_new [u] )\n    : b okfse ( __zs_tree_fse tfse wts last maxb )\n    : i ndir ( vec_len [u] tdir )\n    : i nfse ( vec_len [u] tfse )\n    : ( Vec u ) tree ( vec_new [u] )\n    ? treeless {} {\n        ? & > ndir 0 | ! okfse <= ndir nfse {\n            ( vec_extend [u] tree tdir )\n        } { ? okfse { ( vec_extend [u] tree tfse ) } {} }\n    }\n    ? & ! treeless == ( vec_len [u] tree ) 0 {\n        ^ F\n    } {}\n    : i btype ? treeless 3 2\n    // Four streams above 1023 literals, because that is the only shape\n    // the larger size formats describe.\n    : ( Vec u ) payload ( vec_with_cap [u] + n 32 )\n    : ~ i nstreams 1\n    ? <= n 1023 {\n        : i _s1 ( __zs_huf_stream_enc payload lits 0 n lens codes )\n    } {\n        = nstreams 4\n        : i seg >> + n 3 2\n        : i e1 ? > seg n n seg\n        : i e2 ? > * 2 seg n n * 2 seg\n        : i e3 ? > * 3 seg n n * 3 seg\n        : ( Vec u ) s1 ( vec_new [u] )\n        : ( Vec u ) s2 ( vec_new [u] )\n        : ( Vec u ) s3 ( vec_new [u] )\n        : ( Vec u ) s4 ( vec_new [u] )\n        : i n1 ( __zs_huf_stream_enc s1 lits 0 e1 lens codes )\n        : i n2 ( __zs_huf_stream_enc s2 lits e1 e2 lens codes )\n        : i n3 ( __zs_huf_stream_enc s3 lits e2 e3 lens codes )\n        : i _n4 ( __zs_huf_stream_enc s4 lits e3 n lens codes )\n        ( vec_push [u] payload # u & n1 255 )\n        ( vec_push [u] payload # u & >> n1 8 255 )\n        ( vec_push [u] payload # u & n2 255 )\n        ( vec_push [u] payload # u & >> n2 8 255 )\n        ( vec_push [u] payload # u & n3 255 )\n        ( vec_push [u] payload # u & >> n3 8 255 )\n        ( vec_extend [u] payload s1 )\n        ( vec_extend [u] payload s2 )\n        ( vec_extend [u] payload s3 )\n        ( vec_extend [u] payload s4 )\n    }\n    : i treelen ( vec_len [u] tree )\n    : i csize + treelen ( vec_len [u] payload )\n    // Each size format describes only so large a section; pick the one\n    // that fits, and fall back to raw literals if none does.\n    : b one == nstreams 1\n    : b fits ? one & <= csize 1023 <= n 1023\n    & <= n 262143 <= csize 262143\n    : i hdrlen ? one 3 ? & <= n 16383 <= csize 16383 4 5\n    // Only worth it if the whole section beats raw literals.\n    : b win & fits < + hdrlen csize n\n    ? win {\n        ? one {\n            : i hv | | btype << 0 2 | << n 4 << csize 14\n            ( vec_push [u] body # u & hv 255 )\n            ( vec_push [u] body # u & >> hv 8 255 )\n            ( vec_push [u] body # u & >> hv 16 255 )\n        } { ? == hdrlen 4 {\n                : i hv | | btype << 2 2 | << n 4 << csize 18\n                ( vec_push [u] body # u & hv 255 )\n                ( vec_push [u] body # u & >> hv 8 255 )\n                ( vec_push [u] body # u & >> hv 16 255 )\n                ( vec_push [u] body # u & >> hv 24 255 )\n            } {\n                : i hv | | btype << 3 2 | << n 4 << csize 22\n                ( vec_push [u] body # u & hv 255 )\n                ( vec_push [u] body # u & >> hv 8 255 )\n                ( vec_push [u] body # u & >> hv 16 255 )\n                ( vec_push [u] body # u & >> hv 24 255 )\n                ( vec_push [u] body # u & >> # u64 hv 32 255 )\n            } }\n        ( vec_extend [u] body tree )\n        ( vec_extend [u] body payload )\n    } {}\n    : b used win\n    // A tree that actually went out is what the decoder now holds.\n    ? & used ! treeless {\n        : *i hlp2 ( vec_data [i] . wst hlen )\n        : *i lnp2 ( vec_data [i] lens )\n        : ~ i t 0\n        ~ < t ZS_MAX_SYMS { = . hlp2 t # i . lnp2 t = t + t 1 }\n        = . wst hvalid 1\n    } {}\n    ^ used\n}\n\n// ── Block assembly ──────────────────────────────────────────────────\n\n@ __zs_lit_header ( Vec u ) body i n → v {\n    ? < n 32 {\n        ( vec_push [u] body # u << n 3 )\n    } { ? < n 4096 {\n            : i hv | 4 << n 4\n            ( vec_push [u] body # u & hv 255 )\n            ( vec_push [u] body # u & >> hv 8 255 )\n        } {\n            : i hv | 12 << n 4\n            ( vec_push [u] body # u & hv 255 )\n            ( vec_push [u] body # u & >> hv 8 255 )\n            ( vec_push [u] body # u & >> hv 16 255 )\n        } }\n}\n\n@ __zs_nseq_header ( Vec u ) body i n → v {\n    ? < n 128 {\n        ( vec_push [u] body # u n )\n    } { ? < n 0x7F00 {\n            ( vec_push [u] body # u + 128 >> n 8 )\n            ( vec_push [u] body # u & n 255 )\n        } {\n            ( vec_push [u] body # u 255 )\n            : i r - n 0x7F00\n            ( vec_push [u] body # u & r 255 )\n            ( vec_push [u] body # u & >> r 8 255 )\n        } }\n}\n\n@ __zs_block_header ( Vec u ) out i btype i size i last → v {\n    : i hdr | | last << btype 1 << size 3\n    ( vec_push [u] out # u & hdr 255 )\n    ( vec_push [u] out # u & >> hdr 8 255 )\n    ( vec_push [u] out # u & >> hdr 16 255 )\n}\n\n// Write one block: the sequences if they pay for themselves, the raw\n// bytes if they do not. A compressed block that is not smaller than its\n// content is not allowed to be written as one.\n// Pick and emit one sequence table. A block with enough sequences pays\n// for a distribution of its own; a short one takes the predefined table\n// and spends no header bytes at all. `modeslot[0]` receives the mode.\n@ __zs_seq_ctable ( Vec u ) tabs ( Vec i ) counts i nsym i maxlog i nseq\n( Vec i ) deffreq i defn i deflog ( Vec i ) modeslot\ninout ZsWst st i which → ZsCt {\n    : *i mp ( vec_data [i] modeslot )\n    : *i cntp ( vec_data [i] counts )\n    // The standing table (what the decoder still holds), if any.\n    : ( Vec i ) prev ? == which 0 . st lldist ? == which 1 . st ofdist . st mldist\n    : i prevok ? == which 0 . st llok ? == which 1 . st ofok . st mlok\n    : i prevlog ? == which 0 . st lllg ? == which 1 . st oflg . st mllg\n    : *i prevp ( vec_data [i] prev )\n    : ~ i cost_rep -1\n    ? == prevok 1 { = cost_rep ( __zs_dist_cost cntp nsym prevp 64 prevlog ) } {}\n    // The predefined table: normalize its -1 entries to one cell for\n    // pricing (that is what they get in the real table).\n    : ( Vec i ) defnorm ( __zs_zeros 64 )\n    : *i dfp ( vec_data [i] deffreq )\n    : *i dnp ( vec_data [i] defnorm )\n    : ~ i q 0\n    ~ < q defn {\n        : i f # i . dfp q\n        = . dnp q ? < f 0 1 f\n        = q + q 1\n    }\n    : i cost_pre ( __zs_dist_cost cntp nsym dnp 64 deflog )\n    // A fresh table, if the block can pay for one.\n    : ~ i cost_new -1\n    : ( Vec i ) norm ( __zs_zeros ? > nsym 0 nsym 1 )\n    : ( Vec u ) ncount ( vec_new [u] )\n    : ~ i newlog 0\n    : ~ i maxsym -1\n    ? >= nseq 24 {\n        // Resolution the sequence count can pay for, floored at what\n        // the alphabet needs and capped at what the format allows.\n        : ~ i log - ( __zs_hbit - nseq 1 ) 2\n        : i minlog + ( __zs_hbit nsym ) 1\n        ? < log minlog { = log minlog } {}\n        ? < log 5 { = log 5 } {}\n        ? > log maxlog { = log maxlog } {}\n        = maxsym ( __zs_normalize counts norm nsym log )\n        ? > maxsym 0 {\n            = newlog log\n            : ~ ZsBw w @ ZsBw { ( vec_new [u] ) 0 0 }\n            ( __zs_write_ncount w norm + maxsym 1 log )\n            ( __zs_bw_close_fwd w )\n            ( vec_extend [u] ncount . w buf )\n            : *i normp ( vec_data [i] norm )\n            = cost_new + ( __zs_dist_cost cntp nsym normp nsym log )\n            << * ( vec_len [u] ncount ) 8 4\n        } {}\n    } {}\n    // Cheapest wins; a tie keeps the standing table (no header bytes,\n    // no new state).\n    : ~ i mode 0\n    ? & >= cost_rep 0 | < cost_new 0 <= cost_rep cost_new {\n        ? <= cost_rep cost_pre { = mode 3 } {}\n    } {}\n    ? & != mode 3 >= cost_new 0 {\n        ? < cost_new cost_pre { = mode 2 } {}\n    } {}\n    = . mp 0 mode\n    ? == mode 2 {\n        ( vec_extend [u] tabs ncount )\n        : ZsCt ct ( __zs_ct_build norm + maxsym 1 newlog )\n        // The fresh table becomes the standing one.\n        : *i normp2 ( vec_data [i] norm )\n        = q 0\n        ~ < q 64 { = . prevp q ? < q nsym # i . normp2 q 0 = q + q 1 }\n        ? == which 0 { = . st lllg newlog = . st llok 1 } {\n            ? == which 1 { = . st oflg newlog = . st ofok 1 } {\n                = . st mllg newlog\n                = . st mlok 1\n            }\n        }\n        ^ ct\n    } {}\n    ? == mode 3 {\n        ^ ( __zs_ct_build prev 64 prevlog )\n    } {}\n    // Predefined: it becomes the standing table too — that is what the\n    // decoder will be holding. Stored RAW, -1 cells intact: the decoder\n    // builds its predef table with the \"less than one\" cells taken from\n    // the TOP, and a repeat must rebuild exactly that table, not a\n    // lookalike whose 1-cells were spread normally.\n    = q 0\n    ~ < q 64 { = . prevp q ? < q defn # i . dfp q 0 = q + q 1 }\n    ? == which 0 { = . st lllg deflog = . st llok 1 } {\n        ? == which 1 { = . st oflg deflog = . st ofok 1 } {\n            = . st mllg deflog\n            = . st mlok 1\n        }\n    }\n    ^ ( __zs_ct_build deffreq defn deflog )\n}\n\n@ __zs_write_block ( Vec u ) out ( Vec u ) src i bstart i blen i last\n( Vec u ) lits ( Vec i ) sll ( Vec i ) sml ( Vec i ) soff\n( Vec i ) llb ( Vec i ) mlb ( Vec i ) llx ( Vec i ) mlx inout ZsWst wst → v {\n    : i nseq ( vec_len [i] sll )\n    : i nlit ( vec_len [u] lits )\n    : ( Vec u ) body ( vec_with_cap [u] + blen 64 )\n    ? ! ( __zs_lit_compress body lits wst ) {\n        ( __zs_lit_header body nlit )\n        ( vec_extend [u] body lits )\n    } {}\n    ( __zs_nseq_header body nseq )\n    ? > nseq 0 {\n        : *i llp ( vec_data [i] sll )\n        : *i mlp ( vec_data [i] sml )\n        : *i ofp ( vec_data [i] soff )\n        : *i llxp ( vec_data [i] llx )\n        : *i mlxp ( vec_data [i] mlx )\n        : *i llbp ( vec_data [i] llb )\n        : *i mlbp ( vec_data [i] mlb )\n        // Codes first: they are needed twice, to count and to encode.\n        : ( Vec i ) llc ( __zs_zeros nseq )\n        : ( Vec i ) mlc ( __zs_zeros nseq )\n        : ( Vec i ) ofc ( __zs_zeros nseq )\n        : ( Vec i ) llcnt ( __zs_zeros 36 )\n        : ( Vec i ) mlcnt ( __zs_zeros 53 )\n        : ( Vec i ) ofcnt ( __zs_zeros 32 )\n        : *i llcp ( vec_data [i] llc )\n        : *i mlcp ( vec_data [i] mlc )\n        : *i ofcp ( vec_data [i] ofc )\n        : *i llnp ( vec_data [i] llcnt )\n        : *i mlnp ( vec_data [i] mlcnt )\n        : *i ofnp ( vec_data [i] ofcnt )\n        : ~ i k 0\n        ~ < k nseq {\n            : i a ( __zs_ll_code llb # i . llp k )\n            : i b ( __zs_ml_code mlb # i . mlp k )\n            : i c ( __zs_hbit # i . ofp k )\n            = . llcp k a\n            = . mlcp k b\n            = . ofcp k c\n            = . llnp a + # i . llnp a 1\n            = . mlnp b + # i . mlnp b 1\n            = . ofnp c + # i . ofnp c 1\n            = k + k 1\n        }\n        // Tables, into a side buffer: the mode byte that describes them\n        // has to be written first.\n        : ( Vec i ) deffreq ( __zs_zeros 64 )\n        : ( Vec i ) modeslot ( __zs_zeros 3 )\n        : ( Vec u ) tabs ( vec_new [u] )\n        : *i msp ( vec_data [i] modeslot )\n        : i lldefn ( __zs_ll_default deffreq )\n        : ZsCt llct ( __zs_seq_ctable tabs llcnt 36 9 nseq deffreq lldefn 6 modeslot wst 0 )\n        : i llmode # i . msp 0\n        : i ofdefn ( __zs_of_default deffreq )\n        : ZsCt ofct ( __zs_seq_ctable tabs ofcnt 32 8 nseq deffreq ofdefn 5 modeslot wst 1 )\n        : i ofmode # i . msp 0\n        : i mldefn ( __zs_ml_default deffreq )\n        : ZsCt mlct ( __zs_seq_ctable tabs mlcnt 53 9 nseq deffreq mldefn 6 modeslot wst 2 )\n        : i mlmode # i . msp 0\n        ( vec_push [u] body # u | | << llmode 6 << ofmode 4 << mlmode 2 )\n        ( vec_extend [u] body tabs )\n        : ~ ZsBw w @ ZsBw { ( vec_new [u] ) 0 0 }\n        // Encoded last sequence first: the decoder reads backward.\n        : i lastn - nseq 1\n        : i llc0 # i . llcp lastn\n        : i mlc0 # i . mlcp lastn\n        : i ofc0 # i . ofcp lastn\n        : ~ i lls ( __zs_ct_init llct llc0 )\n        : ~ i ofs ( __zs_ct_init ofct ofc0 )\n        : ~ i mls ( __zs_ct_init mlct mlc0 )\n        ( __zs_bw_add w - # i . llp lastn # i . llbp llc0 # i . llxp llc0 )\n        ( __zs_bw_add w - # i . mlp lastn # i . mlbp mlc0 # i . mlxp mlc0 )\n        ( __zs_bw_add w - # i . ofp lastn << 1 ofc0 ofc0 )\n        : ~ i n - nseq 2\n        ~ >= n 0 {\n            : i a # i . llcp n\n            : i b # i . mlcp n\n            : i c # i . ofcp n\n            // Offset, match, then literals: the reverse of the order a\n            // decoder updates its states in, because it reads this\n            // stream from the far end.\n            = ofs ( __zs_ct_encode w ofct ofs c )\n            = mls ( __zs_ct_encode w mlct mls b )\n            = lls ( __zs_ct_encode w llct lls a )\n            ( __zs_bw_add w - # i . llp n # i . llbp a # i . llxp a )\n            ( __zs_bw_add w - # i . mlp n # i . mlbp b # i . mlxp b )\n            ( __zs_bw_add w - # i . ofp n << 1 c c )\n            = n - n 1\n        }\n        ( __zs_ct_flush w mlct mls )\n        ( __zs_ct_flush w ofct ofs )\n        ( __zs_ct_flush w llct lls )\n        ( __zs_bw_close w )\n        ( vec_extend [u] body . w buf )\n    } {}\n    : i bodylen ( vec_len [u] body )\n    ? < bodylen blen {\n        ( __zs_block_header out 2 bodylen last )\n        ( vec_extend [u] out body )\n    } {\n        ( __zs_block_header out 0 blen last )\n        ( vec_extend_range [u] out src bstart blen )\n    }\n}\n\n// Frame header. The window has to cover the largest match distance the\n// block encoder can emit, and the declared content size lets a decoder\n// allocate the output in one go.\n@ __zs_write_header ( Vec u ) out i srclen → v {\n    ( __zs_pu32 out ZS_MAGIC )\n    : i fcs_flag ? <= srclen 0xFFFFFFFF 2 3\n    : ~ i wlog 10\n    ~ & < wlog 27 < << 1 wlog srclen { = wlog + wlog 1 }\n    : i desc | << fcs_flag 6 4  // + content checksum\n    ( vec_push [u] out # u desc )\n    ( vec_push [u] out # u << - wlog 10 3 )  // exponent, mantissa 0\n    ? == fcs_flag 2 {\n        ( __zs_pu32 out srclen )\n    } {\n        ( __zs_pu32 out & srclen 0xFFFFFFFF )\n        ( __zs_pu32 out >> # u64 srclen 32 )\n    }\n    ^ v\n}\n\n// Greedy search depth per level — and an honest one.\n//\n// Level 1 takes the first candidate in the chain and never looks aside;\n// levels 2–12 examine four and consider starting one byte later (lazy\n// matching), worth about 4 % on text. Depth beyond four was measured\n// and did not pay UNDER A GREEDY PARSE: the extra candidates are longer\n// matches further back, each locally cheaper and globally worse,\n// because taking one abandons the recent offset the following\n// sequences would have ridden for two bits apiece. That is not a law of\n// nature, it is the greedy parse showing through — and from level 13 up\n// the encoder switches to the optimal parser (__zs_encode_opt), where\n// depth DOES pay because every candidate is priced against every other\n// path through the block.\n@ __zs_attempts i level → i {\n    ? <= level 1 { ^ 1 } {}\n    ^ 4\n}\n\n// The best match at `at`, priced rather than merely measured. A longer\n// match that lies further back is not automatically better: its offset\n// costs bits, and a match at the most recent offset costs almost none.\n// Choosing by length alone is why more search made the output BIGGER —\n// level 12 lost to level 1 on ordinary text until this was a price.\n//\n// Reports through `out`: slot 0 = length, slot 1 = offset. Returns the\n// score in eighths of a bit saved, or -1 when nothing matched.\n@ __zs_find * u p ( Vec i ) htab ( Vec i ) chain i hlog i chainmask\ni attempts i at i bend i rep0 i ll ( Vec i ) out → i {\n    : *i hp ( vec_data [i] htab )\n    : *i cp ( vec_data [i] chain )\n    : *i op ( vec_data [i] out )\n    : ~ i best 0\n    : ~ i bestoff 0\n    : ~ i bestscore -1\n    // The most recent offset first: it is two bits, so it wins ties and\n    // most near-ties, and keeping it alive is what makes runs cheap.\n    ? & > ll 0 & > rep0 0 >= - at rep0 0 {\n        : i ml ( __zs_matchlen p - at rep0 at bend )\n        ? >= ml ZS_MIN_MATCH {\n            = best ml\n            = bestoff rep0\n            = bestscore - * ml ZS_LIT_BITS 1\n        } {}\n    } {}\n    : ~ i cand # i . hp ( __zs_hash4 p at hlog )\n    : ~ i tries attempts\n    ~ & > cand 0 > tries 0 {\n        : i cpos - cand 1\n        : i off - at cpos\n        ? > off 0 {\n            : i ml ( __zs_matchlen p cpos at bend )\n            ? >= ml ZS_MIN_MATCH {\n                : i cost ? & > ll 0 == off rep0 1 ( __zs_hbit off )\n                : i score - * ml ZS_LIT_BITS cost\n                ? > score bestscore {\n                    = best ml\n                    = bestoff off\n                    = bestscore score\n                } {}\n            } {}\n        } {}\n        = cand # i . cp & cpos chainmask\n        = tries - tries 1\n    }\n    = . op 0 best\n    = . op 1 bestoff\n    ^ bestscore\n}\n\n// ── Optimal parser (levels 13+) ─────────────────────────────────────\n//\n// The greedy parser above chooses each match on its own; this one\n// chooses the SET of matches with the smallest priced total, which is\n// the difference between beating `zstd -3` and chasing `zstd -19`.\n//\n// It is a shortest-path problem: node = position in the block, edge =\n// one literal or one match, weight = the bits that choice will cost.\n// Positions are relaxed left to right, so by the time a position is\n// extended its own cost is final. Three things make the classic DP\n// match this format:\n//\n//  * REPEAT OFFSETS make edge weights history-dependent — offbase 1\n//    costs ~2 bits but only if the offset matches a rep slot, and the\n//    slots depend on the path. Exact handling would need reps in the\n//    node state; like the reference's btultra2, each position instead\n//    CARRIES the rep state of its best arrival, which is exact along\n//    the chosen path and approximate off it.\n//  * The LITERALS-LENGTH code prices a whole run at once, so a match\n//    edge pays its run's LL code and a literal edge pays only the\n//    literal byte; the run length rides along in the arrival state.\n//  * Prices must come from somewhere. Pass 1 prices literals from the\n//    block's own byte counts and sequences from the predefined\n//    distributions; pass 2 reprices everything from what pass 1\n//    actually chose and parses again. (The reference's btultra2 is\n//    this same two-pass shape.)\n//\n// A match longer than `suff` ends deliberation: it is taken whole and\n// the parse resumes after it, exactly so that a megabyte of zeros costs\n// one decision, not half a billion relaxations.\n\n: i ZS_OPT_SUFF 1024\n: i ZS_OPT_INF 0x0FFFFFFFFFFFFFFF\n: i ZS_H3_MASK 65535\n\n// log2 in 1/16ths of a bit: floor-log2 plus a parabolic correction for\n// the fraction, within ~0.02 bit of the true value — plenty for prices.\n@ __zs_log2_16 i x → i {\n    ? <= x 1 { ^ 0 } {}\n    : i l ( __zs_hbit x )\n    : ~ i frac 0\n    ? >= l 4 { = frac & >> x - l 4 15 } { = frac & << x - 4 l 15 }\n    : i corr >> + * * frac - 16 frac 5 128 8\n    ^ + + << l 4 frac corr\n}\n\n// Symbol prices from occurrence counts: price = log2(total/count),\n// in 1/16 bits. An unseen symbol prices one bit past the rarest.\n@ __zs_prices_16 * i cnt i n * i price → v {\n    : ~ i total 0\n    : ~ i s 0\n    ~ < s n { = total + total # i . cnt s = s + s 1 }\n    ? <= total 0 { = total 1 } {}\n    : i lt ( __zs_log2_16 total )\n    = s 0\n    ~ < s n {\n        : i c # i . cnt s\n        = . price s ? > c 0 - lt ( __zs_log2_16 c ) + lt 16\n        = s + s 1\n    }\n}\n\n@ __zs_fill0 * i pp i n → v {\n    : ~ i s 0\n    ~ < s n { = . pp s 0 = s + s 1 }\n}\n\n// Code lookups over raw base-table pointers (the Vec forms above walk\n// the same tables; these avoid re-fetching vec_data in a hot loop).\n@ __zs_ll_code_p * i basep i ll → i {\n    ? < ll 16 { ^ ll } {}\n    : ~ i c 16\n    ~ & < c 35 <= # i . basep + c 1 ll { = c + c 1 }\n    ^ c\n}\n\n@ __zs_ml_code_p * i basep i ml → i {\n    ? < ml 35 { ^ - ml 3 } {}\n    : ~ i c 32\n    ~ & < c 52 <= # i . basep + c 1 ml { = c + c 1 }\n    ^ c\n}\n\n// Everything one block's parse needs, behind one pointer: the input,\n// the match-finder tables, the DP arrays, and the price tables. The\n// arrays are allocated once per encode and rewritten per block.\n: ZsOpt {\n    * u p\n    i bstart\n    i blen\n    * i hp  // hash4 chain heads\n    * i cp  // chain links\n    * i h3p  // hash3 last-position table\n    i hlog\n    i chainmask\n    i attempts\n    i insert  // 1 = build the chain while parsing (pass 1 only)\n    i suff\n    i skipu  // skip-until, set by an immediate take\n    * i cost  // DP: best arrival price, 1/16 bits\n    * i bln  // DP: arriving match length; 0 = literal\n    * i bof  // DP: arriving match offset (raw distance)\n    * i bll  // DP: the match's literal-run length\n    * i blit  // DP: literal run at this arrival\n    * i br0  // DP: rep state at this arrival\n    * i br1\n    * i br2\n    * i litp  // price tables (extras folded in)\n    * i llp\n    * i mlp\n    * i ofp\n    * i llbp  // code base tables\n    * i mlbp\n    * i cco  // candidate cache: offsets, 8 slots per position\n    * i ccm  // candidate cache: match lengths\n    * i ccn  // candidate cache: how many slots are filled\n    i rep0  // rep state at block start\n    i rep1\n    i rep2\n}\n\n// Relax one candidate over a run of lengths. The ML code is stepped\n// incrementally — its base table is sorted, so a length walk crosses\n// each code boundary once instead of looking every length up.\n@ __zs_opt_relax inout ZsOpt o i k i cbase i off i ofprice i lmin i lmax\ni nr0 i nr1 i nr2 → v {\n    ? > lmin lmax { ^ v } {}\n    : *i costp . o cost\n    : *i blnp . o bln\n    : *i bofp . o bof\n    : *i bllp . o bll\n    : *i blitp . o blit\n    : *i br0p . o br0\n    : *i br1p . o br1\n    : *i br2p . o br2\n    : *i mlpp . o mlp\n    : *i mlbp2 . o mlbp\n    : i lr # i . blitp k\n    : i c0 + cbase ofprice\n    : ~ i mlc ( __zs_ml_code_p mlbp2 lmin )\n    : ~ i nextb ? < mlc 52 # i . mlbp2 + mlc 1 ZS_OPT_INF\n    : ~ i L lmin\n    ~ <= L lmax {\n        ~ >= L nextb {\n            = mlc + mlc 1\n            = nextb ? < mlc 52 # i . mlbp2 + mlc 1 ZS_OPT_INF\n        }\n        : i tot + c0 # i . mlpp mlc\n        : i j + k L\n        ? < tot # i . costp j {\n            = . costp j tot\n            = . blnp j L\n            = . bofp j off\n            = . bllp j lr\n            = . blitp j 0\n            = . br0p j nr0\n            = . br1p j nr1\n            = . br2p j nr2\n        } {}\n        = L + L 1\n    }\n}\n\n// All match edges out of position k: the three rep offsets (length 3\n// up — an ML code starts at 3, and a rep is the cheapest offset there\n// is), one hash3 candidate (the length-3 matches the 4-byte hash can\n// never see), and the hash4 chain. Chain candidates are kept only when\n// strictly longer than everything cheaper: the chain yields offsets in\n// increasing order, so for any target length the first candidate that\n// covers it is the cheapest, and each candidate relaxes only the\n// lengths the previous one could not reach.\n@ __zs_opt_matches inout ZsOpt o i k → v {\n    : *u p . o p\n    : i bstart . o bstart\n    : i n . o blen\n    : i absk + bstart k\n    : i bend + bstart n\n    : *i costp . o cost\n    : *i blitp . o blit\n    : *i br0p . o br0\n    : *i br1p . o br1\n    : *i br2p . o br2\n    : *i llpp . o llp\n    : *i ofpp . o ofp\n    : *i llbp2 . o llbp\n    : i ck # i . costp k\n    : i lr # i . blitp k\n    : i r0 # i . br0p k\n    : i r1 # i . br1p k\n    : i r2 # i . br2p k\n    : i cbase + ck # i . llpp 0\n    : i suff . o suff\n    : ~ i bestlen 2\n    : ~ i bestoff 0\n    : ~ i bestofp 0\n    // Rep candidates. With no literals before the match the slots\n    // shift: rep0 itself is unreachable and rep0-1 appears — mirror of\n    // the decoder's __zs_offset.\n    : ~ i ri 0\n    ~ < ri 3 {\n        : ~ i rv 0\n        ? > lr 0 {\n            = rv ? == ri 0 r0 ? == ri 1 r1 r2\n        } {\n            = rv ? == ri 0 r1 ? == ri 1 r2 - r0 1\n        }\n        : i ob + ri 1\n        ? & > rv 0 <= rv absk {\n            : i src0 - absk rv\n            ? == # i . p src0 # i . p absk {\n                : i ml ( __zs_matchlen p src0 absk bend )\n                ? >= ml 3 {\n                    : i ofprice # i . ofpp ( __zs_hbit ob )\n                    : ~ i nr0 rv\n                    : ~ i nr1 r0\n                    : ~ i nr2 r1\n                    ? == rv r0 { = nr1 r1 = nr2 r2 } {\n                        ? == rv r1 { = nr2 r2 } {}\n                    }\n                    ? >= ml suff {\n                        ? > ml bestlen {\n                            = bestlen ml\n                            = bestoff rv\n                            = bestofp ofprice\n                        } {}\n                    } {\n                        ( __zs_opt_relax o k cbase rv ofprice 3 ml nr0 nr1 nr2 )\n                        ? > ml bestlen {\n                            = bestlen ml\n                            = bestoff rv\n                            = bestofp ofprice\n                        } {}\n                    }\n                } {}\n            } {}\n        } {}\n        = ri + ri 1\n    }\n    : *i ccop . o cco\n    : *i ccmp . o ccm\n    : *i ccnp . o ccn\n    : i cslot << k 3\n    ? == . o insert 1 {\n        = . ccnp k 0\n        ? < bestlen suff {\n            // hash3: one candidate, the most recent position with these\n            // three bytes. Worth having because a length-3 match at a\n            // small offset beats three literals, and the 4-byte hash is\n            // blind to it.\n            : *i h3pp . o h3p\n            : i v3 | | # i . p absk << # i . p + absk 1 8 << # i . p + absk 2 16\n            : i h3i2 * 2 & >> * v3 2654435761 16 ZS_H3_MASK\n            : ~ i h3q 0\n            ~ & < h3q 2 < bestlen suff {\n                : i cand3 # i . h3pp + h3i2 h3q\n                = h3q + h3q 1\n                ? > cand3 0 {\n                    : i cpos - cand3 1\n                    : i off - absk cpos\n                    ? & > off 0 == # i . p cpos # i . p absk {\n                        : i ml ( __zs_matchlen p cpos absk bend )\n                        ? & >= ml 3 > ml bestlen {\n                            : i ofprice # i . ofpp ( __zs_hbit + off 3 )\n                            : ~ i nr0 off\n                            : ~ i nr1 r0\n                            : ~ i nr2 r1\n                            ? == off r0 { = nr1 r1 = nr2 r2 } {\n                                ? == off r1 { = nr2 r2 } {}\n                            }\n                            = . ccop + cslot # i . ccnp k off\n                            = . ccmp + cslot # i . ccnp k ml\n                            = . ccnp k + # i . ccnp k 1\n                            ? >= ml suff {\n                                = bestlen ml\n                                = bestoff off\n                                = bestofp ofprice\n                            } {\n                                : i lmin ? > + bestlen 1 3 + bestlen 1 3\n                                ( __zs_opt_relax o k cbase off ofprice lmin ml nr0 nr1 nr2 )\n                                = bestlen ml\n                                = bestoff off\n                                = bestofp ofprice\n                            }\n                        } {}\n                    } {}\n                } {}\n            }\n        } {}\n        ? & < bestlen suff <= + k 4 n {\n            : *i hpp . o hp\n            : *i cp2 . o cp\n            : i chainmask . o chainmask\n            : i h4 ( __zs_hash4 p absk . o hlog )\n            : ~ i cand # i . hpp h4\n            : ~ i tries . o attempts\n            : ~ i walked * . o attempts 8\n            : ~ b stop F\n            ~ & & > cand 0 > tries 0 & > walked 0 ! stop {\n                : i cpos - cand 1\n                : i off - absk cpos\n                = walked - walked 1\n                ? > off 0 {\n                    = tries - tries 1\n                    ? >= + absk bestlen bend { = stop T } {\n                        // One byte decides whether this candidate can\n                        // even beat the best so far; most cannot.\n                        ? == # i . p + cpos bestlen # i . p + absk bestlen {\n                            : i ml ( __zs_matchlen p cpos absk bend )\n                            ? & >= ml 4 > ml bestlen {\n                                : i ofprice # i . ofpp ( __zs_hbit + off 3 )\n                                : ~ i nr0 off\n                                : ~ i nr1 r0\n                                : ~ i nr2 r1\n                                ? == off r0 { = nr1 r1 = nr2 r2 } {\n                                    ? == off r1 { = nr2 r2 } {}\n                                }\n                                ? < # i . ccnp k 8 {\n                                    = . ccop + cslot # i . ccnp k off\n                                    = . ccmp + cslot # i . ccnp k ml\n                                    = . ccnp k + # i . ccnp k 1\n                                } {}\n                                ? >= ml suff {\n                                    = bestlen ml\n                                    = bestoff off\n                                    = bestofp ofprice\n                                    = stop T\n                                } {\n                                    : i lmin ? > + bestlen 1 4 + bestlen 1 4\n                                    ( __zs_opt_relax o k cbase off ofprice lmin ml nr0 nr1 nr2 )\n                                    = bestlen ml\n                                    = bestoff off\n                                    = bestofp ofprice\n                                }\n                            } {}\n                        } {}\n                    }\n                } {}\n                = cand # i . cp2 & cpos chainmask\n            }\n        } {}\n    } {\n        // Pass 2: the chain now holds positions AHEAD of this one as\n        // well as behind it, so walking it from the head would spend\n        // the whole budget on the future. The candidates pass 1 found\n        // are cached per position instead — reps are re-evaluated\n        // (their offsets depend on the path, which repricing changes),\n        // the ladder is replayed.\n        : i cn # i . ccnp k\n        : ~ i ci 0\n        ~ < ci cn {\n            : i off # i . ccop + cslot ci\n            : i ml0 # i . ccmp + cslot ci\n            : i ml ? > ml0 - n k - n k ml0\n            : i minl ? >= ml0 4 4 3\n            ? > ml bestlen {\n                : i ofprice # i . ofpp ( __zs_hbit + off 3 )\n                : ~ i nr0 off\n                : ~ i nr1 r0\n                : ~ i nr2 r1\n                ? == off r0 { = nr1 r1 = nr2 r2 } {\n                    ? == off r1 { = nr2 r2 } {}\n                }\n                ? >= ml suff {\n                    = bestlen ml\n                    = bestoff off\n                    = bestofp ofprice\n                    = ci cn\n                } {\n                    : i lmin ? > + bestlen 1 minl + bestlen 1 minl\n                    ( __zs_opt_relax o k cbase off ofprice lmin ml nr0 nr1 nr2 )\n                    = bestlen ml\n                    = bestoff off\n                    = bestofp ofprice\n                }\n            } {}\n            = ci + ci 1\n        }\n    }\n    // A match past `suff` ends deliberation: relax its endpoint alone\n    // and jump the cursor past it.\n    ? >= bestlen suff {\n        : ~ i nr0 bestoff\n        : ~ i nr1 r0\n        : ~ i nr2 r1\n        ? == bestoff r0 { = nr1 r1 = nr2 r2 } {\n            ? == bestoff r1 { = nr2 r2 } {}\n        }\n        ( __zs_opt_relax o k cbase bestoff bestofp bestlen bestlen nr0 nr1 nr2 )\n        = . o skipu + k bestlen\n    } {}\n}\n\n@ __zs_opt_parse inout ZsOpt o → v {\n    : *u p . o p\n    : i bstart . o bstart\n    : i n . o blen\n    : i bend + bstart n\n    : *i costp . o cost\n    : *i blnp . o bln\n    : *i blitp . o blit\n    : *i br0p . o br0\n    : *i br1p . o br1\n    : *i br2p . o br2\n    : *i litpp . o litp\n    : *i llpp . o llp\n    : *i llbp3 . o llbp\n    : *i hpp . o hp\n    : *i cp2 . o cp\n    : *i h3pp . o h3p\n    : i hlog . o hlog\n    : i chainmask . o chainmask\n    : i do_insert . o insert\n    = . costp 0 0\n    = . blnp 0 0\n    = . blitp 0 0\n    = . br0p 0 . o rep0\n    = . br1p 0 . o rep1\n    = . br2p 0 . o rep2\n    : ~ i k 1\n    ~ <= k n { = . costp k ZS_OPT_INF = k + k 1 }\n    = . o skipu 0\n    = k 0\n    ~ < k n {\n        ? >= k . o skipu {\n            : i absk + bstart k\n            : i ck # i . costp k\n            // The literal edge. Its price carries the MARGINAL growth\n            // of the literals-length code: a run that will cost a\n            // bigger LL code when the next match lands must look more\n            // expensive while it is being laid down, or the parse\n            // prefers literals it cannot actually afford. (This is a\n            // potential transform — the total over any path is\n            // unchanged, the attribution moves; the match edge pays\n            // llp[0] as its share below.)\n            : i run0 # i . blitp k\n            : i dll - # i . llpp ( __zs_ll_code_p llbp3 + run0 1 )\n            # i . llpp ( __zs_ll_code_p llbp3 run0 )\n            : i lc + + ck # i . litpp # i . p absk dll\n            ? < lc # i . costp + k 1 {\n                = . costp + k 1 lc\n                = . blnp + k 1 0\n                = . blitp + k 1 + # i . blitp k 1\n                = . br0p + k 1 # i . br0p k\n                = . br1p + k 1 # i . br1p k\n                = . br2p + k 1 # i . br2p k\n            } {}\n            ? <= + k 3 n { ( __zs_opt_matches o k ) } {}\n            ? == do_insert 1 {\n                ? <= + absk 4 bend {\n                    : i h4 ( __zs_hash4 p absk hlog )\n                    = . cp2 & absk chainmask # i . hpp h4\n                    = . hpp h4 + absk 1\n                } {}\n                ? <= + absk 3 bend {\n                    : i v3 | | # i . p absk << # i . p + absk 1 8 << # i . p + absk 2 16\n                    : i h3w * 2 & >> * v3 2654435761 16 ZS_H3_MASK\n                    = . h3pp + h3w 1 # i . h3pp h3w\n                    = . h3pp h3w + absk 1\n                } {}\n            } {}\n        } {}\n        = k + k 1\n    }\n}\n\n// Walk the DP result back into sequences, then forward into the\n// (lits, sll, sml, soff) form the block writer takes. The forward walk\n// replays the decoder's rep bookkeeping, so the offbase chosen here is\n// exactly what the decoder will resolve back to the same distance.\n@ __zs_opt_extract inout ZsOpt o ( Vec u ) srcv ( Vec u ) lits\n( Vec i ) sll ( Vec i ) sml ( Vec i ) soff * i reps → v {\n    : i n . o blen\n    : i bstart . o bstart\n    : *i blnp . o bln\n    : *i bofp . o bof\n    : *i bllp . o bll\n    : ( Vec i ) tll ( vec_new [i] )\n    : ( Vec i ) tml ( vec_new [i] )\n    : ( Vec i ) tof ( vec_new [i] )\n    : ~ i j n\n    ~ > j 0 {\n        : i L # i . blnp j\n        ? == L 0 { = j - j 1 } {\n            : i ll # i . bllp j\n            ( vec_push [i] tll ll )\n            ( vec_push [i] tml L )\n            ( vec_push [i] tof # i . bofp j )\n            = j - j + L ll\n        }\n    }\n    : i ns ( vec_len [i] tll )\n    : *i tllp ( vec_data [i] tll )\n    : *i tmlp ( vec_data [i] tml )\n    : *i tofp ( vec_data [i] tof )\n    : ~ i r0 # i . reps 0\n    : ~ i r1 # i . reps 1\n    : ~ i r2 # i . reps 2\n    : ~ i cur bstart\n    : ~ i si - ns 1\n    ~ >= si 0 {\n        : i ll # i . tllp si\n        : i L # i . tmlp si\n        : i off # i . tofp si\n        ( vec_extend_range [u] lits srcv cur ll )\n        = cur + cur ll\n        : ~ i ob + off 3\n        ? > ll 0 {\n            ? == off r0 { = ob 1 } {\n                ? == off r1 { = ob 2 } {\n                    ? == off r2 { = ob 3 } {}\n                }\n            }\n        } {\n            ? == off r1 { = ob 1 } {\n                ? == off r2 { = ob 2 } {\n                    ? == off - r0 1 { = ob 3 } {}\n                }\n            }\n        }\n        ( vec_push [i] sll ll )\n        ( vec_push [i] sml L )\n        ( vec_push [i] soff ob )\n        : ~ i nr0 off\n        : ~ i nr1 r0\n        : ~ i nr2 r1\n        ? == off r0 { = nr1 r1 = nr2 r2 } {\n            ? == off r1 { = nr2 r2 } {}\n        }\n        = r0 nr0\n        = r1 nr1\n        = r2 nr2\n        = cur + cur L\n        = si - si 1\n    }\n    ( vec_extend_range [u] lits srcv cur - + bstart n cur )\n    = . reps 0 r0\n    = . reps 1 r1\n    = . reps 2 r2\n}\n\n// Sequence-code prices as the FSE coder will really charge them: the\n// counts are normalized onto the same table the writer will build, and\n// the price of a symbol is its share of that table — quantization\n// included. Entropy-of-counts flattered symbols whose share rounds\n// down, and the parse bought sequences the coder then billed higher.\n@ __zs_seq_prices_q * i cnt i nsym i maxlog i nseq * i price → v {\n    : ( Vec i ) cnt2 ( __zs_zeros nsym )\n    : ( Vec i ) norm ( __zs_zeros nsym )\n    : *i c2 ( vec_data [i] cnt2 )\n    : ~ i t 0\n    ~ < t nsym { = . c2 t # i . cnt t = t + t 1 }\n    : ~ i log - ( __zs_hbit ? > nseq 1 - nseq 1 1 ) 2\n    : i minlog + ( __zs_hbit nsym ) 1\n    ? < log minlog { = log minlog } {}\n    ? < log 5 { = log 5 } {}\n    ? > log maxlog { = log maxlog } {}\n    : i maxsym ( __zs_normalize cnt2 norm nsym log )\n    ? <= maxsym 0 {\n        ( __zs_prices_16 cnt nsym price )\n        ^ v\n    } {}\n    : *i np ( vec_data [i] norm )\n    : i lt << log 4\n    = t 0\n    ~ < t nsym {\n        : i share # i . np t\n        = . price t ? > share 0 - lt ( __zs_log2_16 share ) + lt 16\n        = t + t 1\n    }\n}\n\n// Prices for pass 2: what pass 1 actually chose, counted and turned\n// into bits. Extra-bit costs are folded into each code's price so the\n// relax loop adds one number.\n@ __zs_opt_reprice inout ZsOpt o ( Vec u ) lits ( Vec i ) sll ( Vec i ) sml\n( Vec i ) soff ( Vec i ) scratch ( Vec i ) llx ( Vec i ) mlx → v {\n    : *i cnt ( vec_data [i] scratch )\n    : *i litpp . o litp\n    : *i llpp . o llp\n    : *i mlpp . o mlp\n    : *i ofpp . o ofp\n    : *i llbp2 . o llbp\n    : *i mlbp2 . o mlbp\n    : *i llxp ( vec_data [i] llx )\n    : *i mlxp ( vec_data [i] mlx )\n    : i ns ( vec_len [i] sll )\n    // Literals are priced at the INTEGER bit lengths a Huffman tree\n    // would actually assign, not at fractional entropy: the section is\n    // Huffman-coded, so a byte costs a whole number of bits, and a\n    // parse priced on the fraction buys literals the coder cannot\n    // deliver that cheaply.\n    ( __zs_fill0 cnt 256 )\n    : i nlit ( vec_len [u] lits )\n    : *u lp ( vec_data [u] lits )\n    : ~ i q 0\n    ~ < q nlit { : i b # i . lp q = . cnt b + # i . cnt b 1 = q + q 1 }\n    : ( Vec i ) hlens ( __zs_zeros 256 )\n    : ( Vec i ) hcnt ( __zs_zeros 256 )\n    : *i hcp ( vec_data [i] hcnt )\n    = q 0\n    ~ < q 256 { = . hcp q # i . cnt q = q + q 1 }\n    : i hmax ( __zs_huf_lengths hcnt hlens 256 )\n    : *i hlp3 ( vec_data [i] hlens )\n    ? & > hmax 0 <= hmax ZS_MAX_HUF_BITS {\n        = q 0\n        ~ < q 256 {\n            : i hl # i . hlp3 q\n            = . litpp q ? > hl 0 << hl 4 + << hmax 4 16\n            = q + q 1\n        }\n    } {\n        ( __zs_prices_16 cnt 256 litpp )\n        = q 0\n        ~ < q 256 {\n            ? < # i . litpp q 16 { = . litpp q 16 } {}\n            = q + q 1\n        }\n    }\n    // Sequence codes.\n    : *i sllp ( vec_data [i] sll )\n    : *i smlp ( vec_data [i] sml )\n    : *i sofp ( vec_data [i] soff )\n    ( __zs_fill0 cnt 64 )\n    = q 0\n    ~ < q ns {\n        : i c ( __zs_ll_code_p llbp2 # i . sllp q )\n        = . cnt c + # i . cnt c 1\n        = q + q 1\n    }\n    ( __zs_seq_prices_q cnt 36 9 ns llpp )\n    = q 0\n    ~ < q 36 {\n        = . llpp q + # i . llpp q << # i . llxp q 4\n        = q + q 1\n    }\n    ( __zs_fill0 cnt 64 )\n    = q 0\n    ~ < q ns {\n        : i c ( __zs_ml_code_p mlbp2 # i . smlp q )\n        = . cnt c + # i . cnt c 1\n        = q + q 1\n    }\n    ( __zs_seq_prices_q cnt 53 9 ns mlpp )\n    = q 0\n    ~ < q 53 {\n        = . mlpp q + # i . mlpp q << # i . mlxp q 4\n        = q + q 1\n    }\n    ( __zs_fill0 cnt 64 )\n    = q 0\n    ~ < q ns {\n        : i c ( __zs_hbit # i . sofp q )\n        = . cnt c + # i . cnt c 1\n        = q + q 1\n    }\n    ( __zs_seq_prices_q cnt 32 8 ns ofpp )\n    = q 0\n    ~ < q 32 {\n        = . ofpp q + # i . ofpp q << q 4\n        = q + q 1\n    }\n}\n\n// Pass-1 prices: literals from the block's own byte counts (most of\n// the win — 'e' is not priced like 'q'), sequence codes from the\n// format's predefined distributions.\n@ __zs_opt_price_static inout ZsOpt o i bstart i blen ( Vec i ) scratch\n( Vec i ) llx ( Vec i ) mlx i variant → v {\n    : *i cnt ( vec_data [i] scratch )\n    : *u p . o p\n    : *i litpp . o litp\n    : *i llpp . o llp\n    : *i mlpp . o mlp\n    : *i ofpp . o ofp\n    : *i llxp ( vec_data [i] llx )\n    : *i mlxp ( vec_data [i] mlx )\n    ( __zs_fill0 cnt 256 )\n    : ~ i q 0\n    ~ < q blen {\n        : i b # i . p + bstart q\n        = . cnt b + # i . cnt b 1\n        = q + q 1\n    }\n    ( __zs_prices_16 cnt 256 litpp )\n    = q 0\n    ~ < q 256 {\n        ? < # i . litpp q 16 { = . litpp q 16 } {}\n        = q + q 1\n    }\n    : i lln ( __zs_ll_default scratch )\n    = q 0\n    ~ < q 36 { ? < # i . cnt q 1 { = . cnt q 1 } {} = q + q 1 }\n    ( __zs_prices_16 cnt 36 llpp )\n    = q 0\n    ~ < q 36 {\n        = . llpp q + # i . llpp q << # i . llxp q 4\n        = q + q 1\n    }\n    // Two seed families, both fed to the same keep-the-cheapest\n    // selector: predefined match-length prices settle into one\n    // equilibrium, optimistic ones into another, and which wins is a\n    // property of the data — dictionary text prefers the optimist,\n    // word salad the predefined. Guessing is not required when both\n    // fixed points can simply be visited.\n    ? == variant 1 {\n        = q 0\n        ~ < q 53 {\n            = . mlpp q + << # i . mlxp q 4 48\n            = q + q 1\n        }\n    } {\n        : i mln ( __zs_ml_default scratch )\n        = q 0\n        ~ < q 53 { ? < # i . cnt q 1 { = . cnt q 1 } {} = q + q 1 }\n        ( __zs_prices_16 cnt 53 mlpp )\n        = q 0\n        ~ < q 53 {\n            = . mlpp q + # i . mlpp q << # i . mlxp q 4\n            = q + q 1\n        }\n    }\n    // Offset codes are priced OPTIMISTICALLY in pass 1: the code cost\n    // is set near its floor and only the unavoidable extra bits stay\n    // real. Pessimism here is self-fulfilling — a match kind that pass\n    // 1 never takes is absent from the statistics, so pass 2 prices it\n    // high, so it is never taken; the marginal length-3 match at a\n    // kilobyte's distance sits exactly on that edge, and the iteration\n    // must be allowed to DISCOVER it before the numbers judge it.\n    = q 0\n    ~ < q 32 {\n        = . ofpp q + << q 4 32\n        = q + q 1\n    }\n}\n\n@ __zs_encode_opt ( Vec u ) src i level → ( Vec u ) {\n    : i n ( vec_len [u] src )\n    : ( Vec u ) out ( vec_with_cap [u] + >> n 1 64 )\n    ( __zs_write_header out n )\n    ? == n 0 {\n        ( __zs_block_header out 0 0 1 )\n        ( __zs_pu32 out & ( xxh64 src ) 0xFFFFFFFF )\n        ^ out\n    } {}\n    : *u p ( vec_data [u] src )\n    : ~ i hlog 12\n    ~ & < hlog 20 < << 1 hlog n { = hlog + hlog 1 }\n    : ( Vec i ) htab ( __zs_zeros << 1 hlog )\n    : i chainbits ? > hlog 18 18 hlog\n    : i chainmask - << 1 + chainbits 2 1\n    : ( Vec i ) chain ( __zs_zeros + chainmask 1 )\n    : ( Vec i ) h3tab ( __zs_zeros * 2 + ZS_H3_MASK 1 )\n    : ( Vec i ) llx ( __zs_ll_extra )\n    : ( Vec i ) mlx ( __zs_ml_extra )\n    : ( Vec i ) llb ( __zs_ll_base llx )\n    : ( Vec i ) mlb ( __zs_ml_base mlx )\n    : i cap + ZS_BLOCK_MAX 1\n    : ( Vec i ) vcost ( __zs_zeros cap )\n    : ( Vec i ) vbln ( __zs_zeros cap )\n    : ( Vec i ) vbof ( __zs_zeros cap )\n    : ( Vec i ) vbll ( __zs_zeros cap )\n    : ( Vec i ) vblit ( __zs_zeros cap )\n    : ( Vec i ) vbr0 ( __zs_zeros cap )\n    : ( Vec i ) vbr1 ( __zs_zeros cap )\n    : ( Vec i ) vbr2 ( __zs_zeros cap )\n    : ( Vec i ) vlitp ( __zs_zeros 256 )\n    : ( Vec i ) vllp ( __zs_zeros 36 )\n    : ( Vec i ) vmlp ( __zs_zeros 53 )\n    : ( Vec i ) vofp ( __zs_zeros 32 )\n    : ( Vec i ) scratch ( __zs_zeros 256 )\n    : ( Vec i ) reps3 ( __zs_zeros 3 )\n    : ( Vec i ) vcco ( __zs_zeros << cap 3 )\n    : ( Vec i ) vccm ( __zs_zeros << cap 3 )\n    : ( Vec i ) vccn ( __zs_zeros cap )\n    : *i reps3p ( vec_data [i] reps3 )\n    // The parse state points into the arrays above (which outlive it);\n    // a local the parser advances in place (`inout`).\n    : i attempts ? >= level 19 256 ? >= level 16 64 32\n    : ~ ZsOpt o @ ZsOpt {\n        p 0 0\n        ( vec_data [i] htab ) ( vec_data [i] chain ) ( vec_data [i] h3tab )\n        hlog chainmask attempts 0 ZS_OPT_SUFF 0\n        ( vec_data [i] vcost ) ( vec_data [i] vbln ) ( vec_data [i] vbof )\n        ( vec_data [i] vbll ) ( vec_data [i] vblit )\n        ( vec_data [i] vbr0 ) ( vec_data [i] vbr1 ) ( vec_data [i] vbr2 )\n        ( vec_data [i] vlitp ) ( vec_data [i] vllp ) ( vec_data [i] vmlp ) ( vec_data [i] vofp )\n        ( vec_data [i] llb ) ( vec_data [i] mlb )\n        ( vec_data [i] vcco ) ( vec_data [i] vccm ) ( vec_data [i] vccn )\n        0 0 0\n    }\n    : ( Vec u ) lits ( vec_with_cap [u] ZS_BLOCK_MAX )\n    : ( Vec i ) sll ( vec_new [i] )\n    : ( Vec i ) sml ( vec_new [i] )\n    : ( Vec i ) soff ( vec_new [i] )\n    : ~ ZsWst wst ( __zs_wst_new )\n    : ~ i rep0 1\n    : ~ i rep1 4\n    : ~ i rep2 8\n    : ~ i pos 0\n    // Balanced blocks: 196 kB as 98+98 beats 128+68 — the tables of two\n    // like-sized blocks fit their halves better than one full block's\n    // tables fit a full block and a stub's fit a stub.\n    : i nblk / + n - ZS_BLOCK_MAX 1 ZS_BLOCK_MAX\n    : i tgt / + n - nblk 1 nblk\n    ~ < pos n {\n        : i blen ? > - n pos tgt tgt - n pos\n        = . o bstart pos\n        = . o blen blen\n        = . o rep0 rep0\n        = . o rep1 rep1\n        = . o rep2 rep2\n        : i nseeds ? >= level 19 2 1\n        : i extra ? >= level 19 15 ? >= level 16 3 1\n        : ( Vec u ) bestbody ( vec_new [u] )\n        : ( Vec i ) bestreps ( __zs_zeros 3 )\n        : *i bestrepsp ( vec_data [i] bestreps )\n        : ~ i bestsz ZS_OPT_INF\n        : i last ? == + pos blen n 1 0\n        ( __zs_wst_save wst 0 )\n        : ~ i seed 0\n        ~ < seed nseeds {\n            ( __zs_opt_price_static o pos blen scratch llx mlx seed )\n            // The chain and the candidate cache are built by the very\n            // first parse of the block; every later parse — later\n            // rounds and the other seed alike — replays the cache.\n            = . o insert ? == seed 0 1 0\n            ( __zs_opt_parse o )\n            = . o insert 0\n            ( vec_clear [u] lits )\n            ( vec_clear [i] sll )\n            ( vec_clear [i] sml )\n            ( vec_clear [i] soff )\n            = . reps3p 0 rep0\n            = . reps3p 1 rep1\n            = . reps3p 2 rep2\n            ( __zs_opt_extract o src lits sll sml soff reps3p )\n            : ~ i round 0\n            ~ <= round extra {\n                ? > round 0 {\n                    ( __zs_opt_reprice o lits sll sml soff scratch llx mlx )\n                    ( __zs_opt_parse o )\n                    ( vec_clear [u] lits )\n                    ( vec_clear [i] sll )\n                    ( vec_clear [i] sml )\n                    ( vec_clear [i] soff )\n                    = . reps3p 0 rep0\n                    = . reps3p 1 rep1\n                    = . reps3p 2 rep2\n                    ( __zs_opt_extract o src lits sll sml soff reps3p )\n                } {}\n                : ( Vec u ) trial ( vec_new [u] )\n                // Every trial writes from the SAME pre-block state: a\n                // repeat or treeless choice may only reference tables\n                // that actually went out in a previous block, never\n                // ones a losing trial of THIS block imagined sending.\n                ( __zs_wst_restore wst 0 )\n                ( __zs_write_block trial src pos blen last lits sll sml soff\n                llb mlb llx mlx wst )\n                ? < ( vec_len [u] trial ) bestsz {\n                    = bestsz ( vec_len [u] trial )\n                    ( vec_clear [u] bestbody )\n                    ( vec_extend [u] bestbody trial )\n                    = . bestrepsp 0 # i . reps3p 0\n                    = . bestrepsp 1 # i . reps3p 1\n                    = . bestrepsp 2 # i . reps3p 2\n                    ( __zs_wst_save wst 1 )\n                } {}\n                = round + round 1\n            }\n            = seed + seed 1\n        }\n        // The winner's post-block state is what the next block builds on.\n        ( __zs_wst_restore wst 1 )\n        ( vec_extend [u] out bestbody )\n        = rep0 # i . bestrepsp 0\n        = rep1 # i . bestrepsp 1\n        = rep2 # i . bestrepsp 2\n        = pos + pos blen\n    }\n    ( __zs_pu32 out & ( xxh64 src ) 0xFFFFFFFF )\n    ^ out\n}\n\n@ zstd_encode_at ( Vec u ) src i level → ( Vec u ) {\n    ? >= level 13 { ^ ( __zs_encode_opt src level ) } {}\n    : i n ( vec_len [u] src )\n    : ( Vec u ) out ( vec_with_cap [u] + >> n 1 64 )\n    ( __zs_write_header out n )\n    ? == n 0 {\n        ( __zs_block_header out 0 0 1 )\n        ( __zs_pu32 out & ( xxh64 src ) 0xFFFFFFFF )\n        ^ out\n    } {}\n    : *u p ( vec_data [u] src )\n    : i attempts ( __zs_attempts level )\n    // Hash table sized to the input, chain covering the whole window.\n    : ~ i hlog 12\n    ~ & < hlog 20 < << 1 hlog n { = hlog + hlog 1 }\n    : ( Vec i ) htab ( __zs_zeros << 1 hlog )\n    : i chainbits ? > hlog 18 18 hlog\n    : i chainmask - << 1 + chainbits 2 1\n    : ( Vec i ) chain ( __zs_zeros + chainmask 1 )\n    : *i hp ( vec_data [i] htab )\n    : *i cp ( vec_data [i] chain )\n    // Sequence code tables, shared by every block.\n    : ( Vec i ) llx ( __zs_ll_extra )\n    : ( Vec i ) mlx ( __zs_ml_extra )\n    : ( Vec i ) llb ( __zs_ll_base llx )\n    : ( Vec i ) mlb ( __zs_ml_base mlx )\n    : ( Vec u ) lits ( vec_with_cap [u] ZS_BLOCK_MAX )\n    : ( Vec i ) sll ( vec_new [i] )\n    : ( Vec i ) sml ( vec_new [i] )\n    : ( Vec i ) soff ( vec_new [i] )\n    : ( Vec i ) found ( __zs_zeros 2 )\n    : ~ ZsWst wst ( __zs_wst_new )\n    : *i fp ( vec_data [i] found )\n    : b lazy >= level 2\n    : ~ i rep0 1\n    : ~ i rep1 4\n    : ~ i rep2 8\n    : ~ i pos 0\n    ~ < pos n {\n        : i bstart pos\n        : i bend ? > - n pos ZS_BLOCK_MAX + pos ZS_BLOCK_MAX n\n        : i blen - bend bstart\n        ( vec_clear [u] lits )\n        ( vec_clear [i] sll )\n        ( vec_clear [i] sml )\n        ( vec_clear [i] soff )\n        : ~ i anchor bstart\n        : ~ i at bstart\n        ~ <= + at ZS_MIN_MATCH bend {\n            : i score ( __zs_find p htab chain hlog chainmask attempts\n            at bend rep0 - at anchor found )\n            : ~ i best # i . fp 0\n            : ~ i bestoff # i . fp 1\n            // Lazy matching: a match starting one byte later may be\n            // worth more than this one even after paying for the extra\n            // literal. Only from level 5 up — it doubles the searching.\n            ? & lazy & >= best ZS_MIN_MATCH <= + at + ZS_MIN_MATCH 1 bend {\n                : i s2 ( __zs_find p htab chain hlog chainmask attempts\n                + at 1 bend rep0 + 1 - at anchor found )\n                ? > s2 + score ZS_LIT_BITS {\n                    // Take the literal at `at` and start there instead.\n                    : i hl ( __zs_hash4 p at hlog )\n                    = . cp & at chainmask # i . hp hl\n                    = . hp hl + at 1\n                    = at + at 1\n                    = best # i . fp 0\n                    = bestoff # i . fp 1\n                } {}\n            } {}\n            ? >= best ZS_MIN_MATCH {\n                : i ll - at anchor\n                ( vec_extend_range [u] lits src anchor ll )\n                // The most recent offset costs two bits instead of\n                // twenty — but only when literals precede it, because\n                // with none the repeat slots shift by one.\n                : ~ i ofbase + bestoff 3\n                ? & > ll 0 == bestoff rep0 {\n                    = ofbase 1\n                } {\n                    = rep2 rep1\n                    = rep1 rep0\n                    = rep0 bestoff\n                }\n                ( vec_push [i] sll ll )\n                ( vec_push [i] sml best )\n                ( vec_push [i] soff ofbase )\n                // Index every position the match covers, so a later\n                // match can start anywhere inside it.\n                : ~ i k at\n                ~ & < k + at best <= + k ZS_MIN_MATCH bend {\n                    : i hk ( __zs_hash4 p k hlog )\n                    = . cp & k chainmask # i . hp hk\n                    = . hp hk + k 1\n                    = k + k 1\n                }\n                = at + at best\n                = anchor at\n            } {\n                : i h ( __zs_hash4 p at hlog )\n                = . cp & at chainmask # i . hp h\n                = . hp h + at 1\n                = at + at 1\n            }\n        }\n        ( vec_extend_range [u] lits src anchor - bend anchor )\n        : i last ? == bend n 1 0\n        ( __zs_write_block out src bstart blen last lits sll sml soff\n        llb mlb llx mlx wst )\n        = pos bend\n    }\n    ( __zs_pu32 out & ( xxh64 src ) 0xFFFFFFFF )\n    ^ out\n}\n\n@ zstd_encode ( Vec u ) src → ( Vec u ) {\n    ^ ( zstd_encode_at src 3 )\n}\n\n// The first frame's declared content size, without decoding anything.\n// Absent when the frame was produced by a streaming compressor that did\n// not know the size in advance.\n@ zstd_content_size ( Vec u ) src → ?i {\n    : i n ( vec_len [u] src )\n    ? < n 6 { ^ @ ?i { F } } {}\n    : *u p ( vec_data [u] src )\n    : i magic | | | # i . p 0 << # i . p 1 8 << # i . p 2 16 << # i . p 3 24\n    ? != magic ZS_MAGIC { ^ @ ?i { F } } {}\n    : i desc # i . p 4\n    : i fcs_flag >> desc 6\n    : i single & >> desc 5 1\n    : i did_flag & desc 3\n    ? & == single 0 == fcs_flag 0 { ^ @ ?i { F } } {}\n    : ~ i at 5\n    ? == single 0 { = at + at 1 } {}\n    = at + at ? == did_flag 0 0 ? == did_flag 1 1 ? == did_flag 2 2 4\n    : i nbytes ? == fcs_flag 0 1 ? == fcs_flag 1 2 ? == fcs_flag 2 4 8\n    ? > + at nbytes n { ^ @ ?i { F } } {}\n    : ~ i fcs 0\n    : ~ i k 0\n    ~ < k nbytes {\n        = fcs | fcs << # i . p + at k * k 8\n        = k + k 1\n    }\n    ? == nbytes 2 { = fcs + fcs 256 } {}\n    ^ @ ?i { T fcs }\n}\n","bytes":120855}