// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/htslib/cram/thread_pool.c
// Function: t_pool_dispatch2
int t_pool_dispatch2(t_pool *p, t_results_queue *q,
		     void *(*func)(void *arg), void *arg, int nonblock) {
    t_pool_job *j;

#ifdef DEBUG
    fprintf(stderr, "Dispatching job for queue %p, serial %d\n", q, q->curr_serial);
#endif

    pthread_mutex_lock(&p->pool_m);

    if (p->njobs >= p->qsize && nonblock == 1) {
	pthread_mutex_unlock(&p->pool_m);
	errno = EAGAIN;
	return -1;
    }

    if (!(j = malloc(sizeof(*j))))
	return -1;
    j->func = func;
    j->arg = arg;
    j->next = NULL;
    j->p = p;
    j->q = q;
    if (q) {
	pthread_mutex_lock(&q->result_m);
	j->serial = q->curr_serial;
	pthread_mutex_unlock(&q->result_m);
    } else {
	j->serial = 0;
    }

    if (q) {
	pthread_mutex_lock(&q->result_m);
	q->curr_serial++;
	q->pending++;
	pthread_mutex_unlock(&q->result_m);
    }

    // Check if queue is full
    if (nonblock == 0)
	while (p->njobs >= p->qsize)
	    pthread_cond_wait(&p->full_c, &p->pool_m);

    p->njobs++;
    
//    if (q->curr_serial % 100 == 0)
//	fprintf(stderr, "p->njobs = %d    p->qsize = %d\n", p->njobs, p->qsize);

    if (p->tail) {
	p->tail->next = j;
	p->tail = j;
    } else {
	p->head = p->tail = j;
    }

#ifdef DEBUG
    fprintf(stderr, "Dispatched (serial %d)\n", j->serial);
#endif

    // Let a worker know we have data.
#ifdef IN_ORDER
    // 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 (p->t_stack_top >= 0 && p->njobs > p->tsize - p->nwaiting)
	pthread_cond_signal(&p->t[p->t_stack_top].pending_c);
#else
    pthread_cond_signal(&p->pending_c);
#endif

    pthread_mutex_unlock(&p->pool_m);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r14
leaq 0x30(%rdi), %rbx
movq %rbx, %rdi
callq 0x6ae0
cmpl $0x1, %ebp
jne 0x5ac5c
movl (%r14), %eax
cmpl %eax, 0x4(%r14)
jl 0x5ac5c
movq %rbx, %rdi
callq 0x64f0
callq 0x6060
movl $0xb, (%rax)
jmp 0x5acc7
movq %r15, (%rsp)
pushq $0x30
popq %rdi
callq 0x69d0
testq %rax, %rax
je 0x5acc7
movq %rax, %r15
movq %r13, (%rax)
movq (%rsp), %rax
movq %rax, 0x8(%r15)
andq $0x0, 0x10(%r15)
movq %r14, 0x18(%r15)
movq %r12, 0x20(%r15)
testq %r12, %r12
je 0x5accc
leaq 0x20(%r12), %r13
movq %r13, %rdi
callq 0x6ae0
movl 0x14(%r12), %eax
movl %eax, 0x28(%r15)
movq %r13, %rdi
callq 0x64f0
movq %r13, %rdi
callq 0x6ae0
incl 0x14(%r12)
incl 0x1c(%r12)
movq %r13, %rdi
callq 0x64f0
jmp 0x5acd1
pushq $-0x1
popq %rax
jmp 0x5ad2a
andl $0x0, 0x28(%r15)
testl %ebp, %ebp
je 0x5ad39
movl 0x4(%r14), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x4(%r14)
movq 0x18(%r14), %rcx
testq %rcx, %rcx
cmoveq %r14, %rcx
movq %r15, 0x10(%rcx)
movq %r15, 0x18(%r14)
movslq 0xf0(%r14), %rcx
testq %rcx, %rcx
js 0x5ad20
movl 0x20(%r14), %edx
subl 0x8(%r14), %edx
cmpl %edx, %eax
jl 0x5ad20
movq 0x28(%r14), %rax
imulq $0x50, %rcx, %rcx
leaq (%rax,%rcx), %rdi
addq $0x18, %rdi
callq 0x6040
movq %rbx, %rdi
callq 0x64f0
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb8(%r14), %r12
movl 0x4(%r14), %eax
cmpl (%r14), %eax
jl 0x5acd9
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6b90
jmp 0x5ad40