// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: bgzf_flush
int bgzf_flush(BGZF *fp)
{
    if (!fp->is_write) return 0;
#ifdef BGZF_MT
    if (fp->mt) {
        int ret = 0;
        if (fp->block_offset) ret = mt_queue(fp);
        if (!ret) ret = mt_flush_queue(fp);

        // We maintain mt->block_address when threading as the
        // main code can call bgzf_tell without any locks.
        // (The result from tell are wrong, but we only care about the last
        // 16-bits worth except for the final flush process.
        pthread_mutex_lock(&fp->mt->idx_m);
        fp->block_address = fp->mt->block_address;
        pthread_mutex_unlock(&fp->mt->idx_m);

        return ret;
    }
#endif

    if (!fp->is_compressed) {
        return hflush(fp->fp);
    }

    while (fp->block_offset > 0) {
        int block_length;
        if ( fp->idx_build_otf )
        {
            bgzf_index_add_block(fp);
            fp->idx->ublock_addr += fp->block_offset;
        }
        block_length = deflate_block(fp, fp->block_offset);
        if (block_length < 0) {
            hts_log_debug("Deflate block operation failed: %s", bgzf_zerr(block_length, NULL));
            return -1;
        }
        if (hwrite(fp->fp, fp->compressed_block, block_length) != block_length) {
            hts_log_error("File write failed (wrong size)");
            fp->errcode |= BGZF_ERR_IO; // possibly truncated file
            return -1;
        }
        fp->block_address += block_length;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl (%rdi), %eax
xorl %ebp, %ebp
btl $0x11, %eax
jae 0xcd78
movq %rdi, %rbx
movq 0x48(%rdi), %r12
testq %r12, %r12
je 0xcc5d
cmpl $0x0, 0x10(%rbx)
je 0xcbd0
movq %rbx, %rdi
callq 0xcdd5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xcd52
movq 0x48(%rbx), %r12
leaq 0x30(%r12), %r14
movq %r14, %rdi
callq 0x4750
cmpl $0x0, 0x58(%r12)
je 0xcc32
leaq 0x8(%rsp), %r15
movq 0x20(%r12), %rdi
callq 0x36155
movl %eax, %ebp
movq %r14, %rdi
callq 0x4340
testl %ebp, %ebp
jne 0xcd4d
movq $0x0, 0x8(%rsp)
movq $0x989680, 0x10(%rsp)   # imm = 0x989680
movq %r15, %rdi
xorl %esi, %esi
callq 0x4100
movq %r14, %rdi
callq 0x4750
cmpl $0x0, 0x58(%r12)
jne 0xcbea
movq %r14, %rdi
callq 0x4340
movq 0x20(%r12), %rdi
callq 0x36d84
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xcd52
xorl %ebp, %ebp
cmpw (%rbx), %bp
sbbl %ebp, %ebp
jmp 0xcd52
btl $0x1e, %eax
jb 0xcc7a
movq 0x40(%rbx), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf22b
movl 0x10(%rbx), %esi
testl %esi, %esi
jle 0xcd78
cmpl $0x0, 0x58(%rbx)
je 0xcc9f
movq %rbx, %rdi
callq 0xb715
movslq 0x10(%rbx), %rsi
movq 0x50(%rbx), %rax
addq %rsi, 0x10(%rax)
movq %rbx, %rdi
callq 0xcede
testl %eax, %eax
js 0xcd89
movq 0x30(%rbx), %r15
movq 0x40(%rbx), %r12
movl %eax, %r14d
testb $0x2, 0x30(%r12)
movq 0x18(%r12), %r13
jne 0xccf0
movq %r13, %rax
subq 0x8(%r12), %rax
cmpq %r14, %rax
jae 0xccf0
addq %r14, %r13
subq (%r12), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xee24
movq 0x18(%r12), %r13
movq %r13, 0x10(%r12)
movq 0x8(%r12), %rdi
subq %rdi, %r13
cmpq %r14, %r13
ja 0xcd03
cmpq (%r12), %rdi
je 0xcd24
cmpq %r14, %r13
movq %r14, %rbp
cmovbq %r13, %rbp
movq %r15, %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbp, 0x8(%r12)
cmpq %r14, %r13
jb 0xcd26
jmp 0xcd3a
xorl %ebp, %ebp
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0xf300
movq %rax, %rbp
cmpq %r14, %rbp
jne 0xcdb1
addq %r14, 0x18(%rbx)
movl 0x10(%rbx), %esi
xorl %ebp, %ebp
jmp 0xcc7d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xe0, %r14d
movq 0x48(%rbx), %rdi
addq %r14, %rdi
callq 0x4750
movq 0x48(%rbx), %rdi
movq 0x70(%rdi), %rax
movq %rax, 0x18(%rbx)
addq %r14, %rdi
callq 0x4340
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
xorl %esi, %esi
callq 0xadc9
leaq 0x87f65(%rip), %rsi     # 0x94cfe
leaq 0x87f69(%rip), %rdx     # 0x94d09
movl $0x5, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0xcdce
leaq 0x87f46(%rip), %rsi     # 0x94cfe
leaq 0x87f6d(%rip), %rdx     # 0x94d2c
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
orb $0x4, (%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xcd78