// File: /workspace/github_c_makefile_star2_O3/DavidSM64[P]DKR-Decompressor/DKRGzip.c
// Function: build_tree_1(tree_desc*)
void build_tree_1(tree_desc* desc){
    ct_data *tree   = desc->dyn_tree;
    ct_data *stree  = desc->static_tree;
    int elems            = desc->elems;
    int n, m;          /* iterate over heap elements */
    int max_code = -1; /* largest code with non zero frequency */
    int node = elems;  /* next internal node of the tree */

    /* Construct the initial heap, with least frequent element in
     * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
     * heap[0] is not used.
     */
    heap_len = 0, heap_max = HEAP_SIZE;

    for (n = 0; n < elems; n++) {
        if (tree[n].Freq != 0) {
            heap[++heap_len] = 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. So to avoid special checks later on we force at least
     * two codes of non zero frequency.
     */
    while (heap_len < 2) {
        int new_ = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
        tree[new_].Freq = 1;
        depth[new_] = 0;
        opt_len--; if (stree) static_len -= stree[new_].Len;
        /* new is 0 or 1 so it does not have extra bits */
    }
    desc->max_code = max_code;

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

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

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

        /* Create a new node father of n and m */
        tree[node].Freq = tree[n].Freq + tree[m].Freq;
        depth[node] = (u8) (MAX(depth[n], depth[m]) + 1);
        tree[n].Dad = tree[m].Dad = (u16)node;
#ifdef DUMP_B_TREE
        if (tree == bl_tree) {
            fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
                    node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
        }
#endif
        /* and insert the new node in the heap */
        heap[SMALLEST] = node++;
        pqdownheap(tree, SMALLEST);

    } while (heap_len >= 2);

    heap[--heap_max] = heap[SMALLEST];

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

    /* The field len is now set, we can generate the bit codes */
    gen_codes ((ct_data *)tree, max_code);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq (%rdi), %rbx
movq 0x8(%rdi), %rax
movslq 0x1c(%rdi), %r13
xorl %ecx, %ecx
movl $0x0, 0x71e6(%rip)      # 0xc494
movl $0x23d, 0x71e0(%rip)    # imm = 0x23D
leaq 0x68e1(%rip), %r10      # 0xbba0
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
testq %r13, %r13
jle 0x5318
xorl %edx, %edx
leaq 0x71cd(%rip), %rsi      # 0xc4a0
cmpw $0x0, (%rbx,%rdx,4)
je 0x52fa
movslq 0x71b3(%rip), %r9       # 0xc494
leaq 0x1(%r9), %r8
movl %r8d, 0x71a8(%rip)      # 0xc494
movl %edx, 0x4(%r10,%r9,4)
movb $0x0, (%rdx,%rsi)
movl %edx, %r9d
jmp 0x5301
movw $0x0, 0x2(%rbx,%rdx,4)
incq %rdx
cmpq %rdx, %r13
jne 0x52d3
movl 0x7184(%rip), %r12d     # 0xc494
cmpl $0x1, %r12d
jle 0x531b
jmp 0x5375
xorl %r12d, %r12d
leaq 0x717e(%rip), %rdx      # 0xc4a0
leal 0x1(%r9), %esi
cmpl $0x2, %r9d
cmovll %esi, %r9d
cmovgel %ecx, %esi
leal 0x1(%r12), %r8d
movl %r8d, 0x7157(%rip)      # 0xc494
movslq %r12d, %r8
movl %esi, 0x4(%r10,%r8,4)
movslq %esi, %rsi
movw $0x1, (%rbx,%rsi,4)
decl 0x879c(%rip)            # 0xdaf0
movb $0x0, (%rsi,%rdx)
testq %rax, %rax
je 0x5368
movzwl 0x2(%rax,%rsi,4), %esi
subl %esi, 0x878c(%rip)      # 0xdaf4
movl 0x7125(%rip), %r12d     # 0xc494
cmpl $0x2, %r12d
jl 0x5322
movq %rdi, -0x38(%rbp)
movq %r9, -0x30(%rbp)
movl %r9d, 0x24(%rdi)
shrl %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4fdf
leal -0x1(%r12), %eax
cmpl $0x1, %r12d
movl %eax, %r12d
ja 0x5384
movl 0x70f1(%rip), %eax      # 0xc494
movq %r13, %r14
movslq 0x67f7(%rip), %r15      # 0xbba4
leal -0x1(%rax), %ecx
movl %ecx, 0x70de(%rip)      # 0xc494
cltq
leaq 0x67e1(%rip), %r12      # 0xbba0
movl (%r12,%rax,4), %eax
movl %eax, 0x67db(%rip)      # 0xbba4
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4fdf
movslq 0x67c7(%rip), %rax      # 0xbba4
movslq 0x70b4(%rip), %rcx      # 0xc498
movl %r15d, -0x4(%r12,%rcx,4)
leaq -0x2(%rcx), %rdx
movl %edx, 0x70a5(%rip)      # 0xc498
movl %eax, -0x8(%r12,%rcx,4)
movzwl (%rbx,%rax,4), %ecx
addw (%rbx,%r15,4), %cx
movw %cx, (%rbx,%r13,4)
leaq 0x7093(%rip), %rsi      # 0xc4a0
movzbl (%r15,%rsi), %ecx
movzbl (%rax,%rsi), %edx
cmpb %dl, %cl
cmoval %ecx, %edx
incb %dl
movb %dl, (%r13,%rsi)
movw %r13w, 0x2(%rbx,%rax,4)
movw %r13w, 0x2(%rbx,%r15,4)
incq %r14
movl %r13d, 0x676c(%rip)     # 0xbba4
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4fdf
movl 0x7049(%rip), %eax      # 0xc494
movq %r14, %r13
cmpl $0x1, %eax
jg 0x53a6
movl 0x6747(%rip), %eax      # 0xbba4
movslq 0x7034(%rip), %rcx      # 0xc498
leaq -0x1(%rcx), %rdx
movl %edx, 0x702a(%rip)      # 0xc498
leaq 0x672b(%rip), %rdx      # 0xbba0
movl %eax, -0x4(%rdx,%rcx,4)
movq -0x38(%rbp), %rdi
callq 0x50a5
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4eda