// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/sam.c
// Function: sam_state_destroy
int sam_state_destroy(htsFile *fp) {
    int ret = 0;

    if (!fp->state)
        return 0;

    SAM_state *fd = fp->state;
    if (fd->p) {
        if (fd->h) {
            // Notify sam_dispatcher we're closing
            pthread_mutex_lock(&fd->command_m);
            if (fd->command != SAM_CLOSE_DONE)
                fd->command = SAM_CLOSE;
            pthread_cond_signal(&fd->command_c);
            ret = -fd->errcode;
            if (fd->q)
                hts_tpool_wake_dispatch(fd->q); // unstick the reader

            if (!fp->is_write && fd->q && fd->dispatcher_set) {
                for (;;) {
                    // Avoid deadlocks with dispatcher
                    if (fd->command == SAM_CLOSE_DONE)
                        break;
                    hts_tpool_wake_dispatch(fd->q);
                    pthread_mutex_unlock(&fd->command_m);
                    hts_usleep(10000);
                    pthread_mutex_lock(&fd->command_m);
                }
            }
            pthread_mutex_unlock(&fd->command_m);

            if (fp->is_write) {
                // Dispatch the last partial block.
                sp_bams *gb = fd->curr_bam;
                if (!ret && gb && gb->nbams > 0 && fd->q)
                    ret = hts_tpool_dispatch(fd->p, fd->q, sam_format_worker, gb);

                // Flush and drain output
                if (fd->q)
                    hts_tpool_process_flush(fd->q);
                pthread_mutex_lock(&fd->command_m);
                if (!ret) ret = -fd->errcode;
                pthread_mutex_unlock(&fd->command_m);

                while (!ret && fd->q && !hts_tpool_process_empty(fd->q)) {
                    hts_usleep(10000);
                    pthread_mutex_lock(&fd->command_m);
                    ret = -fd->errcode;
                    // not empty but shutdown implies error
                    if (hts_tpool_process_is_shutdown(fd->q) && !ret)
                        ret = EIO;
                    pthread_mutex_unlock(&fd->command_m);
                }
                if (fd->q)
                    hts_tpool_process_shutdown(fd->q);
            }

            // Wait for it to acknowledge
            if (fd->dispatcher_set)
                pthread_join(fd->dispatcher, NULL);
            if (!ret) ret = -fd->errcode;
        }

        // Tidy up memory
        if (fd->q)
            hts_tpool_process_destroy(fd->q);

        if (fd->own_pool && fp->format.compression == no_compression) {
            hts_tpool_destroy(fd->p);
            fd->p = NULL;
        }
        pthread_mutex_destroy(&fd->lines_m);
        pthread_mutex_destroy(&fd->command_m);
        pthread_cond_destroy(&fd->command_c);

        sp_lines *l = fd->lines;
        while (l) {
            sp_lines *n = l->next;
            free(l->data);
            free(l);
            l = n;
        }

        sp_bams *b = fd->bams;
        while (b) {
            if (fd->curr_bam == b)
                fd->curr_bam = NULL;
            sp_bams *n = b->next;
            sam_free_sp_bams(b);
            b = n;
        }

        if (fd->curr_bam)
            sam_free_sp_bams(fd->curr_bam);

        // Decrement counter by one, maybe destroying too.
        // This is to permit the caller using bam_hdr_destroy
        // before sam_close without triggering decode errors
        // in the background threads.
        bam_hdr_destroy(fd->h);
    }

    free(fp->state);
    fp->state = NULL;
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x40(%rdi), %r12
testq %r12, %r12
je 0x24df9
movq %rdi, %rbx
cmpq $0x0, 0x8(%r12)
je 0x24e00
cmpq $0x0, (%r12)
je 0x24e07
leaq 0x78(%r12), %r14
movq %r14, %rdi
callq 0x4750
cmpl $0x2, 0xd0(%r12)
je 0x24cb4
movl $0x1, 0xd0(%r12)
leaq 0xa0(%r12), %rdi
callq 0x4040
movl 0xd4(%r12), %r13d
movq 0x40(%r12), %rdi
testq %rdi, %rdi
je 0x24cd8
callq 0x3817e
testb $0x2, (%rbx)
jne 0x24d3e
cmpq $0x0, 0x40(%r12)
je 0x24d3e
cmpl $0x0, 0x50(%r12)
je 0x24d3e
cmpl $0x2, 0xd0(%r12)
je 0x24d3e
leaq 0x8(%rsp), %r15
movq 0x40(%r12), %rdi
callq 0x3817e
movq %r14, %rdi
callq 0x4340
movq $0x0, 0x8(%rsp)
movq $0x989680, 0x10(%rsp)   # imm = 0x989680
movq %r15, %rdi
xorl %esi, %esi
callq 0x4100
movq %r14, %rdi
callq 0x4750
cmpl $0x2, 0xd0(%r12)
jne 0x24cfd
movl %r13d, %ebp
negl %ebp
movq %r14, %rdi
callq 0x4340
testb $0x2, (%rbx)
je 0x24dd5
movq 0x68(%r12), %rcx
testl %r13d, %r13d
sete %al
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x24d91
xorl %ebp, %ebp
cmpl $0x0, 0x18(%rcx)
jle 0x24d91
movq 0x40(%r12), %rsi
testq %rsi, %rsi
je 0x24d91
movq 0x8(%r12), %rdi
leaq 0x4e91(%rip), %rdx      # 0x29c1b
callq 0x37f30
movl %eax, %ebp
movq 0x40(%r12), %rdi
testq %rdi, %rdi
je 0x24da0
callq 0x381ae
movq %r14, %rdi
callq 0x4750
testl %ebp, %ebp
jne 0x24db6
xorl %ebp, %ebp
subl 0xd4(%r12), %ebp
movq %r14, %rdi
callq 0x4340
testl %ebp, %ebp
je 0x24ee4
movq 0x40(%r12), %rdi
testq %rdi, %rdi
je 0x24dd5
callq 0x374c6
cmpl $0x0, 0x50(%r12)
je 0x24de9
movq 0x48(%r12), %rdi
xorl %esi, %esi
callq 0x4410
testl %ebp, %ebp
jne 0x24e09
xorl %ebp, %ebp
subl 0xd4(%r12), %ebp
jmp 0x24e09
xorl %ebp, %ebp
jmp 0x24ed3
xorl %ebp, %ebp
jmp 0x24ec2
xorl %ebp, %ebp
movq 0x40(%r12), %rdi
testq %rdi, %rdi
je 0x24e18
callq 0x37316
cmpl $0x0, 0x10(%r12)
je 0x24e39
cmpl $0x0, 0x54(%rbx)
jne 0x24e39
movq 0x8(%r12), %rdi
callq 0x3831c
movq $0x0, 0x8(%r12)
leaq 0x18(%r12), %rdi
callq 0x4590
leaq 0x78(%r12), %rdi
callq 0x4590
leaq 0xa0(%r12), %rdi
callq 0x49a0
movq 0x58(%r12), %r14
testq %r14, %r14
je 0x24e80
movq (%r14), %r15
movq 0x10(%r14), %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
movq %r15, %r14
testq %r15, %r15
jne 0x24e64
movq 0x60(%r12), %rdi
testq %rdi, %rdi
je 0x24eaa
cmpq %rdi, 0x68(%r12)
jne 0x24e9a
movq $0x0, 0x68(%r12)
movq (%rdi), %r14
callq 0x29df4
movq %r14, %rdi
testq %r14, %r14
jne 0x24e8a
movq 0x68(%r12), %rdi
testq %rdi, %rdi
je 0x24eb9
callq 0x29df4
movq (%r12), %rdi
callq 0x22ae6
movq 0x40(%rbx), %rdi
callq 0x45c0
movq $0x0, 0x40(%rbx)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%r12), %rdi
testq %rdi, %rdi
je 0x24dc6
leaq 0x8(%rsp), %r15
callq 0x373c6
testl %eax, %eax
jne 0x24dc6
movq $0x0, 0x8(%rsp)
movq $0x989680, 0x10(%rsp)   # imm = 0x989680
movq %r15, %rdi
xorl %esi, %esi
callq 0x4100
movq %r14, %rdi
callq 0x4750
xorl %r13d, %r13d
subl 0xd4(%r12), %r13d
setb %bpl
movq 0x40(%r12), %rdi
callq 0x37527
testb $0x1, %bpl
movl $0x5, %ebp
cmovnel %r13d, %ebp
testl %eax, %eax
cmovel %r13d, %ebp
movq %r14, %rdi
callq 0x4340
testl %ebp, %ebp
jne 0x24dc6
movq 0x40(%r12), %rdi
testq %rdi, %rdi
jne 0x24ef7
jmp 0x24dc6