// File: /workspace/github_c_makefile_star2_O1/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 0xce24
movq %rdi, %rbx
cmpq $0x0, 0x48(%rdi)
je 0xccff
cmpl $0x0, 0x10(%rbx)
je 0xcc6e
movq %rbx, %rdi
callq 0xce81
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xcdfe
movq 0x48(%rbx), %r12
leaq 0x30(%r12), %r14
movq %r14, %rdi
callq 0x4750
cmpl $0x0, 0x58(%r12)
je 0xccd4
leaq 0x8(%rsp), %r15
movq 0x20(%r12), %rdi
callq 0x37527
movl %eax, %ebp
movq %r14, %rdi
callq 0x4340
testl %ebp, %ebp
jne 0xcdf9
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 0xcc8c
movq %r14, %rdi
callq 0x4340
movq 0x20(%r12), %rdi
callq 0x381ae
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xcdfe
xorl %ebp, %ebp
cmpw (%rbx), %bp
sbbl %ebp, %ebp
jmp 0xcdfe
btl $0x1e, %eax
jb 0xcd1c
movq 0x40(%rbx), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf3e7
cmpl $0x0, 0x10(%rbx)
jle 0xce24
cmpl $0x0, 0x58(%rbx)
je 0xcd40
movq %rbx, %rdi
callq 0xb782
movslq 0x10(%rbx), %rax
movq 0x50(%rbx), %rcx
addq %rax, 0x10(%rcx)
movl 0x10(%rbx), %esi
movq %rbx, %rdi
callq 0xcf8a
testl %eax, %eax
js 0xce35
movq 0x30(%rbx), %r15
movq 0x40(%rbx), %r12
movl %eax, %r14d
testb $0x2, 0x30(%r12)
jne 0xcd91
movq 0x18(%r12), %rsi
movq %rsi, %rax
subq 0x8(%r12), %rax
cmpq %r14, %rax
jae 0xcd91
addq %r14, %rsi
subq (%r12), %rsi
movq %r12, %rdi
callq 0xefdd
movq 0x18(%r12), %rax
movq %rax, 0x10(%r12)
movq 0x8(%r12), %rdi
movq 0x18(%r12), %rbp
subq %rdi, %rbp
cmpq %r14, %rbp
ja 0xcda9
cmpq (%r12), %rdi
je 0xcdd6
cmpq %r14, %rbp
movq %r14, %r13
cmovbq %rbp, %r13
movq %r15, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, 0x8(%r12)
cmpq %r14, %rbp
jae 0xcde9
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
jmp 0xcde1
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %r13
cmpq %r14, %r13
jne 0xce5d
addq %r14, 0x18(%rbx)
xorl %ebp, %ebp
jmp 0xcd1c
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 0xadd0
leaq 0x8ae79(%rip), %rsi     # 0x97cbe
leaq 0x8ae7d(%rip), %rdx     # 0x97cc9
movl $0x5, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0xce7a
leaq 0x8ae5a(%rip), %rsi     # 0x97cbe
leaq 0x8ae81(%rip), %rdx     # 0x97cec
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
orb $0x4, (%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xce24