// File: /workspace/github_c_makefile_star2_O0/DavidSM64[P]DKR-Decompressor/DKRGzip.c
// Function: scan_tree(ct_data*, int)
void scan_tree (ct_data* tree, int max_code){
    int n;                     /* iterates over all tree elements */
    int prevlen = -1;          /* last emitted length */
    int curlen;                /* length of current code */
    int nextlen = tree[0].Len; /* length of next code */
    int count = 0;             /* repeat count of the current code */
    int max_count = 7;         /* max repeat count */
    int min_count = 4;         /* min repeat count */

    if (nextlen == 0) max_count = 138, min_count = 3;
    tree[max_code+1].Len = (u16)0xffff; /* guard */

    for (n = 0; n <= max_code; n++) {
        curlen = nextlen; nextlen = tree[n+1].Len;
        if (++count < max_count && curlen == nextlen) {
            continue;
        } else if (count < min_count) {
            bl_tree[curlen].Freq += count;
        } else if (curlen != 0) {
            if (curlen != prevlen) bl_tree[curlen].Freq++;
            bl_tree[REP_3_6].Freq++;
        } else if (count <= 10) {
            bl_tree[REPZ_3_10].Freq++;
        } else {
            bl_tree[REPZ_11_138].Freq++;
        }
        count = 0; prevlen = curlen;
        if (nextlen == 0) {
            max_count = 138, min_count = 3;
        } else if (curlen == nextlen) {
            max_count = 6, min_count = 3;
        } else {
            max_count = 7, min_count = 4;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x7, -0x24(%rbp)
movl $0x4, -0x28(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x99d6
movl $0x8a, -0x24(%rbp)
movl $0x3, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl -0xc(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw $0xffff, 0x2(%rax,%rcx,4) # imm = 0xFFFF
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jg 0x9b2c
movl -0x1c(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl -0x10(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
cmpl -0x24(%rbp), %eax
jge 0x9a33
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9a33
jmp 0x9b1e
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x9a5f
movl -0x20(%rbp), %edx
movslq -0x18(%rbp), %rcx
leaq 0x80f7(%rip), %rax      # 0x11b40
movzwl (%rax,%rcx,4), %eax
addl %edx, %eax
movw %ax, %dx
leaq 0x80e7(%rip), %rax      # 0x11b40
movw %dx, (%rax,%rcx,4)
jmp 0x9acf
cmpl $0x0, -0x18(%rbp)
je 0x9a9f
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x9a8b
movslq -0x18(%rbp), %rcx
leaq 0x80c8(%rip), %rax      # 0x11b40
movw (%rax,%rcx,4), %dx
addw $0x1, %dx
leaq 0x80b9(%rip), %rax      # 0x11b40
movw %dx, (%rax,%rcx,4)
movw 0x80ee(%rip), %ax       # 0x11b80
addw $0x1, %ax
movw %ax, 0x80e3(%rip)       # 0x11b80
jmp 0x9acd
cmpl $0xa, -0x20(%rbp)
jg 0x9ab9
movw 0x80d8(%rip), %ax       # 0x11b84
addw $0x1, %ax
movw %ax, 0x80cd(%rip)       # 0x11b84
jmp 0x9acb
movw 0x80c8(%rip), %ax       # 0x11b88
addw $0x1, %ax
movw %ax, 0x80bd(%rip)       # 0x11b88
jmp 0x9acd
jmp 0x9acf
jmp 0x9ad1
movl $0x0, -0x20(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x9af4
movl $0x8a, -0x24(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x9b1c
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9b0c
movl $0x6, -0x24(%rbp)
movl $0x3, -0x28(%rbp)
jmp 0x9b1a
movl $0x7, -0x24(%rbp)
movl $0x4, -0x28(%rbp)
jmp 0x9b1c
jmp 0x9b1e
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x99f1
popq %rbp
retq
nop