// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq 0x48(%rdi), %rbp
leaq 0x30(%rbp), %r12
leaq 0x80(%rbp), %rbx
leaq 0xa8(%rbp), %r14
movq %r12, %rdi
callq 0x4750
movq (%rbp), %rdi
callq 0x49174
movq %rax, %r13
movq %r12, %rdi
callq 0x4340
testq %r13, %r13
je 0xb51d
andl $0x0, 0x20018(%r13)
andq $0x0, 0x10008(%r13)
andq $0x0, 0x20010(%r13)
andl $0x0, 0x20028(%r13)
movq %r15, (%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0xad2b
testl %eax, %eax
jne 0xb448
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
andl $0x0, (%rsp)
leaq 0x1b7e(%rip), %rdx      # 0xcf43
movq %r13, %rcx
leaq 0x1bb9(%rip), %r8       # 0xcf88
movq %r8, %r9
callq 0x2c8b1
testl %eax, %eax
js 0xb515
movq %rbx, %rdi
callq 0x4750
movl 0xd8(%rbp), %eax
decl %eax
cmpl $0x4, %eax
ja 0xb416
leaq 0x6b52d(%rip), %rcx     # 0x76928
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x4040
jmp 0xb416
movq %r15, %rdi
callq 0xd026
movq %rbx, %rdi
callq 0x4340
movq %r12, %rdi
callq 0x4750
movq (%rbp), %rdi
callq 0x49174
movq %rax, %r13
movq %r12, %rdi
callq 0x4340
testq %r13, %r13
jne 0xb37b
jmp 0xb53f
leaq 0x20018(%r13), %rax
movq %rax, 0x10(%rsp)
cmpl $0x10, 0x20018(%r13)
je 0xb55d
movl $0x1, 0x20028(%r13)
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
andl $0x0, (%rsp)
leaq 0x1bf1(%rip), %rdx      # 0xd071
movq %r13, %rcx
leaq 0x1afe(%rip), %r8       # 0xcf88
movq %r8, %r9
callq 0x2c8b1
testl %eax, %eax
js 0xb596
movq 0x10(%rsp), %r13
cmpl $0x0, (%r13)
jne 0xb5a0
movq %rbx, %rdi
callq 0x4750
movl 0xd8(%rbp), %eax
testl %eax, %eax
jne 0xb4cd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x47c0
movl 0xd8(%rbp), %eax
decl %eax
cmpl $0x4, %eax
ja 0xb4f6
leaq 0x6b439(%rip), %rcx     # 0x76914
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x4040
jmp 0xb4f6
movq %r15, %rdi
callq 0xd026
movq %rbx, %rdi
callq 0x4340
jmp 0xb4aa
movq %r15, %rdi
callq 0xcfbb
movq %rbx, %rdi
callq 0x4340
jmp 0xb356
movq %r13, %rdi
callq 0xcf88
movq %rbx, %rdi
callq 0x4750
movl $0x5, 0xd8(%rbp)
movq %r14, %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x4340
movq 0x20(%rbp), %rdi
callq 0x2bd67
xorl %r13d, %r13d
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
andl $0x0, (%rsp)
leaq 0x1b01(%rip), %rdx      # 0xd071
leaq 0x1a11(%rip), %r8       # 0xcf88
movq %r13, %rcx
movq %r8, %r9
callq 0x2c8b1
testl %eax, %eax
js 0xb596
movq 0x20(%rbp), %rdi
callq 0x2be76
movq 0x10(%rsp), %r13
jmp 0xb54b
movq %r13, %rdi
callq 0xcf88
jmp 0xb53f
movq 0x20(%rbp), %rdi
callq 0x2bd67
jmp 0xb54b