// File: /workspace/github_c_makefile_star2_O3/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), %r14
testq %r14, %r14
je 0x243be
movq %rdi, %rbx
cmpq $0x0, 0x8(%r14)
je 0x243c5
cmpq $0x0, (%r14)
je 0x243cc
leaq 0x78(%r14), %r15
movq %r15, %rdi
callq 0x4750
cmpl $0x2, 0xd0(%r14)
je 0x242ec
movl $0x1, 0xd0(%r14)
leaq 0xa0(%r14), %rdi
callq 0x4040
movl 0xd4(%r14), %r13d
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x2430d
callq 0x36d54
testb $0x2, (%rbx)
jne 0x2436e
cmpq $0x0, 0x40(%r14)
je 0x2436e
cmpl $0x0, 0x50(%r14)
je 0x2436e
cmpl $0x2, 0xd0(%r14)
je 0x2436e
leaq 0x8(%rsp), %r12
movq 0x40(%r14), %rdi
callq 0x36d54
movq %r15, %rdi
callq 0x4340
movq $0x0, 0x8(%rsp)
movq $0x989680, 0x10(%rsp)   # imm = 0x989680
movq %r12, %rdi
xorl %esi, %esi
callq 0x4100
movq %r15, %rdi
callq 0x4750
cmpl $0x2, 0xd0(%r14)
jne 0x2432f
movl %r13d, %ebp
negl %ebp
movq %r15, %rdi
callq 0x4340
testb $0x2, (%rbx)
je 0x24425
movq 0x68(%r14), %rcx
testl %r13d, %r13d
sete %al
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x243d2
cmpl $0x0, 0x18(%rcx)
jle 0x243d0
movq 0x40(%r14), %rsi
testq %rsi, %rsi
je 0x243f6
movq 0x8(%r14), %rdi
leaq 0x4c4f(%rip), %rdx      # 0x29004
callq 0x36af8
movl %eax, %ebp
jmp 0x243d2
xorl %ebp, %ebp
jmp 0x24504
xorl %ebp, %ebp
jmp 0x244f4
xorl %ebp, %ebp
jmp 0x24444
xorl %ebp, %ebp
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x243e0
callq 0x36d84
movq %r15, %rdi
callq 0x4750
testl %ebp, %ebp
je 0x243fe
movq %r15, %rdi
callq 0x4340
jmp 0x24417
movq %r15, %rdi
callq 0x4750
movl 0xd4(%r14), %ebp
movq %r15, %rdi
callq 0x4340
testl %ebp, %ebp
je 0x24582
negl %ebp
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x24425
callq 0x360f4
cmpl $0x0, 0x50(%r14)
je 0x24437
movq 0x48(%r14), %rdi
xorl %esi, %esi
callq 0x4410
testl %ebp, %ebp
jne 0x24444
xorl %ebp, %ebp
subl 0xd4(%r14), %ebp
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x24452
callq 0x35f44
cmpl $0x0, 0x10(%r14)
je 0x24470
cmpl $0x0, 0x54(%rbx)
jne 0x24470
movq 0x8(%r14), %rdi
callq 0x36efb
movq $0x0, 0x8(%r14)
leaq 0x18(%r14), %rdi
callq 0x4590
leaq 0x78(%r14), %rdi
callq 0x4590
leaq 0xa0(%r14), %rdi
callq 0x49a0
movq 0x58(%r14), %r15
testq %r15, %r15
je 0x244b3
movq (%r15), %r12
movq 0x10(%r15), %rdi
callq 0x45c0
movq %r15, %rdi
callq 0x45c0
movq %r12, %r15
testq %r12, %r12
jne 0x24497
movq 0x60(%r14), %rdi
testq %rdi, %rdi
je 0x244da
cmpq %rdi, 0x68(%r14)
jne 0x244ca
movq $0x0, 0x68(%r14)
movq (%rdi), %r15
callq 0x291e5
movq %r15, %rdi
testq %r15, %r15
jne 0x244bc
movq 0x68(%r14), %rdi
testq %rdi, %rdi
je 0x244e8
callq 0x291e5
movq (%r14), %rdi
callq 0x22102
movq 0x40(%rbx), %r14
movq %r14, %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
callq 0x35ff4
testl %eax, %eax
jne 0x24592
movq $0x0, 0x8(%rsp)
movq $0x989680, 0x10(%rsp)   # imm = 0x989680
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x4100
movq %r15, %rdi
callq 0x4750
xorl %r12d, %r12d
subl 0xd4(%r14), %r12d
setb %bpl
movq 0x40(%r14), %rdi
callq 0x36155
movl %eax, %r13d
movq %r15, %rdi
callq 0x4340
testb $0x1, %bpl
movl $0x5, %ebp
cmovnel %r12d, %ebp
testl %r13d, %r13d
cmovel %r12d, %ebp
testl %ebp, %ebp
jne 0x24417
movq 0x40(%r14), %rdi
testq %rdi, %rdi
jne 0x24515
xorl %ebp, %ebp
jmp 0x24425
xorl %ebp, %ebp
jmp 0x24417