// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/zlib-1.3.0.1/gzwrite.c
// Function: gz_write
local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
    z_size_t put = len;

    /* if len is zero, avoid unnecessary operations */
    if (len == 0)
        return 0;

    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
        return 0;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
            return 0;
    }

    /* for small len, copy to input buffer, otherwise compress directly */
    if (len < state->size) {
        /* copy to input buffer, compress when full */
        do {
            unsigned have, copy;

            if (state->strm.avail_in == 0)
                state->strm.next_in = state->in;
            have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
                              state->in);
            copy = state->size - have;
            if (copy > len)
                copy = (unsigned)len;
            memcpy(state->in + have, buf, copy);
            state->strm.avail_in += copy;
            state->x.pos += copy;
            buf = (const char *)buf + copy;
            len -= copy;
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
                return 0;
        } while (len);
    }
    else {
        /* consume whatever's left in the input buffer */
        if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
            return 0;

        /* directly compress user buffer to file */
        state->strm.next_in = (z_const Bytef *)buf;
        do {
            unsigned n = (unsigned)-1;
            if (n > len)
                n = (unsigned)len;
            state->strm.avail_in = n;
            state->x.pos += n;
            if (gz_comp(state, Z_NO_FLUSH) == -1)
                return 0;
            len -= n;
        } while (len);
    }

    /* input was all buffered or compressed */
    return put;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdx, %rdx
je 0x9ac12
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
cmpl $0x0, 0x28(%rdi)
jne 0x9ab1a
movq %r14, %rdi
callq 0x9af8a
cmpl $-0x1, %eax
je 0x9ac12
cmpl $0x0, 0x70(%r14)
je 0x9ab3e
movl $0x0, 0x70(%r14)
movq 0x68(%r14), %rsi
movq %r14, %rdi
callq 0x9ad51
cmpl $-0x1, %eax
je 0x9ac12
movl 0x28(%r14), %eax
cmpq %rbx, %rax
jbe 0x9abbb
movq %rbx, %r13
movl 0x88(%r14), %edx
testq %rdx, %rdx
je 0x9ab63
movq 0x30(%r14), %rax
movq 0x80(%r14), %rcx
jmp 0x9ab71
movq 0x30(%r14), %rax
movq %rax, 0x80(%r14)
movq %rax, %rcx
addq %rdx, %rcx
subq %rax, %rcx
movl 0x28(%r14), %edx
subl %ecx, %edx
cmpq %rdx, %r13
movl %r13d, %r12d
cmovael %edx, %r12d
movl %ecx, %edi
addq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xb520
addl %r12d, 0x88(%r14)
addq %r12, 0x10(%r14)
subq %r12, %r13
je 0x9ac14
addq %r12, %r15
movq %r14, %rdi
xorl %esi, %esi
callq 0x9b081
cmpl $-0x1, %eax
jne 0x9ab4a
jmp 0x9ac12
cmpl $0x0, 0x88(%r14)
je 0x9abd4
movq %r14, %rdi
xorl %esi, %esi
callq 0x9b081
cmpl $-0x1, %eax
je 0x9ac12
movq %r15, 0x80(%r14)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %rbx, %r12
cmpq %r15, %r12
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmovbq %r12, %r13
movl %r13d, 0x88(%r14)
addq %r13, 0x10(%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x9b081
cmpl $-0x1, %eax
je 0x9ac12
subq %r13, %r12
jne 0x9abe4
jmp 0x9ac14
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq