// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/bgzf.c
// Function: inflate_gzip_block
static int inflate_gzip_block(BGZF *fp)
{
    // we will set this to true when we detect EOF, so we don't bang against the EOF more than once per call
    int input_eof = 0;

    // write to the part of the output buffer after block_offset
    fp->gz_stream->next_out = (Bytef*)fp->uncompressed_block + fp->block_offset;
    fp->gz_stream->avail_out = BGZF_MAX_BLOCK_SIZE - fp->block_offset;

    while ( fp->gz_stream->avail_out != 0 ) {
        // until we fill the output buffer (or hit EOF)

        if ( !input_eof && fp->gz_stream->avail_in == 0 ) {
            // we are out of input data in the buffer. Get more.
            fp->gz_stream->next_in = fp->compressed_block;
            int ret = hread(fp->fp, fp->compressed_block, BGZF_BLOCK_SIZE);
            if ( ret < 0 ) {
                // hread had an error. Pass it on.
                return ret;
            }
            fp->gz_stream->avail_in = ret;
            if ( fp->gz_stream->avail_in < BGZF_BLOCK_SIZE ) {
                // we have reached EOF but the decompressor hasn't necessarily
                input_eof = 1;
            }
        }

        fp->gz_stream->msg = NULL;
        // decompress as much data as we can
        int ret = inflate(fp->gz_stream, Z_SYNC_FLUSH);

        if ( (ret < 0 && ret != Z_BUF_ERROR) || ret == Z_NEED_DICT ) {
            // an error occurred, other than running out of space
            hts_log_error("Inflate operation failed: %s", bgzf_zerr(ret, ret == Z_DATA_ERROR ? fp->gz_stream : NULL));
            fp->errcode |= BGZF_ERR_ZLIB;
            return -1;
        } else if ( ret == Z_STREAM_END ) {
            // we finished a GZIP member

            // scratch for peeking to see if the file is over
            char c;
            if (fp->gz_stream->avail_in > 0 || hpeek(fp->fp, &c, 1) == 1) {
                // there is more data; try and read another GZIP member in the remaining data
                int reset_ret = inflateReset(fp->gz_stream);
                if (reset_ret != Z_OK) {
                    hts_log_error("Call to inflateReset failed: %s", bgzf_zerr(reset_ret, NULL));
                    fp->errcode |= BGZF_ERR_ZLIB;
                    return -1;
                }
            } else {
                // we consumed all the input data and hit Z_STREAM_END
                // so stop looping, even if we never fill the output buffer
                break;
            }
        } else if ( ret == Z_BUF_ERROR && input_eof && fp->gz_stream->avail_out > 0 ) {
            // the gzip file has ended prematurely
            hts_log_error("Gzip file truncated");
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
    }

    // when we get here, the buffer is full or there is an EOF after a complete gzip member
    return BGZF_MAX_BLOCK_SIZE - fp->gz_stream->avail_out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movslq 0x10(%rdi), %rax
movl $0x10000, %ecx          # imm = 0x10000
subl %eax, %ecx
addq 0x28(%rdi), %rax
movq 0x60(%rdi), %rdx
movq %rax, 0x18(%rdx)
movl %ecx, 0x20(%rdx)
leaq 0x8c918(%rip), %r14     # 0x9820c
leaq 0x8c924(%rip), %r13     # 0x9821f
xorl %r15d, %r15d
movq 0x60(%rbx), %rax
cmpl $0x0, 0x20(%rax)
je 0xbadf
testl %r15d, %r15d
je 0xb91c
movl $0x1, %r15d
jmp 0xb9c7
xorl %r15d, %r15d
cmpl $0x0, 0x8(%rax)
jne 0xb9c7
movl %ebp, 0x4(%rsp)
movq %r14, %rbp
movq 0x30(%rbx), %r13
movq %r13, (%rax)
movq 0x40(%rbx), %r15
movq 0x8(%r15), %rsi
movq 0x10(%r15), %r14
subq %rsi, %r14
movl $0xff00, %eax           # imm = 0xFF00
cmpq %rax, %r14
movl $0xff00, %r12d          # imm = 0xFF00
cmovbq %r14, %r12
movq %r13, %rdi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%r15)
cmpq $0xfeff, %r14           # imm = 0xFEFF
ja 0xb98d
testb $0x2, 0x30(%r15)
je 0xb98d
movl $0xff00, %edx           # imm = 0xFF00
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0xf2cd
movq %rax, %r12
movq %rbp, %r14
leaq 0x8c888(%rip), %r13     # 0x9821f
testl %r12d, %r12d
movl 0x4(%rsp), %ebp
js 0xb9b8
movq 0x60(%rbx), %rax
movl %r12d, 0x8(%rax)
xorl %r15d, %r15d
cmpl $0xff00, %r12d          # imm = 0xFF00
setb %r15b
jmp 0xb9be
xorl %r15d, %r15d
movl %r12d, %ebp
testl %r12d, %r12d
js 0xbaeb
movq 0x60(%rbx), %rdi
movq $0x0, 0x30(%rdi)
movl $0x2, %esi
callq 0x4380
testl %eax, %eax
sets %cl
cmpl $-0x5, %eax
setne %dl
cmpl $0x2, %eax
je 0xba54
andb %dl, %cl
jne 0xba54
cmpl $0x1, %eax
jne 0xba96
movq 0x60(%rbx), %rax
cmpl $0x0, 0x8(%rax)
jne 0xba23
movq 0x40(%rbx), %rdi
movl $0x1, %edx
leaq 0x3(%rsp), %rsi
callq 0xf24d
movl $0x3, %r12d
cmpq $0x1, %rax
jne 0xba8b
movq 0x60(%rbx), %rdi
callq 0x46e0
testl %eax, %eax
je 0xbad4
movl %eax, %edi
xorl %esi, %esi
callq 0xadd0
movl $0x1, %r12d
movl $0x1, %edi
movq %r14, %rsi
leaq 0x8c7ea(%rip), %rdx     # 0x9823c
jmp 0xba79
cmpl $-0x3, %eax
jne 0xba5f
movq 0x60(%rbx), %rsi
jmp 0xba61
xorl %esi, %esi
movl %eax, %edi
callq 0xadd0
movl $0x1, %r12d
movl $0x1, %edi
movq %r14, %rsi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
orb $0x1, (%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %r12d, %r12d
je 0xb8fe
jmp 0xbad9
cmpl $-0x5, %eax
setne %al
testl %r15d, %r15d
sete %cl
xorl %r12d, %r12d
orb %al, %cl
jne 0xba8b
movq 0x60(%rbx), %rax
cmpl $0x0, 0x20(%rax)
je 0xba8b
movl $0x1, %r12d
movl $0x1, %edi
movq %r14, %rsi
leaq 0x8c794(%rip), %rdx     # 0x9825c
xorl %eax, %eax
callq 0x14378
orb $0x4, (%rbx)
jmp 0xba86
xorl %r12d, %r12d
jmp 0xba8b
cmpl $0x3, %r12d
jne 0xbaeb
movq 0x60(%rbx), %rax
movl $0x10000, %ebp          # imm = 0x10000
subl 0x20(%rax), %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq