// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x4730
jmp 0x4e312
movq -0x18(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x4e32c
movq -0x18(%rbp), %rax
movl 0x44(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jl 0x4e356
cmpl $0x1, 0x10(%rbp)
jne 0x4e356
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
callq 0x4050
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4e535
movl $0x40, %edi
callq 0x4650
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x4e383
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4e535
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x38(%rax)
movq -0x40(%rbp), %rax
movq %rcx, 0x38(%rax)
cmpl $0x0, 0x10(%rbp)
jne 0x4e4ae
jmp 0x4e3fd
movq -0x18(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x4e41c
movq -0x18(%rbp), %rax
movl 0x44(%rax), %ecx
movq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpl 0x28(%rdx), %ecx
movb %al, -0x41(%rbp)
jl 0x4e43b
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x50(%rcx)
movb %al, -0x41(%rbp)
jne 0x4e43b
movq -0x18(%rbp), %rax
cmpl $0x0, 0x58(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x4e444
jmp 0x4e461
movq -0x18(%rbp), %rdi
addq $0x90, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
addq $0x38, %rsi
callq 0x47a0
jmp 0x4e3fd
movq -0x18(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x4e475
movq -0x18(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x4e497
movq -0x40(%rbp), %rdi
callq 0x45a0
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4e535
movq -0x18(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0x4e4ac
movq -0x18(%rbp), %rax
movl $0x0, 0x58(%rax)
jmp 0x4e4ae
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl 0x44(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4e4f1
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x4e505
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x4e507
jmp 0x4e509
movq -0x18(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x4e521
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x4e590
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop