// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
callq 0x4730
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4de62
jmp 0x4e1d2
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4de91
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x128(%rax)
je 0x4de93
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x120(%rax)
je 0x4de93
jmp 0x4deb2
leaq 0xa5628(%rip), %rdi     # 0xf34c2
leaq 0xa559d(%rip), %rsi     # 0xf343e
movl $0x20f, %edx            # imm = 0x20F
leaq 0xa564c(%rip), %rcx     # 0xf34f9
callq 0x42e0
movl $0x0, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x4df09
movq -0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x4df09
movq -0x40(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x40(%rbp), %rcx
subl 0x48(%rcx), %eax
movq -0x40(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jle 0x4df09
movq -0x40(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x4df09
movl $0x1, -0x2c(%rbp)
jmp 0x4df42
cmpq $0x0, -0x40(%rbp)
je 0x4df1f
movq -0x40(%rbp), %rax
movq 0x120(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x4df21
xorl %eax, %eax
cmpq $0x0, -0x40(%rbp)
movb %al, -0x59(%rbp)
je 0x4df3b
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x4decd
cmpl $0x0, -0x2c(%rbp)
jne 0x4e01b
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
cmpl $-0x1, 0x30(%rax)
je 0x4df71
movq -0x20(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jle 0x4df7f
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movl $0x1, (%rax,%rcx,4)
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
movq -0x20(%rbp), %rsi
addq $0x38, %rsi
callq 0x47a0
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x4e00b
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x44(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x4dffe
movl -0x44(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x30(%rax)
jmp 0x4e00b
jmp 0x4e000
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4dfd4
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x4de4c
movq -0x40(%rbp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x8(%rcx)
movb %al, -0x5a(%rbp)
je 0x4e053
movq -0x40(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x40(%rbp), %rcx
subl 0x48(%rcx), %eax
movq -0x40(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
setg %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0x4e05f
jmp 0x4e18c
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x4e06e
jmp 0x4e1d4
movq -0x40(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x4e07d
jmp 0x4e18c
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x4e099
jmp 0x4e0b8
leaq 0xa5474(%rip), %rdi     # 0xf3514
leaq 0xa5397(%rip), %rsi     # 0xf343e
movl $0x24b, %edx            # imm = 0x24B
leaq 0xa5446(%rip), %rcx     # 0xf34f9
callq 0x42e0
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x40(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0x4e0da
movq -0x40(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
movq -0x40(%rbp), %rcx
movl 0x44(%rcx), %eax
movl %eax, %edx
addl $-0x1, %edx
movl %edx, 0x44(%rcx)
movq -0x40(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jl 0x4e10f
movq -0x40(%rbp), %rdi
addq $0x90, %rdi
callq 0x4150
movq -0x40(%rbp), %rax
cmpl $0x0, 0x44(%rax)
jne 0x4e129
movq -0x40(%rbp), %rdi
addq $0xc0, %rdi
callq 0x4040
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
jmp 0x4e145
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x4eb70
cmpl $0x0, %eax
jge 0x4e171
jmp 0x4e1eb
movq -0x28(%rbp), %rdi
callq 0x45a0
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
callq 0x4730
jmp 0x4e028
movq -0x40(%rbp), %rcx
movl 0x5c(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x5c(%rcx)
cmpl $0x0, %eax
jne 0x4e1a9
movq -0x40(%rbp), %rdi
callq 0x4d090
jmp 0x4e1cd
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4e1cb
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x120(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x4e1cd
jmp 0x4de4c
jmp 0x4e1d4
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x4e259
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
callq 0x4730
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x4e244
jmp 0x4e215
movq -0x58(%rbp), %rdi
callq 0x4d370
movq -0x58(%rbp), %rax
movl $0x2, 0x50(%rax)
movq -0x58(%rbp), %rax
movq 0x120(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x4e215
jmp 0x4e244
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)