// File: tomfunkhouser[P]gaps/pkgs/png/inflate.c
// Function: updatewindow
local int updatewindow(strm, out)
z_streamp strm;
unsigned out;
{
    struct inflate_state FAR *state;
    unsigned copy, dist;

    state = (struct inflate_state FAR *)strm->state;

    /* if it hasn't been done already, allocate space for the window */
    if (state->window == Z_NULL) {
        state->window = (unsigned char FAR *)
                        ZALLOC(strm, 1U << state->wbits,
                               sizeof(unsigned char));
        if (state->window == Z_NULL) return 1;
    }

    /* if window not in use yet, initialize */
    if (state->wsize == 0) {
        state->wsize = 1U << state->wbits;
        state->wnext = 0;
        state->whave = 0;
    }

    /* copy state->wsize or less output bytes into the circular window */
    copy = out - strm->avail_out;
    if (copy >= state->wsize) {
        zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
        state->wnext = 0;
        state->whave = state->wsize;
    }
    else {
        dist = state->wsize - state->wnext;
        if (dist > copy) dist = copy;
        zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
        copy -= dist;
        if (copy) {
            zmemcpy(state->window, strm->next_out - copy, copy);
            state->wnext = copy;
            state->whave = state->wsize;
        }
        else {
            state->wnext += dist;
            if (state->wnext == state->wsize) state->wnext = 0;
            if (state->whave < state->wsize) state->whave += dist;
        }
    }
    return 0;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movl %esi, 0x14(%rsp)
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x11d926
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x18(%rsp), %rcx
movq 0x50(%rcx), %rdi
movq 0x8(%rsp), %rcx
movl 0x30(%rcx), %ecx
movl $0x1, %esi
shll %cl, %esi
movl $0x1, %edx
callq *%rax
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x8(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x11d924
movl $0x1, 0x24(%rsp)
jmp 0x11dae1
jmp 0x11d926
movq 0x8(%rsp), %rax
cmpl $0x0, 0x34(%rax)
jne 0x11d962
movq 0x8(%rsp), %rax
movl 0x30(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x34(%rax)
movq 0x8(%rsp), %rax
movl $0x0, 0x3c(%rax)
movq 0x8(%rsp), %rax
movl $0x0, 0x38(%rax)
movl 0x14(%rsp), %eax
movq 0x18(%rsp), %rcx
subl 0x20(%rcx), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x8(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jb 0x11d9d4
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rsi
movq 0x8(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rsi
movq 0x8(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edx
callq 0xf3d0
movq 0x8(%rsp), %rax
movl $0x0, 0x3c(%rax)
movq 0x8(%rsp), %rax
movl 0x34(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x38(%rax)
jmp 0x11dad9
movq 0x8(%rsp), %rax
movl 0x34(%rax), %eax
movq 0x8(%rsp), %rcx
subl 0x3c(%rcx), %eax
movl %eax, (%rsp)
movl (%rsp), %eax
cmpl 0x4(%rsp), %eax
jbe 0x11d9f7
movl 0x4(%rsp), %eax
movl %eax, (%rsp)
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x8(%rsp), %rax
movl 0x3c(%rax), %eax
addq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x4(%rsp), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rsi
movl (%rsp), %eax
movl %eax, %edx
callq 0xf3d0
movl (%rsp), %ecx
movl 0x4(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
je 0x11da89
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x4(%rsp), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rsi
movl 0x4(%rsp), %eax
movl %eax, %edx
callq 0xf3d0
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x3c(%rax)
movq 0x8(%rsp), %rax
movl 0x34(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x38(%rax)
jmp 0x11dad7
movl (%rsp), %ecx
movq 0x8(%rsp), %rax
addl 0x3c(%rax), %ecx
movl %ecx, 0x3c(%rax)
movq 0x8(%rsp), %rax
movl 0x3c(%rax), %eax
movq 0x8(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jne 0x11dab5
movq 0x8(%rsp), %rax
movl $0x0, 0x3c(%rax)
movq 0x8(%rsp), %rax
movl 0x38(%rax), %eax
movq 0x8(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jae 0x11dad5
movl (%rsp), %ecx
movq 0x8(%rsp), %rax
addl 0x38(%rax), %ecx
movl %ecx, 0x38(%rax)
jmp 0x11dad7
jmp 0x11dad9
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw (%rax,%rax)