// File: /workspace/github_c_makefile_star2_O2/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), %rcx
movq %rcx, -0x38(%rbp)
movq 0x10(%rdi), %rcx
movq %rcx, -0x40(%rbp)
movl 0x18(%rdi), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x24(%rdi), %ecx
movslq 0x20(%rdi), %rdx
xorl %edi, %edi
leaq 0x6f9a(%rip), %rsi      # 0xbb70
cmpq $0x20, %rdi
je 0x4be7
andw $0x0, (%rdi,%rsi)
addq $0x2, %rdi
jmp 0x4bd6
movslq 0x789a(%rip), %rdi      # 0xc488
leaq 0x6f9b(%rip), %r12      # 0xbb90
movslq (%r12,%rdi,4), %rdi
andw $0x0, 0x2(%rax,%rdi,4)
movslq 0x7882(%rip), %r14      # 0xc488
leal 0x1(%r14), %r8d
xorl %r9d, %r9d
cmpq $0x23b, %r14            # imm = 0x23B
jg 0x4cae
movq %r12, %r10
movslq 0x4(%r12,%r14,4), %r12
movzwl 0x2(%rax,%r12,4), %edi
movzwl 0x2(%rax,%rdi,4), %edi
xorl %r15d, %r15d
movl %edx, %r11d
cmpl %edi, %edx
setle %r13b
jle 0x4c40
incl %edi
movl %edi, %r11d
movw %r11w, 0x2(%rax,%r12,4)
cmpl %ecx, %r12d
jg 0x4c9a
movslq %r11d, %rdi
incw (%rsi,%rdi,2)
xorl %edi, %edi
cmpl -0x2c(%rbp), %r12d
jl 0x4c6a
movl %r12d, %edi
subl -0x2c(%rbp), %edi
movslq %edi, %rdi
movq -0x40(%rbp), %rbx
movl (%rbx,%rdi,4), %edi
movzwl (%rax,%r12,4), %ebx
addl %edi, %r11d
imull %ebx, %r11d
addl %r11d, 0x8e63(%rip)     # 0xdae0
cmpq $0x0, -0x38(%rbp)
je 0x4c9a
movq -0x38(%rbp), %r11
movzwl 0x2(%r11,%r12,4), %r11d
addl %r11d, %edi
imull %ebx, %edi
addl %edi, 0x8e4a(%rip)      # 0xdae4
movb %r13b, %r15b
addl %r15d, %r9d
incl %r8d
incq %r14
movq %r10, %r12
jmp 0x4c0d
testl %r9d, %r9d
je 0x4d6f
movq %rdx, %rdi
shlq $0x20, %rdi
movabsq $0x100000000, %r10      # imm = 0x100000000
addq %rdi, %r10
leaq (%rsi,%rdx,2), %r11
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
movq %r11, %r14
movq %r10, %rdi
addq %rbx, %rdi
movzwl -0x2(%r14), %r15d
addq $-0x2, %r14
testw %r15w, %r15w
je 0x4cdf
decl %r15d
movw %r15w, (%r14)
sarq $0x1f, %rdi
addw $0x2, (%rdi,%rsi)
decw (%rsi,%rdx,2)
leal -0x2(%r9), %edi
cmpl $0x2, %r9d
movl %edi, %r9d
jg 0x4cd9
movq %rdx, %r9
subq $0x1, %r9
jb 0x4d6f
movzwl (%rsi,%rdx,2), %r10d
movslq %r8d, %rdi
leaq (%r12,%rdi,4), %rbx
addq $-0x4, %rbx
xorl %r11d, %r11d
testl %r10d, %r10d
je 0x4d6a
movslq (%rbx,%r11,4), %rdi
decq %r11
cmpl %ecx, %edi
jg 0x4d2e
movzwl 0x2(%rax,%rdi,4), %r14d
movl %edx, %ebx
subl %r14d, %ebx
je 0x4d5f
movzwl (%rax,%rdi,4), %r14d
imull %r14d, %ebx
addl %ebx, 0x8d86(%rip)      # 0xdae0
movw %dx, 0x2(%rax,%rdi,4)
movl %r8d, %r8d
addq %r11, %r8
decl %r10d
jmp 0x4d20
movq %r9, %rdx
jmp 0x4d15
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq