// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x1c(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, 0x8e50(%rip)      # 0x124f4
movl $0x23d, 0x8e4a(%rip)    # imm = 0x23D
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x971d
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzwl (%rax,%rcx,4), %eax
cmpl $0x0, %eax
je 0x9701
movl -0x20(%rbp), %edx
movl %edx, -0x28(%rbp)
movl 0x8e1a(%rip), %eax      # 0x124f4
addl $0x1, %eax
movl %eax, 0x8e11(%rip)      # 0x124f4
movslq %eax, %rcx
leaq 0x8513(%rip), %rax      # 0x11c00
movl %edx, (%rax,%rcx,4)
movslq -0x20(%rbp), %rcx
leaq 0x8e05(%rip), %rax      # 0x12500
movb $0x0, (%rax,%rcx)
jmp 0x9710
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movw $0x0, 0x2(%rax,%rcx,4)
jmp 0x9712
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x96b5
jmp 0x971f
cmpl $0x2, 0x8dce(%rip)      # 0x124f4
jge 0x97bc
cmpl $0x2, -0x28(%rbp)
jge 0x9740
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x9747
xorl %eax, %eax
movl %eax, -0x34(%rbp)
jmp 0x9747
movl -0x34(%rbp), %eax
movl 0x8da4(%rip), %ecx      # 0x124f4
addl $0x1, %ecx
movl %ecx, 0x8d9b(%rip)      # 0x124f4
movslq %ecx, %rdx
leaq 0x849d(%rip), %rcx      # 0x11c00
movl %eax, (%rcx,%rdx,4)
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movw $0x1, (%rax,%rcx,4)
movslq -0x30(%rbp), %rcx
leaq 0x8d7e(%rip), %rax      # 0x12500
movb $0x0, (%rax,%rcx)
movl 0xa3c4(%rip), %eax      # 0x13b50
addl $-0x1, %eax
movl %eax, 0xa3bb(%rip)      # 0x13b50
cmpq $0x0, -0x18(%rbp)
je 0x97b7
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
movzwl 0x2(%rax,%rcx,4), %ecx
movl 0xa3a5(%rip), %eax      # 0x13b54
subl %ecx, %eax
movl %eax, 0xa39d(%rip)      # 0x13b54
jmp 0x971f
movl -0x28(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x24(%rax)
movl 0x8d28(%rip), %eax      # 0x124f4
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x20(%rbp)
cmpl $0x1, -0x20(%rbp)
jl 0x97f4
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x9170
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x97d7
jmp 0x97f6
movl 0x8408(%rip), %eax      # 0x11c04
movl %eax, -0x20(%rbp)
movl 0x8cef(%rip), %eax      # 0x124f4
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x8ce4(%rip)      # 0x124f4
movslq %eax, %rcx
leaq 0x83e6(%rip), %rax      # 0x11c00
movl (%rax,%rcx,4), %eax
movl %eax, 0x83e1(%rip)      # 0x11c04
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x9170
movl 0x83cd(%rip), %eax      # 0x11c04
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %edx
movl 0x8cb5(%rip), %eax      # 0x124f8
addl $-0x1, %eax
movl %eax, 0x8cac(%rip)      # 0x124f8
movslq %eax, %rcx
leaq 0x83aa(%rip), %rax      # 0x11c00
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %edx
movl 0x8c96(%rip), %eax      # 0x124f8
addl $-0x1, %eax
movl %eax, 0x8c8d(%rip)      # 0x124f8
movslq %eax, %rcx
leaq 0x838b(%rip), %rax      # 0x11c00
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzwl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movzwl (%rcx,%rdx,4), %ecx
addl %ecx, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movw %dx, (%rax,%rcx,4)
movslq -0x20(%rbp), %rcx
leaq 0x8c54(%rip), %rax      # 0x12500
movzbl (%rax,%rcx), %eax
movslq -0x24(%rbp), %rdx
leaq 0x8c45(%rip), %rcx      # 0x12500
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jl 0x98d6
movslq -0x20(%rbp), %rcx
leaq 0x8c32(%rip), %rax      # 0x12500
movb (%rax,%rcx), %al
movb %al, -0x35(%rbp)
jmp 0x98e7
movslq -0x24(%rbp), %rcx
leaq 0x8c1f(%rip), %rax      # 0x12500
movb (%rax,%rcx), %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
movzbl %al, %eax
addl $0x1, %eax
movb %al, %dl
movslq -0x2c(%rbp), %rcx
leaq 0x8c03(%rip), %rax      # 0x12500
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movw %dx, 0x2(%rax,%rcx,4)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movl %eax, 0x82d3(%rip)      # 0x11c04
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x9170
cmpl $0x2, 0x8bae(%rip)      # 0x124f4
jge 0x97f6
movl 0x82b2(%rip), %edx      # 0x11c04
movl 0x8ba0(%rip), %eax      # 0x124f8
addl $-0x1, %eax
movl %eax, 0x8b97(%rip)      # 0x124f8
movslq %eax, %rcx
leaq 0x8295(%rip), %rax      # 0x11c00
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rdi
callq 0x9330
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x8fe0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)