// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0x4880
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x60aa2
cmpl $0x1, -0x2c(%rbp)
jne 0x60aa2
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0x4420
callq 0x4070
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x60c28
movl $0x30, %edi
callq 0x47a0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x60ac2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x60c28
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
cmpq $0x0, -0x18(%rbp)
je 0x60b43
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x4880
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x14(%rax)
movq -0x38(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x4420
jmp 0x60b4e
movq -0x38(%rbp), %rax
movl $0x0, 0x28(%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x60b81
jmp 0x60b56
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x60b7f
movq -0x10(%rbp), %rdi
addq $0xb8, %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
callq 0x48d0
jmp 0x60b56
jmp 0x60b81
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x60bb7
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x60bcb
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xf0(%rax)
jl 0x60c14
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq -0x10(%rbp), %rdx
subl 0x8(%rdx), %ecx
cmpl %ecx, %eax
jle 0x60c14
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0xf0(%rax), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x18, %rdi
callq 0x4040
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0x4420
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)