// File: karel-brinda[P]ococo/ext/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) {
                if (hts_verbose >= 2) {
                    fprintf(stderr, "[E::%s] bgzf_read_block error %d after %zd of %zu bytes\n", __func__, ret, bytes_read, length);
                }
                fp->errcode |= BGZF_ERR_ZLIB;
                return -1;
            }
            available = fp->block_length - fp->block_offset;
            if (available <= 0) break;
        }
        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;
    }
    if (fp->block_offset == fp->block_length) {
        fp->block_address = htell(fp->fp);
        fp->block_offset = fp->block_length = 0;
    }
    fp->uncompressed_address += bytes_read;
    return bytes_read;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
ja 0x64be8
movq $0x0, -0x8(%rbp)
jmp 0x64db4
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x10, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x64bfb
jmp 0x64c1a
leaq 0x1934c(%rip), %rdi     # 0x7df4e
leaq 0x1920c(%rip), %rsi     # 0x7de15
movl $0x2b2, %edx            # imm = 0x2B2
leaq 0x1934b(%rip), %rcx     # 0x7df60
callq 0x4360
jmp 0x64c1c
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x64d5e
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jg 0x64cd9
movq -0x10(%rbp), %rdi
callq 0x63aa0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x64cbb
leaq 0x25982(%rip), %rax     # 0x8a5e0
cmpl $0x2, (%rax)
jl 0x64c8d
movq 0x25366(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x44(%rbp), %ecx
movq -0x28(%rbp), %r8
movq -0x20(%rbp), %r9
leaq 0x1930b(%rip), %rsi     # 0x7df8a
leaq 0x1933d(%rip), %rdx     # 0x7dfc3
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x1, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movq $-0x1, -0x8(%rbp)
jmp 0x64db4
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jg 0x64cd7
jmp 0x64d5e
jmp 0x64cd9
movq -0x20(%rbp), %rax
subq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jae 0x64cf8
movq -0x20(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x64d00
movslq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rax
addq %rax, %rsi
movslq -0x34(%rbp), %rdx
callq 0x4460
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x64c1c
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x64d9c
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x641a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop