// File: /workspace/github_c_makefile_star2_O1/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 0xd715
movq %rdi, %rbx
movl (%rdi), %eax
notl %eax
testl $0x40020000, %eax       # imm = 0x40020000
jne 0xd600
movq %rbx, %rdi
callq 0xcc29
testl %eax, %eax
je 0xd4e8
movq %rbx, %rdi
callq 0xd726
jmp 0xd715
orl $0x1ff00000, (%rbx)     # imm = 0x1FF00000
movq %rbx, %rdi
xorl %esi, %esi
callq 0xcf8a
testl %eax, %eax
js 0xd58e
movq 0x30(%rbx), %rcx
movq 0x40(%rbx), %r12
movl %eax, %r15d
testb $0x2, 0x30(%r12)
jne 0xd544
movq 0x18(%r12), %rsi
movq %rsi, %rax
subq 0x8(%r12), %rax
cmpq %r15, %rax
jae 0xd544
addq %r15, %rsi
subq (%r12), %rsi
movq %r12, %rdi
movq %rcx, %r14
callq 0xefdd
movq %r14, %rcx
movq 0x18(%r12), %rax
movq %rax, 0x10(%r12)
movq 0x8(%r12), %rdi
movq 0x18(%r12), %r14
subq %rdi, %r14
cmpq %r15, %r14
ja 0xd55c
cmpq (%r12), %rdi
je 0xd5b9
cmpq %r15, %r14
movq %r15, %r13
cmovbq %r14, %r13
movq %rcx, %rsi
movq %r13, %rdx
movq %rcx, (%rsp)
callq 0x43c0
addq %r13, 0x8(%r12)
cmpq %r15, %r14
jae 0xd5cc
movq %r12, %rdi
movq (%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
jmp 0xd5c4
movl %eax, %edi
xorl %esi, %esi
callq 0xadd0
leaq 0x8a7e2(%rip), %rsi     # 0x97d80
leaq 0x8a724(%rip), %rdx     # 0x97cc9
movl $0x5, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0xd4db
movq %r12, %rdi
movq %rcx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xf4c6
movq %rax, %r13
testq %r13, %r13
js 0xd5de
movq 0x40(%rbx), %rdi
callq 0xf3e7
testl %eax, %eax
je 0xd600
leaq 0x8a79b(%rip), %rsi     # 0x97d80
leaq 0x8a79f(%rip), %rdx     # 0x97d8b
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
orb $0x4, (%rbx)
jmp 0xd715
movq %rbx, %rdi
callq 0xd726
movl (%rbx), %eax
testl %eax, %eax
jns 0xd660
movq 0x60(%rbx), %rdi
testq %rdi, %rdi
je 0xd624
btl $0x11, %eax
jb 0xd628
callq 0x44e0
jmp 0xd62d
xorl %eax, %eax
jmp 0xd62d
callq 0x42a0
testl %eax, %eax
je 0xd657
movl %eax, %edi
xorl %esi, %esi
callq 0xadd0
leaq 0x8a73f(%rip), %rsi     # 0x97d80
leaq 0x8a755(%rip), %rdx     # 0x97d9d
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
movq 0x60(%rbx), %rdi
callq 0x45c0
movq 0x40(%rbx), %rdi
callq 0xf6d0
testl %eax, %eax
jne 0xd715
movq %rbx, %rdi
callq 0xd752
movq 0x28(%rbx), %rdi
callq 0x45c0
testb $0x2, 0x2(%rbx)
jne 0xd706
movq 0x38(%rbx), %rax
movq (%rax), %r14
cmpl $0x0, (%r14)
je 0xd6da
movl $0x4, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x10(%r14), %rax
movl %r13d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %r12d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0xd6c7
movq 0x20(%r14), %rax
movq (%rax,%r15,2), %rdi
callq 0x45c0
incq %r13
movl (%r14), %eax
addq $0xc, %r15
addl $0x2, %r12d
cmpq %rax, %r13
jb 0xd6a1
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
xorl %ebp, %ebp
cmpw (%rbx), %bp
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