// File: tomfunkhouser[P]gaps/pkgs/png/trees.c
// Function: tr_tally
int ZLIB_INTERNAL _tr_tally (s, dist, lc)
    deflate_state *s;
    unsigned dist;  /* distance of matched string */
    unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
{
    s->d_buf[s->last_lit] = (ush)dist;
    s->l_buf[s->last_lit++] = (uch)lc;
    if (dist == 0) {
        /* lc is the unmatched char */
        s->dyn_ltree[lc].Freq++;
    } else {
        s->matches++;
        /* Here, lc is the match length - MIN_MATCH */
        dist--;             /* dist = match distance - 1 */
        Assert((ush)dist < (ush)MAX_DIST(s) &&
               (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
               (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");

        s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
        s->dyn_dtree[d_code(dist)].Freq++;
    }

#ifdef TRUNCATE_BLOCK
    /* Try to guess if it is profitable to stop the current block here */
    if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
        /* Compute an upper bound for the compressed length */
        ulg out_length = (ulg)s->last_lit*8L;
        ulg in_length = (ulg)((long)s->strstart - s->block_start);
        int dcode;
        for (dcode = 0; dcode < D_CODES; dcode++) {
            out_length += (ulg)s->dyn_dtree[dcode].Freq *
                (5L+extra_dbits[dcode]);
        }
        out_length >>= 3;
        Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
               s->last_lit, in_length, out_length,
               100L - out_length*100L/in_length));
        if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
    }
#endif
    return (s->last_lit == s->lit_bufsize-1);
    /* We avoid equality with lit_bufsize because of wraparound at 64K
     * on 16 bit machines and because stored blocks are restricted to
     * 64K-1 bytes.
     */
}

// Assembly:
movq %rdi, -0x8(%rsp)
movl %esi, -0xc(%rsp)
movl %edx, -0x10(%rsp)
movl -0xc(%rsp), %eax
movw %ax, %dx
movq -0x8(%rsp), %rax
movq 0x1700(%rax), %rax
movq -0x8(%rsp), %rcx
movl 0x16fc(%rcx), %ecx
movw %dx, (%rax,%rcx,2)
movl -0x10(%rsp), %eax
movb %al, %dl
movq -0x8(%rsp), %rax
movq 0x16f0(%rax), %rax
movq -0x8(%rsp), %rsi
movl 0x16fc(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x16fc(%rsi)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
cmpl $0x0, -0xc(%rsp)
jne 0x121215
movq -0x8(%rsp), %rax
movl -0x10(%rsp), %ecx
movw 0xc4(%rax,%rcx,4), %dx
addw $0x1, %dx
movw %dx, 0xc4(%rax,%rcx,4)
jmp 0x1212d2
movq -0x8(%rsp), %rax
movl 0x1718(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1718(%rax)
movl -0xc(%rsp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rsp)
movq -0x8(%rsp), %rax
movl -0x10(%rsp), %ecx
movl %ecx, %edx
leaq 0x1d4fa(%rip), %rcx     # 0x13e740
movzbl (%rcx,%rdx), %ecx
addl $0x100, %ecx            # imm = 0x100
addl $0x1, %ecx
movslq %ecx, %rcx
movw 0xc4(%rax,%rcx,4), %dx
addw $0x1, %dx
movw %dx, 0xc4(%rax,%rcx,4)
movq -0x8(%rsp), %rax
addq $0x9b8, %rax            # imm = 0x9B8
movq %rax, -0x18(%rsp)
cmpl $0x100, -0xc(%rsp)      # imm = 0x100
jae 0x12129b
movl -0xc(%rsp), %eax
movl %eax, %ecx
leaq 0x1d2af(%rip), %rax     # 0x13e540
movzbl (%rax,%rcx), %eax
movl %eax, -0x1c(%rsp)
jmp 0x1212ba
movl -0xc(%rsp), %eax
shrl $0x7, %eax
addl $0x100, %eax            # imm = 0x100
movl %eax, %eax
movl %eax, %ecx
leaq 0x1d28e(%rip), %rax     # 0x13e540
movzbl (%rax,%rcx), %eax
movl %eax, -0x1c(%rsp)
movq -0x18(%rsp), %rax
movl -0x1c(%rsp), %ecx
movslq %ecx, %rcx
movw (%rax,%rcx,4), %dx
addw $0x1, %dx
movw %dx, (%rax,%rcx,4)
movq -0x8(%rsp), %rax
movl 0x16fc(%rax), %eax
movq -0x8(%rsp), %rcx
movl 0x16f8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
retq
nopw %cs:(%rax,%rax)