// File: tomfunkhouser[P]gaps/pkgs/png/deflate.c
// Function: deflateCopy
int ZEXPORT deflateCopy (dest, source)
    z_streamp dest;
    z_streamp source;
{
#ifdef MAXSEG_64K
    return Z_STREAM_ERROR;
#else
    deflate_state *ds;
    deflate_state *ss;
    ushf *overlay;


    if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
        return Z_STREAM_ERROR;
    }

    ss = source->state;

    zmemcpy(dest, source, sizeof(z_stream));

    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
    if (ds == Z_NULL) return Z_MEM_ERROR;
    dest->state = (struct internal_state FAR *) ds;
    zmemcpy(ds, ss, sizeof(deflate_state));
    ds->strm = dest;

    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
    ds->pending_buf = (uchf *) overlay;

    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
        ds->pending_buf == Z_NULL) {
        deflateEnd (dest);
        return Z_MEM_ERROR;
    }
    /* following zmemcpy do not work for 16-bit MSDOS */
    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);

    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;

    ds->l_desc.dyn_tree = ds->dyn_ltree;
    ds->d_desc.dyn_tree = ds->dyn_dtree;
    ds->bl_desc.dyn_tree = ds->bl_tree;

    return Z_OK;
#endif /* MAXSEG_64K */
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x117c8a
cmpq $0x0, 0x28(%rsp)
je 0x117c8a
movq 0x20(%rsp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x117c97
movl $0xfffffffe, 0x34(%rsp) # imm = 0xFFFFFFFE
jmp 0x117f80
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x70, %edx
callq 0xf3d0
movq 0x28(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
movq 0x50(%rcx), %rdi
movl $0x1, %esi
movl $0x1730, %edx           # imm = 0x1730
callq *%rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x117cf1
movl $0xfffffffc, 0x34(%rsp) # imm = 0xFFFFFFFC
jmp 0x117f80
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x1730, %edx           # imm = 0x1730
callq 0xf3d0
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x28(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
movq 0x50(%rcx), %rdi
movq 0x18(%rsp), %rcx
movl 0x44(%rcx), %esi
movl $0x2, %edx
callq *%rax
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x28(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
movq 0x50(%rcx), %rdi
movq 0x18(%rsp), %rcx
movl 0x44(%rcx), %esi
movl $0x2, %edx
callq *%rax
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x60(%rax)
movq 0x28(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
movq 0x50(%rcx), %rdi
movq 0x18(%rsp), %rcx
movl 0x74(%rcx), %esi
movl $0x2, %edx
callq *%rax
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x68(%rax)
movq 0x28(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x28(%rsp), %rcx
movq 0x50(%rcx), %rdi
movq 0x18(%rsp), %rcx
movl 0x16f8(%rcx), %esi
movl $0x4, %edx
callq *%rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x50(%rax)
je 0x117e0e
movq 0x18(%rsp), %rax
cmpq $0x0, 0x60(%rax)
je 0x117e0e
movq 0x18(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x117e0e
movq 0x18(%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x117e25
movq 0x28(%rsp), %rdi
callq 0x115350
movl $0xfffffffc, 0x34(%rsp) # imm = 0xFFFFFFFC
jmp 0x117f80
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x18(%rsp), %rax
movl 0x44(%rax), %eax
shll %eax
movl %eax, %eax
movl %eax, %edx
shlq $0x0, %rdx
callq 0xf3d0
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x60(%rax), %rsi
movq 0x18(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, %edx
shlq %rdx
callq 0xf3d0
movq 0x18(%rsp), %rax
movq 0x68(%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x68(%rax), %rsi
movq 0x18(%rsp), %rax
movl 0x74(%rax), %eax
movl %eax, %edx
shlq %rdx
callq 0xf3d0
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movl %eax, %edx
callq 0xf3d0
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rsp), %rdx
movq 0x10(%rdx), %rdx
subq %rdx, %rax
addq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movl 0x16f8(%rax), %eax
shrq %rax
shlq %rax
addq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x1700(%rax)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rsp), %rax
movl 0x16f8(%rax), %eax
imulq $0x3, %rax, %rax
addq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x16f0(%rax)
movq 0x18(%rsp), %rcx
addq $0xc4, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0xb48(%rax)
movq 0x18(%rsp), %rcx
addq $0x9b8, %rcx            # imm = 0x9B8
movq 0x18(%rsp), %rax
movq %rcx, 0xb60(%rax)
movq 0x18(%rsp), %rcx
addq $0xaac, %rcx            # imm = 0xAAC
movq 0x18(%rsp), %rax
movq %rcx, 0xb78(%rax)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax)