// File: karel-brinda[P]ococo/ext/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
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x6589f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65add
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x10, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x659e8
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1d, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x659e8
movq -0x10(%rbp), %rdi
callq 0x65080
cmpl $0x0, %eax
je 0x658e3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65add
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0xe00fffff, %ecx       # imm = 0xE00FFFFF
orl $0x1ff00000, %ecx       # imm = 0x1FF00000
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x65480
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x65945
leaq 0x24cce(%rip), %rax     # 0x8a5e0
cmpl $0x3, (%rax)
jl 0x65939
movq 0x246b2(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x18(%rbp), %ecx
leaq 0x186a2(%rip), %rsi     # 0x7dfcd
leaq 0x18727(%rip), %rdx     # 0x7e059
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65add
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movslq -0x18(%rbp), %rdx
callq 0x65590
cmpq $0x0, %rax
jl 0x65976
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x6a8e0
cmpl $0x0, %eax
je 0x659ce
leaq 0x24c63(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x659a1
movq 0x24647(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x186d1(%rip), %rsi     # 0x7e064
leaq 0x186bf(%rip), %rdx     # 0x7e059
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
movl (%rax), %edx
andl $0xffff, %edx           # imm = 0xFFFF
orl $0x4, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65add
movq -0x10(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x659e6
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x65af0
jmp 0x659e8
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x65a82
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x10, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x65a1d
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x45d0
movl %eax, -0x14(%rbp)
jmp 0x65a2d
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x4310
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x65a75
leaq 0x24ba6(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x65a75
movq 0x2458a(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x63940
movq -0x20(%rbp), %rdi
movq %rax, %rcx
leaq 0x18617(%rip), %rsi     # 0x7e07e
leaq 0x185eb(%rip), %rdx     # 0x7e059
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x6ad60
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x65aa1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x65add
movq -0x10(%rbp), %rdi
callq 0x65c40
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x46a0
movq -0x10(%rbp), %rdi
callq 0x65ca0
movq -0x10(%rbp), %rdi
callq 0x46a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)