// File: /workspace/github_cpp_makefile_star2_O3/sisong[P]ApkDiffPatch/zlib1.3.1/inftrees.c
// Function: inflate_table
int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens,
                                unsigned codes, code FAR * FAR *table,
                                unsigned FAR *bits, unsigned short FAR *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 fill;              /* index for replicating entries */
    unsigned low;               /* low bits for current root entry */
    unsigned mask;              /* mask for low root bits */
    code here;                  /* table entry for duplication */
    code FAR *next;             /* next available space in table */
    const unsigned short FAR *base;     /* base value table to use */
    const unsigned short FAR *extra;    /* extra bits table to use */
    unsigned match;             /* use base and extra for symbol >= match */
    unsigned short count[MAXBITS+1];    /* number of codes of each length */
    unsigned short offs[MAXBITS+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, 203, 77};
    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..MAXBITS.  The caller must assure this.
       1..MAXBITS 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..MAXBITS) */
    for (len = 0; len <= MAXBITS; 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 = MAXBITS; 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 <= MAXBITS; 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 < MAXBITS; 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 */
        match = 20;
        break;
    case LENS:
        base = lbase;
        extra = lext;
        match = 257;
        break;
    default:    /* DISTS */
        base = dbase;
        extra = dext;
        match = 0;
    }

    /* 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 (;;) {
        /* create table entry */
        here.bits = (unsigned char)(len - drop);
        if (work[sym] + 1U < match) {
            here.op = (unsigned char)0;
            here.val = work[sym];
        }
        else if (work[sym] >= match) {
            here.op = (unsigned char)(extra[work[sym] - match]);
            here.val = base[work[sym] - match];
        }
        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 %r9, -0x70(%rsp)
