// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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++;
	q->pending++;
	pthread_mutex_unlock(&q->result_m);
    } else {
	j->serial = 0;
    }

    // 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
subq $0x18, %rsp
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %r14
leaq 0x30(%rdi), %rbx
movq %rbx, %rdi
callq 0x47a0
movl 0x4(%r14), %r13d
cmpl $0x1, %ebp
jne 0x4153a
cmpl (%r14), %r13d
jl 0x4153a
movq %rbx, %rdi
callq 0x43f0
callq 0x4070
movl $0xb, (%rax)
jmp 0x415a8
movq %r15, 0x8(%rsp)
movl $0x30, %edi
callq 0x4700
testq %rax, %rax
je 0x415a8
movq %rax, %r15
movq 0x10(%rsp), %rax
movq %rax, (%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r15)
movq $0x0, 0x10(%r15)
movq %r14, 0x18(%r15)
movq %r12, 0x20(%r15)
testq %r12, %r12
je 0x415b2
leaq 0x20(%r12), %r13
movq %r13, %rdi
callq 0x47a0
movl 0x14(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x14(%r12)
movl %eax, 0x28(%r15)
incl 0x1c(%r12)
movq %r13, %rdi
callq 0x43f0
movl 0x4(%r14), %r13d
jmp 0x415ba
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x41635
movl $0x0, 0x28(%r15)
testl %ebp, %ebp
jne 0x415de
cmpl (%r14), %r13d
jl 0x415de
leaq 0xb8(%r14), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x47f0
movl 0x4(%r14), %r13d
cmpl (%r14), %r13d
jge 0x415ca
leal 0x1(%r13), %eax
movl %eax, 0x4(%r14)
movq 0x18(%r14), %rax
testq %rax, %rax
cmoveq %r14, %rax
movq %r15, 0x10(%rax)
movq %r15, 0x18(%r14)
movslq 0xf0(%r14), %rax
testq %rax, %rax
js 0x4162b
movl 0x20(%r14), %ecx
subl 0x8(%r14), %ecx
cmpl %ecx, %r13d
jl 0x4162b
movq 0x28(%r14), %rcx
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %rdi
addq $0x18, %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x43f0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq