// File: /workspace/github_c_makefile_star2_O2/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), %rbx
movq %rbx, %rdi
callq 0x4750
movq %r15, 0x8(%rsp)
leaq 0x18(%r15), %rax
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x8(%r14)
jne 0x2c825
movq 0x10(%r14), %rax
movq %rax, %r13
testq %rax, %rax
je 0x2c588
cmpq $0x0, 0x128(%rax)
je 0x2c83e
cmpq $0x0, 0x120(%rax)
movq %rax, %r13
je 0x2c83e
testq %r13, %r13
je 0x2c5c0
movq 0x8(%r13), %rbp
testq %rbp, %rbp
je 0x2c5af
movl 0x28(%r13), %ecx
subl 0x48(%r13), %ecx
cmpl 0x4c(%r13), %ecx
jle 0x2c5af
cmpl $0x0, 0x50(%r13)
je 0x2c641
movq 0x120(%r13), %r13
testq %r13, %r13
je 0x2c5c0
cmpq %rax, %r13
jne 0x2c588
incl (%r14)
movl 0x30(%r14), %ecx
cmpl $-0x1, %ecx
sete %dl
movq 0x8(%rsp), %rax
movl 0x8(%rax), %eax
cmpl %eax, %ecx
setg %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0x2c5e5
movl %eax, 0x30(%r14)
cltq
movq 0x28(%r14), %rcx
movl $0x1, (%rcx,%rax,4)
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x47c0
movq 0x28(%r14), %rax
movq 0x8(%rsp), %rcx
movslq 0x8(%rcx), %rcx
andl $0x0, (%rax,%rcx,4)
orl $-0x1, 0x30(%r14)
movl 0x18(%r14), %ecx
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2c639
cmpl $0x0, (%rax,%rdx,4)
jne 0x2c635
incq %rdx
jmp 0x2c625
movl %edx, 0x30(%r14)
decl (%r14)
jmp 0x2c552
incl 0x5c(%r13)
leaq 0x90(%r13), %rax
movq %rax, 0x18(%rsp)
leaq 0xc0(%r13), %rax
movq %rax, 0x10(%rsp)
testq %rbp, %rbp
je 0x2c7b1
movl 0x28(%r13), %eax
movl 0x4c(%r13), %ecx
movl %eax, %edx
subl 0x48(%r13), %edx
cmpl %ecx, %edx
jle 0x2c7b1
cmpl $0x0, 0x8(%r14)
jne 0x2c825
cmpl $0x0, 0x50(%r13)
jne 0x2c7b1
cmpq %r14, 0x28(%rbp)
jne 0x2c85d
movq 0x20(%rbp), %rdx
movq %rdx, 0x8(%r13)
testq %rdx, %rdx
jne 0x2c6ae
andq $0x0, 0x10(%r13)
incl %ecx
movl %ecx, 0x4c(%r13)
movl 0x44(%r13), %edx
leal -0x1(%rdx), %ecx
movl %ecx, 0x44(%r13)
cmpl %eax, %edx
jl 0x2c6d1
movq 0x18(%rsp), %rdi
callq 0x4170
movl 0x44(%r13), %ecx
testl %ecx, %ecx
jne 0x2c6df
movq 0x10(%rsp), %rdi
callq 0x4040
decl 0x4(%r14)
movq %rbx, %rdi
callq 0x4340
movq 0x8(%rbp), %rdi
callq *(%rbp)
movq %rax, %r12
movq 0x30(%rbp), %r15
movq (%r15), %rdi
addq $0x38, %rdi
callq 0x4750
decl 0x4c(%r15)
jne 0x2c717
leaq 0xf0(%r15), %rdi
callq 0x4040
cmpl $0x0, 0x54(%r15)
jne 0x2c78c
pushq $0x20
popq %rdi
callq 0x4680
testq %rax, %rax
je 0x2c7e1
andq $0x0, (%rax)
movq %r12, 0x18(%rax)
movq 0x18(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq 0x38(%rbp), %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 0x2c87c
cmpq %rax, %rcx
jne 0x2c78c
leaq 0x60(%r15), %rdi
callq 0x4170
movq (%r15), %rdi
addq $0x38, %rdi
callq 0x4340
movq %rbp, %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x4750
movq 0x8(%r13), %rbp
jmp 0x2c65d
decl 0x5c(%r13)
je 0x2c7d4
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x2c552
movq 0x120(%rax), %rax
movq %rax, 0x10(%r14)
jmp 0x2c552
movq %r13, %rdi
callq 0x2bd67
jmp 0x2c552
movq (%r15), %rdi
addq $0x38, %rdi
callq 0x4340
movq %r15, %rdi
callq 0x2bf17
movq %rbx, %rdi
callq 0x4750
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x2c825
movq %r15, %r14
movq %r14, %rdi
callq 0x2bf3c
movl $0x2, 0x50(%r14)
movq 0x120(%r14), %r14
cmpq %r15, %r14
jne 0x2c809
movq %rbx, %rdi
callq 0x4340
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4e5dd(%rip), %rdi     # 0x7ae22
leaq 0x4e552(%rip), %rsi     # 0x7ad9e
leaq 0x4e606(%rip), %rcx     # 0x7ae59
movl $0x20f, %edx            # imm = 0x20F
callq 0x42f0
leaq 0x4e610(%rip), %rdi     # 0x7ae74
leaq 0x4e533(%rip), %rsi     # 0x7ad9e
leaq 0x4e5e7(%rip), %rcx     # 0x7ae59
movl $0x24b, %edx            # imm = 0x24B
callq 0x42f0
leaq 0x4e5fb(%rip), %rdi     # 0x7ae7e
leaq 0x4e514(%rip), %rsi     # 0x7ad9e
leaq 0x4e626(%rip), %rcx     # 0x7aeb7
movl $0x85, %edx
callq 0x42f0