// File: /workspace/github_c_makefile_star2_O1/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), %r10
movq 0x10(%rdi), %rcx
movq %rcx, -0x38(%rbp)
movl 0x18(%rdi), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x24(%rdi), %ecx
movslq 0x20(%rdi), %rdx
xorps %xmm0, %xmm0
movaps %xmm0, 0x69a1(%rip)     # 0xbb90
movaps %xmm0, 0x698a(%rip)     # 0xbb80
movslq 0x729b(%rip), %rsi      # 0xc498
leaq 0x699c(%rip), %r12      # 0xbba0
movslq (%r12,%rsi,4), %rsi
movw $0x0, 0x2(%rax,%rsi,4)
movslq 0x7282(%rip), %r8       # 0xc498
leaq 0x6963(%rip), %rdi      # 0xbb80
cmpq $0x23b, %r8             # imm = 0x23B
jg 0x52d6
addq $0x2, %r8
xorl %r9d, %r9d
movq %r12, %rbx
movslq -0x4(%r12,%r8,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 0x5257
incl %esi
movl %esi, %r13d
movw %r13w, 0x2(%rax,%r15,4)
cmpl %ecx, %r15d
jg 0x52af
movslq %r13d, %rsi
incw (%rdi,%rsi,2)
xorl %r11d, %r11d
cmpl -0x2c(%rbp), %r15d
jl 0x5283
movl %r15d, %esi
subl -0x2c(%rbp), %esi
movslq %esi, %rsi
movq -0x38(%rbp), %r11
movl (%r11,%rsi,4), %r11d
movzwl (%rax,%r15,4), %esi
addl %r11d, %r13d
imull %esi, %r13d
addl %r13d, 0x885a(%rip)     # 0xdaf0
testq %r10, %r10
je 0x52af
movzwl 0x2(%r10,%r15,4), %r15d
addl %r15d, %r11d
imull %esi, %r11d
addl %r11d, 0x8845(%rip)     # 0xdaf4
movb %r12b, %r14b
addl %r14d, %r9d
movq %r8, %rsi
incq %rsi
cmpl $0x23d, %r8d            # imm = 0x23D
movq %rsi, %r8
movq %rbx, %r12
jne 0x5231
movl $0x23d, %r8d            # imm = 0x23D
jmp 0x52dc
incl %r8d
xorl %r9d, %r9d
testl %r9d, %r9d
je 0x5397
movq %rdx, %rsi
shlq $0x20, %rsi
movabsq $0x100000000, %r10      # imm = 0x100000000
addq %rsi, %r10
leaq (%rdi,%rdx,2), %r11
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
movq %r11, %r15
movq %r10, %r14
addq %rbx, %r14
movzwl -0x2(%r15), %esi
addq $-0x2, %r15
testw %si, %si
je 0x530d
decl %esi
movw %si, (%r15)
sarq $0x1f, %r14
addw $0x2, (%r14,%rdi)
decw (%rdi,%rdx,2)
leal -0x2(%r9), %esi
cmpl $0x2, %r9d
movl %esi, %r9d
jg 0x5307
testl %edx, %edx
je 0x5397
movzwl (%rdi,%rdx,2), %r9d
movslq %r8d, %rsi
leaq (%r12,%rsi,4), %rbx
addq $-0x4, %rbx
xorl %r10d, %r10d
testl %r9d, %r9d
je 0x5392
movslq (%rbx,%r10,4), %r11
decq %r10
cmpl %ecx, %r11d
jg 0x5356
movzwl 0x2(%rax,%r11,4), %ebx
movl %edx, %esi
subl %ebx, %esi
je 0x5387
movzwl (%rax,%r11,4), %ebx
imull %ebx, %esi
addl %esi, 0x876f(%rip)      # 0xdaf0
movw %dx, 0x2(%rax,%r11,4)
movl %r8d, %r8d
addq %r10, %r8
decl %r9d
jmp 0x5348
decq %rdx
jne 0x5343
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq