// File: /workspace/github_c_makefile_star2_O3/DavidSM64[P]DKR-Decompressor/DKRGzip.c
// Function: gen_bitlen(tree_desc*)
void gen_bitlen(tree_desc* desc){
    ct_data *tree  = desc->dyn_tree;
    int *extra     = desc->extra_bits;
    int base            = desc->extra_base;
    int max_code        = desc->max_code;
    int max_length      = desc->max_length;
    ct_data *stree = desc->static_tree;
    int h;              /* heap index */
    int n, m;           /* iterate over the tree elements */
    int bits;           /* bit length */
    int xbits;          /* extra bits */
    u16 f;              /* frequency */
    int overflow = 0;   /* number of elements with bit length too large */

    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;

    /* In a first pass, compute the optimal bit lengths (which may
     * overflow in the case of the bit length tree).
     */
    tree[heap[heap_max]].Len = 0; /* root of the heap */

    for (h = heap_max+1; h < HEAP_SIZE; h++) {
        n = heap[h];
        bits = tree[tree[n].Dad].Len + 1;
        if (bits > max_length) bits = max_length, overflow++;
        tree[n].Len = (u16)bits;
        /* We overwrite tree[n].Dad which is no longer needed */

        if (n > max_code) continue; /* not a leaf node */

        bl_count[bits]++;
        xbits = 0;
        if (n >= base) xbits = extra[n-base];
        f = tree[n].Freq;
        opt_len += (u32)f * (bits + xbits);
        if (stree) static_len += (u32)f * (stree[n].Len + xbits);
    }
    if (overflow == 0) return;

    //Trace((stderr,"\nbit length overflow\n"));
    /* This happens for example on obj2 and pic of the Calgary corpus */

    /* Find the first bit length which could increase: */
    do {
        bits = max_length-1;
        while (bl_count[bits] == 0) bits--;
        bl_count[bits]--;      /* move one leaf down the tree */
        bl_count[bits+1] += 2; /* move one overflow item as its brother */
        bl_count[max_length]--;
        /* The brother of the overflow item also moves one step up,
         * but this does not affect bl_count[max_length]
         */
        overflow -= 2;
    } while (overflow > 0);

    /* Now recompute all bit lengths, scanning in increasing frequency.
     * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
     * lengths instead of fixing only the wrong ones. This idea is taken
     * from 'ar' written by Haruhiko Okumura.)
     */
    for (bits = max_length; bits != 0; bits--) {
        n = bl_count[bits];
        while (n != 0) {
            m = heap[--h];
            if (m > max_code) continue;
            if (tree[m].Len != (unsigned) bits) {
                //Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
                opt_len += ((long)bits-(long)tree[m].Len)*(long)tree[m].Freq;
                tree[m].Len = (u16)bits;
            }
            n--;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%rdi), %rax
movq 0x8(%rdi), %r9
movq 0x10(%rdi), %rcx
movq %rcx, -0x30(%rbp)
movl 0x18(%rdi), %r11d
movl 0x24(%rdi), %ecx
movslq 0x20(%rdi), %rdx
xorps %xmm0, %xmm0
movaps %xmm0, 0x6aba(%rip)     # 0xbb90
movaps %xmm0, 0x6aa3(%rip)     # 0xbb80
movslq 0x73b4(%rip), %rsi      # 0xc498
leaq 0x6ab5(%rip), %rdi      # 0xbba0
movslq (%rdi,%rsi,4), %rsi
movw $0x0, 0x2(%rax,%rsi,4)
movslq 0x739b(%rip), %rbx      # 0xc498
cmpq $0x23b, %rbx            # imm = 0x23B
jg 0x527b
addq $0x2, %rbx
xorl %r8d, %r8d
leaq 0x6a68(%rip), %rdi      # 0xbb80
leaq 0x6a81(%rip), %rsi      # 0xbba0
movslq -0x4(%rsi,%rbx,4), %r15
movzwl 0x2(%rax,%r15,4), %esi
movzwl 0x2(%rax,%rsi,4), %esi
xorl %r14d, %r14d
movl %edx, %r13d
cmpl %esi, %edx
setle %r12b
jle 0x5142
incl %esi
movl %esi, %r13d
movw %r13w, 0x2(%rax,%r15,4)
cmpl %ecx, %r15d
jg 0x5199
movslq %r13d, %rsi
incw (%rdi,%rsi,2)
xorl %r10d, %r10d
cmpl %r11d, %r15d
jl 0x516d
movl %r15d, %esi
subl %r11d, %esi
movslq %esi, %rsi
movq -0x30(%rbp), %r10
movl (%r10,%rsi,4), %r10d
movzwl (%rax,%r15,4), %esi
addl %r10d, %r13d
imull %esi, %r13d
addl %r13d, 0x8970(%rip)     # 0xdaf0
testq %r9, %r9
je 0x5199
movzwl 0x2(%r9,%r15,4), %r15d
addl %r15d, %r10d
imull %esi, %r10d
addl %r10d, 0x895b(%rip)     # 0xdaf4
movb %r12b, %r14b
addl %r14d, %r8d
movq %rbx, %rsi
incq %rsi
cmpl $0x23d, %ebx            # imm = 0x23D
movq %rsi, %rbx
jne 0x5118
testl %r8d, %r8d
je 0x527b
movq %rdx, %rsi
shlq $0x20, %rsi
movabsq $0x100000000, %r9       # imm = 0x100000000
addq %rsi, %r9
leaq (%rdi,%rdx,2), %r10
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
movq %r10, %r14
movq %r9, %rbx
addq %r11, %rbx
movzwl -0x2(%r14), %esi
addq $-0x2, %r14
testw %si, %si
je 0x51e5
decl %esi
movw %si, (%r14)
sarq $0x1f, %rbx
addw $0x2, (%rbx,%rdi)
decw (%rdi,%rdx,2)
leal -0x2(%r8), %esi
cmpl $0x2, %r8d
movl %esi, %r8d
jg 0x51df
testl %edx, %edx
je 0x527b
movl $0x23d, %r8d            # imm = 0x23D
movzwl (%rdi,%rdx,2), %r9d
movslq %r8d, %rsi
leaq 0x6971(%rip), %r10      # 0xbba0
leaq (%r10,%rsi,4), %rbx
addq $-0x4, %rbx
xorl %r10d, %r10d
testl %r9d, %r9d
je 0x5276
movslq (%rbx,%r10,4), %r11
decq %r10
cmpl %ecx, %r11d
jg 0x523a
movzwl 0x2(%rax,%r11,4), %ebx
movl %edx, %esi
subl %ebx, %esi
je 0x526b
movzwl (%rax,%r11,4), %ebx
imull %ebx, %esi
addl %esi, 0x888b(%rip)      # 0xdaf0
movw %dx, 0x2(%rax,%r11,4)
movl %r8d, %r8d
addq %r10, %r8
decl %r9d
jmp 0x5225
decq %rdx
jne 0x5220
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq