// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/hts.c
// Function: hts_close
int hts_close(htsFile *fp)
{
    int ret = 0, save;
    if (!fp) {
        errno = EINVAL;
        return -1;
    }

    switch (fp->format.format) {
    case binary_format:
    case bam:
    case bcf:
        ret = bgzf_close(fp->fp.bgzf);
        break;

    case cram:
        if (!fp->is_write) {
            switch (cram_eof(fp->fp.cram)) {
            case 2:
                hts_log_warning("EOF marker is absent. The input is probably truncated");
                break;
            case 0:  /* not at EOF, but may not have wanted all seqs */
            default: /* case 1, expected EOF */
                break;
            }
        }
        ret = cram_close(fp->fp.cram);
        break;

    case empty_format:
    case text_format:
    case bed:
    case fasta_format:
    case fastq_format:
    case sam:
    case vcf:
        if (fp->format.format == sam)
            ret = sam_state_destroy(fp);
        else if (fp->format.format == fastq_format ||
                 fp->format.format == fasta_format)
            fastq_state_destroy(fp);

        if (fp->format.compression != no_compression)
            ret |= bgzf_close(fp->fp.bgzf);
        else
            ret |= hclose(fp->fp.hfile);
        break;

    default:
        ret = -1;
        break;
    }

    if (fp->idx) {
        // Close deferred index file handle, if present.
        // Unfortunately this means errors on the index will get mixed with
        // those on the main file, but as we only have the EOF block left to
        // write it hopefully won't happen that often.
        ret |= hts_idx_close_otf_fp(fp->idx);
    }

    save = errno;
    sam_hdr_destroy(fp->bam_header);
    hts_idx_destroy(fp->idx);
    hts_filter_free(fp->filter);
    free(fp->fn);
    free(fp->fn_aux);
    free(fp->line.s);
    free(fp);
    errno = save;
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x15725
movq %rdi, %rbx
movl 0x4c(%rdi), %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
decl %eax
cmpl $0x10, %eax
ja 0x157a4
xorl %r14d, %r14d
leaq 0x830cb(%rip), %rcx     # 0x987dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x38(%rbx), %rdi
callq 0xd4a4
jmp 0x157a2
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x15851
movq %rbx, %rdi
callq 0x29f80
jmp 0x1574f
movq %rbx, %rdi
callq 0x24c5b
movl %eax, %r14d
cmpl $0x0, 0x54(%rbx)
movq 0x38(%rbx), %rdi
je 0x15760
callq 0xd4a4
jmp 0x15765
callq 0xf6d0
movl %eax, %ebp
orl %r14d, %ebp
jmp 0x157a4
testb $0x2, (%rbx)
jne 0x15799
movq 0x38(%rbx), %rdi
callq 0x55e07
cmpl $0x2, %eax
jne 0x15799
leaq 0x83a74(%rip), %rsi     # 0x991fa
leaq 0x83a77(%rip), %rdx     # 0x99204
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
movq 0x38(%rbx), %rdi
callq 0x55b67
movl %eax, %ebp
movq 0x68(%rbx), %r15
testq %r15, %r15
je 0x15802
movq 0x98(%r15), %rdi
testq %rdi, %rdi
je 0x157fe
xorl %r14d, %r14d
testb $0x40, 0x3(%rdi)
jne 0x157dd
movq 0x20(%r15), %rax
movq %rsp, %rsi
movq %rax, (%rsi)
movl $0x8, %edx
callq 0xd0bb
movq %rax, %r14
shrq $0x3f, %r14
movq 0x98(%r15), %rdi
callq 0xd4a4
shrl $0x1f, %eax
orl %r14d, %eax
movq $0x0, 0x98(%r15)
negl %eax
jmp 0x15800
xorl %eax, %eax
orl %eax, %ebp
callq 0x4050
movq %rax, %r14
movl (%rax), %r15d
movq 0x78(%rbx), %rdi
callq 0x22ae6
movq 0x68(%rbx), %rdi
callq 0x1585e
movq 0x80(%rbx), %rdi
callq 0x1d13d
movq 0x28(%rbx), %rdi
callq 0x45c0
movq 0x30(%rbx), %rdi
callq 0x45c0
movq 0x20(%rbx), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
movl %r15d, (%r14)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq