// File: /workspace/github_c_makefile_star2_O2/Cyan4973[P]FiniteStateEntropy/programs/zlibh.c
// Function: inflate_table
int inflate_table(codetype type, unsigned short * lens, unsigned codes, code * *table, unsigned *bits, unsigned short *work)
{
    unsigned len;               /* a code's length in bits */
    unsigned sym;               /* index of code symbols */
    unsigned min, max;          /* minimum and maximum code lengths */
    unsigned root;              /* number of index bits for root table */
    unsigned curr;              /* number of index bits for current table */
    unsigned drop;              /* code bits to drop for sub-table */
    int left;                   /* number of prefix codes available */
    unsigned used;              /* code entries in table used */
    unsigned huff;              /* Huffman code */
    unsigned incr;              /* for incrementing code, index */
    unsigned low;               /* low bits for current root entry */
    unsigned mask;              /* mask for low root bits */
    code here;                  /* table entry for duplication */
    code *next;             /* next available space in table */
    const unsigned short *base;     /* base value table to use */
    const unsigned short *extra;    /* extra bits table to use */
    int end;                    /* use base and extra for symbol > end */
    unsigned short count[ZLIBH_MAX_BITS+1];    /* number of codes of each length */
    unsigned short offs[ZLIBH_MAX_BITS+1];     /* offsets in table for each length */
    static const unsigned short lbase[31] = { /* Length codes 257..285 base */
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
    static const unsigned short lext[31] = { /* Length codes 257..285 extra */
        16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
    static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
        8193, 12289, 16385, 24577, 0, 0};
    static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
        16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
        23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
        28, 28, 29, 29, 64, 64};

    /*
    Process a set of code lengths to create a canonical Huffman code.  The
    code lengths are lens[0..codes-1].  Each length corresponds to the
    symbols 0..codes-1.  The Huffman code is generated by first sorting the
    symbols by length from short to long, and retaining the symbol order
    for codes with equal lengths.  Then the code starts with all zero bits
    for the first code of the shortest length, and the codes are integer
    increments for the same length, and zeros are appended as the length
    increases.  For the deflate format, these bits are stored backwards
    from their more natural integer increment ordering, and so when the
    decoding tables are built in the large loop below, the integer codes
    are incremented backwards.

    This routine assumes, but does not check, that all of the entries in
    lens[] are in the range 0..ZLIBH_MAX_BITS.  The caller must assure this.
    1..ZLIBH_MAX_BITS is interpreted as that code length.  zero means that that
    symbol does not occur in this code.

    The codes are sorted by computing a count of codes for each length,
    creating from that a table of starting indices for each length in the
    sorted table, and then entering the symbols in order in the sorted
    table.  The sorted table is work[], with that space being provided by
    the caller.

    The length counts are used for other purposes as well, i.e. finding
    the minimum and maximum length codes, determining if there are any
    codes at all, checking for a valid set of lengths, and looking ahead
    at length counts to determine sub-table sizes when building the
    decoding tables.
    */

    /* accumulate lengths for codes (assumes lens[] all in 0..ZLIBH_MAX_BITS) */
    for (len = 0; len <= ZLIBH_MAX_BITS; len++)
        count[len] = 0;
    for (sym = 0; sym < codes; sym++)
        count[lens[sym]]++;

    /* bound code lengths, force root to be within code lengths */
    root = *bits;
    for (max = ZLIBH_MAX_BITS; max >= 1; max--)
        if (count[max] != 0) break;
    if (root > max) root = max;
    if (max == 0) {                     /* no symbols to code at all */
        here.op = (unsigned char)64;    /* invalid code marker */
        here.bits = (unsigned char)1;
        here.val = (unsigned short)0;
        *(*table)++ = here;             /* make a table to force an error */
        *(*table)++ = here;
        *bits = 1;
        return 0;     /* no symbols, but wait for decoding to report error */
    }
    for (min = 1; min < max; min++)
        if (count[min] != 0) break;
    if (root < min) root = min;

    /* check for an over-subscribed or incomplete set of lengths */
    left = 1;
    for (len = 1; len <= ZLIBH_MAX_BITS; len++) {
        left <<= 1;
        left -= count[len];
        if (left < 0) return -1;        /* over-subscribed */
    }
    if (left > 0 && (type == CODES || max != 1))
        return -1;                      /* incomplete set */

    /* generate offsets into symbol table for each length for sorting */
    offs[1] = 0;
    for (len = 1; len < ZLIBH_MAX_BITS; len++)
        offs[len + 1] = offs[len] + count[len];

    /* sort symbols by length, by symbol order within each length */
    for (sym = 0; sym < codes; sym++)
        if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;

    /*
    Create and fill in decoding tables.  In this loop, the table being
    filled is at next and has curr index bits.  The code being used is huff
    with length len.  That code is converted to an index by dropping drop
    bits off of the bottom.  For codes where len is less than drop + curr,
    those top drop + curr - len bits are incremented through all values to
    fill the table with replicated entries.

    root is the number of index bits for the root table.  When len exceeds
    root, sub-tables are created pointed to by the root entry with an index
    of the low root bits of huff.  This is saved in low to check for when a
    new sub-table should be started.  drop is zero when the root table is
    being filled, and drop is root when sub-tables are being filled.

    When a new sub-table is needed, it is necessary to look ahead in the
    code lengths to determine what size sub-table is needed.  The length
    counts are used for this, and so count[] is decremented as codes are
    entered in the tables.

    used keeps track of how many table entries have been allocated from the
    provided *table space.  It is checked for LENS and DIST tables against
    the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
    the initial root table size constants.  See the comments in inftrees.h
    for more information.

    sym increments through all symbols, and the loop terminates when
    all codes of length max, i.e. all codes, have been processed.  This
    routine permits incomplete codes, so another loop after this one fills
    in the rest of the decoding tables with invalid code markers.
    */

    /* set up for code type */
    switch (type) {
    case CODES:
        base = extra = work;    /* dummy value--not used */
        end = 19;
        break;
    case LENS:
        base = lbase;
        base -= 257;
        extra = lext;
        extra -= 257;
        end = 256;
        break;
    case DISTS:
    default:            /* DISTS */
        base = dbase;
        extra = dext;
        end = -1;
    }

    /* initialize state for loop */
    huff = 0;                   /* starting code */
    sym = 0;                    /* starting code symbol */
    len = min;                  /* starting code length */
    next = *table;              /* current table to fill in */
    curr = root;                /* current table index bits */
    drop = 0;                   /* current bits to drop from code for index */
    low = (unsigned)(-1);       /* trigger new sub-table when len > root */
    used = 1U << root;          /* use root table entries */
    mask = used - 1;            /* mask for comparing low */

    /* check available table space */
    if ((type == LENS && used > ENOUGH_LENS) ||
        (type == DISTS && used > ENOUGH_DISTS))
        return 1;

    /* process all codes and make table entries */
    for (;;) {
        unsigned fill;              /* index for replicating entries */

        /* create table entry */
        here.bits = (unsigned char)(len - drop);
        if ((int)(work[sym]) < end) {
            here.op = (unsigned char)0;
            here.val = work[sym];
        }
        else if ((int)(work[sym]) > end) {
            here.op = (unsigned char)(extra[work[sym]]);
            here.val = base[work[sym]];
        }
        else {
            here.op = (unsigned char)(32 + 64);         /* end of block */
            here.val = 0;
        }

        /* replicate for those indices with low len bits equal to huff */
        incr = 1U << (len - drop);
        fill = 1U << curr;
        min = fill;                 /* save offset to next table */
        do {
            fill -= incr;
            next[(huff >> drop) + fill] = here;
        } while (fill != 0);

        /* backwards increment the len-bit code huff */
        incr = 1U << (len - 1);
        while (huff & incr)
            incr >>= 1;
        if (incr != 0) {
            huff &= incr - 1;
            huff += incr;
        }
        else
            huff = 0;

        /* go to next symbol, update count, len */
        sym++;
        if (--(count[len]) == 0) {
            if (len == max) break;
            len = lens[work[sym]];
        }

        /* create new sub-table if needed */
        if (len > root && (huff & mask) != low) {
            /* if first time, transition to sub-tables */
            if (drop == 0)
                drop = root;

            /* increment past last table */
            next += min;            /* here min is 1 << curr */

            /* determine length of next table */
            curr = len - drop;
            left = (int)(1 << curr);
            while (curr + drop < max) {
                left -= count[curr + drop];
                if (left <= 0) break;
                curr++;
                left <<= 1;
            }

            /* check for enough space */
            used += 1U << curr;
            if ((type == LENS && used > ENOUGH_LENS) ||
                (type == DISTS && used > ENOUGH_DISTS))
                return 1;

            /* point entry in root table to sub-table */
            low = huff & mask;
            (*table)[low].op = (unsigned char)curr;
            (*table)[low].bits = (unsigned char)root;
            (*table)[low].val = (unsigned short)(next - *table);
        }
    }

    /* fill in remaining table entry if code is incomplete (guaranteed to have
    at most one remaining entry, since if the code is incomplete, the
    maximum code length that was allowed to get this far is one bit) */
    if (huff != 0) {
        here.op = (unsigned char)64;            /* invalid code marker */
        here.bits = (unsigned char)(len - drop);
        here.val = (unsigned short)0;
        next[huff] = here;
    }

    /* set return parameters */
    *table += used;
    *bits = root;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r10
xorl %eax, %eax
cmpq $0x10, %rax
je 0x5ee5
andw $0x0, -0x40(%rsp,%rax,2)
incq %rax
jmp 0x5ed4
movl %edx, %ecx
xorl %eax, %eax
cmpq %rax, %rcx
je 0x5efc
movzwl (%rsi,%rax,2), %edx
incw -0x40(%rsp,%rdx,2)
incq %rax
jmp 0x5ee9
movl (%r8), %r12d
movl $0xf, %eax
movl %eax, %ebp
subl $0x1, %eax
jb 0x5f3e
movl %ebp, %edx
cmpw $0x0, -0x40(%rsp,%rdx,2)
je 0x5f04
movl $0x1, %ebx
cmpl %ebp, %r12d
cmovael %ebp, %r12d
movabsq $0x1, %r14
cmpq %r14, %rdx
je 0x5f6b
cmpw $0x0, -0x40(%rsp,%r14,2)
jne 0x5f6e
incq %r14
jmp 0x5f2b
movq (%r10), %rax
leaq 0x4(%rax), %rcx
movq %rcx, (%r10)
movl $0x140, %ecx            # imm = 0x140
movl %ecx, (%rax)
movq (%r10), %rax
leaq 0x4(%rax), %rdx
movq %rdx, (%r10)
movl %ecx, (%rax)
movl $0x1, %r12d
movl %r12d, (%r8)
xorl %eax, %eax
jmp 0x6092
movl %ebp, %r14d
cmpl %r14d, %r12d
cmovbel %r14d, %r12d
movabsq $0x1, %r11
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r11
je 0x5f9e
addl %ebx, %ebx
movzwl -0x40(%rsp,%r11,2), %edx
incq %r11
subl %edx, %ebx
jns 0x5f84
jmp 0x6092
testl %ebx, %ebx
je 0x5fb7
testl %edi, %edi
sete %r11b
cmpl $0x1, %ebp
setne %dl
orb %r11b, %dl
jne 0x6092
andw $0x0, -0xe(%rsp)
xorl %eax, %eax
cmpq $0xe, %rax
je 0x5fd9
movzwl -0x3e(%rsp,%rax,2), %edx
addw -0xe(%rsp,%rax,2), %dx
movw %dx, -0xc(%rsp,%rax,2)
incq %rax
jmp 0x5fbf
xorl %eax, %eax
cmpq %rax, %rcx
je 0x6002
movzwl (%rsi,%rax,2), %edx
testq %rdx, %rdx
je 0x5ffd
movzwl -0x10(%rsp,%rdx,2), %ebx
leal 0x1(%rbx), %r11d
movw %r11w, -0x10(%rsp,%rdx,2)
movw %ax, (%r9,%rbx,2)
incq %rax
jmp 0x5fdb
testl %edi, %edi
je 0x603a
cmpl $0x1, %edi
jne 0x604f
leaq 0xa35e(%rip), %rcx      # 0x10370
movq $-0x202, %rax           # imm = 0xFDFE
addq %rax, %rcx
movq %rcx, -0x58(%rsp)
leaq 0xa308(%rip), %rcx      # 0x10330
addq %rax, %rcx
movq %rcx, -0x60(%rsp)
movl $0x100, -0x70(%rsp)     # imm = 0x100
jmp 0x6070
movl $0x13, %eax
movl %eax, -0x70(%rsp)
movq %r9, -0x60(%rsp)
movq %r9, -0x58(%rsp)
jmp 0x6070
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x70(%rsp)
leaq 0xa391(%rip), %rax      # 0x103f0
movq %rax, -0x58(%rsp)
leaq 0xa345(%rip), %rax      # 0x103b0
movq %rax, -0x60(%rsp)
movl $0x1, %eax
movl %eax, %edx
movl %r12d, %ecx
shll %cl, %edx
cmpl $0x1, %edi
jne 0x6087
cmpl $0x9, %r12d
ja 0x6092
cmpl $0x2, %edi
jne 0x60a1
cmpl $0x9, %r12d
jbe 0x60a1
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edi, -0x50(%rsp)
movq %r8, -0x20(%rsp)
movq %rdx, -0x68(%rsp)
leal -0x1(%rdx), %eax
movl %eax, -0x4c(%rsp)
movq %r10, -0x48(%rsp)
movq (%r10), %r15
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %eax
xorl %edi, %edi
movl %r12d, %ecx
movl $0x0, -0x78(%rsp)
xorl %r10d, %r10d
movl %edx, -0x6c(%rsp)
movq %r9, -0x18(%rsp)
shll %cl, %eax
movl %eax, -0x74(%rsp)
movl %r12d, %r11d
movl %edi, %ecx
movzwl (%r9,%rcx,2), %r12d
movzwl %r12w, %ecx
cmpl %ecx, -0x70(%rsp)
jle 0x6100
xorl %r13d, %r13d
jmp 0x611f
jge 0x6119
addl %ecx, %ecx
movq -0x58(%rsp), %rax
movb (%rax,%rcx), %r13b
movq -0x60(%rsp), %rax
movzwl (%rax,%rcx), %r12d
jmp 0x611f
movb $0x60, %r13b
xorl %r12d, %r12d
movl %r14d, %ebx
movl -0x78(%rsp), %eax
subl %eax, %ebx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %ebx, %ecx
shll %cl, %r8d
movl %r10d, %edx
movl %eax, %ecx
shrl %cl, %edx
addl %r8d, %edx
movl -0x74(%rsp), %eax
movl %eax, %ecx
leal (%rdx,%rcx), %eax
movb %r13b, (%r15,%rax,4)
movb %bl, 0x1(%r15,%rax,4)
movw %r12w, 0x2(%r15,%rax,4)
addl %r8d, %ecx
jne 0x6143
leal -0x1(%r14), %ecx
movl $0x1, %r8d
movl %r8d, %edx
shll %cl, %edx
movl %edx, %ecx
shrl %edx
testl %r10d, %ecx
jne 0x6169
leal -0x1(%rcx), %eax
andl %r10d, %eax
addl %ecx, %eax
cmpl $0x1, %ecx
movl %eax, %r10d
movl $0x0, %eax
cmovbl %eax, %r10d
incl %edi
movl %r14d, %eax
decw -0x40(%rsp,%rax,2)
movq -0x18(%rsp), %r9
movl %r11d, %r12d
jne 0x61b2
cmpl %ebp, %r14d
je 0x6292
movl %edi, %eax
movzwl (%r9,%rax,2), %eax
movzwl (%rsi,%rax,2), %r14d
cmpl %r12d, %r14d
jbe 0x60e7
movl %r10d, %ebx
andl -0x4c(%rsp), %ebx
cmpl -0x6c(%rsp), %ebx
je 0x60e7
movl -0x78(%rsp), %edx
testl %edx, %edx
cmovel %r12d, %edx
movl -0x74(%rsp), %eax
leaq (%r15,%rax,4), %r15
movl %r14d, %ecx
movl %edx, -0x78(%rsp)
subl %edx, %ecx
movl %r8d, %r12d
shll %cl, %r12d
movl %r14d, %eax
cmpl %ebp, %eax
jae 0x6207
movl %eax, %ecx
movzwl -0x40(%rsp,%rcx,2), %ecx
subl %ecx, %r12d
jle 0x6207
addl %r12d, %r12d
incl %eax
jmp 0x61f0
movl -0x50(%rsp), %ecx
cmpl $0x2, %ecx
sete %r13b
cmpl $0x1, %ecx
sete -0x74(%rsp)
movl %eax, %ecx
subl -0x78(%rsp), %ecx
shll %cl, %r8d
movq -0x68(%rsp), %rdx
addl %r8d, %edx
cmpl $0x355, %edx            # imm = 0x355
setae %r12b
movq %rdx, -0x68(%rsp)
cmpl $0x251, %edx            # imm = 0x251
setae %r8b
testb %r12b, -0x74(%rsp)
jne 0x62d0
andb %r8b, %r13b
jne 0x62d0
movl -0x78(%rsp), %edx
negb %dl
movzbl %dl, %edx
addl %eax, %edx
movq -0x48(%rsp), %rax
movq (%rax), %rax
movl %ebx, %r8d
movb %dl, (%rax,%r8,4)
movl %r11d, %r12d
movb %r12b, 0x1(%rax,%r8,4)
movl %r15d, %edx
subl %eax, %edx
shrl $0x2, %edx
movw %dx, 0x2(%rax,%r8,4)
movl %ebx, -0x6c(%rsp)
movl $0x1, %eax
jmp 0x60e1
testl %r10d, %r10d
je 0x62b6
movl -0x78(%rsp), %eax
negb %al
movzbl %al, %eax
addl %ebp, %eax
movl %r10d, %ecx
movb $0x40, (%r15,%rcx,4)
movb %al, 0x1(%r15,%rcx,4)
andw $0x0, 0x2(%r15,%rcx,4)
movl -0x68(%rsp), %eax
shlq $0x2, %rax
movq -0x48(%rsp), %rcx
addq %rax, (%rcx)
movq -0x20(%rsp), %r8
jmp 0x5f61
movl $0x1, %eax
jmp 0x6092