// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_io.c
// Function: cram_close
int cram_close(cram_fd *fd) {
    spare_bams *bl, *next;
    int i, ret = 0;

    if (!fd)
        return -1;

    if (fd->mode == 'w' && fd->ctr) {
        if(fd->ctr->slice)
            cram_update_curr_slice(fd->ctr, fd->version);

        if (-1 == cram_flush_container_mt(fd, fd->ctr))
            ret = -1;
    }

    if (fd->mode != 'w')
        cram_drain_rqueue(fd);

    if (fd->pool && fd->eof >= 0 && fd->rqueue) {
        hts_tpool_process_flush(fd->rqueue);

        if (0 != cram_flush_result(fd))
            ret = -1;

        if (fd->mode == 'w')
            fd->ctr = NULL; // prevent double freeing

        //fprintf(stderr, "CRAM: destroy queue %p\n", fd->rqueue);

        hts_tpool_process_destroy(fd->rqueue);
    }

    pthread_mutex_destroy(&fd->metrics_lock);
    pthread_mutex_destroy(&fd->ref_lock);
    pthread_mutex_destroy(&fd->range_lock);
    pthread_mutex_destroy(&fd->bam_list_lock);

    if (ret == 0 && fd->mode == 'w') {
        /* Write EOF block */
        if (0 != cram_write_eof_block(fd))
            ret = -1;
    }

    for (bl = fd->bl; bl; bl = next) {
        int max_rec = fd->seqs_per_slice * fd->slices_per_container;

        next = bl->next;
        free_bam_list(bl->bams, max_rec);
        free(bl);
    }

    if (hclose(fd->fp) != 0)
        ret = -1;

    if (fd->file_def)
        cram_free_file_def(fd->file_def);

    if (fd->header)
        sam_hdr_destroy(fd->header);

    free(fd->prefix);

    if (fd->ctr)
        cram_free_container(fd->ctr);

    if (fd->ctr_mt && fd->ctr_mt != fd->ctr)
        cram_free_container(fd->ctr_mt);

    if (fd->refs)
        refs_free(fd->refs);
    if (fd->ref_free)
        free(fd->ref_free);

    for (i = 0; i < DS_END; i++)
        if (fd->m[i])
            free(fd->m[i]);

    if (fd->tags_used) {
        khint_t k;

        for (k = kh_begin(fd->tags_used); k != kh_end(fd->tags_used); k++) {
            if (kh_exist(fd->tags_used, k))
                free(kh_val(fd->tags_used, k));
        }

        kh_destroy(m_metrics, fd->tags_used);
    }

    if (fd->index)
        cram_index_free(fd);

    if (fd->own_pool && fd->pool)
        hts_tpool_destroy(fd->pool);

    if (fd->idxfp)
        if (bgzf_close(fd->idxfp) < 0)
            ret = -1;

    free(fd);

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x28(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x79b26
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x79ee7
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x79b86
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x79b86
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq $0x0, 0x90(%rax)
je 0x79b61
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x62160
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x76a00
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x79b84
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
jmp 0x79b86
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
je 0x79b99
movq -0x10(%rbp), %rdi
callq 0x595e0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x88b8(%rax)
je 0x79c10
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8890(%rax)
jl 0x79c10
movq -0x10(%rbp), %rax
cmpq $0x0, 0x88c0(%rax)
je 0x79c10
movq -0x10(%rbp), %rax
movq 0x88c0(%rax), %rdi
callq 0x4e740
movq -0x10(%rbp), %rdi
callq 0x76ba0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x79bea
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x79c00
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x88c0(%rax), %rdi
callq 0x4d090
movq -0x10(%rbp), %rdi
addq $0x88c8, %rdi           # imm = 0x88C8
callq 0x4570
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4570
movq -0x10(%rbp), %rdi
addq $0x8918, %rdi           # imm = 0x8918
callq 0x4570
movq -0x10(%rbp), %rdi
addq $0x8948, %rdi           # imm = 0x8948
callq 0x4570
cmpl $0x0, -0x28(%rbp)
jne 0x79c7a
movq -0x10(%rbp), %rax
cmpl $0x77, 0x8(%rax)
jne 0x79c7a
movq -0x10(%rbp), %rdi
callq 0x79980
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x79c78
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
jmp 0x79c7a
movq -0x10(%rbp), %rax
movq 0x8940(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x79cd6
movq -0x10(%rbp), %rax
movl 0x214(%rax), %eax
movq -0x10(%rbp), %rcx
imull 0x21c(%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x75210
movq -0x18(%rbp), %rdi
callq 0x45a0
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x79c89
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x14920
cmpl $0x0, %eax
je 0x79cee
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x79d06
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x77730
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x79d1e
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x31e70
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x45a0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x79d43
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x74a70
movq -0x10(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x79d6d
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
je 0x79d6d
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x74a70
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x79d85
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0x717d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x79d9d
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x45a0
movl $0x0, -0x24(%rbp)
cmpl $0x2f, -0x24(%rbp)
jge 0x79ddf
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, 0x90(%rax,%rcx,8)
je 0x79dd2
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq 0x90(%rax,%rcx,8), %rdi
callq 0x45a0
jmp 0x79dd4
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x79da4
movq -0x10(%rbp), %rax
cmpq $0x0, 0x208(%rax)
je 0x79e6a
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x208(%rcx), %rcx
cmpl (%rcx), %eax
je 0x79e5a
movq -0x10(%rbp), %rax
movq 0x208(%rax), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x30(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x79e4d
movq -0x10(%rbp), %rax
movq 0x208(%rax), %rax
movq 0x20(%rax), %rax
movl -0x30(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x45a0
jmp 0x79e4f
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x79df4
movq -0x10(%rbp), %rax
movq 0x208(%rax), %rdi
callq 0x79ef0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8878(%rax)
je 0x79e81
movq -0x10(%rbp), %rdi
callq 0x6aff0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x88b0(%rax)
je 0x79eac
movq -0x10(%rbp), %rax
cmpq $0x0, 0x88b8(%rax)
je 0x79eac
movq -0x10(%rbp), %rax
movq 0x88b8(%rax), %rdi
callq 0x4e970
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8988(%rax)
je 0x79ed8
movq -0x10(%rbp), %rax
movq 0x8988(%rax), %rdi
callq 0x103d0
cmpl $0x0, %eax
jge 0x79ed6
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
jmp 0x79ed8
movq -0x10(%rbp), %rdi
callq 0x45a0
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq