// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/bgzf.c
// Function: bgzf_mt_reader
static void *bgzf_mt_reader(void *vp) {
    BGZF *fp = (BGZF *)vp;
    mtaux_t *mt = fp->mt;

restart:
    pthread_mutex_lock(&mt->job_pool_m);
    bgzf_job *j = pool_alloc(mt->job_pool);
    pthread_mutex_unlock(&mt->job_pool_m);
    if (!j) goto err;
    j->errcode = 0;
    j->comp_len = 0;
    j->uncomp_len = 0;
    j->hit_eof = 0;
    j->fp = fp;

    while (bgzf_mt_read_block(fp, j) == 0) {
        // Dispatch
        if (hts_tpool_dispatch3(mt->pool, mt->out_queue, bgzf_decode_func, j,
                                job_cleanup, job_cleanup, 0) < 0) {
            job_cleanup(j);
            goto err;
        }

        // Check for command
        pthread_mutex_lock(&mt->command_m);
        switch (mt->command) {
        case SEEK:
            bgzf_mt_seek(fp);  // Sets mt->command to SEEK_DONE
            pthread_mutex_unlock(&mt->command_m);
            goto restart;

        case HAS_EOF:
            bgzf_mt_eof(fp);   // Sets mt->command to HAS_EOF_DONE
            break;

        case SEEK_DONE:
        case HAS_EOF_DONE:
            pthread_cond_signal(&mt->command_c);
            break;

        case CLOSE:
            pthread_cond_signal(&mt->command_c);
            pthread_mutex_unlock(&mt->command_m);
            hts_tpool_process_destroy(mt->out_queue);
            return NULL;

        default:
            break;
        }
        pthread_mutex_unlock(&mt->command_m);

        // Allocate buffer for next block
        pthread_mutex_lock(&mt->job_pool_m);
        j = pool_alloc(mt->job_pool);
        pthread_mutex_unlock(&mt->job_pool_m);
        if (!j) {
            hts_tpool_process_destroy(mt->out_queue);
            return NULL;
        }
        j->errcode = 0;
        j->comp_len = 0;
        j->uncomp_len = 0;
        j->hit_eof = 0;
        j->fp = fp;
    }

    if (j->errcode == BGZF_ERR_MT) {
        // Attempt to multi-thread decode a raw gzip stream cannot be done.
        // We tear down the multi-threaded decoder and revert to the old code.
        if (hts_tpool_dispatch3(mt->pool, mt->out_queue, bgzf_nul_func, j,
                                job_cleanup, job_cleanup, 0) < 0) {
            job_cleanup(j);
            hts_tpool_process_destroy(mt->out_queue);
            return NULL;
        }
        hts_tpool_process_ref_decr(mt->out_queue);
        return &j->errcode;
    }

    // Dispatch an empty block so EOF is spotted.
    // We also use this mechanism for returning errors, in which case
    // j->errcode is set already.

    j->hit_eof = 1;
    if (hts_tpool_dispatch3(mt->pool, mt->out_queue, bgzf_nul_func, j,
                            job_cleanup, job_cleanup, 0) < 0) {
        job_cleanup(j);
        hts_tpool_process_destroy(mt->out_queue);
        return NULL;
    }
    if (j->errcode != 0) {
        hts_tpool_process_destroy(mt->out_queue);
        return &j->errcode;
    }

    // We hit EOF so can stop reading, but we may get a subsequent
    // seek request.  In this case we need to restart the reader.
    //
    // To handle this we wait on a condition variable and then
    // monitor the command. (This could be either seek or close.)
    for (;;) {
        pthread_mutex_lock(&mt->command_m);
        if (mt->command == NONE)
            pthread_cond_wait(&mt->command_c, &mt->command_m);
        switch(mt->command) {
        default:
            pthread_mutex_unlock(&mt->command_m);
            break;

        case SEEK:
            bgzf_mt_seek(fp);
            pthread_mutex_unlock(&mt->command_m);
            goto restart;

        case HAS_EOF:
            bgzf_mt_eof(fp);   // Sets mt->command to HAS_EOF_DONE
            pthread_mutex_unlock(&mt->command_m);
            break;

        case SEEK_DONE:
        case HAS_EOF_DONE:
            pthread_cond_signal(&mt->command_c);
            pthread_mutex_unlock(&mt->command_m);
            break;

        case CLOSE:
            pthread_cond_signal(&mt->command_c);
            pthread_mutex_unlock(&mt->command_m);
            hts_tpool_process_destroy(mt->out_queue);
            return NULL;
        }
    }

 err:
    pthread_mutex_lock(&mt->command_m);
    mt->command = CLOSE;
    pthread_cond_signal(&mt->command_c);
    pthread_mutex_unlock(&mt->command_m);
    hts_tpool_process_destroy(mt->out_queue);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x48(%rdi), %r14
leaq 0x30(%r14), %r15
movq %r15, %rdi
callq 0x4750
movq (%r14), %rdi
callq 0x57daa
movq %rax, %r13
movq %r15, 0x10(%rsp)
movq %r15, %rdi
callq 0x4340
testq %r13, %r13
je 0xca88
leaq 0x80(%r14), %r15
leaq 0xa8(%r14), %r12
xorl %eax, %eax
leaq 0x880f7(%rip), %rbp     # 0x94958
movl %eax, 0x20018(%r13)
movq %rax, 0x10008(%r13)
movq %rax, 0x20010(%r13)
movl %eax, 0x20028(%r13)
movq %rbx, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc0ff
testl %eax, %eax
je 0xc94b
leaq 0x20018(%r13), %rax
movq %rax, 0x18(%rsp)
cmpl $0x10, 0x20018(%r13)
je 0xcad4
movl $0x1, 0x20028(%r13)
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movl $0x0, (%rsp)
leaq 0x2346(%rip), %rdx      # 0xec15
movq %r13, %rcx
leaq 0x2250(%rip), %r8       # 0xeb29
movq %r8, %r9
callq 0x36b0d
testl %eax, %eax
js 0xcb10
movq 0x18(%rsp), %r13
cmpl $0x0, (%r13)
jne 0xcb1a
movq %r15, %rdi
callq 0x4750
movl 0xd8(%r14), %eax
testl %eax, %eax
jne 0xc91e
movq %r12, %rdi
movq %r15, %rsi
callq 0x47c0
movl 0xd8(%r14), %eax
decl %eax
cmpl $0x4, %eax
ja 0xc941
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movq %r12, %rdi
callq 0x4040
jmp 0xc941
movq %rbx, %rdi
callq 0xebca
movq %r15, %rdi
callq 0x4340
jmp 0xc8f9
movq %r12, 0x20(%rsp)
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movl $0x0, (%rsp)
leaq 0x217e(%rip), %rdx      # 0xeae4
movq %r13, %rcx
leaq 0x21b9(%rip), %r8       # 0xeb29
movq %r8, %r9
callq 0x36b0d
testl %eax, %eax
js 0xca80
movq %r15, %rdi
callq 0x4750
movl 0xd8(%r14), %eax
decl %eax
cmpl $0x4, %eax
ja 0xc9b8
leaq 0x87fa7(%rip), %rcx     # 0x94944
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, %rdi
callq 0x4040
jmp 0xc9b8
movq %rbx, %rdi
callq 0xebca
movq %rbp, %r12
movq %rbx, %rbp
movq %r15, %rdi
callq 0x4340
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq (%r14), %rdi
callq 0x57daa
movq %rax, %r13
movq %rbx, %rdi
callq 0x4340
testq %r13, %r13
je 0xcab6
xorl %eax, %eax
movl %eax, 0x20018(%r13)
movq %rax, 0x10008(%r13)
movq %rax, 0x20010(%r13)
movl %eax, 0x20028(%r13)
movq %rbp, %rbx
movq %rbp, (%r13)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xc0ff
testl %eax, %eax
movq %r12, %rbp
movq 0x20(%rsp), %r12
je 0xc950
jmp 0xc894
movq %rbx, %rdi
callq 0xeb5c
movq %r15, %rdi
callq 0x4340
movq 0x10(%rsp), %rdi
callq 0x4750
movq (%r14), %rdi
callq 0x57daa
movq %rax, %r13
movq 0x10(%rsp), %rdi
callq 0x4340
testq %r13, %r13
movl $0x0, %eax
jne 0xc861
jmp 0xca88
movq %r12, %rdi
callq 0x4040
movq %r15, %rdi
jmp 0xcab1
movq %r13, %rdi
callq 0xeb29
leaq 0x80(%r14), %rbx
movq %rbx, %rdi
callq 0x4750
movl $0x5, 0xd8(%r14)
leaq 0xa8(%r14), %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
movq 0x20(%r14), %rdi
callq 0x35f44
xorl %r13d, %r13d
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movl $0x0, (%rsp)
leaq 0x212b(%rip), %rdx      # 0xec15
leaq 0x2038(%rip), %r8       # 0xeb29
movq %r13, %rcx
movq %r8, %r9
callq 0x36b0d
testl %eax, %eax
js 0xcb10
movq 0x20(%r14), %rdi
callq 0x36053
movq 0x18(%rsp), %r13
jmp 0xcac2
movq %r13, %rdi
callq 0xeb29
jmp 0xcab6
movq 0x20(%r14), %rdi
callq 0x35f44
jmp 0xcac2