// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/zlib-1.3.0.1/gzwrite.c
// Function: gz_comp
local int gz_comp(gz_statep state, int flush) {
    int ret, writ;
    unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
    z_streamp strm = &(state->strm);

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

    /* write directly if requested */
    if (state->direct) {
        while (strm->avail_in) {
            put = strm->avail_in > max ? max : strm->avail_in;
            writ = write(state->fd, strm->next_in, put);
            if (writ < 0) {
                gz_error(state, Z_ERRNO, zstrerror());
                return -1;
            }
            strm->avail_in -= (unsigned)writ;
            strm->next_in += writ;
        }
        return 0;
    }

    /* check for a pending reset */
    if (state->reset) {
        /* don't start a new gzip member unless there is data to write */
        if (strm->avail_in == 0)
            return 0;
        deflateReset(strm);
        state->reset = 0;
    }

    /* run deflate() on provided input until it produces no more output */
    ret = Z_OK;
    do {
        /* write out current buffer contents if full, or if flushing, but if
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
            while (strm->next_out > state->x.next) {
                put = strm->next_out - state->x.next > (int)max ? max :
                      (unsigned)(strm->next_out - state->x.next);
                writ = write(state->fd, state->x.next, put);
                if (writ < 0) {
                    gz_error(state, Z_ERRNO, zstrerror());
                    return -1;
                }
                state->x.next += writ;
            }
            if (strm->avail_out == 0) {
                strm->avail_out = state->size;
                strm->next_out = state->out;
                state->x.next = state->out;
            }
        }

        /* compress */
        have = strm->avail_out;
        ret = deflate(strm, flush);
        if (ret == Z_STREAM_ERROR) {
            gz_error(state, Z_STREAM_ERROR,
                      "internal error: deflate stream corrupt");
            return -1;
        }
        have -= strm->avail_out;
    } while (have);

    /* if that completed a deflate stream, allow another to start */
    if (flush == Z_FINISH)
        state->reset = 1;

    /* all done, no errors */
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x0, 0x28(%rdi)
jne 0x9b0a5
movq %rbx, %rdi
callq 0x9af8a
cmpl $-0x1, %eax
je 0x9b236
cmpl $0x0, 0x40(%rbx)
je 0x9b108
movl 0x88(%rbx), %edx
xorl %r14d, %r14d
testl %edx, %edx
je 0x9b23c
movq 0x80(%rbx), %rsi
movl $0x40000000, %ebp       # imm = 0x40000000
cmpl %ebp, %edx
cmovael %ebp, %edx
movl 0x1c(%rbx), %edi
callq 0xb200
testl %eax, %eax
js 0x9b1f9
movl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq 0x80(%rbx), %rsi
movl 0x88(%rbx), %edx
subl %eax, %edx
movl %edx, 0x88(%rbx)
movq %rsi, 0x80(%rbx)
jne 0x9b0c8
jmp 0x9b23c
leaq 0x80(%rbx), %r14
cmpl $0x0, 0x60(%rbx)
je 0x9b131
cmpl $0x0, 0x88(%rbx)
je 0x9b21d
movq %r14, %rdi
callq 0x9b81f
movl $0x0, 0x60(%rbx)
movl 0xa0(%rbx), %r12d
xorl %eax, %eax
movl $0x40000000, %r15d      # imm = 0x40000000
testl %r12d, %r12d
je 0x9b159
cmpl $0x4, %ebp
setne %cl
cmpl $0x1, %eax
sete %al
testl %ebp, %ebp
je 0x9b1c7
orb %al, %cl
je 0x9b1c7
movq 0x8(%rbx), %rax
movq 0x98(%rbx), %rcx
cmpq %rax, %rcx
jbe 0x9b1a8
subq %rax, %rcx
cmpq $0x40000001, %rcx       # imm = 0x40000001
movl %ecx, %edx
cmovgeq %r15, %rdx
movl 0x1c(%rbx), %edi
movq %rax, %rsi
callq 0xb200
testl %eax, %eax
js 0x9b1f9
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq 0x8(%rbx), %rax
movq %rax, 0x8(%rbx)
movq 0x98(%rbx), %rcx
cmpq %rax, %rcx
ja 0x9b169
movl 0xa0(%rbx), %r12d
testl %r12d, %r12d
jne 0x9b1c7
movl 0x28(%rbx), %r12d
movl %r12d, 0xa0(%rbx)
movq 0x38(%rbx), %rax
movq %rax, 0x98(%rbx)
movq %rax, 0x8(%rbx)
movq %r14, %rdi
movl %ebp, %esi
callq 0x9c117
cmpl $-0x2, %eax
je 0x9b222
movl 0xa0(%rbx), %ecx
cmpl %ecx, %r12d
movl %ecx, %r12d
jne 0x9b140
xorl %r14d, %r14d
cmpl $0x4, %ebp
jne 0x9b23c
movl $0x1, 0x60(%rbx)
jmp 0x9b23c
callq 0xb060
movl (%rax), %edi
callq 0xb1d0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, %rdx
callq 0x99e17
jmp 0x9b23c
xorl %r14d, %r14d
jmp 0x9b23c
leaq 0x5cc78(%rip), %rdx     # 0xf7ea1
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x99e17
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq