// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0xac40
movq %rdi, %rbx
testb $0x2, 0x2(%rdi)
jne 0xac9c
movq %rdx, %r15
movq %rsi, %r12
xorl %r14d, %r14d
movq %r15, %rbp
subq %r14, %rbp
jbe 0xac3a
movl 0x8(%rbx), %eax
movl 0x10(%rbx), %ecx
subl %ecx, %eax
jg 0xabed
movq %rbx, %rdi
callq 0x9d84
testl %eax, %eax
jne 0xac45
movl 0x8(%rbx), %r8d
movl 0x10(%rbx), %ecx
movl %r8d, %eax
subl %ecx, %eax
je 0xac1f
js 0xac6b
movl %eax, %r13d
cmpq %r13, %rbp
cmovbq %rbp, %r13
movslq %ecx, %rsi
addq 0x28(%rbx), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x43c0
movl 0x10(%rbx), %eax
addl %r13d, %eax
movl %eax, 0x10(%rbx)
addq %r13, %r12
addq %r13, %r14
cmpl 0x8(%rbx), %eax
jne 0xabbf
jmp 0xac24
testl %r8d, %r8d
je 0xac3a
movq %rbx, %rdi
callq 0xa7e5
movq %rax, 0x18(%rbx)
andl $0x0, 0x8(%rbx)
andl $0x0, 0x10(%rbx)
jmp 0xabbf
addq %r14, 0x20(%rbx)
jmp 0xac8a
xorl %r14d, %r14d
jmp 0xac8a
movzwl (%rbx), %ecx
leaq 0x6bfb2(%rip), %rsi     # 0x76c01
leaq 0x6bfb5(%rip), %rdx     # 0x76c0b
pushq $0x1
popq %rdi
movq %r14, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x110c6
orb $0x1, (%rbx)
jmp 0xac86
leaq 0x6bf8f(%rip), %rsi     # 0x76c01
leaq 0x6bfd3(%rip), %rdx     # 0x76c4c
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
orb $0x8, (%rbx)
pushq $-0x1
popq %r14
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6bf1b(%rip), %rdi     # 0x76bbe
leaq 0x6bf26(%rip), %rsi     # 0x76bd0
leaq 0x6bf26(%rip), %rcx     # 0x76bd7
movl $0x4dc, %edx            # imm = 0x4DC
callq 0x42f0