@triton.jit
def code_to_seqlen_kernel(code_ptr, seqlen_ptr, B, N, BLK: tl.constexpr):
    pid = tl.program_id(0)
    out = tl.zeros((1,), dtype=tl.int32)
    for nidx in range(tl.cdiv(N, BLK)):
        offs_n = nidx * BLK + tl.arange(0, BLK)
        mask_n = offs_n < N
        code = tl.load(code_ptr + offs_n, mask=mask_n, other=32767 << 48)
        bidx = (code >> 48 & 32767).to(tl.int32)
        x = tl.min((bidx == pid).to(tl.int32) * (offs_n - 65535), axis=0)
        out = tl.minimum(out, x)
    out = tl.where(out == 0, -1, out + 65535)
    tl.store(seqlen_ptr + pid + tl.arange(0, 1), out)
    tl.store(seqlen_ptr + B, N, mask=pid == B - 1)
