// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x355aa
movl $0x0, -0x4(%rbp)
jmp 0x35970
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x35951
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0x35835
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
movq -0x20(%rbp), %rax
cmpl $0x2, 0xd0(%rax)
je 0x355fb
movq -0x20(%rbp), %rax
movl $0x1, 0xd0(%rax)
movq -0x20(%rbp), %rdi
addq $0xa0, %rdi
callq 0x4040
movq -0x20(%rbp), %rcx
xorl %eax, %eax
subl 0xd4(%rcx), %eax
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x35632
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4e6f0
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x3569d
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x3569d
movq -0x20(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x3569d
jmp 0x35659
movq -0x20(%rbp), %rax
cmpl $0x2, 0xd0(%rax)
jne 0x35668
jmp 0x3569b
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4e6f0
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
movl $0x2710, %edi           # imm = 0x2710
callq 0x3bbc0
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
jmp 0x35659
jmp 0x3569d
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x35803
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x3570f
cmpq $0x0, -0x28(%rbp)
je 0x3570f
movq -0x28(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x3570f
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x3570f
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x28(%rbp), %rcx
leaq 0x6509(%rip), %rdx      # 0x3bc10
callq 0x4e290
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x35727
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4e740
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
cmpl $0x0, -0x14(%rbp)
jne 0x35749
movq -0x20(%rbp), %rcx
xorl %eax, %eax
subl 0xd4(%rcx), %eax
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
xorl %eax, %eax
cmpl $0x0, -0x14(%rbp)
movb %al, -0x49(%rbp)
jne 0x35789
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x40(%rcx)
movb %al, -0x49(%rbp)
je 0x35789
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d190
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x35792
jmp 0x357e9
movl $0x2710, %edi           # imm = 0x2710
callq 0x3bbc0
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4730
movq -0x20(%rbp), %rcx
xorl %eax, %eax
subl 0xd4(%rcx), %eax
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d3d0
cmpl $0x0, %eax
je 0x357d7
cmpl $0x0, -0x14(%rbp)
jne 0x357d7
movl $0x5, -0x14(%rbp)
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4330
jmp 0x35756
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x35801
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d330
jmp 0x35803
movq -0x20(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x3581e
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4400
cmpl $0x0, -0x14(%rbp)
jne 0x35833
movq -0x20(%rbp), %rcx
xorl %eax, %eax
subl 0xd4(%rcx), %eax
movl %eax, -0x14(%rbp)
jmp 0x35835
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x3584d
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x4d090
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x3587a
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
jne 0x3587a
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4e970
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x20(%rbp), %rdi
addq $0x18, %rdi
callq 0x4570
movq -0x20(%rbp), %rdi
addq $0x78, %rdi
callq 0x4570
movq -0x20(%rbp), %rdi
addq $0xa0, %rdi
callq 0x49a0
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x358e2
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x45a0
movq -0x30(%rbp), %rdi
callq 0x45a0
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x358b0
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x3592d
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
cmpq -0x40(%rbp), %rax
jne 0x3590f
movq -0x20(%rbp), %rax
movq $0x0, 0x68(%rax)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3be60
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x358ee
movq -0x20(%rbp), %rax
cmpq $0x0, 0x68(%rax)
je 0x35945
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x3be60
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x3bf00
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x45a0
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)