// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq (%rdi), %r13
leaq 0x38(%r13), %rbx
movq %rbx, %rdi
callq 0x4750
cmpl $0x0, 0x8(%r13)
je 0x37b73
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 0x18(%r14), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x10(%r13), %rax
movq %rax, %r12
testq %rax, %rax
je 0x37bac
cmpq $0x0, 0x128(%rax)
je 0x37eee
cmpq $0x0, 0x120(%rax)
movq %rax, %r12
je 0x37eee
testq %r12, %r12
je 0x37bdc
cmpq $0x0, 0x8(%r12)
je 0x37bd2
movl 0x28(%r12), %ecx
subl 0x48(%r12), %ecx
cmpl 0x4c(%r12), %ecx
jle 0x37bd2
cmpl $0x0, 0x50(%r12)
je 0x37bed
movq 0x120(%r12), %r12
jmp 0x37bdf
xorl %r12d, %r12d
movb $0x1, %cl
testq %r12, %r12
je 0x37bef
cmpq %rax, %r12
jne 0x37bac
jmp 0x37bef
xorl %ecx, %ecx
testb %cl, %cl
je 0x37c67
incl (%r13)
movl 0x30(%r13), %eax
cmpl $-0x1, %eax
je 0x37c06
cmpl 0x8(%r14), %eax
jle 0x37c0e
movl 0x8(%r14), %eax
movl %eax, 0x30(%r13)
movq 0x28(%r13), %rax
movslq 0x8(%r14), %rcx
movl $0x1, (%rax,%rcx,4)
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x47c0
movq 0x28(%r13), %rax
movslq 0x8(%r14), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0xffffffff, 0x30(%r13) # imm = 0xFFFFFFFF
movslq 0x18(%r13), %rcx
testq %rcx, %rcx
jle 0x37e24
xorl %edx, %edx
cmpl $0x0, (%rax,%rdx,4)
jne 0x37e20
incq %rdx
cmpq %rdx, %rcx
jne 0x37c50
jmp 0x37e24
incl 0x5c(%r12)
movq 0x8(%r12), %r15
testq %r15, %r15
je 0x37dfc
leaq 0x90(%r12), %rax
movq %rax, 0x20(%rsp)
leaq 0xc0(%r12), %rax
movq %rax, 0x18(%rsp)
movl 0x28(%r12), %eax
movl 0x4c(%r12), %ecx
movl %eax, %edx
subl 0x48(%r12), %edx
cmpl %ecx, %edx
jle 0x37dfc
cmpl $0x0, 0x8(%r13)
jne 0x37e3b
cmpl $0x0, 0x50(%r12)
jne 0x37dfc
cmpq %r13, 0x28(%r15)
jne 0x37ecf
movq 0x20(%r15), %rdx
movq %rdx, 0x8(%r12)
testq %rdx, %rdx
jne 0x37ce5
movq $0x0, 0x10(%r12)
incl %ecx
movl %ecx, 0x4c(%r12)
movl 0x44(%r12), %ecx
leal -0x1(%rcx), %edx
movl %edx, 0x44(%r12)
cmpl %eax, %ecx
jl 0x37d07
movq 0x20(%rsp), %rdi
callq 0x4170
cmpl $0x0, 0x44(%r12)
jne 0x37d19
movq 0x18(%rsp), %rdi
callq 0x4040
decl 0x4(%r13)
movq %rbx, %rdi
callq 0x4340
movq 0x8(%r15), %rdi
callq *(%r15)
movq %rax, %rbp
movq 0x30(%r15), %r14
movq (%r14), %rdi
addq $0x38, %rdi
callq 0x4750
decl 0x4c(%r14)
jne 0x37d51
leaq 0xf0(%r14), %rdi
callq 0x4040
cmpl $0x0, 0x54(%r14)
jne 0x37dd2
movl $0x20, %edi
callq 0x4680
testq %rax, %rax
je 0x37e42
movq $0x0, (%rax)
movq %rbp, 0x18(%rax)
movq 0x18(%r15), %rcx
movq %rcx, 0x8(%rax)
movq 0x38(%r15), %rcx
movq %rcx, 0x10(%rax)
incl 0x48(%r14)
movq 0x20(%r14), %rsi
testq %rsi, %rsi
je 0x37d9c
leaq 0x20(%r14), %rdx
movq %rax, (%rsi)
jmp 0x37da4
movq %rax, 0x20(%r14)
leaq 0x18(%r14), %rdx
movq %rax, (%rdx)
movq 0x30(%r14), %rax
cmpq %rax, %rcx
setae %dl
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
sete %sil
orb %dl, %sil
je 0x37f0d
cmpq %rax, %rcx
jne 0x37dd2
leaq 0x60(%r14), %rdi
callq 0x4170
movq (%r14), %rdi
addq $0x38, %rdi
callq 0x4340
movq %r15, %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x4750
movq 0x8(%r12), %r15
testq %r15, %r15
jne 0x37c94
decl 0x5c(%r12)
je 0x37e2f
movq 0x10(%r13), %rcx
xorl %eax, %eax
testq %rcx, %rcx
movq 0x8(%rsp), %r14
je 0x37e60
movq 0x120(%rcx), %rcx
movq %rcx, 0x10(%r13)
jmp 0x37e60
movl %edx, 0x30(%r13)
decl (%r13)
movl $0x2, %eax
jmp 0x37e60
movq %r12, %rdi
callq 0x37316
xorl %eax, %eax
jmp 0x37e5b
movl $0xb, %eax
jmp 0x37e5b
movq (%r14), %rdi
addq $0x38, %rdi
callq 0x4340
movq %r14, %rdi
callq 0x374c6
movl $0xe, %eax
movq 0x8(%rsp), %r14
cmpl $0x1, %eax
jle 0x37e6c
cmpl $0x2, %eax
je 0x37e74
jmp 0x37e84
testl %eax, %eax
jne 0x37b62
cmpl $0x0, 0x8(%r13)
je 0x37b81
jmp 0x37b5a
cmpl $0xb, %eax
je 0x37b5a
cmpl $0xe, %eax
jne 0x37b62
movq %rbx, %rdi
callq 0x4750
movq 0x10(%r13), %r15
testq %r15, %r15
je 0x37b5a
movq %r15, %r14
movq %r14, %rdi
callq 0x374eb
movl $0x2, 0x50(%r14)
movq 0x120(%r14), %r14
cmpq %r15, %r14
jne 0x37eae
jmp 0x37b5a
leaq 0x6402e(%rip), %rdi     # 0x9bf04
leaq 0x63f51(%rip), %rsi     # 0x9be2e
leaq 0x64005(%rip), %rcx     # 0x9bee9
movl $0x24b, %edx            # imm = 0x24B
callq 0x42f0
leaq 0x63fbd(%rip), %rdi     # 0x9beb2
leaq 0x63f32(%rip), %rsi     # 0x9be2e
leaq 0x63fe6(%rip), %rcx     # 0x9bee9
movl $0x20f, %edx            # imm = 0x20F
callq 0x42f0
leaq 0x63ffa(%rip), %rdi     # 0x9bf0e
leaq 0x63f13(%rip), %rsi     # 0x9be2e
leaq 0x64025(%rip), %rcx     # 0x9bf47
movl $0x85, %edx
callq 0x42f0