// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x101c4
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
andq $0xffff, %rax           # imm = 0xFFFF
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rax
subq %rax, %rcx
movq -0x10(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0xfdc0
movq %rax, -0x8(%rbp)
jmp 0x10365
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x11, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x101e7
jmp 0x10206
leaq 0xded22(%rip), %rdi     # 0xeef10
leaq 0xdebf0(%rip), %rsi     # 0xeede5
movl $0x7f3, %edx            # imm = 0x7F3
leaq 0xded4d(%rip), %rcx     # 0xeef4e
callq 0x42e0
jmp 0x10208
cmpq $0x0, -0x38(%rbp)
jle 0x10359
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x50(%rcx), %rcx
subl (%rcx), %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
movq 0x50(%rcx), %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jae 0x10285
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
addq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x50(%rcx), %rcx
movq 0x8(%rcx), %rcx
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movq %rax, -0x60(%rbp)
jmp 0x10290
movl $0x10000, %eax          # imm = 0x10000
movq %rax, -0x60(%rbp)
jmp 0x10290
movq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x54(%rbp)
movslq -0x54(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x102c7
movq -0x38(%rbp), %rax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movslq -0x54(%rbp), %rdx
callq 0x43b0
movl -0x54(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x10(%rax), %ecx
movl %ecx, 0x10(%rax)
movl -0x54(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movslq -0x54(%rbp), %rcx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rax
cmpq -0x48(%rbp), %rax
jne 0x10354
movq -0x10(%rbp), %rdi
callq 0xff50
cmpl $0x0, %eax
je 0x10336
movq $-0x1, -0x8(%rbp)
jmp 0x10365
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x0, (%rax)
jle 0x10352
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x10354
jmp 0x10208
movq -0x20(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nop