// File: /workspace/github_c_makefile_star2_O0/Cyan4973[P]FiniteStateEntropy/programs/zlibh.c
// Function: gen_bitlen
static void gen_bitlen(tree_desc* desc, int* huf_heap, int heap_max, unsigned short* bl_count)
{
    ct_data *tree        = desc->dyn_tree;
    int max_code         = desc->max_code;
    const ct_data *stree = desc->stat_desc->static_tree;
    const int *extra     = desc->stat_desc->extra_bits;
    int base             = desc->stat_desc->extra_base;
    int max_length       = desc->stat_desc->max_length;
    unsigned long *csize = desc->comp_size;
    int h;              /* heap index */
    int n, m;           /* iterate over the tree elements */
    int bits;           /* bit length */
    int xbits;          /* extra bits */
    unsigned short f;   /* frequency */
    int overflow = 0;   /* number of elements with bit length too large */

    for (bits = 0; bits <= ZLIBH_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[huf_heap[heap_max]].Len = 0; /* root of the heap */

    for (h = heap_max+1; h < ZLIBH_HEAP_SIZE; h++) {
        n = huf_heap[h];
        bits = tree[tree[n].Dad].Len + 1;
        if (bits > max_length) bits = max_length, overflow++;
        tree[n].Len = (unsigned short)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;
        csize[0] += (unsigned long)f * (bits + xbits);
        if (stree) csize[1] += (unsigned long)f * (stree[n].Len + xbits);
    }
    if (overflow == 0) return;

    //    fprintf(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 ZLIBH_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 = huf_heap[--h];
            if (m > max_code) continue;
            if ((unsigned) tree[m].Len != (unsigned) bits) {
                //                fprintf(stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits);
                csize[0] += ((long)bits - (long)tree[m].Len)*(long)tree[m].Freq;
                tree[m].Len = (unsigned short)bits;
            }
            n--;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x60(%rbp)
cmpl $0xf, -0x60(%rbp)
jg 0xdffc
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rcx
movw $0x0, (%rax,%rcx,2)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xdfdd
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movw $0x0, 0x2(%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
cmpl $0x203, -0x54(%rbp)     # imm = 0x203
jge 0xe12b
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x58(%rbp), %rdx
movzwl 0x2(%rcx,%rdx,4), %ecx
movzwl 0x2(%rax,%rcx,4), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x48(%rbp), %eax
jle 0xe06a
movl -0x48(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movl -0x60(%rbp), %eax
movw %ax, %dx
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x58(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0xe08a
jmp 0xe11d
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rcx
movw (%rax,%rcx,2), %dx
addw $0x1, %dx
movw %dx, (%rax,%rcx,2)
movl $0x0, -0x64(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x44(%rbp), %eax
jl 0xe0c0
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
subl -0x44(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movw (%rax,%rcx,4), %ax
movw %ax, -0x66(%rbp)
movzwl -0x66(%rbp), %eax
movl %eax, %ecx
movl -0x60(%rbp), %eax
addl -0x64(%rbp), %eax
cltq
imulq %rax, %rcx
movq -0x50(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
cmpq $0x0, -0x38(%rbp)
je 0xe11b
movzwl -0x66(%rbp), %eax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rdx
movzwl 0x2(%rax,%rdx,4), %eax
addl -0x64(%rbp), %eax
cltq
imulq %rax, %rcx
movq -0x50(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0xe11d
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xe01c
cmpl $0x0, -0x6c(%rbp)
jne 0xe136
jmp 0xe266
jmp 0xe138
movl -0x48(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0xe15d
movl -0x60(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xe141
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rcx
movw (%rax,%rcx,2), %dx
addw $-0x1, %dx
movw %dx, (%rax,%rcx,2)
movq -0x20(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %edx
addl $0x2, %edx
movw %dx, (%rax,%rcx,2)
movq -0x20(%rbp), %rax
movslq -0x48(%rbp), %rcx
movw (%rax,%rcx,2), %dx
addw $-0x1, %dx
movw %dx, (%rax,%rcx,2)
movl -0x6c(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jg 0xe138
movl -0x48(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0xe266
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
je 0xe256
movq -0x10(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x54(%rbp)
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0xe1f5
jmp 0xe1cb
movq -0x28(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movzwl 0x2(%rax,%rcx,4), %eax
cmpl -0x60(%rbp), %eax
je 0xe248
movslq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rax
movslq -0x5c(%rbp), %rdx
movzwl 0x2(%rax,%rdx,4), %eax
subq %rax, %rcx
movq -0x28(%rbp), %rax
movslq -0x5c(%rbp), %rdx
movzwl (%rax,%rdx,4), %eax
imulq %rax, %rcx
movq -0x50(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x60(%rbp), %eax
movw %ax, %dx
movq -0x28(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xe1cb
jmp 0xe258
movl -0x60(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xe1b2
popq %rbp
retq
nopl (%rax,%rax)