// File: tomfunkhouser[P]gaps/pkgs/png/trees.c
// Function: scan_tree
local void scan_tree (s, tree, max_code)
    deflate_state *s;
    ct_data *tree;   /* the tree to be scanned */
    int max_code;    /* and its largest code of non zero frequency */
{
    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 = (ush)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) {
            s->bl_tree[curlen].Freq += count;
        } else if (curlen != 0) {
            if (curlen != prevlen) s->bl_tree[curlen].Freq++;
            s->bl_tree[REP_3_6].Freq++;
        } else if (count <= 10) {
            s->bl_tree[REPZ_3_10].Freq++;
        } else {
            s->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:
movq %rdi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
movl %edx, -0x14(%rsp)
movl $0xffffffff, -0x1c(%rsp) # imm = 0xFFFFFFFF
movq -0x10(%rsp), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x24(%rsp)
movl $0x0, -0x28(%rsp)
movl $0x7, -0x2c(%rsp)
movl $0x4, -0x30(%rsp)
cmpl $0x0, -0x24(%rsp)
jne 0x121a62
movl $0x8a, -0x2c(%rsp)
movl $0x3, -0x30(%rsp)
movq -0x10(%rsp), %rax
movl -0x14(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw $0xffff, 0x2(%rax,%rcx,4) # imm = 0xFFFF
movl $0x0, -0x18(%rsp)
movl -0x18(%rsp), %eax
cmpl -0x14(%rsp), %eax
jg 0x121beb
movl -0x24(%rsp), %eax
movl %eax, -0x20(%rsp)
movq -0x10(%rsp), %rax
movl -0x18(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x2(%rax,%rcx,4), %eax
movl %eax, -0x24(%rsp)
movl -0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rsp)
cmpl -0x2c(%rsp), %eax
jge 0x121ace
movl -0x20(%rsp), %eax
cmpl -0x24(%rsp), %eax
jne 0x121ace
jmp 0x121bdb
movl -0x28(%rsp), %eax
cmpl -0x30(%rsp), %eax
jge 0x121afd
movl -0x28(%rsp), %esi
movq -0x8(%rsp), %rax
movslq -0x20(%rsp), %rcx
movzwl 0xaac(%rax,%rcx,4), %edx
addl %esi, %edx
movw %dx, 0xaac(%rax,%rcx,4)
jmp 0x121b80
cmpl $0x0, -0x20(%rsp)
je 0x121b45
movl -0x20(%rsp), %eax
cmpl -0x1c(%rsp), %eax
je 0x121b2c
movq -0x8(%rsp), %rax
movslq -0x20(%rsp), %rcx
movw 0xaac(%rax,%rcx,4), %dx
addw $0x1, %dx
movw %dx, 0xaac(%rax,%rcx,4)
movq -0x8(%rsp), %rax
movw 0xaec(%rax), %cx
addw $0x1, %cx
movw %cx, 0xaec(%rax)
jmp 0x121b7e
cmpl $0xa, -0x28(%rsp)
jg 0x121b65
movq -0x8(%rsp), %rax
movw 0xaf0(%rax), %cx
addw $0x1, %cx
movw %cx, 0xaf0(%rax)
jmp 0x121b7c
movq -0x8(%rsp), %rax
movw 0xaf4(%rax), %cx
addw $0x1, %cx
movw %cx, 0xaf4(%rax)
jmp 0x121b7e
jmp 0x121b80
jmp 0x121b82
movl $0x0, -0x28(%rsp)
movl -0x20(%rsp), %eax
movl %eax, -0x1c(%rsp)
cmpl $0x0, -0x24(%rsp)
jne 0x121bab
movl $0x8a, -0x2c(%rsp)
movl $0x3, -0x30(%rsp)
jmp 0x121bd9
movl -0x20(%rsp), %eax
cmpl -0x24(%rsp), %eax
jne 0x121bc7
movl $0x6, -0x2c(%rsp)
movl $0x3, -0x30(%rsp)
jmp 0x121bd7
movl $0x7, -0x2c(%rsp)
movl $0x4, -0x30(%rsp)
jmp 0x121bd9
jmp 0x121bdb
movl -0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rsp)
jmp 0x121a80
retq
nopl (%rax)