// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq 0x40(%rdi), %r14
testq %r14, %r14
je 0x1f432
movq %rdi, %rbx
cmpq $0x0, 0x8(%r14)
je 0x1f48d
cmpq $0x0, (%r14)
je 0x1f494
leaq 0x78(%r14), %r15
movq %r15, %rdi
callq 0x4750
cmpl $0x2, 0xd0(%r14)
je 0x1f3d4
movl $0x1, 0xd0(%r14)
leaq 0xa0(%r14), %rdi
callq 0x4040
movl 0xd4(%r14), %r12d
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x1f3f5
callq 0x2caef
testb $0x2, (%rbx)
jne 0x1f439
cmpq $0x0, 0x40(%r14)
je 0x1f439
cmpl $0x0, 0x50(%r14)
je 0x1f439
cmpl $0x2, 0xd0(%r14)
je 0x1f439
movq 0x40(%r14), %rdi
callq 0x2caef
movq %r15, %rdi
callq 0x4340
callq 0x22c70
movq %r15, %rdi
callq 0x4750
jmp 0x1f408
xorl %ebp, %ebp
jmp 0x1f605
movl %r12d, %ebp
negl %ebp
movq %r15, %rdi
callq 0x4340
testb $0x2, (%rbx)
je 0x1f533
movq 0x68(%r14), %rcx
testl %r12d, %r12d
sete %al
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x1f49d
cmpl $0x0, 0x18(%rcx)
jle 0x1f49b
movq 0x40(%r14), %rsi
testq %rsi, %rsi
je 0x1f616
movq 0x8(%r14), %rdi
leaq 0x380d(%rip), %rdx      # 0x22c91
callq 0x2c89f
movl %eax, %ebp
jmp 0x1f49d
xorl %ebp, %ebp
jmp 0x1f5f8
xorl %ebp, %ebp
jmp 0x1f552
xorl %ebp, %ebp
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x1f4ab
callq 0x2cb1f
movq %r15, %rdi
callq 0x4750
testl %ebp, %ebp
jne 0x1f4c0
xorl %ebp, %ebp
subl 0xd4(%r14), %ebp
movq %r15, %rdi
callq 0x4340
testl %ebp, %ebp
jne 0x1f521
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x1f531
callq 0x2be17
testl %eax, %eax
jne 0x1f521
callq 0x22c70
movq %r15, %rdi
callq 0x4750
xorl %r13d, %r13d
subl 0xd4(%r14), %r13d
setb %bpl
movq 0x40(%r14), %rdi
callq 0x2bf78
movl %eax, %r12d
movq %r15, %rdi
callq 0x4340
testb $0x1, %bpl
pushq $0x5
popq %rbp
cmovnel %r13d, %ebp
testl %r12d, %r12d
cmovel %r13d, %ebp
jmp 0x1f4c8
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x1f533
callq 0x2bf17
jmp 0x1f533
xorl %ebp, %ebp
cmpl $0x0, 0x50(%r14)
je 0x1f545
movq 0x48(%r14), %rdi
xorl %esi, %esi
callq 0x4410
testl %ebp, %ebp
jne 0x1f552
xorl %ebp, %ebp
subl 0xd4(%r14), %ebp
movq 0x40(%r14), %rdi
testq %rdi, %rdi
je 0x1f560
callq 0x2bd67
cmpl $0x0, 0x10(%r14)
je 0x1f57b
cmpl $0x0, 0x54(%rbx)
jne 0x1f57b
movq 0x8(%r14), %rdi
callq 0x2cc8f
andq $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 0x1f5bb
movq (%r15), %r12
movq 0x10(%r15), %rdi
callq 0x45c0
movq %r15, %rdi
callq 0x45c0
movq %r12, %r15
jmp 0x1f59d
movq 0x60(%r14), %rdi
movq 0x68(%r14), %rax
testq %rdi, %rdi
je 0x1f5df
cmpq %rdi, %rax
jne 0x1f5d2
andq $0x0, 0x68(%r14)
movq (%rdi), %r15
callq 0x22e0d
movq %r15, %rdi
jmp 0x1f5bf
testq %rax, %rax
je 0x1f5ec
movq %rax, %rdi
callq 0x22e0d
movq (%r14), %rdi
callq 0x1d61e
movq 0x40(%rbx), %r14
movq %r14, %rdi
callq 0x45c0
andq $0x0, 0x40(%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x1f4ab