// File: /workspace/github_c_makefile_star2_O1/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
movl $0x0, 0x70cc(%rip)      # 0xc494
movl $0x23d, 0x70c6(%rip)    # imm = 0x23D
leaq 0x67c7(%rip), %r9       # 0xbba0
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
testq %r13, %r13
jle 0x5423
xorl %ecx, %ecx
leaq 0x70b3(%rip), %rdx      # 0xc4a0
cmpw $0x0, (%rbx,%rcx,4)
je 0x5414
movslq 0x7099(%rip), %rsi      # 0xc494
leaq 0x1(%rsi), %r8
movl %r8d, 0x708e(%rip)      # 0xc494
movl %ecx, 0x4(%r9,%rsi,4)
movb $0x0, (%rcx,%rdx)
movl %ecx, %r10d
jmp 0x541b
movw $0x0, 0x2(%rbx,%rcx,4)
incq %rcx
cmpq %rcx, %r13
jne 0x53ed
movl 0x706a(%rip), %r12d     # 0xc494
cmpl $0x1, %r12d
jg 0x548c
xorl %ecx, %ecx
leaq 0x7067(%rip), %rdx      # 0xc4a0
leal 0x1(%r10), %esi
cmpl $0x2, %r10d
cmovll %esi, %r10d
cmovgel %ecx, %esi
leal 0x1(%r12), %r8d
movl %r8d, 0x7040(%rip)      # 0xc494
movslq %r12d, %r8
movl %esi, 0x4(%r9,%r8,4)
movslq %esi, %rsi
movw $0x1, (%rbx,%rsi,4)
decl 0x8685(%rip)            # 0xdaf0
movb $0x0, (%rsi,%rdx)
testq %rax, %rax
je 0x547f
movzwl 0x2(%rax,%rsi,4), %esi
subl %esi, 0x8675(%rip)      # 0xdaf4
movl 0x700e(%rip), %r12d     # 0xc494
cmpl $0x2, %r12d
jl 0x5439
movq %rdi, -0x38(%rbp)
movq %r10, -0x30(%rbp)
movl %r10d, 0x24(%rdi)
cmpl $0x2, %r12d
jb 0x54ba
shrl %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x5100
leal -0x1(%r12), %eax
cmpl $0x1, %r12d
movl %eax, %r12d
jg 0x54a1
movq %r13, %r14
movslq 0x66e0(%rip), %r15      # 0xbba4
movslq 0x6fc9(%rip), %rax      # 0xc494
leal -0x1(%rax), %ecx
movl %ecx, 0x6fc0(%rip)      # 0xc494
leaq 0x66c5(%rip), %r12      # 0xbba0
movl (%r12,%rax,4), %eax
movl %eax, 0x66bf(%rip)      # 0xbba4
movq %rbx, %rdi
movl $0x1, %esi
callq 0x5100
movslq 0x66ab(%rip), %rax      # 0xbba4
movslq 0x6f98(%rip), %rcx      # 0xc498
leaq -0x1(%rcx), %rdx
movl %edx, 0x6f8e(%rip)      # 0xc498
movl %r15d, -0x4(%r12,%rcx,4)
leaq -0x2(%rcx), %rdx
movl %edx, 0x6f7f(%rip)      # 0xc498
movl %eax, -0x8(%r12,%rcx,4)
movzwl (%rbx,%rax,4), %ecx
addw (%rbx,%r15,4), %cx
movw %cx, (%rbx,%r13,4)
leaq 0x6f6d(%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, 0x6646(%rip)     # 0xbba4
movq %rbx, %rdi
movl $0x1, %esi
callq 0x5100
cmpl $0x1, 0x6f22(%rip)      # 0xc494
movq %r14, %r13
jg 0x54bd
movl 0x6623(%rip), %eax      # 0xbba4
movslq 0x6f10(%rip), %rcx      # 0xc498
leaq -0x1(%rcx), %rdx
movl %edx, 0x6f06(%rip)      # 0xc498
leaq 0x6607(%rip), %rdx      # 0xbba0
movl %eax, -0x4(%rdx,%rcx,4)
movq -0x38(%rbp), %rdi
callq 0x51bc
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4ff4