movq %rcx, %r10
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rsp)
movaps %xmm0, -0x30(%rsp)
testl %edx, %edx
je 0x50a2a
movl %edx, %eax
xorl %ecx, %ecx
movzwl (%rsi,%rcx,2), %r9d
incw -0x40(%rsp,%r9,2)
incq %rcx
cmpq %rcx, %rax
jne 0x50a17
movl (%r8), %r11d
movl $0xf, %ebx
movl %ebx, %eax
cmpw $0x0, -0x40(%rsp,%rax,2)
jne 0x50a77
decl %ebx
jne 0x50a32
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, %r15d
movl %r15d, (%r8)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl %ebx, %r11d
cmovael %ebx, %r11d
movl $0x1, %ecx
movl $0x1, %r14d
cmpl $0x2, %ebx
jb 0x50aa8
movl $0x1, %r14d
cmpw $0x0, -0x40(%rsp,%r14,2)
jne 0x50aa8
incq %r14
cmpq %r14, %rax
jne 0x50a94
movl %ebx, %r14d
cmpl %r14d, %r11d
cmovbel %r14d, %r11d
movl $0x1, %r9d
addl %ecx, %ecx
movzwl -0x40(%rsp,%r9,2), %eax
subl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
js 0x50a68
incq %r9
cmpq $0x10, %r9
jne 0x50ab5
movq %r8, -0x18(%rsp)
testl %ecx, %ecx
je 0x50aed
testl %edi, %edi
sete %cl
cmpl $0x1, %ebx
setne %r8b
orb %cl, %r8b
jne 0x50a68
movw $0x0, -0xe(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
addw -0x3e(%rsp,%rax,2), %cx
movw %cx, -0xc(%rsp,%rax,2)
incq %rax
cmpq $0xe, %rax
jne 0x50af8
testl %edx, %edx
je 0x50b3e
movl %edx, %eax
xorl %ecx, %ecx
movzwl (%rsi,%rcx,2), %edx
testq %rdx, %rdx
je 0x50b36
movzwl -0x10(%rsp,%rdx,2), %r8d
leal 0x1(%r8), %r9d
movw %r9w, -0x10(%rsp,%rdx,2)
movq -0x70(%rsp), %rdx
movw %cx, (%rdx,%r8,2)
incq %rcx
cmpq %rcx, %rax
jne 0x50b13
testl %edi, %edi
je 0x50b8a
cmpl $0x1, %edi
jne 0x50dbe
movl $0x1, %eax
cmpl $0x9, %r11d
ja 0x50a68
movb $0x1, %al
movl %eax, -0x80(%rsp)
movl $0x101, -0x78(%rsp)     # imm = 0x101
leaq 0x15471(%rip), %rax     # 0x65fe0
movq %rax, -0x50(%rsp)
leaq 0x15425(%rip), %rax     # 0x65fa0
movq %rax, -0x58(%rsp)
movl $0x0, -0x7c(%rsp)
jmp 0x50bb1
movl $0x14, -0x78(%rsp)
movl $0x0, -0x7c(%rsp)
movq -0x70(%rsp), %rax
movq %rax, -0x58(%rsp)
movq %rax, -0x50(%rsp)
movl $0x0, -0x80(%rsp)
movl $0x1, %eax
movl %r11d, %ecx
shll %cl, %eax
movq %rax, -0x60(%rsp)
decl %eax
movl %eax, -0x48(%rsp)
movq %r10, -0x68(%rsp)
movq (%r10), %r8
movl $0xffffffff, -0x74(%rsp) # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movl %r11d, %ecx
xorl %edi, %edi
xorl %r13d, %r13d
movl %r11d, -0x44(%rsp)
movl $0x1, %ebp
shll %cl, %ebp
movl %r12d, %eax
movq -0x70(%rsp), %rcx
movzwl (%rcx,%rax,2), %r11d
movzwl %r11w, %ecx
leal 0x1(%rcx), %eax
movl -0x78(%rsp), %edx
cmpl %edx, %eax
jae 0x50c0d
xorl %eax, %eax
jmp 0x50c2a
subl %edx, %ecx
jae 0x50c18
movb $0x60, %al
xorl %r11d, %r11d
jmp 0x50c2a
movq -0x50(%rsp), %rax
movb (%rax,%rcx,2), %al
movq -0x58(%rsp), %rdx
movzwl (%rdx,%rcx,2), %r11d
movl %r14d, %edx
subl %edi, %edx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %r10d
movl %r13d, %r15d
movl %edi, %ecx
shrl %cl, %r15d
addl %r10d, %r15d
movl %ebp, %ecx
leal (%r15,%rcx), %r9d
movb %al, (%r8,%r9,4)
movb %dl, 0x1(%r8,%r9,4)
movw %r11w, 0x2(%r8,%r9,4)
addl %r10d, %ecx
jne 0x50c47
leal -0x1(%r14), %ecx
movl $0x1, %r10d
shll %cl, %r10d
movl -0x44(%rsp), %r15d
movl %r10d, %eax
shrl %r10d
testl %r13d, %eax
jne 0x50c71
leal -0x1(%rax), %ecx
andl %r13d, %ecx
addl %eax, %ecx
cmpl $0x1, %eax
movl %ecx, %r13d
movl $0x0, %eax
cmovbl %eax, %r13d
incl %r12d
movl %r14d, %eax
decw -0x40(%rsp,%rax,2)
jne 0x50cba
cmpl %ebx, %r14d
je 0x50d8a
movl %r12d, %eax
movq -0x70(%rsp), %rcx
movzwl (%rcx,%rax,2), %eax
movzwl (%rsi,%rax,2), %r14d
cmpl %r15d, %r14d
jbe 0x50bed
movl %r13d, %edx
andl -0x48(%rsp), %edx
cmpl -0x74(%rsp), %edx
je 0x50bed
testl %edi, %edi
cmovel %r15d, %edi
movl %r14d, %ecx
subl %edi, %ecx
movl $0x1, %eax
shll %cl, %eax
cmpl %ebx, %r14d
jae 0x50d19
movl %ebx, %ecx
subl %edi, %ecx
movl %r14d, %r10d
movl %r10d, %r9d
movzwl -0x40(%rsp,%r9,2), %r9d
subl %r9d, %eax
jle 0x50d0c
addl %eax, %eax
incl %r10d
cmpl %ebx, %r10d
jb 0x50cf2
jmp 0x50d12
subl %edi, %r10d
movl %r10d, %ecx
movl $0x1, %eax
shll %cl, %eax
movq -0x68(%rsp), %r11
movq -0x60(%rsp), %r10
addl %eax, %r10d
cmpl $0x355, %r10d           # imm = 0x355
setae %r9b
movq %r10, -0x60(%rsp)
cmpl $0x251, %r10d           # imm = 0x251
setae %r10b
movl $0x1, %eax
testb %r9b, -0x80(%rsp)
jne 0x50a68
andb -0x7c(%rsp), %r10b
jne 0x50a68
movl %ebp, %eax
leaq (%r8,%rax,4), %r8
movq (%r11), %rax
movl %edx, %r9d
movb %cl, (%rax,%r9,4)
movb %r15b, 0x1(%rax,%r9,4)
movl %r8d, %r10d
subl %eax, %r10d
shrl $0x2, %r10d
movw %r10w, 0x2(%rax,%r9,4)
movl %edx, -0x74(%rsp)
jmp 0x50be6
testl %r13d, %r13d
je 0x50da4
movl %r13d, %eax
movb $0x40, (%r8,%rax,4)
movb %dl, 0x1(%r8,%rax,4)
movw $0x0, 0x2(%r8,%rax,4)
movl -0x60(%rsp), %eax
shlq $0x2, %rax
movq -0x68(%rsp), %rcx
addq %rax, (%rcx)
movq -0x18(%rsp), %r8
jmp 0x50a63
movl $0x0, -0x78(%rsp)
leaq 0x15293(%rip), %rax     # 0x66060
movq %rax, -0x50(%rsp)
leaq 0x15247(%rip), %rax     # 0x66020
movq %rax, -0x58(%rsp)
cmpl $0x2, %edi
sete %al
movl %eax, -0x7c(%rsp)
jne 0x50ba9
movl $0x1, %eax
movl $0x0, -0x80(%rsp)
cmpl $0x9, %r11d
ja 0x50a68
jmp 0x50bb1
nop