// File: /workspace/github_c_makefile_star2_O1/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 0x9b62e
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
cmpl $0x0, 0x28(%rdi)
jne 0x9b53e
movq %r14, %rdi
callq 0x9b99a
cmpl $-0x1, %eax
je 0x9b62e
cmpl $0x0, 0x70(%r14)
je 0x9b562
movl $0x0, 0x70(%r14)
movq 0x68(%r14), %rsi
movq %r14, %rdi
callq 0x9b765
cmpl $-0x1, %eax
je 0x9b62e
movl 0x28(%r14), %eax
cmpq %rbx, %rax
jbe 0x9b5d7
movq %rbx, %r13
movl 0x88(%r14), %eax
testq %rax, %rax
jne 0x9b585
movq 0x30(%r14), %rcx
movq %rcx, 0x80(%r14)
addq 0x80(%r14), %rax
movq 0x30(%r14), %rcx
subq %rcx, %rax
movl 0x28(%r14), %edx
subl %eax, %edx
cmpq %rdx, %r13
movl %r13d, %r12d
cmovael %edx, %r12d
movl %eax, %edi
addq %rcx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xb520
addl %r12d, 0x88(%r14)
addq %r12, 0x10(%r14)
subq %r12, %r13
je 0x9b630
addq %r12, %r15
movq %r14, %rdi
xorl %esi, %esi
callq 0x9ba91
cmpl $-0x1, %eax
jne 0x9b56e
jmp 0x9b62e
cmpl $0x0, 0x88(%r14)
je 0x9b5f0
movq %r14, %rdi
xorl %esi, %esi
callq 0x9ba91
cmpl $-0x1, %eax
je 0x9b62e
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 0x9ba91
cmpl $-0x1, %eax
je 0x9b62e
subq %r13, %r12
jne 0x9b600
jmp 0x9b630
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq