// File: /workspace/github_c_makefile_star2_O2/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 0x123db
movq %rdi, %rbx
movl 0x4c(%rdi), %eax
pushq $-0x1
popq %rbp
decl %eax
cmpl $0x10, %eax
ja 0x12456
xorl %r14d, %r14d
leaq 0x65475(%rip), %rcx     # 0x7783c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x38(%rbx), %rdi
callq 0xbc96
jmp 0x12454
callq 0x4050
movl $0x16, (%rax)
pushq $-0x1
popq %rbp
jmp 0x124f5
movq %rbx, %rdi
callq 0x22f81
jmp 0x12403
movq %rbx, %rdi
callq 0x1f383
movl %eax, %r14d
cmpl $0x0, 0x54(%rbx)
movq 0x38(%rbx), %rdi
je 0x12414
callq 0xbc96
jmp 0x12419
callq 0xd8ee
movl %eax, %ebp
orl %r14d, %ebp
jmp 0x12456
testb $0x2, (%rbx)
jne 0x1244b
movq 0x38(%rbx), %rdi
callq 0x451f0
cmpl $0x2, %eax
jne 0x1244b
leaq 0x65e51(%rip), %rsi     # 0x7828b
leaq 0x65e54(%rip), %rdx     # 0x78295
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq 0x38(%rbx), %rdi
callq 0x44f6a
movl %eax, %ebp
movq 0x68(%rbx), %r15
testq %r15, %r15
je 0x124a6
movq 0x98(%r15), %rdi
testq %rdi, %rdi
je 0x124a2
xorl %r14d, %r14d
testb $0x40, 0x3(%rdi)
jne 0x1248b
movq 0x20(%r15), %rsi
callq 0x188a2
movq %rax, %r14
shrq $0x3f, %r14
movq 0x98(%r15), %rdi
callq 0xbc96
shrl $0x1f, %eax
orl %r14d, %eax
andq $0x0, 0x98(%r15)
negl %eax
jmp 0x124a4
xorl %eax, %eax
orl %eax, %ebp
callq 0x4050
movq %rax, %r14
movl (%rax), %r15d
movq 0x78(%rbx), %rdi
callq 0x1d61e
movq 0x68(%rbx), %rdi
callq 0x12502
movq 0x80(%rbx), %rdi
callq 0x18cf7
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