// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, 0x76e7(%rip)      # 0xc484
xorl %ecx, %ecx
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
movl $0x23d, 0x76d3(%rip)    # imm = 0x23D
pushq $-0x1
popq %r14
leaq 0x6dd0(%rip), %r10      # 0xbb90
leaq 0x76c9(%rip), %r9       # 0xc490
cmpq %rcx, %rdx
je 0x4dff
cmpw $0x0, (%rbx,%rcx,4)
je 0x4df4
movslq 0x76aa(%rip), %rsi      # 0xc484
leaq 0x1(%rsi), %r8
movl %r8d, 0x769f(%rip)      # 0xc484
movl %ecx, 0x4(%r10,%rsi,4)
movb $0x0, (%rcx,%r9)
movl %ecx, %r14d
jmp 0x4dfa
andw $0x0, 0x2(%rbx,%rcx,4)
incq %rcx
jmp 0x4dc7
xorl %ecx, %ecx
movslq 0x767c(%rip), %r12      # 0xc484
cmpq $0x1, %r12
jg 0x4e53
leal 0x1(%r14), %edx
cmpl $0x2, %r14d
cmovll %edx, %r14d
cmovgel %ecx, %edx
leaq 0x1(%r12), %rsi
movl %esi, 0x765c(%rip)      # 0xc484
movl %edx, 0x4(%r10,%r12,4)
movslq %edx, %rdx
movw $0x1, (%rbx,%rdx,4)
decl 0x8ca4(%rip)            # 0xdae0
movb $0x0, (%rdx,%r9)
testq %rax, %rax
je 0x4e01
movzwl 0x2(%rax,%rdx,4), %edx
subl %edx, 0x8c93(%rip)      # 0xdae4
jmp 0x4e01
movq %rdi, -0x38(%rbp)
movl %r14d, 0x24(%rdi)
shrl %r12d
testl %r12d, %r12d
jle 0x4e73
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4afe
decl %r12d
jmp 0x4e5e
movq %r14, -0x30(%rbp)
movl 0x7607(%rip), %eax      # 0xc484
movq %r13, %r14
movslq 0x6d0d(%rip), %r12      # 0xbb94
leal -0x1(%rax), %ecx
movl %ecx, 0x75f4(%rip)      # 0xc484
cltq
leaq 0x6cf7(%rip), %r15      # 0xbb90
movl (%r15,%rax,4), %eax
movl %eax, 0x6cf1(%rip)      # 0xbb94
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0x4afe
movslq 0x6cdf(%rip), %rax      # 0xbb94
movslq 0x75cc(%rip), %rcx      # 0xc488
movl %r12d, -0x4(%r15,%rcx,4)
leaq -0x2(%rcx), %rdx
movl %edx, 0x75bd(%rip)      # 0xc488
movl %eax, -0x8(%r15,%rcx,4)
movzwl (%rbx,%rax,4), %ecx
addw (%rbx,%r12,4), %cx
movw %cx, (%rbx,%r13,4)
leaq 0x75ab(%rip), %rsi      # 0xc490
movzbl (%r12,%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,%r12,4)
incq %r14
movl %r13d, 0x6c84(%rip)     # 0xbb94
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0x4afe
movl 0x7563(%rip), %eax      # 0xc484
movq %r14, %r13
cmpl $0x1, %eax
jg 0x4e80
movl 0x6c61(%rip), %eax      # 0xbb94
movslq 0x754e(%rip), %rcx      # 0xc488
leaq -0x1(%rcx), %rdx
movl %edx, 0x7544(%rip)      # 0xc488
leaq 0x6c45(%rip), %rdx      # 0xbb90
movl %eax, -0x4(%rdx,%rcx,4)
movq -0x38(%rbp), %rdi
callq 0x4ba0
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x49fe