// File: /workspace/github_c_makefile_star2_O3/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 $0xaa8, %rsp            # imm = 0xAA8
movq %rdi, %r15
movq (%rdi), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rdi), %r12
movq 0x18(%rdi), %rax
movq (%rax), %r13
movslq 0x8(%rdi), %rbp
movl $0x0, 0x290(%rsp)
leaq 0x80(%rsp), %rdi
xorl %r14d, %r14d
movl $0x203, %edx            # imm = 0x203
xorl %esi, %esi
callq 0x10c0
testq %rbp, %rbp
jle 0x6975
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movq 0x8(%rsp), %rcx
cmpw $0x0, (%rcx,%rax,4)
je 0x6230
movslq 0x290(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movl %edx, 0x290(%rsp)
movl %eax, 0x294(%rsp,%rcx,4)
movb $0x0, 0x80(%rsp,%rax)
movl %eax, %r8d
jmp 0x623c
movq 0x8(%rsp), %rcx
movw $0x0, 0x2(%rcx,%rax,4)
incq %rax
cmpq %rax, %rbp
jne 0x61fd
movq %r15, 0x10(%rsp)
movl 0x290(%rsp), %r15d
cmpl $0x2, %r15d
jl 0x6610
movq %r8, 0x48(%rsp)
movq 0x10(%rsp), %rax
movl %r8d, 0x8(%rax)
shrl %r15d
leaq 0x290(%rsp), %r12
leaq 0x80(%rsp), %r13
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x765e
leal -0x1(%r15), %eax
cmpl $0x1, %r15d
movl %eax, %r15d
ja 0x627c
leaq 0x290(%rsp), %rax
movl (%rax), %eax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbp,4), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x80(%rsp,%rbp), %rcx
movq %rcx, 0x58(%rsp)
movl $0x205, %r13d           # imm = 0x205
movabsq $0x20200000000, %r14    # imm = 0x20200000000
xorl %r15d, %r15d
movq %rbp, 0x50(%rsp)
movq 0x8(%rsp), %rbp
movslq 0x294(%rsp), %r12
leal -0x1(%rax), %ecx
movl %ecx, 0x290(%rsp)
cltq
movl 0x290(%rsp,%rax,4), %eax
movl %eax, 0x294(%rsp)
movq %rbp, %rdi
leaq 0x290(%rsp), %rsi
leaq 0x80(%rsp), %rbx
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x765e
movslq 0x294(%rsp), %rax
movl %r12d, 0x284(%rsp,%r13,4)
movl %eax, 0x280(%rsp,%r13,4)
movzwl (%rbp,%rax,4), %ecx
addw (%rbp,%r12,4), %cx
movq 0x18(%rsp), %rdx
movw %cx, (%rdx,%r15,4)
movzbl 0x80(%rsp,%r12), %ecx
movzbl 0x80(%rsp,%rax), %edx
cmpb %dl, %cl
cmoval %ecx, %edx
incb %dl
movq 0x58(%rsp), %rcx
movb %dl, (%rcx,%r15)
movq 0x50(%rsp), %rcx
addq %r15, %rcx
movw %cx, 0x2(%rbp,%rax,4)
movw %cx, 0x2(%rbp,%r12,4)
movl %ecx, 0x294(%rsp)
movq %rbp, %rdi
leaq 0x290(%rsp), %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x765e
movl 0x290(%rsp), %eax
addq $-0x2, %r13
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
addq %rcx, %r14
incq %r15
cmpl $0x1, %eax
jg 0x62de
movslq 0x294(%rsp), %rdi
sarq $0x1e, %r14
movl %edi, 0x290(%rsp,%r14)
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movl 0x8(%rsi), %ecx
movq 0x18(%rsi), %rdx
movq (%rdx), %r8
movq 0x8(%rdx), %r9
movq %r9, 0x18(%rsp)
movl 0x10(%rdx), %r10d
movslq 0x18(%rdx), %rdx
movq 0x10(%rsi), %rsi
pxor %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
movw $0x0, 0x2(%rax,%rdi,4)
cmpq $0x204, %r13            # imm = 0x204
jg 0x658f
addq $-0x3, %r13
movslq %r13d, %r11
addq $0x2, %r11
xorl %edi, %edi
movslq 0x28c(%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 0x6458
incl %r9d
movl %r9d, %r12d
movw %r12w, 0x2(%rax,%r15,4)
cmpl %ecx, %r15d
jg 0x64b2
movslq %r12d, %r9
incw 0x20(%rsp,%r9,2)
xorl %r13d, %r13d
cmpl %r10d, %r15d
jl 0x6487
movl %r15d, %r9d
subl %r10d, %r9d
movslq %r9d, %r9
movq 0x18(%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 0x64b2
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 0x6430
testl %edi, %edi
je 0x658f
movq %rdx, %r9
shlq $0x20, %r9
movabsq $0x100000000, %r8       # imm = 0x100000000
addq %r9, %r8
leaq (%rsp,%rdx,2), %r9
addq $0x20, %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 0x6501
decl %ebp
movw %bp, (%r14)
sarq $0x1f, %r11
addw $0x2, 0x20(%rsp,%r11)
decw 0x20(%rsp,%rdx,2)
leal -0x2(%rdi), %r11d
cmpl $0x2, %edi
movl %r11d, %edi
jg 0x64fb
testl %edx, %edx
je 0x658f
movl $0x203, %edi            # imm = 0x203
leaq 0x28c(%rsp), %r8
movzwl 0x20(%rsp,%rdx,2), %r9d
testl %r9d, %r9d
je 0x658a
movslq %edi, %r10
leaq (%r8,%r10,4), %r11
movslq (%r11), %r10
decl %edi
addq $-0x4, %r11
cmpl %ecx, %r10d
jg 0x6557
movzwl 0x2(%rax,%r10,4), %r14d
movq %rdx, %r11
subq %r14, %r11
je 0x6585
movzwl (%rax,%r10,4), %r14d
imulq %r14, %r11
addq %r11, (%rsi)
movw %dx, 0x2(%rax,%r10,4)
decl %r9d
jne 0x6550
decq %rdx
jne 0x6545
movl $0x1, %eax
xorl %ecx, %ecx
addw 0x1e(%rsp,%rax,2), %cx
addl %ecx, %ecx
movw %cx, 0x60(%rsp,%rax,2)
incq %rax
cmpq $0x10, %rax
jne 0x6596
movq 0x48(%rsp), %r9
leal 0x1(%r9), %eax
cmpl $0x1, %eax
adcl $0x1, %r9d
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movzwl 0x2(%rcx,%rax,4), %ecx
testl %ecx, %ecx
je 0x6603
movl %ecx, %esi
movzwl 0x60(%rsp,%rsi,2), %edx
movl %edx, %edi
incl %edi
movw %di, 0x60(%rsp,%rsi,2)
incl %ecx
xorl %esi, %esi
movl %edx, %r8d
andl $0x1, %r8d
movzwl %dx, %edx
movl %esi, %edi
orl %r8d, %edi
shrl %edx
leal (%rdi,%rdi), %esi
decl %ecx
cmpl $0x1, %ecx
ja 0x65df
movq 0x8(%rsp), %rcx
movw %di, (%rcx,%rax,4)
incq %rax
cmpq %r9, %rax
jne 0x65bd
jmp 0x6981
testl %r15d, %r15d
movq 0x10(%rsp), %r15
je 0x6975
movq 0x18(%r15), %rcx
movq 0x8(%rcx), %rax
movl 0x10(%rcx), %ecx
movl %r8d, 0x8(%r15)
testl %r8d, %r8d
jle 0x691b
movl %r8d, %esi
leal 0x7(%r8), %edx
decq %rsi
movq %rsi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x3, %edx
shlq $0x5, %rdx
movdqa 0xca57(%rip), %xmm1     # 0x130b0
movdqa 0xca5f(%rip), %xmm2     # 0x130c0
movdqa 0xca67(%rip), %xmm3     # 0x130d0
movdqa 0xca6f(%rip), %xmm4     # 0x130e0
xorl %esi, %esi
movdqa 0xca75(%rip), %xmm5     # 0x130f0
pxor %xmm5, %xmm0
pcmpeqd %xmm6, %xmm6
movdqa 0xd875(%rip), %xmm7     # 0x13f00
movdqa %xmm4, %xmm8
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm9    # xmm9 = xmm10[0,0,2,2]
pshuflw $0xe8, %xmm9, %xmm11    # xmm11 = xmm9[0,2,2,3,4,5,6,7]
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm8     # xmm8 = xmm8[1,1,3,3]
pshuflw $0xe8, %xmm8, %xmm12    # xmm12 = xmm8[0,2,2,3,4,5,6,7]
pand %xmm11, %xmm12
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
pshuflw $0xe8, %xmm10, %xmm11   # xmm11 = xmm10[0,2,2,3,4,5,6,7]
por %xmm12, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
movd %xmm11, %edi
testb $0x1, %dil
je 0x66f3
movq 0x8(%rsp), %rdi
movw $0x0, 0x2(%rdi,%rsi)
pand %xmm9, %xmm8
por %xmm10, %xmm8
packssdw %xmm8, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
movd %xmm8, %edi
shrl $0x10, %edi
testb $0x1, %dil
je 0x6726
movq 0x8(%rsp), %rdi
movw $0x0, 0x6(%rdi,%rsi)
movdqa %xmm3, %xmm9
pxor %xmm5, %xmm9
movdqa %xmm9, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm8    # xmm8 = xmm10[0,0,2,2]
pcmpeqd %xmm0, %xmm9
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
movdqa %xmm9, %xmm11
pand %xmm8, %xmm11
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
por %xmm10, %xmm11
packssdw %xmm11, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
pextrw $0x2, %xmm11, %edi
testb $0x1, %dil
je 0x6787
movq 0x8(%rsp), %rdi
movw $0x0, 0xa(%rdi,%rsi)
pshufhw $0x84, %xmm8, %xmm8     # xmm8 = xmm8[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm9, %xmm9     # xmm9 = xmm9[0,1,2,3,4,5,4,6]
pand %xmm8, %xmm9
pshufhw $0x84, %xmm10, %xmm8    # xmm8 = xmm10[0,1,2,3,4,5,4,6]
por %xmm9, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
pextrw $0x3, %xmm8, %edi
testb $0x1, %dil
je 0x67c5
movq 0x8(%rsp), %rdi
movw $0x0, 0xe(%rdi,%rsi)
movdqa %xmm2, %xmm8
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm9    # xmm9 = xmm10[0,0,2,2]
pshuflw $0xe8, %xmm9, %xmm11    # xmm11 = xmm9[0,2,2,3,4,5,6,7]
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm8     # xmm8 = xmm8[1,1,3,3]
pshuflw $0xe8, %xmm8, %xmm12    # xmm12 = xmm8[0,2,2,3,4,5,6,7]
pand %xmm11, %xmm12
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
pshuflw $0xe8, %xmm10, %xmm11   # xmm11 = xmm10[0,2,2,3,4,5,6,7]
por %xmm12, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
pextrw $0x4, %xmm11, %edi
testb $0x1, %dil
je 0x682e
movq 0x8(%rsp), %rdi
movw $0x0, 0x12(%rdi,%rsi)
pand %xmm9, %xmm8
por %xmm10, %xmm8
packssdw %xmm8, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
pextrw $0x5, %xmm8, %edi
testb $0x1, %dil
je 0x685f
movq 0x8(%rsp), %rdi
movw $0x0, 0x16(%rdi,%rsi)
movdqa %xmm1, %xmm9
pxor %xmm5, %xmm9
movdqa %xmm9, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm8    # xmm8 = xmm10[0,0,2,2]
pcmpeqd %xmm0, %xmm9
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
movdqa %xmm9, %xmm11
pand %xmm8, %xmm11
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
por %xmm10, %xmm11
packssdw %xmm11, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
pextrw $0x6, %xmm11, %edi
testb $0x1, %dil
je 0x68c0
movq 0x8(%rsp), %rdi
movw $0x0, 0x1a(%rdi,%rsi)
pshufhw $0x84, %xmm8, %xmm8     # xmm8 = xmm8[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm9, %xmm9     # xmm9 = xmm9[0,1,2,3,4,5,4,6]
pand %xmm8, %xmm9
pshufhw $0x84, %xmm10, %xmm8    # xmm8 = xmm10[0,1,2,3,4,5,4,6]
por %xmm9, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
pextrw $0x7, %xmm8, %edi
testb $0x1, %dil
je 0x68fe
movq 0x8(%rsp), %rdi
movw $0x0, 0x1e(%rdi,%rsi)
paddq %xmm7, %xmm4
paddq %xmm7, %xmm3
paddq %xmm7, %xmm2
paddq %xmm7, %xmm1
addq $0x20, %rsi
cmpq %rsi, %rdx
jne 0x668b
movslq %r8d, %rdx
movq 0x8(%rsp), %rsi
movw $0x1, 0x2(%rsi,%rdx,4)
xorl %esi, %esi
subl %ecx, %r8d
jl 0x6937
movslq %r8d, %rcx
movl (%rax,%rcx,4), %esi
movq 0x8(%rsp), %rax
movzwl (%rax,%rdx,4), %eax
leal 0x1(%rsi), %ecx
movslq %ecx, %rcx
imulq %rax, %rcx
addq %rcx, (%r12)
testq %r13, %r13
je 0x6968
movzwl 0x2(%r13,%rdx,4), %ecx
movslq %esi, %rsi
addq %rcx, %rsi
imulq %rax, %rsi
addq %rsi, 0x8(%r12)
movq 0x8(%rsp), %rax
movw $0x0, (%rax,%rdx,4)
jmp 0x6981
movl %r14d, 0x8(%r15)
movq 0x8(%rsp), %rax
movl %r14d, (%rax)
addq $0xaa8, %rsp            # imm = 0xAA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq