// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/thread_pool.c
// Function: hts_tpool_dispatch3
int hts_tpool_dispatch3(hts_tpool *p, hts_tpool_process *q,
                        void *(*exec_func)(void *arg), void *arg,
                        void (*job_cleanup)(void *arg),
                        void (*result_cleanup)(void *data),
                        int nonblock) {
    hts_tpool_job *j;

    pthread_mutex_lock(&p->pool_m);

    DBG_OUT(stderr, "Dispatching job for queue %p, serial %"PRId64"\n",
            q, q->curr_serial);

    if ((q->no_more_input || q->n_input >= q->qsize) && nonblock == 1) {
        pthread_mutex_unlock(&p->pool_m);
        errno = EAGAIN;
        return -1;
    }

    if (!(j = malloc(sizeof(*j)))) {
        pthread_mutex_unlock(&p->pool_m);
        return -1;
    }
    j->func = exec_func;
    j->arg = arg;
    j->job_cleanup = job_cleanup;
    j->result_cleanup = result_cleanup;
    j->next = NULL;
    j->p = p;
    j->q = q;
    j->serial = q->curr_serial++;

    if (nonblock == 0) {
        while ((q->no_more_input || q->n_input >= q->qsize) &&
               !q->shutdown && !q->wake_dispatch) {
            pthread_cond_wait(&q->input_not_full_c, &q->p->pool_m);
        }
        if (q->no_more_input || q->shutdown) {
            free(j);
            pthread_mutex_unlock(&p->pool_m);
            return -1;
        }
        if (q->wake_dispatch) {
            //fprintf(stderr, "Wake => non-block for this operation\n");
            q->wake_dispatch = 0;
        }
    }

    p->njobs++;    // total across all queues
    q->n_input++;  // queue specific

    if (q->input_tail) {
        q->input_tail->next = j;
        q->input_tail = j;
    } else {
        q->input_head = q->input_tail = j;
    }

    DBG_OUT(stderr, "Dispatched (serial %"PRId64")\n", j->serial);

    // Let a worker know we have data.
    // Keep incoming queue at 1 per running thread, so there is always
    // something waiting when they end their current task.  If we go above
    // this signal to start more threads (if available). This has the effect
    // of concentrating jobs to fewer cores when we are I/O bound, which in
    // turn benefits systems with auto CPU frequency scaling.
    if (!q->shutdown)
        wake_next_worker(q, 1);

    pthread_mutex_unlock(&p->pool_m);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movl 0x60(%rsp), %r15d
movq %rdi, %r13
leaq 0x38(%rdi), %r12
movq %r12, %rdi
callq 0x4750
movl 0x40(%r14), %ebp
testl %ebp, %ebp
je 0x2c8f8
cmpl $0x1, %r15d
je 0x2c908
jmp 0x2c920
cmpl $0x1, %r15d
jne 0x2c920
movl 0x28(%r14), %eax
cmpl %eax, 0x44(%r14)
jl 0x2c920
movq %r12, %rdi
callq 0x4340
callq 0x4050
movl $0xb, (%rax)
jmp 0x2ca1a
movq %r12, 0x8(%rsp)
pushq $0x40
popq %rdi
callq 0x4680
testq %rax, %rax
je 0x2ca10
movq %rax, %r12
movq %rbx, (%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r12)
movq 0x18(%rsp), %rax
movq %rax, 0x10(%r12)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%r12)
andq $0x0, 0x20(%r12)
movq %r13, 0x28(%r12)
movq %r14, 0x30(%r12)
movq 0x38(%r14), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x38(%r14)
movq %rax, 0x38(%r12)
cmpl $0x0, 0x60(%rsp)
je 0x2c9c1
incl 0x4(%r13)
incl 0x44(%r14)
movq 0x10(%r14), %rax
leaq 0x20(%rax), %rcx
leaq 0x8(%r14), %rdx
testq %rax, %rax
cmoveq %rdx, %rcx
movq %r12, (%rcx)
movq %r12, 0x10(%r14)
cmpl $0x0, 0x50(%r14)
jne 0x2c9b3
movq %r14, %rdi
callq 0x2ca53
movq 0x8(%rsp), %rdi
callq 0x4340
xorl %eax, %eax
jmp 0x2ca1d
leaq 0x90(%r14), %rbx
testl %ebp, %ebp
jne 0x2c9d6
movl 0x44(%r14), %eax
cmpl 0x28(%r14), %eax
jl 0x2ca00
movl 0x50(%r14), %eax
testl %eax, %eax
jne 0x2c9fa
cmpl $0x0, 0x58(%r14)
jne 0x2c9fa
movq (%r14), %rsi
addq $0x38, %rsi
movq %rbx, %rdi
callq 0x47c0
movl 0x40(%r14), %ebp
jmp 0x2c9c8
testl %ebp, %ebp
jne 0x2ca08
jmp 0x2ca04
movl 0x50(%r14), %eax
testl %eax, %eax
je 0x2ca2c
movq %r12, %rdi
callq 0x45c0
movq 0x8(%rsp), %rdi
callq 0x4340
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x58(%r14)
je 0x2c982
andl $0x0, 0x58(%r14)
jmp 0x2c982