// File: /workspace/github_c_makefile_star2_O0/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
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x40(%rbp)
cmpl $0xf, -0x40(%rbp)
jg 0x93a9
movslq -0x40(%rbp), %rcx
leaq 0x8848(%rip), %rax      # 0x11be0
movw $0x0, (%rax,%rcx,2)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x9387
movq -0x10(%rbp), %rax
movslq 0x9144(%rip), %rdx      # 0x124f8
leaq 0x8845(%rip), %rcx      # 0x11c00
movslq (%rcx,%rdx,4), %rcx
movw $0x0, 0x2(%rax,%rcx,4)
movl 0x912c(%rip), %eax      # 0x124f8
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x23d, -0x34(%rbp)     # imm = 0x23D
jge 0x94e6
movslq -0x34(%rbp), %rcx
leaq 0x8816(%rip), %rax      # 0x11c00
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movzwl 0x2(%rcx,%rdx,4), %ecx
movzwl 0x2(%rax,%rcx,4), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x9423
movl -0x24(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x9443
jmp 0x94d8
movslq -0x40(%rbp), %rcx
leaq 0x8792(%rip), %rax      # 0x11be0
movw (%rax,%rcx,2), %dx
addw $0x1, %dx
leaq 0x8783(%rip), %rax      # 0x11be0
movw %dx, (%rax,%rcx,2)
movl $0x0, -0x44(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jl 0x9483
movq -0x18(%rbp), %rax
movl -0x38(%rbp), %ecx
subl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movw (%rax,%rcx,4), %ax
movw %ax, -0x46(%rbp)
movzwl -0x46(%rbp), %eax
movl -0x40(%rbp), %ecx
addl -0x44(%rbp), %ecx
imull %ecx, %eax
addl 0xa6aa(%rip), %eax      # 0x13b50
movl %eax, 0xa6a4(%rip)      # 0x13b50
cmpq $0x0, -0x30(%rbp)
je 0x94d6
movzwl -0x46(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movzwl 0x2(%rcx,%rdx,4), %ecx
addl -0x44(%rbp), %ecx
imull %ecx, %eax
addl 0xa684(%rip), %eax      # 0x13b54
movl %eax, 0xa67e(%rip)      # 0x13b54
jmp 0x94d8
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x93d2
cmpl $0x0, -0x4c(%rbp)
jne 0x94f1
jmp 0x9654
jmp 0x94f3
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x40(%rbp)
movslq -0x40(%rbp), %rcx
leaq 0x86d9(%rip), %rax      # 0x11be0
movzwl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x951b
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x94fc
movslq -0x40(%rbp), %rcx
leaq 0x86ba(%rip), %rax      # 0x11be0
movw (%rax,%rcx,2), %dx
addw $-0x1, %dx
leaq 0x86ab(%rip), %rax      # 0x11be0
movw %dx, (%rax,%rcx,2)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x8697(%rip), %rax      # 0x11be0
movzwl (%rax,%rcx,2), %eax
addl $0x2, %eax
movw %ax, %dx
leaq 0x8686(%rip), %rax      # 0x11be0
movw %dx, (%rax,%rcx,2)
movslq -0x24(%rbp), %rcx
leaq 0x8677(%rip), %rax      # 0x11be0
movw (%rax,%rcx,2), %dx
addw $-0x1, %dx
leaq 0x8668(%rip), %rax      # 0x11be0
movw %dx, (%rax,%rcx,2)
movl -0x4c(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jg 0x94f3
movl -0x24(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x9654
movslq -0x40(%rbp), %rcx
leaq 0x8636(%rip), %rax      # 0x11be0
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x9644
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
movslq %eax, %rcx
leaq 0x8632(%rip), %rax      # 0x11c00
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x95de
jmp 0x95b1
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzwl 0x2(%rax,%rcx,4), %eax
cmpl -0x40(%rbp), %eax
je 0x9636
movslq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rdx
movzwl 0x2(%rax,%rdx,4), %eax
subq %rax, %rcx
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rdx
movzwl (%rax,%rdx,4), %eax
imulq %rax, %rcx
movl 0xa536(%rip), %eax      # 0x13b50
addq %rcx, %rax
movl %eax, 0xa52d(%rip)      # 0x13b50
movl -0x40(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x95b1
jmp 0x9646
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x9595
popq %rbp
retq
nopw %cs:(%rax,%rax)