// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r8
movl 0x44(%rdi), %r14d
movl 0xa4(%rdi), %r12d
leal -0x106(%r14), %eax
movl %eax, 0x14(%rsp)
movd %r14d, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movdqa %xmm0, 0x20(%rsp)
leaq -0x2(,%r14,2), %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq %rdi, 0x8(%rsp)
movl 0x58(%r8), %r15d
movl 0x9c(%r8), %eax
addl %eax, %r12d
subl %r12d, %r15d
movl 0x44(%r8), %ecx
addl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jb 0x94ba5
movq 0x50(%r8), %rdi
leaq (%rdi,%r14), %rsi
movq %r14, %rdx
callq 0xe370
movq 0x8(%rsp), %r8
movq 0x9c(%r8), %xmm0
psubd 0x20(%rsp), %xmm0
movq %xmm0, 0x9c(%r8)
subq %r14, 0x88(%r8)
movl 0x74(%r8), %eax
movq 0x68(%r8), %rcx
leaq (%rcx,%rax,2), %rcx
addq $-0x2, %rcx
movzwl (%rcx), %edx
subl %r14d, %edx
cmovbl %r13d, %edx
movw %dx, (%rcx)
addq $-0x2, %rcx
decl %eax
jne 0x94b68
movq 0x60(%r8), %rax
addq 0x18(%rsp), %rax
movl %r14d, %ecx
movzwl (%rax), %edx
subl %r14d, %edx
cmovbl %r13d, %edx
movw %dx, (%rax)
addq $-0x2, %rax
decl %ecx
jne 0x94b89
addl %r14d, %r15d
movd %xmm0, %eax
movq (%r8), %rbx
movl 0x8(%rbx), %ecx
testl %ecx, %ecx
je 0x94d06
movl 0xa4(%r8), %r12d
cmpl %r15d, %ecx
movl %r15d, %ebp
cmovbl %ecx, %ebp
testl %r15d, %r15d
je 0x94c2e
movl %eax, %r15d
addq 0x50(%r8), %r15
subl %ebp, %ecx
movl %ecx, 0x8(%rbx)
movq 0x38(%rbx), %rax
movl 0x2c(%rax), %eax
cmpl $0x2, %eax
je 0x94bf5
cmpl $0x1, %eax
jne 0x94c07
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
movl %ebp, %edx
callq 0xa0be4
jmp 0x94c03
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
movl %ebp, %edx
callq 0x92310
movq %rax, 0x60(%rbx)
addq %r12, %r15
movq (%rbx), %rsi
movl %ebp, %r12d
movq %r15, %rdi
movq %r12, %rdx
callq 0xe370
addq %r12, (%rbx)
addq %r12, 0x10(%rbx)
movq 0x8(%rsp), %r8
movl 0xa4(%r8), %r12d
addl %ebp, %r12d
movl %r12d, 0xa4(%r8)
cmpl $0x3, %r12d
jb 0x94c74
movq 0x50(%r8), %rax
movl 0x9c(%r8), %edx
movzbl (%rax,%rdx), %esi
movl %esi, 0x70(%r8)
movb 0x80(%r8), %cl
shll %cl, %esi
leal 0x1(%rdx), %ecx
movzbl (%rax,%rcx), %eax
xorl %esi, %eax
andl 0x7c(%r8), %eax
movl %eax, 0x70(%r8)
cmpl $0x105, %r12d           # imm = 0x105
ja 0x94c81
movq (%r8), %rax
cmpl $0x0, 0x8(%rax)
jne 0x94b03
movq 0x1728(%r8), %rdi
movq 0x58(%r8), %r14
movq %r14, %rbx
subq %rdi, %rbx
jbe 0x94d06
movl 0x9c(%r8), %eax
movl %r12d, %r15d
addq %rax, %r15
cmpq %r15, %rdi
jae 0x94cd0
subq %r15, %r14
movl $0x102, %eax            # imm = 0x102
cmpq %rax, %r14
cmovaeq %rax, %r14
movq 0x50(%r8), %rdi
addq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0xe260
movq 0x8(%rsp), %rax
addq %r15, %r14
jmp 0x94cff
addq $0x102, %r15            # imm = 0x102
subq %rdi, %r15
jbe 0x94d06
cmpq %rbx, %r15
cmovbq %r15, %rbx
addq 0x50(%r8), %rdi
movl %ebx, %edx
xorl %esi, %esi
callq 0xe260
movq 0x8(%rsp), %rax
addq 0x1728(%rax), %rbx
movq %rbx, %r14
movq %r14, 0x1728(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq