// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/bgzf.c
// Function: bgzf_block_write
ssize_t bgzf_block_write(BGZF *fp, const void *data, size_t length)
{
    if ( !fp->is_compressed ) {
        size_t push = length + (size_t) fp->block_offset;
        fp->block_offset = push % BGZF_MAX_BLOCK_SIZE;
        fp->block_address += (push - fp->block_offset);
        return hwrite(fp->fp, data, length);
    }

    const uint8_t *input = (const uint8_t*)data;
    ssize_t remaining = length;
    assert(fp->is_write);
    uint64_t current_block; //keep track of current block
    uint64_t ublock_size; // amount of uncompressed data to be fed into next block
    while (remaining > 0) {
        current_block = fp->idx->moffs - fp->idx->noffs;
        ublock_size = current_block + 1 < fp->idx->moffs ? fp->idx->offs[current_block+1].uaddr-fp->idx->offs[current_block].uaddr : BGZF_MAX_BLOCK_SIZE;
        uint8_t* buffer = (uint8_t*)fp->uncompressed_block;
        int copy_length = ublock_size - fp->block_offset;
        if (copy_length > remaining) copy_length = remaining;
        memcpy(buffer + fp->block_offset, input, copy_length);
        fp->block_offset += copy_length;
        input += copy_length;
        remaining -= copy_length;
        if (fp->block_offset == ublock_size) {
            if (lazy_flush(fp) != 0) return -1;
            if (fp->idx->noffs > 0)
                fp->idx->noffs--;  // decrement noffs to track the blocks
        }
    }
    return length - remaining;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl (%rdi), %eax
btl $0x1e, %eax
jb 0xd2dc
movslq 0x10(%r15), %rax
addq %rbx, %rax
movzwl %ax, %ecx
movl %ecx, 0x10(%r15)
andq $-0x10000, %rax         # imm = 0xFFFF0000
addq %rax, 0x18(%r15)
movq 0x40(%r15), %r15
testb $0x2, 0x30(%r15)
jne 0xd290
movq 0x18(%r15), %rsi
movq %rsi, %rax
subq 0x8(%r15), %rax
cmpq %rbx, %rax
jae 0xd290
addq %rbx, %rsi
subq (%r15), %rsi
movq %r15, %rdi
callq 0xefdd
movq 0x18(%r15), %rax
movq %rax, 0x10(%r15)
movq 0x8(%r15), %rdi
movq 0x18(%r15), %r13
subq %rdi, %r13
cmpq %rbx, %r13
ja 0xd2a9
cmpq (%r15), %rdi
je 0xd3aa
cmpq %rbx, %r13
movq %rbx, %r12
cmovbq %r13, %r12
movq %r14, %rsi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%r15)
cmpq %rbx, %r13
jae 0xd398
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %rcx
jmp 0xd3b5
btl $0x11, %eax
jae 0xd3c8
movq %rbx, %r13
testq %rbx, %rbx
jle 0xd389
movq %rbx, %r13
movq 0x50(%r15), %rcx
movslq 0x4(%rcx), %rax
movslq (%rcx), %rsi
movq %rsi, %rdx
negq %rdx
addq %rax, %rdx
incq %rdx
movl $0x10000, %ebp          # imm = 0x10000
cmpq %rax, %rdx
jae 0xd32d
subq %rsi, %rax
movq 0x8(%rcx), %rcx
shlq $0x4, %rdx
movq (%rcx,%rdx), %rbp
shlq $0x4, %rax
subq (%rcx,%rax), %rbp
movslq 0x10(%r15), %rdi
movl %ebp, %eax
subl %edi, %eax
cltq
cmpq %rax, %r13
cmovll %r13d, %eax
addq 0x28(%r15), %rdi
movslq %eax, %r12
movq %r14, %rsi
movq %r12, %rdx
callq 0x43c0
movslq 0x10(%r15), %rax
addq %r12, %rax
movl %eax, 0x10(%r15)
cmpq %rax, %rbp
jne 0xd37a
movq %r15, %rdi
callq 0xd0a3
testl %eax, %eax
jne 0xd391
movq 0x50(%r15), %rax
movl (%rax), %ecx
testl %ecx, %ecx
jle 0xd37a
decl %ecx
movl %ecx, (%rax)
addq %r12, %r14
subq %r12, %r13
testq %r13, %r13
jg 0xd2f5
subq %r13, %rbx
movq %rbx, %r12
jmp 0xd398
movq $-0x1, %r12
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf4c6
leaq 0x8a93c(%rip), %rdi     # 0x97d0b
leaq 0x8a80a(%rip), %rsi     # 0x97be0
leaq 0x8a96c(%rip), %rcx     # 0x97d49
movl $0x7f3, %edx            # imm = 0x7F3
callq 0x42f0