// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: bgzf_close
int bgzf_close(BGZF* fp)
{
    int ret, block_length;
    if (fp == 0) return -1;
    if (fp->is_write && fp->is_compressed) {
        if (bgzf_flush(fp) != 0) {
            bgzf_close_mt(fp);
            return -1;
        }
        fp->compress_level = -1;
        block_length = deflate_block(fp, 0); // write an empty block
        if (block_length < 0) {
            hts_log_debug("Deflate block operation failed: %s", bgzf_zerr(block_length, NULL));
            bgzf_close_mt(fp);
            return -1;
        }
        if (hwrite(fp->fp, fp->compressed_block, block_length) < 0
            || hflush(fp->fp) != 0) {
            hts_log_error("File write failed");
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
    }

    bgzf_close_mt(fp);

    if ( fp->is_gzip )
    {
        if (fp->gz_stream == NULL) ret = Z_OK;
        else if (!fp->is_write) ret = inflateEnd(fp->gz_stream);
        else ret = deflateEnd(fp->gz_stream);
        if (ret != Z_OK) {
            hts_log_error("Call to inflateEnd/deflateEnd failed: %s", bgzf_zerr(ret, NULL));
        }
        free(fp->gz_stream);
    }
    ret = hclose(fp->fp);
    if (ret != 0) return -1;
    bgzf_index_destroy(fp);
    free(fp->uncompressed_block);
    free_cache(fp);
    ret = fp->errcode ? -1 : 0;
    free(fp);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0xd637
movq %rdi, %rbx
movl (%rdi), %eax
notl %eax
testl $0x40020000, %eax       # imm = 0x40020000
jne 0xd518
movq %rbx, %rdi
callq 0xcb85
testl %eax, %eax
je 0xd411
movq %rbx, %rdi
callq 0xd648
jmp 0xd637
orl $0x1ff00000, (%rbx)     # imm = 0x1FF00000
movq %rbx, %rdi
xorl %esi, %esi
callq 0xcede
testl %eax, %eax
js 0xd4a4
movq 0x30(%rbx), %rcx
movq 0x40(%rbx), %r12
movl %eax, %r14d
testb $0x2, 0x30(%r12)
movq 0x18(%r12), %r13
jne 0xd46c
movq %r13, %rax
subq 0x8(%r12), %rax
cmpq %r14, %rax
jae 0xd46c
addq %r14, %r13
subq (%r12), %r13
movq %r12, %rdi
movq %rcx, %r15
movq %r13, %rsi
callq 0xee24
movq %r15, %rcx
movq 0x18(%r12), %r13
movq %r13, 0x10(%r12)
movq 0x8(%r12), %rdi
subq %rdi, %r13
cmpq %r14, %r13
movq %rcx, (%rsp)
ja 0xd483
cmpq (%r12), %rdi
je 0xd4cf
cmpq %r14, %r13
movq %r14, %r15
cmovbq %r13, %r15
movq %rcx, %rsi
movq %r15, %rdx
callq 0x43c0
addq %r15, 0x8(%r12)
cmpq %r14, %r13
jb 0xd4d2
jmp 0xd4e9
movl %eax, %edi
xorl %esi, %esi
callq 0xadc9
leaq 0x8790c(%rip), %rsi     # 0x94dc0
leaq 0x8784e(%rip), %rdx     # 0x94d09
movl $0x5, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0xd404
xorl %r15d, %r15d
movq %r12, %rdi
movq (%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0xf300
testq %rax, %rax
js 0xd4f6
movq 0x40(%rbx), %rdi
callq 0xf22b
testl %eax, %eax
je 0xd518
leaq 0x878c3(%rip), %rsi     # 0x94dc0
leaq 0x878c7(%rip), %rdx     # 0x94dcb
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
orb $0x4, (%rbx)
jmp 0xd637
movq %rbx, %rdi
callq 0xd648
movl (%rbx), %eax
testl %eax, %eax
jns 0xd574
movq 0x60(%rbx), %rdi
testq %rdi, %rdi
je 0xd56b
btl $0x11, %eax
jb 0xd53c
callq 0x44e0
jmp 0xd541
callq 0x42a0
testl %eax, %eax
je 0xd56b
movl %eax, %edi
xorl %esi, %esi
callq 0xadc9
leaq 0x8786b(%rip), %rsi     # 0x94dc0
leaq 0x87881(%rip), %rdx     # 0x94ddd
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x13de5
movq 0x60(%rbx), %rdi
callq 0x45c0
movq 0x40(%rbx), %rdi
callq 0xf4df
testl %eax, %eax
jne 0xd637
movq %rbx, %rdi
callq 0xd674
movq 0x28(%rbx), %rdi
callq 0x45c0
movl (%rbx), %eax
btl $0x11, %eax
jb 0xd628
movq 0x38(%rbx), %rax
movq (%rax), %r14
movl (%r14), %eax
testl %eax, %eax
je 0xd5f9
movl $0x4, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x10(%r14), %rcx
movq %r13, %rdx
shrq $0x4, %rdx
movl (%rcx,%rdx,4), %edx
movl %r12d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0xd5e7
movq 0x20(%r14), %rax
movq (%rax,%r15,2), %rdi
callq 0x45c0
movl (%r14), %eax
incq %r13
movl %eax, %ecx
addq $0xc, %r15
addl $0x2, %r12d
cmpq %rcx, %r13
jb 0xd5bc
movq 0x18(%r14), %rdi
callq 0x45c0
movq 0x10(%r14), %rdi
callq 0x45c0
movq 0x20(%r14), %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
movq 0x38(%rbx), %rdi
callq 0x45c0
movzwl (%rbx), %eax
xorl %ebp, %ebp
negw %ax
sbbl %ebp, %ebp
movq %rbx, %rdi
callq 0x45c0
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq