// File: /workspace/github_c_makefile_star2_O3/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 0xd20c
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)
movq 0x18(%r15), %r12
jne 0xd1d0
movq %r12, %rax
subq 0x8(%r15), %rax
cmpq %rbx, %rax
jae 0xd1d0
addq %rbx, %r12
subq (%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0xee24
movq 0x18(%r15), %r12
movq %r12, 0x10(%r15)
movq 0x8(%r15), %rdi
subq %rdi, %r12
cmpq %rbx, %r12
ja 0xd1e5
cmpq (%r15), %rdi
je 0xd2da
cmpq %rbx, %r12
movq %rbx, %r13
cmovbq %r12, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, 0x8(%r15)
cmpq %rbx, %r12
jae 0xd2c8
jmp 0xd2dd
btl $0x11, %eax
jae 0xd2fc
movq %rbx, %r13
testq %rbx, %rbx
jle 0xd2b9
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 0xd25d
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 0xd2aa
movq %r15, %rdi
callq 0xcfed
testl %eax, %eax
jne 0xd2c1
movq 0x50(%r15), %rax
movl (%rax), %ecx
testl %ecx, %ecx
jle 0xd2aa
decl %ecx
movl %ecx, (%rax)
addq %r12, %r14
subq %r12, %r13
testq %r13, %r13
jg 0xd225
subq %r13, %rbx
movq %rbx, %r13
jmp 0xd2c8
movq $-0x1, %r13
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf300
leaq 0x87a48(%rip), %rdi     # 0x94d4b
leaq 0x87916(%rip), %rsi     # 0x94c20
leaq 0x87a78(%rip), %rcx     # 0x94d89
movl $0x7f3, %edx            # imm = 0x7F3
callq 0x42f0