// File: /workspace/github_c_makefile_star2_O2/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
pushq $-0x1
popq %rbp
testq %rdi, %rdi
je 0xbe61
movq %rdi, %rbx
movl (%rdi), %eax
notl %eax
testl $0x40020000, %eax       # imm = 0x40020000
jne 0xbd29
movq %rbx, %rdi
callq 0xb609
testl %eax, %eax
je 0xbcd4
movq %rbx, %rdi
callq 0xbe72
jmp 0xbe61
orl $0x1ff00000, (%rbx)     # imm = 0x1FF00000
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb8b0
testl %eax, %eax
js 0xbd4d
movq 0x30(%rbx), %rsi
movq 0x40(%rbx), %rdi
movl %eax, %edx
callq 0xb9a0
testq %rax, %rax
js 0xbd09
movq 0x40(%rbx), %rdi
callq 0xd64f
testl %eax, %eax
je 0xbd29
leaq 0x6b060(%rip), %rsi     # 0x76d70
leaq 0x6b064(%rip), %rdx     # 0x76d7b
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
orb $0x4, (%rbx)
jmp 0xbe61
movq %rbx, %rdi
callq 0xbe72
movl (%rbx), %eax
testl %eax, %eax
jns 0xbdac
movq 0x60(%rbx), %rdi
testq %rdi, %rdi
je 0xbda3
btl $0x11, %eax
jb 0xbd76
callq 0x44e0
jmp 0xbd7b
movl %eax, %edi
xorl %esi, %esi
callq 0x9cf6
leaq 0x6b013(%rip), %rsi     # 0x76d70
leaq 0x6af55(%rip), %rdx     # 0x76cb9
pushq $0x5
popq %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x110c6
jmp 0xbcc7
callq 0x42a0
testl %eax, %eax
je 0xbda3
movl %eax, %edi
xorl %esi, %esi
callq 0x9cf6
leaq 0x6afe1(%rip), %rsi     # 0x76d70
leaq 0x6aff7(%rip), %rdx     # 0x76d8d
pushq $0x1
popq %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x110c6
movq 0x60(%rbx), %rdi
callq 0x45c0
movq 0x40(%rbx), %rdi
callq 0xd8ee
testl %eax, %eax
jne 0xbe61
movq %rbx, %rdi
callq 0xbe9b
movq 0x28(%rbx), %rdi
callq 0x45c0
movl (%rbx), %eax
btl $0x11, %eax
jb 0xbe52
movq 0x38(%rbx), %rax
movq (%rax), %r14
pushq $0x4
popq %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movl (%r14), %eax
cmpq %rax, %r13
jae 0xbe23
movq 0x10(%r14), %rax
movq %r13, %rcx
shrq $0x4, %rcx
movl (%rax,%rcx,4), %eax
movl %r12d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0xbe16
movq 0x20(%r14), %rax
movq (%rax,%r15,2), %rdi
callq 0x45c0
incq %r13
addq $0xc, %r15
addl $0x2, %r12d
jmp 0xbde7
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