// File: tomfunkhouser[P]gaps/pkgs/png/deflate.c
// Function: deflate_rle
local block_state deflate_rle(s, flush)
    deflate_state *s;
    int flush;
{
    int bflush;             /* set if current block must be flushed */
    uInt prev;              /* byte at distance one to match */
    Bytef *scan, *strend;   /* scan goes up to strend for length of run */

    for (;;) {
        /* Make sure that we always have enough lookahead, except
         * at the end of the input file. We need MAX_MATCH bytes
         * for the longest encodable run.
         */
        if (s->lookahead < MAX_MATCH) {
            fill_window(s);
            if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
                return need_more;
            }
            if (s->lookahead == 0) break; /* flush the current block */
        }

        /* See how many times the previous byte repeats */
        s->match_length = 0;
        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
            scan = s->window + s->strstart - 1;
            prev = *scan;
            if (prev == *++scan && prev == *++scan && prev == *++scan) {
                strend = s->window + s->strstart + MAX_MATCH;
                do {
                } while (prev == *++scan && prev == *++scan &&
                         prev == *++scan && prev == *++scan &&
                         prev == *++scan && prev == *++scan &&
                         prev == *++scan && prev == *++scan &&
                         scan < strend);
                s->match_length = MAX_MATCH - (int)(strend - scan);
                if (s->match_length > s->lookahead)
                    s->match_length = s->lookahead;
            }
        }

        /* Emit match if have run of MIN_MATCH or longer, else emit literal */
        if (s->match_length >= MIN_MATCH) {
            check_match(s, s->strstart, s->strstart - 1, s->match_length);

            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);

            s->lookahead -= s->match_length;
            s->strstart += s->match_length;
            s->match_length = 0;
        } else {
            /* No match, output a literal byte */
            Tracevv((stderr,"%c", s->window[s->strstart]));
            _tr_tally_lit (s, s->window[s->strstart], bflush);
            s->lookahead--;
            s->strstart++;
        }
        if (bflush) FLUSH_BLOCK(s, 0);
    }
    FLUSH_BLOCK(s, flush == Z_FINISH);
    return flush == Z_FINISH ? finish_done : block_done;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movl %esi, 0x54(%rsp)
movq 0x58(%rsp), %rax
cmpl $0x102, 0xa4(%rax)      # imm = 0x102
jae 0x1175d2
movq 0x58(%rsp), %rdi
callq 0x1193a0
movq 0x58(%rsp), %rax
cmpl $0x102, 0xa4(%rax)      # imm = 0x102
jae 0x1175bd
cmpl $0x0, 0x54(%rsp)
jne 0x1175bd
movl $0x0, 0x64(%rsp)
jmp 0x117c51
movq 0x58(%rsp), %rax
cmpl $0x0, 0xa4(%rax)
jne 0x1175d0
jmp 0x117b79
jmp 0x1175d2
movq 0x58(%rsp), %rax
movl $0x0, 0x90(%rax)
movq 0x58(%rsp), %rax
cmpl $0x3, 0xa4(%rax)
jb 0x11784e
movq 0x58(%rsp), %rax
cmpl $0x0, 0x9c(%rax)
jbe 0x11784e
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x58(%rsp), %rcx
movl 0x9c(%rcx), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movzbl (%rax), %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rcx), %ecx
cmpl %ecx, %eax
jne 0x11784c
movl 0x4c(%rsp), %eax
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rcx), %ecx
cmpl %ecx, %eax
jne 0x11784c
movl 0x4c(%rsp), %eax
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rcx), %ecx
cmpl %ecx, %eax
jne 0x11784c
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x58(%rsp), %rcx
movl 0x9c(%rcx), %ecx
addq %rcx, %rax
addq $0x102, %rax            # imm = 0x102
movq %rax, 0x38(%rsp)
jmp 0x1176b8
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movzbl 0x1(%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x32(%rsp)
jne 0x1177f1
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
setb %al
movb %al, 0x32(%rsp)
movb 0x32(%rsp), %al
testb $0x1, %al
jne 0x1176b6
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rcx, %rax
movl $0x102, %ecx            # imm = 0x102
subl %eax, %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x90(%rax)
movq 0x58(%rsp), %rax
movl 0x90(%rax), %eax
movq 0x58(%rsp), %rcx
cmpl 0xa4(%rcx), %eax
jbe 0x11784a
movq 0x58(%rsp), %rax
movl 0xa4(%rax), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x90(%rax)
jmp 0x11784c
jmp 0x11784e
movq 0x58(%rsp), %rax
cmpl $0x3, 0x90(%rax)
jb 0x1179e9
movq 0x58(%rsp), %rax
movl 0x90(%rax), %eax
subl $0x3, %eax
movb %al, 0x37(%rsp)
movw $0x1, 0x34(%rsp)
movw 0x34(%rsp), %dx
movq 0x58(%rsp), %rax
movq 0x1700(%rax), %rax
movq 0x58(%rsp), %rcx
movl 0x16fc(%rcx), %ecx
movw %dx, (%rax,%rcx,2)
movb 0x37(%rsp), %dl
movq 0x58(%rsp), %rax
movq 0x16f0(%rax), %rax
movq 0x58(%rsp), %rsi
movl 0x16fc(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x16fc(%rsi)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movw 0x34(%rsp), %ax
addw $-0x1, %ax
movw %ax, 0x34(%rsp)
movq 0x58(%rsp), %rax
movzbl 0x37(%rsp), %ecx
movl %ecx, %edx
leaq 0x26e5b(%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 0x58(%rsp), %rax
addq $0x9b8, %rax            # imm = 0x9B8
movq %rax, 0x28(%rsp)
movzwl 0x34(%rsp), %eax
cmpl $0x100, %eax            # imm = 0x100
jge 0x11793d
movzwl 0x34(%rsp), %eax
movl %eax, %ecx
leaq 0x26c0d(%rip), %rax     # 0x13e540
movzbl (%rax,%rcx), %eax
movl %eax, 0x24(%rsp)
jmp 0x11795c
movzwl 0x34(%rsp), %eax
sarl $0x7, %eax
addl $0x100, %eax            # imm = 0x100
movslq %eax, %rcx
leaq 0x26bec(%rip), %rax     # 0x13e540
movzbl (%rax,%rcx), %eax
movl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
movslq %ecx, %rcx
movw (%rax,%rcx,4), %dx
addw $0x1, %dx
movw %dx, (%rax,%rcx,4)
movq 0x58(%rsp), %rax
movl 0x16fc(%rax), %eax
movq 0x58(%rsp), %rcx
movl 0x16f8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x50(%rsp)
movq 0x58(%rsp), %rax
movl 0x90(%rax), %edx
movq 0x58(%rsp), %rax
movl 0xa4(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0xa4(%rax)
movq 0x58(%rsp), %rax
movl 0x90(%rax), %ecx
movq 0x58(%rsp), %rax
addl 0x9c(%rax), %ecx
movl %ecx, 0x9c(%rax)
movq 0x58(%rsp), %rax
movl $0x0, 0x90(%rax)
jmp 0x117ab9
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x58(%rsp), %rcx
movl 0x9c(%rcx), %ecx
movb (%rax,%rcx), %al
movb %al, 0x33(%rsp)
movq 0x58(%rsp), %rax
movq 0x1700(%rax), %rax
movq 0x58(%rsp), %rcx
movl 0x16fc(%rcx), %ecx
movw $0x0, (%rax,%rcx,2)
movb 0x33(%rsp), %dl
movq 0x58(%rsp), %rax
movq 0x16f0(%rax), %rax
movq 0x58(%rsp), %rsi
movl 0x16fc(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x16fc(%rsi)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movq 0x58(%rsp), %rax
movzbl 0x33(%rsp), %ecx
movw 0xc4(%rax,%rcx,4), %dx
addw $0x1, %dx
movw %dx, 0xc4(%rax,%rcx,4)
movq 0x58(%rsp), %rax
movl 0x16fc(%rax), %eax
movq 0x58(%rsp), %rcx
movl 0x16f8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x50(%rsp)
movq 0x58(%rsp), %rax
movl 0xa4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xa4(%rax)
movq 0x58(%rsp), %rax
movl 0x9c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x9c(%rax)
cmpl $0x0, 0x50(%rsp)
je 0x117b74
movq 0x58(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
cmpq $0x0, 0x88(%rax)
jl 0x117afe
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x58(%rsp), %rcx
movq 0x88(%rcx), %rcx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
jmp 0x117b07
xorl %eax, %eax
movq %rax, 0x10(%rsp)
jmp 0x117b07
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x58(%rsp), %rax
movl 0x9c(%rax), %eax
movl %eax, %edx
movq 0x58(%rsp), %rax
subq 0x88(%rax), %rdx
xorl %ecx, %ecx
callq 0x11f710
movq 0x58(%rsp), %rax
movl 0x9c(%rax), %eax
movl %eax, %ecx
movq 0x58(%rsp), %rax
movq %rcx, 0x88(%rax)
movq 0x58(%rsp), %rax
movq (%rax), %rdi
callq 0x117190
movq 0x58(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, 0x20(%rax)
jne 0x117b72
movl $0x0, 0x64(%rsp)
jmp 0x117c51
jmp 0x117b74
jmp 0x11757d
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x58(%rsp), %rax
cmpq $0x0, 0x88(%rax)
jl 0x117bb2
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x58(%rsp), %rcx
movq 0x88(%rcx), %rcx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, (%rsp)
jmp 0x117bba
xorl %eax, %eax
movq %rax, (%rsp)
jmp 0x117bba
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq 0x58(%rsp), %rax
movl 0x9c(%rax), %eax
movl %eax, %edx
movq 0x58(%rsp), %rax
subq 0x88(%rax), %rdx
cmpl $0x4, 0x54(%rsp)
sete %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x11f710
movq 0x58(%rsp), %rax
movl 0x9c(%rax), %eax
movl %eax, %ecx
movq 0x58(%rsp), %rax
movq %rcx, 0x88(%rax)
movq 0x58(%rsp), %rax
movq (%rax), %rdi
callq 0x117190
movq 0x58(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, 0x20(%rax)
jne 0x117c39
movl 0x54(%rsp), %edx
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x4, %edx
cmovel %ecx, %eax
movl %eax, 0x64(%rsp)
jmp 0x117c51
movl 0x54(%rsp), %edx
movl $0x1, %eax
movl $0x3, %ecx
cmpl $0x4, %edx
cmovel %ecx, %eax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw (%rax,%rax)