// File: /workspace/github_c_makefile_star2_O1/Cyan4973[P]FiniteStateEntropy/programs/zlibh.c
// Function: build_tree
static void build_tree(tree_desc* desc)
{
    ct_data *tree         = desc->dyn_tree;
    const ct_data *stree  = desc->stat_desc->static_tree;
    int elems             = desc->max_code;
    unsigned long *csize  = desc->comp_size;
    int n;                              /* iterate over heap elements */
    int max_code = -1;                  /* largest code with non zero frequency */
    int huf_heap[2*ZLIBH_L_CODES+1];    /* heap used to build the Huffman trees */
    int heap_max;                       /* element of largest frequency */
    unsigned char depth[2*ZLIBH_L_CODES+1];
    /* Depth of each subtree used as tie breaker for trees of equal frequency */

    /* Construct the initial heap, with least frequent element in
    * huf_heap[SMALLEST]. The sons of huf_heap[n] are huf_heap[2*n] and huf_heap[2*n+1].
    * huf_heap[0] is used in replacement of heap_len (original Zlib).
    */

    huf_heap[0] = 0, heap_max = ZLIBH_HEAP_SIZE;

    for (n = 0; n <2*ZLIBH_L_CODES+1; n++)
        depth[n] = 0;

    for (n = 0; n < elems; n++) {
        if (tree[n].Freq != 0) {
            huf_heap[0]++;
            huf_heap[huf_heap[0]] = max_code = n;
            depth[n] = 0;
        } else {
            tree[n].Len = 0;
        }
    }

    /* The pkzip format requires that at least one distance code exists,
    * and that at least one bit should be sent even if there is only one
    * possible code.
    */

    if (huf_heap[0] > 1) {                /* at least two codes (non trivial tree) */
        int node;                           /* new node being created */
        unsigned short bl_count[ZLIBH_MAX_BITS+1];
        /* number of codes at each bit length for an optimal tree */

        desc->max_code = max_code;

        /* The elements huf_heap[huf_heap[0]/2+1 .. huf_heap[0]] are leaves of the tree,
        * establish sub-heaps of increasing lengths:
        */
        for (n = huf_heap[0]/2; n >= 1; n--) pqdownheap(tree, huf_heap, depth, n);

        /* Construct the Huffman tree by repeatedly combining the least two
        * frequent nodes.
        */
        node = elems;                          /* next internal node of the tree */
        do {
            int m;
            pqremove(tree, huf_heap, depth, n);  /* n = node of least frequency */
            m = huf_heap[SMALLEST];              /* m = node of next least frequency */

            huf_heap[--(heap_max)] = n;          /* keep the nodes sorted by frequency */
            huf_heap[--(heap_max)] = m;

            /* Create a new node father of n and m */
            tree[node].Freq = tree[n].Freq + tree[m].Freq;
            depth[node] = (unsigned char)((depth[n] >= depth[m] ? depth[n] : depth[m]) + 1);
            tree[n].Dad = tree[m].Dad = (unsigned short)node;

            /* and insert the new node in the heap */
            huf_heap[SMALLEST] = node++;
            pqdownheap(tree, huf_heap, depth, SMALLEST);

        } while (huf_heap[0] >= 2);

        huf_heap[--(heap_max)] = huf_heap[SMALLEST];

        /* At this point, the fields freq and dad are set. We can now
        * generate the bit lengths.
        */
        gen_bitlen((tree_desc *)desc, huf_heap, heap_max, bl_count);

        /* The field len is now set, we can generate the bit codes */
        gen_codes ((ct_data *)tree, max_code, bl_count);
    }
    else if (huf_heap[0] == 0) {     /* no code at all, create a single dummy zero code */
        desc->max_code = 0;
        tree[0].Len = 0;               // gen_bitlen shortcut
        tree[0].Code = 0;              // gen_codes shortcut (probably useless)
    }
    else {                           /* only one code, create a single one bit code */
        const int *extra     = desc->stat_desc->extra_bits;
        int base             = desc->stat_desc->extra_base;
        int xbits;                     /* extra bits */
        unsigned short f;              /* frequency */

        desc->max_code = max_code;
        for (n = 0; n < max_code; n++) {
            tree[n].Len = 0;
        }
        tree[max_code].Len = 1;        // gen_bitlen shortcut
        xbits = 0;
        if (max_code >= base) xbits = extra[max_code-base];
        f = tree[max_code].Freq;
        csize[0] += (unsigned long)f * (1 + xbits);
        if (stree) csize[1] += (unsigned long)f * (stree[max_code].Len + xbits);
        tree[max_code].Code = 0;       // gen_codes shortcut, not set earlier since it replaces .Freq
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa88, %rsp            # imm = 0xA88
movq %rdi, %rbp
movq (%rdi), %rax
movq %rax, (%rsp)
movq 0x10(%rdi), %r14
movq 0x18(%rdi), %rax
movq (%rax), %r13
movslq 0x8(%rdi), %r12
movl $0x0, 0x270(%rsp)
leaq 0x60(%rsp), %rdi
movl $0x203, %edx            # imm = 0x203
xorl %esi, %esi
callq 0x10c0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
testq %r12, %r12
jle 0x5e7f
xorl %eax, %eax
movq (%rsp), %rcx
cmpw $0x0, (%rcx,%rax,4)
je 0x5e6c
movslq 0x270(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movl %edx, 0x270(%rsp)
movl %eax, 0x274(%rsp,%rcx,4)
movb $0x0, 0x60(%rsp,%rax)
movl %eax, %edi
jmp 0x5e77
movq (%rsp), %rcx
movw $0x0, 0x2(%rcx,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x5e3e
movl 0x270(%rsp), %r15d
cmpl $0x2, %r15d
jl 0x60d3
movq %rdi, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movl %edi, 0x8(%rbp)
shrl %r15d
leaq 0x270(%rsp), %r14
leaq 0x60(%rsp), %r13
movq (%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x7022
leal -0x1(%r15), %eax
cmpl $0x1, %r15d
movl %eax, %r15d
jg 0x5eae
movl $0x205, %r13d           # imm = 0x205
movabsq $0x20200000000, %rax    # imm = 0x20200000000
movq %r12, %r14
movq %rax, 0x8(%rsp)
movslq 0x274(%rsp), %r15
movslq 0x270(%rsp), %rax
leal -0x1(%rax), %ecx
movl %ecx, 0x270(%rsp)
movl 0x270(%rsp,%rax,4), %eax
movl %eax, 0x274(%rsp)
movq (%rsp), %rdi
movq %r12, %rbp
movq %rdi, %r12
leaq 0x270(%rsp), %rsi
leaq 0x60(%rsp), %rbx
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x7022
movslq 0x274(%rsp), %rax
movl %r15d, 0x264(%rsp,%r13,4)
movl %eax, 0x260(%rsp,%r13,4)
movzwl (%r12,%rax,4), %ecx
addw (%r12,%r15,4), %cx
movw %cx, (%r12,%rbp,4)
movzbl 0x60(%rsp,%r15), %ecx
movzbl 0x60(%rsp,%rax), %edx
cmpb %dl, %cl
cmoval %ecx, %edx
incb %dl
movb %dl, 0x60(%rsp,%rbp)
movw %bp, 0x2(%r12,%rax,4)
movw %bp, 0x2(%r12,%r15,4)
incq %r14
movl %ebp, 0x274(%rsp)
movq %r12, %rdi
leaq 0x270(%rsp), %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x7022
movq 0x8(%rsp), %rax
addq $-0x2, %r13
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
addq %rcx, %rax
cmpl $0x1, 0x270(%rsp)
movq %r14, %r12
jg 0x5ee0
movslq 0x274(%rsp), %rdi
sarq $0x1e, %rax
movl %edi, 0x270(%rsp,%rax)
movq 0x30(%rsp), %rsi
movq (%rsi), %rax
movl 0x8(%rsi), %ecx
movq 0x18(%rsi), %rdx
movq (%rdx), %r8
movq 0x8(%rdx), %r9
movq %r9, 0x8(%rsp)
movl 0x10(%rdx), %r10d
movslq 0x18(%rdx), %rdx
movq 0x10(%rsi), %rsi
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movw $0x0, 0x2(%rax,%rdi,4)
cmpq $0x205, %r13            # imm = 0x205
jge 0x615d
addq $-0x3, %r13
movslq %r13d, %r11
addq $0x2, %r11
xorl %edi, %edi
movslq 0x26c(%rsp,%r11,4), %r15
movzwl 0x2(%rax,%r15,4), %r9d
movzwl 0x2(%rax,%r9,4), %r9d
xorl %ebp, %ebp
movl %edx, %r12d
cmpl %r9d, %edx
setle %r14b
jle 0x6053
incl %r9d
movl %r9d, %r12d
movw %r12w, 0x2(%rax,%r15,4)
cmpl %ecx, %r15d
jg 0x60ad
movslq %r12d, %r9
incw 0x10(%rsp,%r9,2)
xorl %r13d, %r13d
cmpl %r10d, %r15d
jl 0x6082
movl %r15d, %r9d
subl %r10d, %r9d
movslq %r9d, %r9
movq 0x8(%rsp), %r13
movl (%r13,%r9,4), %r13d
movzwl (%rax,%r15,4), %r9d
addl %r13d, %r12d
movslq %r12d, %r12
imulq %r9, %r12
addq %r12, (%rsi)
testq %r8, %r8
je 0x60ad
movzwl 0x2(%r8,%r15,4), %r15d
movslq %r13d, %r12
addq %r15, %r12
imulq %r9, %r12
addq %r12, 0x8(%rsi)
movb %r14b, %bpl
addl %ebp, %edi
movq %r11, %r9
incq %r9
cmpl $0x203, %r11d           # imm = 0x203
movq %r9, %r11
jne 0x602b
movl $0x203, %r13d           # imm = 0x203
jmp 0x6163
testl %r15d, %r15d
je 0x629e
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movl 0x10(%rax), %edx
movl %edi, 0x8(%rbp)
testl %edi, %edi
jle 0x6106
movl %edi, %eax
xorl %esi, %esi
movq (%rsp), %r8
movw $0x0, 0x2(%r8,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x60f6
movslq %edi, %rax
movq (%rsp), %rsi
movw $0x1, 0x2(%rsi,%rax,4)
xorl %esi, %esi
subl %edx, %edi
jl 0x6120
movslq %edi, %rdx
movl (%rcx,%rdx,4), %esi
movq (%rsp), %rcx
movzwl (%rcx,%rax,4), %ecx
leal 0x1(%rsi), %edx
movslq %edx, %rdx
imulq %rcx, %rdx
addq %rdx, (%r14)
testq %r13, %r13
je 0x614e
movzwl 0x2(%r13,%rax,4), %edx
movslq %esi, %rsi
addq %rdx, %rsi
imulq %rcx, %rsi
addq %rsi, 0x8(%r14)
movq (%rsp), %rcx
movw $0x0, (%rcx,%rax,4)
jmp 0x62a9
addq $-0x2, %r13
xorl %edi, %edi
testl %edi, %edi
je 0x6221
movq %rdx, %r9
shlq $0x20, %r9
movabsq $0x100000000, %r8       # imm = 0x100000000
addq %r9, %r8
leaq (%rsp,%rdx,2), %r9
addq $0x10, %r9
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq %r9, %r14
movq %r8, %r11
addq %r10, %r11
movzwl -0x2(%r14), %ebp
addq $-0x2, %r14
testw %bp, %bp
je 0x6197
decl %ebp
movw %bp, (%r14)
sarq $0x1f, %r11
addw $0x2, 0x10(%rsp,%r11)
decw 0x10(%rsp,%rdx,2)
leal -0x2(%rdi), %r11d
cmpl $0x2, %edi
movl %r11d, %edi
jg 0x6191
testl %edx, %edx
je 0x6221
leaq 0x26c(%rsp), %rdi
movzwl 0x10(%rsp,%rdx,2), %r8d
testl %r8d, %r8d
je 0x621c
movslq %r13d, %r9
leaq (%rdi,%r9,4), %r10
movslq (%r10), %r9
decl %r13d
addq $-0x4, %r10
cmpl %ecx, %r9d
jg 0x61e8
movzwl 0x2(%rax,%r9,4), %r11d
movq %rdx, %r10
subq %r11, %r10
je 0x6217
movzwl (%rax,%r9,4), %r11d
imulq %r11, %r10
addq %r10, (%rsi)
movw %dx, 0x2(%rax,%r9,4)
decl %r8d
jne 0x61e1
decq %rdx
jne 0x61d6
xorl %ecx, %ecx
movl $0x1, %eax
andl $0xfffe, %ecx           # imm = 0xFFFE
movzwl 0xe(%rsp,%rax,2), %edx
addl %ecx, %edx
addl %edx, %edx
movw %dx, 0x40(%rsp,%rax,2)
incq %rax
movl %edx, %ecx
cmpq $0x10, %rax
jne 0x6228
movq 0x38(%rsp), %r8
leal 0x1(%r8), %eax
cmpl $0x1, %eax
adcl $0x1, %r8d
xorl %eax, %eax
movq (%rsp), %rcx
movzwl 0x2(%rcx,%rax,4), %ecx
testl %ecx, %ecx
je 0x6294
movl %ecx, %esi
movzwl 0x40(%rsp,%rsi,2), %edx
leal 0x1(%rdx), %edi
movw %di, 0x40(%rsp,%rsi,2)
incl %ecx
xorl %esi, %esi
movl %edx, %edi
andl $0x1, %edi
orl %esi, %edi
shrl %edx
leal (%rdi,%rdi), %esi
decl %ecx
cmpl $0x1, %ecx
jg 0x6279
movq (%rsp), %rcx
movw %di, (%rcx,%rax,4)
incq %rax
cmpq %r8, %rax
jne 0x6259
jmp 0x62a9
xorl %eax, %eax
movl %eax, 0x8(%rbp)
movq (%rsp), %rcx
movl %eax, (%rcx)
addq $0xa88, %rsp            # imm = 0xA88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq