// File: tomfunkhouser[P]gaps/pkgs/png/deflate.c
// Function: fill_window
local void fill_window(s)
    deflate_state *s;
{
    register unsigned n, m;
    register Posf *p;
    unsigned more;    /* Amount of free space at the end of the window. */
    uInt wsize = s->w_size;

    do {
        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);

        /* Deal with !@#$% 64K limit: */
        if (sizeof(int) <= 2) {
            if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
                more = wsize;

            } else if (more == (unsigned)(-1)) {
                /* Very unlikely, but possible on 16 bit machine if
                 * strstart == 0 && lookahead == 1 (input done a byte at time)
                 */
                more--;
            }
        }

        /* If the window is almost full and there is insufficient lookahead,
         * move the upper half to the lower one to make room in the upper half.
         */
        if (s->strstart >= wsize+MAX_DIST(s)) {

            zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
            s->match_start -= wsize;
            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
            s->block_start -= (long) wsize;

            /* Slide the hash table (could be avoided with 32 bit values
               at the expense of memory usage). We slide even when level == 0
               to keep the hash table consistent if we switch back to level > 0
               later. (Using level 0 permanently is not an optimal usage of
               zlib, so we don't care about this pathological case.)
             */
            n = s->hash_size;
            p = &s->head[n];
            do {
                m = *--p;
                *p = (Pos)(m >= wsize ? m-wsize : NIL);
            } while (--n);

            n = wsize;
#ifndef FASTEST
            p = &s->prev[n];
            do {
                m = *--p;
                *p = (Pos)(m >= wsize ? m-wsize : NIL);
                /* If n is not on any hash chain, prev[n] is garbage but
                 * its value will never be used.
                 */
            } while (--n);
#endif
            more += wsize;
        }
        if (s->strm->avail_in == 0) return;

        /* If there was no sliding:
         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
         *    more == window_size - lookahead - strstart
         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
         * => more >= window_size - 2*WSIZE + 2
         * In the BIG_MEM or MMAP case (not yet supported),
         *   window_size == input_size + MIN_LOOKAHEAD  &&
         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
         * Otherwise, window_size == 2*WSIZE so more >= 2.
         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
         */
        Assert(more >= 2, "more < 2");

        n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
        s->lookahead += n;

        /* Initialize the hash value now that we have some input: */
        if (s->lookahead >= MIN_MATCH) {
            s->ins_h = s->window[s->strstart];
            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#if MIN_MATCH != 3
            Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
        }
        /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
         * but this is not important since only literal bytes will be emitted.
         */

    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);

    /* If the WIN_INIT bytes after the end of the current data have never been
     * written, then zero those bytes in order to avoid memory check reports of
     * the use of uninitialized (or uninitialised as Julian writes) bytes by
     * the longest match routines.  Update the high water mark for the next
     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
     */
    if (s->high_water < s->window_size) {
        ulg curr = s->strstart + (ulg)(s->lookahead);
        ulg init;

        if (s->high_water < curr) {
            /* Previous high water mark below current data -- zero WIN_INIT
             * bytes or up to end of window, whichever is less.
             */
            init = s->window_size - curr;
            if (init > WIN_INIT)
                init = WIN_INIT;
            zmemzero(s->window + curr, (unsigned)init);
            s->high_water = curr + init;
        }
        else if (s->high_water < (ulg)curr + WIN_INIT) {
            /* High water mark at or above current data, but below current data
             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
             * to end of window, whichever is less.
             */
            init = (ulg)curr + WIN_INIT - s->high_water;
            if (init > s->window_size - s->high_water)
                init = s->window_size - s->high_water;
            zmemzero(s->window + s->high_water, (unsigned)init);
            s->high_water += init;
        }
    }
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0xa4(%rcx), %ecx
subq %rcx, %rax
movq 0x40(%rsp), %rcx
movl 0x9c(%rcx), %ecx
subq %rcx, %rax
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rax
movl 0x9c(%rax), %eax
movl 0x28(%rsp), %ecx
movq 0x40(%rsp), %rdx
movl 0x44(%rdx), %edx
subl $0x106, %edx            # imm = 0x106
addl %edx, %ecx
cmpl %ecx, %eax
jb 0x119573
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rsi
movl 0x28(%rsp), %eax
addq %rax, %rsi
movl 0x28(%rsp), %eax
movl %eax, %edx
callq 0xf3d0
movl 0x28(%rsp), %edx
movq 0x40(%rsp), %rax
movl 0xa0(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0xa0(%rax)
movl 0x28(%rsp), %edx
movq 0x40(%rsp), %rax
movl 0x9c(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x9c(%rax)
movl 0x28(%rsp), %eax
movl %eax, %edx
movq 0x40(%rsp), %rax
movq 0x88(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x88(%rax)
movq 0x40(%rsp), %rax
movl 0x74(%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movq 0x68(%rax), %rax
movl 0x3c(%rsp), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, %rcx
addq $-0x2, %rcx
movq %rcx, 0x30(%rsp)
movzwl -0x2(%rax), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x28(%rsp), %eax
jb 0x1194c8
movl 0x38(%rsp), %eax
subl 0x28(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x1194d0
xorl %eax, %eax
movl %eax, 0x14(%rsp)
jmp 0x1194d0
movl 0x14(%rsp), %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, (%rax)
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
cmpl $0x0, %eax
jne 0x119497
movl 0x28(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movq 0x60(%rax), %rax
movl 0x3c(%rsp), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, %rcx
addq $-0x2, %rcx
movq %rcx, 0x30(%rsp)
movzwl -0x2(%rax), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x28(%rsp), %eax
jb 0x119540
movl 0x38(%rsp), %eax
subl 0x28(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0x119548
xorl %eax, %eax
movl %eax, 0x10(%rsp)
jmp 0x119548
movl 0x10(%rsp), %eax
movw %ax, %cx
movq 0x30(%rsp), %rax
movw %cx, (%rax)
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
cmpl $0x0, %eax
jne 0x11950f
movl 0x28(%rsp), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, 0x8(%rax)
jne 0x119586
jmp 0x1197e4
movq 0x40(%rsp), %rax
movq (%rax), %rdi
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x40(%rsp), %rax
movl 0x9c(%rax), %eax
addq %rax, %rsi
movq 0x40(%rsp), %rax
movl 0xa4(%rax), %eax
addq %rax, %rsi
movl 0x2c(%rsp), %edx
callq 0x1197f0
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %ecx
movq 0x40(%rsp), %rax
addl 0xa4(%rax), %ecx
movl %ecx, 0xa4(%rax)
movq 0x40(%rsp), %rax
cmpl $0x3, 0xa4(%rax)
jb 0x119649
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x9c(%rcx), %ecx
movzbl (%rax,%rcx), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x70(%rax)
movq 0x40(%rsp), %rax
movl 0x70(%rax), %eax
movq 0x40(%rsp), %rcx
movl 0x80(%rcx), %ecx
shll %cl, %eax
movl %eax, %ecx
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x40(%rsp), %rdx
movl 0x9c(%rdx), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rax,%rdx), %eax
xorl %eax, %ecx
movq 0x40(%rsp), %rax
andl 0x7c(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x70(%rax)
jmp 0x11964b
movq 0x40(%rsp), %rcx
xorl %eax, %eax
cmpl $0x106, 0xa4(%rcx)      # imm = 0x106
movb %al, 0xf(%rsp)
jae 0x119675
movq 0x40(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, 0x8(%rax)
setne %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x1193b5
movq 0x40(%rsp), %rax
movq 0x1728(%rax), %rax
movq 0x40(%rsp), %rcx
cmpq 0x58(%rcx), %rax
jae 0x1197e4
movq 0x40(%rsp), %rax
movl 0x9c(%rax), %eax
movq 0x40(%rsp), %rcx
movl 0xa4(%rcx), %ecx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rax
movq 0x1728(%rax), %rax
cmpq 0x20(%rsp), %rax
jae 0x11972d
movq 0x40(%rsp), %rax
movq 0x58(%rax), %rax
subq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x102, 0x18(%rsp)      # imm = 0x102
jbe 0x1196f4
movq $0x102, 0x18(%rsp)      # imm = 0x102
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rdi
addq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rax
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0xf280
movq 0x20(%rsp), %rcx
addq 0x18(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x1728(%rax)
jmp 0x1197e2
movq 0x40(%rsp), %rax
movq 0x1728(%rax), %rax
movq 0x20(%rsp), %rcx
addq $0x102, %rcx            # imm = 0x102
cmpq %rcx, %rax
jae 0x1197e0
movq 0x20(%rsp), %rax
addq $0x102, %rax            # imm = 0x102
movq 0x40(%rsp), %rcx
subq 0x1728(%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rcx
movq 0x58(%rcx), %rcx
movq 0x40(%rsp), %rdx
subq 0x1728(%rdx), %rcx
cmpq %rcx, %rax
jbe 0x1197a3
movq 0x40(%rsp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rsp), %rcx
subq 0x1728(%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x40(%rsp), %rax
addq 0x1728(%rax), %rdi
movq 0x18(%rsp), %rax
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0xf280
movq 0x18(%rsp), %rcx
movq 0x40(%rsp), %rax
addq 0x1728(%rax), %rcx
movq %rcx, 0x1728(%rax)
jmp 0x1197e2
jmp 0x1197e4
addq $0x48, %rsp
retq
nopl (%rax)