// File: /workspace/github_c_makefile_star2_O1/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 0x9bab5
movq %rbx, %rdi
callq 0x9b99a
cmpl $-0x1, %eax
je 0x9bc2f
cmpl $0x0, 0x40(%rbx)
je 0x9bb11
movl 0x88(%rbx), %edx
xorl %r14d, %r14d
testl %edx, %edx
je 0x9bc35
movl $0x40000000, %ebp       # imm = 0x40000000
cmpl %ebp, %edx
cmovael %ebp, %edx
movl 0x1c(%rbx), %edi
movq 0x80(%rbx), %rsi
callq 0xb200
testl %eax, %eax
js 0x9bbf2
movl 0x88(%rbx), %edx
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x80(%rbx)
subl %eax, %edx
movl %edx, 0x88(%rbx)
jne 0x9bad1
jmp 0x9bc35
leaq 0x80(%rbx), %r14
cmpl $0x0, 0x60(%rbx)
je 0x9bb3a
cmpl $0x0, 0x88(%rbx)
je 0x9bc16
movq %r14, %rdi
callq 0x9c20a
movl $0x0, 0x60(%rbx)
xorl %eax, %eax
movl $0x40000000, %r15d      # imm = 0x40000000
cmpl $0x0, 0xa0(%rbx)
je 0x9bb5f
cmpl $0x4, %ebp
setne %cl
cmpl $0x1, %eax
sete %al
testl %ebp, %ebp
je 0x9bbbe
orb %al, %cl
je 0x9bbbe
movq 0x8(%rbx), %rax
movq 0x98(%rbx), %rcx
cmpq %rax, %rcx
jbe 0x9bb9d
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 0x9bbf2
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq 0x8(%rbx), %rax
movq %rax, 0x8(%rbx)
jmp 0x9bb63
cmpl $0x0, 0xa0(%rbx)
jne 0x9bbbe
movl 0x28(%rbx), %eax
movl %eax, 0xa0(%rbx)
movq 0x38(%rbx), %rax
movq %rax, 0x98(%rbx)
movq %rax, 0x8(%rbx)
movl 0xa0(%rbx), %r12d
movq %r14, %rdi
movl %ebp, %esi
callq 0x9caf6
cmpl $-0x2, %eax
je 0x9bc1b
cmpl 0xa0(%rbx), %r12d
jne 0x9bb42
xorl %r14d, %r14d
cmpl $0x4, %ebp
jne 0x9bc35
movl $0x1, 0x60(%rbx)
jmp 0x9bc35
callq 0xb060
movl (%rax), %edi
callq 0xb1d0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, %rdx
callq 0x9a863
jmp 0x9bc35
xorl %r14d, %r14d
jmp 0x9bc35
leaq 0x5c317(%rip), %rdx     # 0xf7f39
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x9a863
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq