// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: bgzf_read
ssize_t bgzf_read(BGZF *fp, void *data, size_t length)
{
    ssize_t bytes_read = 0;
    uint8_t *output = (uint8_t*)data;
    if (length <= 0) return 0;
    assert(fp->is_write == 0);
    while (bytes_read < length) {
        int copy_length, available = fp->block_length - fp->block_offset;
        uint8_t *buffer;
        if (available <= 0) {
            int ret = bgzf_read_block(fp);
            if (ret != 0) {
                hts_log_error("Read block operation failed with error %d after %zd of %zu bytes", fp->errcode, bytes_read, length);
                fp->errcode |= BGZF_ERR_ZLIB;
                return -1;
            }
            available = fp->block_length - fp->block_offset;
            if (available == 0) {
                if (fp->block_length == 0)
                    break; // EOF

                // Offset was at end of block (see commit e9863a0)
                fp->block_address = bgzf_htell(fp);
                fp->block_offset = fp->block_length = 0;
                continue;
            } else if (available < 0) {
                // Block offset was set to an invalid coordinate
                hts_log_error("BGZF block offset %d set beyond block size %d",
                              fp->block_offset, fp->block_length);
                fp->errcode |= BGZF_ERR_MISUSE;
                return -1;
            }
        }
        copy_length = length - bytes_read < available? length - bytes_read : available;
        buffer = (uint8_t*)fp->uncompressed_block;
        memcpy(output, buffer + fp->block_offset, copy_length);
        fp->block_offset += copy_length;
        output += copy_length;
        bytes_read += copy_length;

        // For raw gzip streams this avoids short reads.
        if (fp->block_offset == fp->block_length) {
            fp->block_address = bgzf_htell(fp);
            fp->block_offset = fp->block_length = 0;
        }
    }

    fp->uncompressed_address += bytes_read;

    return bytes_read;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdx, %rdx
je 0xbfad
movq %rdi, %rbx
testb $0x2, 0x2(%rdi)
jne 0xc00b
movq %rdx, %r15
movq %rsi, %r12
movl 0x8(%rbx), %eax
movl 0x10(%rbx), %ecx
xorl %r14d, %r14d
subl %ecx, %eax
jg 0xbf4e
movq %rbx, %rdi
callq 0xae59
testl %eax, %eax
jne 0xbfb2
movl 0x8(%rbx), %r8d
movl 0x10(%rbx), %ecx
movl %r8d, %eax
subl %ecx, %eax
je 0xbf87
js 0xbfda
movq %r15, %r13
subq %r14, %r13
movl %eax, %eax
cmpq %rax, %r13
cmovaeq %rax, %r13
movslq %ecx, %rsi
addq 0x28(%rbx), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x43c0
movl 0x10(%rbx), %ecx
addl %r13d, %ecx
movl %ecx, 0x10(%rbx)
addq %r13, %r12
addq %r13, %r14
movl 0x8(%rbx), %eax
cmpl %eax, %ecx
je 0xbf8c
jmp 0xbfa2
testl %r8d, %r8d
je 0xbfa7
movq %rbx, %rdi
callq 0xb7a5
movq %rax, 0x18(%rbx)
xorl %ecx, %ecx
movl %ecx, 0x8(%rbx)
movl %ecx, 0x10(%rbx)
xorl %eax, %eax
cmpq %r15, %r14
jb 0xbf2a
addq %r14, 0x20(%rbx)
jmp 0xbffe
xorl %r14d, %r14d
jmp 0xbffe
movzwl (%rbx), %ecx
leaq 0x88c95(%rip), %rsi     # 0x94c51
leaq 0x88c98(%rip), %rdx     # 0x94c5b
movl $0x1, %edi
movq %r14, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x13de5
orb $0x1, (%rbx)
jmp 0xbff7
leaq 0x88c70(%rip), %rsi     # 0x94c51
leaq 0x88cb4(%rip), %rdx     # 0x94c9c
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
orb $0x8, (%rbx)
movq $-0x1, %r14
movq %r14, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x88bfc(%rip), %rdi     # 0x94c0e
leaq 0x88c07(%rip), %rsi     # 0x94c20
leaq 0x88c07(%rip), %rcx     # 0x94c27
movl $0x4dc, %edx            # imm = 0x4DC
callq 0x42f0