// File: /workspace/github_c_makefile_star2_O2/zhou-lab[P]tbmate/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) return -1;
        fp->compress_level = -1;
        block_length = deflate_block(fp, 0); // write an empty block
        if (block_length < 0) {
            if (hts_verbose >= 3) fprintf(stderr, "[E::%s] deflate_block error %d\n", __func__, block_length);
            return -1;
        }
        if (hwrite(fp->fp, fp->compressed_block, block_length) < 0
            || hflush(fp->fp) != 0) {
            if (hts_verbose >= 1) fprintf(stderr, "[E::%s] file write error\n", __func__);
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
#ifdef BGZF_MT
        if (fp->mt) mt_destroy(fp->mt);
#endif
    }
    if ( fp->is_gzip )
    {
        if (!fp->is_write) ret = inflateEnd(fp->gz_stream);
        else ret = deflateEnd(fp->gz_stream);
        if (ret != Z_OK && hts_verbose >= 1)
            fprintf(stderr, "[E::%s] inflateEnd/deflateEnd failed: %s\n",
                    __func__, 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(fp->compressed_block);
    free_cache(fp);
    free(fp);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $-0x1
popq %rbp
testq %rdi, %rdi
je 0x21859
movq %rdi, %rbx
movl (%rdi), %eax
testl $0x30000, %eax          # imm = 0x30000
sete %cl
testl $0x60000000, %eax       # imm = 0x60000000
sete %al
orb %cl, %al
je 0x21718
movl (%rbx), %eax
testl %eax, %eax
jns 0x2179f
movq 0x58(%rbx), %rdi
testl $0x30000, %eax          # imm = 0x30000
je 0x2177d
callq 0x33d0
jmp 0x21782
movq %rbx, %rdi
callq 0x21218
testl %eax, %eax
jne 0x21859
orl $0x1ff00000, (%rbx)     # imm = 0x1FF00000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2144e
testl %eax, %eax
js 0x21849
movq 0x28(%rbx), %rsi
movq 0x38(%rbx), %rdi
movl %eax, %edx
callq 0x21541
testq %rax, %rax
js 0x21765
movq 0x38(%rbx), %rdi
callq 0x24092
testl %eax, %eax
je 0x2186a
leaq 0x31d24(%rip), %rax     # 0x53490
cmpl $0x0, (%rax)
jg 0x21980
orb $0x4, (%rbx)
jmp 0x21859
callq 0x30e0
testl %eax, %eax
je 0x21796
leaq 0x31d03(%rip), %rcx     # 0x53490
cmpl $0x0, (%rcx)
jg 0x2194d
movq 0x58(%rbx), %rdi
callq 0x3370
movq 0x38(%rbx), %rdi
callq 0x242a6
testl %eax, %eax
jne 0x21859
movq %rbx, %rdi
callq 0x219ca
movq 0x20(%rbx), %rdi
callq 0x3370
movq 0x28(%rbx), %rdi
callq 0x3370
testb $0x3, 0x2(%rbx)
jne 0x2183d
movq 0x30(%rbx), %r14
pushq $0x4
popq %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movl (%r14), %eax
cmpq %rax, %r13
jae 0x2181a
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 0x2180d
movq 0x20(%r14), %rax
movq (%rax,%r15,2), %rdi
callq 0x3370
incq %r13
addq $0xc, %r15
addl $0x2, %r12d
jmp 0x217de
movq 0x18(%r14), %rdi
callq 0x3370
movq 0x10(%r14), %rdi
callq 0x3370
movq 0x20(%r14), %rdi
callq 0x3370
movq %r14, %rdi
callq 0x3370
movq %rbx, %rdi
callq 0x3370
xorl %ebp, %ebp
jmp 0x21859
leaq 0x31c40(%rip), %rcx     # 0x53490
cmpl $0x3, (%rcx)
jge 0x219a4
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rbx), %r14
testq %r14, %r14
je 0x216fc
leaq 0x38(%r14), %r15
movq %r15, %rdi
callq 0x37b0
movl $0x1, 0xc(%r14)
movl $0x0, 0x10(%r14)
leaq 0x60(%r14), %rdi
movq %rdi, (%rsp)
callq 0x3820
movq %r15, %rdi
callq 0x3850
pushq $0x1
popq %r12
movslq (%r14), %rax
cmpq %rax, %r12
jge 0x218c8
movq 0x30(%r14), %rax
movq (%rax,%r12,8), %rdi
xorl %esi, %esi
callq 0x3050
incq %r12
jmp 0x218ac
xorl %r12d, %r12d
movslq 0x4(%r14), %rax
cmpq %rax, %r12
jge 0x218e6
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0x3370
incq %r12
jmp 0x218cb
pushq $0x8
popq %r13
xorl %r12d, %r12d
movslq (%r14), %rax
cmpq %rax, %r12
jge 0x2190b
movq 0x28(%r14), %rax
movq (%rax,%r13), %rdi
callq 0x3370
incq %r12
addq $0x20, %r13
jmp 0x218ed
movq 0x18(%r14), %rdi
callq 0x3370
movq 0x20(%r14), %rdi
callq 0x3370
movq 0x28(%r14), %rdi
callq 0x3370
movq 0x30(%r14), %rdi
callq 0x3370
movq (%rsp), %rdi
callq 0x3070
movq %r15, %rdi
callq 0x3800
movq %r14, %rdi
callq 0x3370
jmp 0x216fc
movq 0x31694(%rip), %rcx     # 0x52fe8
movq (%rcx), %r14
movl %eax, %edi
xorl %esi, %esi
callq 0x20481
leaq 0x20557(%rip), %rsi     # 0x41ebe
leaq 0x2052b(%rip), %rdx     # 0x41e99
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x37f0
jmp 0x21796
movq 0x31661(%rip), %rax     # 0x52fe8
movq (%rax), %rdi
leaq 0x20513(%rip), %rsi     # 0x41ea4
leaq 0x20501(%rip), %rdx     # 0x41e99
xorl %eax, %eax
callq 0x37f0
jmp 0x21775
movq 0x3163d(%rip), %rcx     # 0x52fe8
movq (%rcx), %rdi
leaq 0x20458(%rip), %rsi     # 0x41e0d
leaq 0x204dd(%rip), %rdx     # 0x41e99
movl %eax, %ecx
xorl %eax, %eax
callq 0x37f0
jmp 0x21859