// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/thread_pool.c
// Function: tpool_worker
static void *tpool_worker(void *arg) {
    hts_tpool_worker *w = (hts_tpool_worker *)arg;
    hts_tpool *p = w->p;
    hts_tpool_job *j;

    pthread_mutex_lock(&p->pool_m);
    while (!p->shutdown) {
        // Pop an item off the pool queue

        assert(p->q_head == 0 || (p->q_head->prev && p->q_head->next));

        int work_to_do = 0;
        hts_tpool_process *first = p->q_head, *q = first;
        do {
            // Iterate over queues, finding one with jobs and also
            // room to put the result.
            //if (q && q->input_head && !hts_tpool_process_output_full(q)) {
            if (q && q->input_head
                && q->qsize - q->n_output > q->n_processing
                && !q->shutdown) {
                work_to_do = 1;
                break;
            }

            if (q) q = q->next;
        } while (q && q != first);

        if (!work_to_do) {
            // We scanned all queues and cannot process any, so we wait.
            p->nwaiting++;

            // Push this thread to the top of the waiting stack
            if (p->t_stack_top == -1 || p->t_stack_top > w->idx)
                p->t_stack_top = w->idx;

            p->t_stack[w->idx] = 1;
//            printf("%2d: no work.  In=%d Proc=%d Out=%d  full=%d\n",
//                   w->idx, p->q_head->n_input, p->q_head->n_processing, p->q_head->n_output,
//                   hts_tpool_process_output_full(p->q_head));
            pthread_cond_wait(&w->pending_c, &p->pool_m);
            p->t_stack[w->idx] = 0;

            /* Find new t_stack_top */
            int i;
            p->t_stack_top = -1;
            for (i = 0; i < p->tsize; i++) {
                if (p->t_stack[i]) {
                    p->t_stack_top = i;
                    break;
                }
            }

            p->nwaiting--;
            continue; // To outer loop.
        }

        // Otherwise work_to_do, so process as many items in this queue as
        // possible before switching to another queue.  This means threads
        // often end up being dedicated to one type of work.
        q->ref_count++;
        while (q->input_head && q->qsize - q->n_output > q->n_processing) {
            if (p->shutdown)
                goto shutdown;

            if (q->shutdown)
                // Queue shutdown, but there may be other queues
                break;

            j = q->input_head;
            assert(j->p == p);

            if (!(q->input_head = j->next))
                q->input_tail = NULL;

            // Transitioning from full queue to not-full means we can wake up
            // any blocked dispatch threads.  We broadcast this as it's only
            // happening once (on the transition) rather than every time we
            // are below qsize.
            // (I wish I could remember why io_lib rev 3660 changed this from
            //  == to >=, but keeping it just in case!)
            q->n_processing++;
            if (q->n_input-- >= q->qsize)
                pthread_cond_broadcast(&q->input_not_full_c);

            if (q->n_input == 0)
                pthread_cond_signal(&q->input_empty_c);

            p->njobs--; // Total number of jobs; used to adjust to CPU scaling

            pthread_mutex_unlock(&p->pool_m);

            DBG_OUT(stderr, "%d: Processing queue %p, serial %"PRId64"\n",
                    hts_tpool_worker_id(j->p), q, j->serial);

            if (hts_tpool_add_result(j, j->func(j->arg)) < 0)
                goto err;
            //memset(j, 0xbb, sizeof(*j));
            free(j);

            pthread_mutex_lock(&p->pool_m);
        }
        if (--q->ref_count == 0) { // we were the last user
            hts_tpool_process_destroy(q);
        } else {
            // Out of jobs on this queue, so restart search from next one.
            // This is equivalent to "work-stealing".
            if (p->q_head)
                p->q_head = p->q_head->next;
        }
    }

 shutdown:
    pthread_mutex_unlock(&p->pool_m);
#ifdef DEBUG
    fprintf(stderr, "%d: Shutting down\n", hts_tpool_worker_id(p));
#endif
    return NULL;

 err:
#ifdef DEBUG
    fprintf(stderr, "%d: Failed to add result\n", hts_tpool_worker_id(p));
#endif
    // Hard failure, so shutdown all queues
    pthread_mutex_lock(&p->pool_m);
    hts_tpool_process *first = p->q_head, *q = first;
    if (q) {
        do {
            hts_tpool_process_shutdown_locked(q);
            q->shutdown = 2; // signify error.
            q = q->next;
        } while (q != first);
    }
    pthread_mutex_unlock(&p->pool_m);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r15
movq (%rdi), %r14
leaq 0x38(%r14), %rbp
movq %rbp, %rdi
callq 0x4750
cmpl $0x0, 0x8(%r14)
je 0x3678e
movq %rbp, %rdi
callq 0x4340
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%r15), %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq 0x10(%r14), %rax
movq %rax, %r12
testq %rax, %rax
je 0x367c7
cmpq $0x0, 0x128(%rax)
je 0x36a97
cmpq $0x0, 0x120(%rax)
movq %rax, %r12
je 0x36a97
testq %r12, %r12
je 0x36805
movq 0x8(%r12), %r13
testq %r13, %r13
je 0x367f3
movl 0x28(%r12), %ecx
subl 0x48(%r12), %ecx
cmpl 0x4c(%r12), %ecx
jle 0x367f3
cmpl $0x0, 0x50(%r12)
je 0x36889
movq 0x120(%r12), %r12
testq %r12, %r12
je 0x36805
cmpq %rax, %r12
jne 0x367c7
incl (%r14)
movl 0x30(%r14), %ecx
cmpl $-0x1, %ecx
sete %dl
movl 0x8(%r15), %eax
cmpl %eax, %ecx
setg %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0x36826
movl %eax, 0x30(%r14)
cltq
movq 0x28(%r14), %rcx
movl $0x1, (%rcx,%rax,4)
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x47c0
movq 0x28(%r14), %rax
movslq 0x8(%r15), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0xffffffff, 0x30(%r14) # imm = 0xFFFFFFFF
movslq 0x18(%r14), %rcx
testq %rcx, %rcx
jle 0x36876
xorl %edx, %edx
cmpl $0x0, (%rax,%rdx,4)
jne 0x36872
incq %rdx
cmpq %rdx, %rcx
jne 0x36862
jmp 0x36876
movl %edx, 0x30(%r14)
decl (%r14)
cmpl $0x0, 0x8(%r14)
je 0x3679c
jmp 0x36775
incl 0x5c(%r12)
leaq 0x90(%r12), %rax
movq %rax, 0x10(%rsp)
leaq 0xc0(%r12), %rax
movq %rax, 0x8(%rsp)
movl 0x28(%r12), %eax
movl 0x4c(%r12), %ecx
movl %eax, %edx
subl 0x48(%r12), %edx
cmpl %ecx, %edx
jle 0x36a16
cmpl $0x0, 0x8(%r14)
jne 0x36775
cmpl $0x0, 0x50(%r12)
jne 0x36a16
cmpq %r14, 0x28(%r13)
jne 0x36ab6
movq 0x20(%r13), %rdx
movq %rdx, 0x8(%r12)
testq %rdx, %rdx
jne 0x368f9
movq $0x0, 0x10(%r12)
incl %ecx
movl %ecx, 0x4c(%r12)
movl 0x44(%r12), %edx
leal -0x1(%rdx), %ecx
movl %ecx, 0x44(%r12)
cmpl %eax, %edx
jl 0x36920
movq 0x10(%rsp), %rdi
callq 0x4170
movl 0x44(%r12), %ecx
testl %ecx, %ecx
jne 0x3692e
movq 0x8(%rsp), %rdi
callq 0x4040
decl 0x4(%r14)
movq %rbp, %rbx
movq %rbp, %rdi
callq 0x4340
movq 0x8(%r13), %rdi
callq *(%r13)
movq %rax, %rbp
movq 0x30(%r13), %r15
movq (%r15), %rdi
addq $0x38, %rdi
callq 0x4750
decl 0x4c(%r15)
jne 0x3696a
leaq 0xf0(%r15), %rdi
callq 0x4040
cmpl $0x0, 0x54(%r15)
jne 0x369e4
movl $0x20, %edi
callq 0x4680
testq %rax, %rax
je 0x36a47
movq $0x0, (%rax)
movq %rbp, 0x18(%rax)
movq 0x18(%r13), %rcx
movq %rcx, 0x8(%rax)
movq 0x38(%r13), %rcx
movq %rcx, 0x10(%rax)
incl 0x48(%r15)
movq 0x20(%r15), %rdx
leaq 0x18(%r15), %rsi
testq %rdx, %rdx
cmovneq %rdx, %rsi
movq %rax, (%rsi)
movq %rax, 0x20(%r15)
movq 0x30(%r15), %rax
cmpq %rax, %rcx
setae %dl
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
sete %sil
orb %dl, %sil
je 0x36ad5
cmpq %rax, %rcx
jne 0x369e4
leaq 0x60(%r15), %rdi
callq 0x4170
movq (%r15), %rdi
addq $0x38, %rdi
callq 0x4340
movq %r13, %rdi
callq 0x45c0
movq %rbx, %rbp
movq %rbx, %rdi
callq 0x4750
movq 0x8(%r12), %r13
testq %r13, %r13
movq 0x20(%rsp), %r15
jne 0x368a8
decl 0x5c(%r12)
je 0x36a3a
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x36879
movq 0x120(%rax), %rax
movq %rax, 0x10(%r14)
jmp 0x36879
movq %r12, %rdi
callq 0x35f44
jmp 0x36879
movq (%r15), %rdi
addq $0x38, %rdi
callq 0x4340
movq %r15, %rdi
callq 0x360f4
movq %rbx, %rbp
movq %rbx, %rdi
callq 0x4750
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x36775
movq %r15, %r14
movq %r14, %rdi
callq 0x36119
movl $0x2, 0x50(%r14)
movq 0x120(%r14), %r14
cmpq %r15, %r14
jne 0x36a76
jmp 0x36775
leaq 0x62424(%rip), %rdi     # 0x98ec2
leaq 0x62399(%rip), %rsi     # 0x98e3e
leaq 0x6244d(%rip), %rcx     # 0x98ef9
movl $0x20f, %edx            # imm = 0x20F
callq 0x42f0
leaq 0x62457(%rip), %rdi     # 0x98f14
leaq 0x6237a(%rip), %rsi     # 0x98e3e
leaq 0x6242e(%rip), %rcx     # 0x98ef9
movl $0x24b, %edx            # imm = 0x24B
callq 0x42f0
leaq 0x62442(%rip), %rdi     # 0x98f1e
leaq 0x6235b(%rip), %rsi     # 0x98e3e
leaq 0x6246d(%rip), %rcx     # 0x98f57
movl $0x85, %edx
callq 0x42f0