// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rdi, %rbx
movq 0x48(%rdi), %r14
leaq 0x30(%r14), %r15
movq %r15, %rdi
callq 0x4750
movq (%r14), %rdi
callq 0x5a822
movq %rax, %r13
movq %r15, 0x10(%rsp)
movq %r15, %rdi
callq 0x4340
testq %r13, %r13
je 0xcb32
leaq 0x80(%r14), %r15
leaq 0xa8(%r14), %r12
xorl %eax, %eax
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 0xc1cb
testl %eax, %eax
jne 0xca2b
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movl $0x0, (%rsp)
leaq 0x22e5(%rip), %rdx      # 0xec5d
movq %r13, %rcx
leaq 0x2320(%rip), %r8       # 0xeca2
movq %r8, %r9
callq 0x37f45
testl %eax, %eax
js 0xcb2a
movq %r15, %rdi
callq 0x4750
movl 0xd8(%r14), %eax
decl %eax
cmpl $0x4, %eax
ja 0xc9ca
leaq 0x8af55(%rip), %rcx     # 0x97904
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, %rdi
callq 0x4040
jmp 0xc9ca
movq %rbx, %rdi
callq 0xed43
movq %rbx, %rbp
movq %r15, %rdi
callq 0x4340
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4750
movq (%r14), %rdi
callq 0x5a822
movq %rax, %r13
movq %rbx, %rdi
callq 0x4340
testq %r13, %r13
je 0xcb60
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
jmp 0xc952
leaq 0x20018(%r13), %rbp
cmpl $0x10, 0x20018(%r13)
je 0xcb7d
movl $0x1, 0x20028(%r13)
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movl $0x0, (%rsp)
leaq 0x232d(%rip), %rdx      # 0xed8e
movq %r13, %rcx
leaq 0x2237(%rip), %r8       # 0xeca2
movq %r8, %r9
callq 0x37f45
testl %eax, %eax
js 0xcbb4
cmpl $0x0, (%rbp)
leaq 0x8ae92(%rip), %r13     # 0x97918
jne 0xcbbe
movq %r15, %rdi
callq 0x4750
cmpl $0x0, 0xd8(%r14)
jne 0xcaa9
movq %r12, %rdi
movq %r15, %rsi
callq 0x47c0
movl 0xd8(%r14), %eax
decl %eax
cmpl $0x4, %eax
ja 0xcad3
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq %r12, %rdi
callq 0x4040
jmp 0xcad3
movq %rbx, %rdi
callq 0xed43
movq %r15, %rdi
callq 0x4340
jmp 0xca8c
movq %rbx, %rdi
callq 0xecd5
movq %r15, %rdi
callq 0x4340
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x4750
movq (%r14), %rdi
callq 0x5a822
movq %rax, %r13
movq %rbp, %rdi
callq 0x4340
testq %r13, %r13
movl $0x0, %eax
jne 0xc92f
jmp 0xcb32
movq %r12, %rdi
callq 0x4040
movq %r15, %rdi
jmp 0xcb5b
movq %r13, %rdi
callq 0xeca2
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 0x37316
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x18, %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 0x21fb(%rip), %rdx      # 0xed8e
leaq 0x2108(%rip), %r8       # 0xeca2
movq %r13, %rcx
movq %r8, %r9
callq 0x37f45
testl %eax, %eax
js 0xcbb4
movq 0x20(%r14), %rdi
callq 0x37425
jmp 0xcb6b
movq %r13, %rdi
callq 0xeca2
jmp 0xcb60
movq 0x20(%r14), %rdi
callq 0x37316
jmp 0xcb6b