// File: /workspace/github_c_makefile_star2_O3/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
subq $0x18, %rsp
movl %r8d, %r15d
movq %rcx, 0x10(%rsp)
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r14
leaq 0x30(%rdi), %rbx
movq %rbx, %rdi
callq 0x6ae0
movl 0x4(%r14), %ebp
cmpl $0x1, %r15d
jne 0x658d9
cmpl (%r14), %ebp
jl 0x658d9
movq %rbx, %rdi
callq 0x64f0
callq 0x6060
movl $0xb, (%rax)
jmp 0x6594f
movl %r15d, 0xc(%rsp)
movl $0x30, %edi
callq 0x69d0
testq %rax, %rax
je 0x6594f
movq %rax, %r15
movq %r13, (%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r15)
movq $0x0, 0x10(%r15)
movq %r14, 0x18(%r15)
movq %r12, 0x20(%r15)
testq %r12, %r12
je 0x65959
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
movl 0x4(%r14), %ebp
jmp 0x65961
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x659dd
movl $0x0, 0x28(%r15)
cmpl $0x0, 0xc(%rsp)
jne 0x65988
cmpl (%r14), %ebp
jl 0x65988
leaq 0xb8(%r14), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6b90
movl 0x4(%r14), %ebp
cmpl (%r14), %ebp
jge 0x65974
leal 0x1(%rbp), %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 0x659d3
movl 0x20(%r14), %ecx
subl 0x8(%r14), %ecx
cmpl %ecx, %ebp
jl 0x659d3
movq 0x28(%r14), %rcx
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %rdi
addq $0x18, %rdi
callq 0x6040
movq %rbx, %rdi
callq 0x64f0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq