// File: /workspace/github_c_makefile_star2_O0/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
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 0x6ae0
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x98c52
cmpl $0x1, -0x2c(%rbp)
jne 0x98c52
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0x64e0
callq 0x6060
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x98dfe
movl $0x30, %edi
callq 0x69c0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x98c72
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x98dfe
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 0x98cde
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x6ae0
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x64e0
jmp 0x98ce9
movq -0x38(%rbp), %rax
movl $0x0, 0x28(%rax)
cmpq $0x0, -0x18(%rbp)
je 0x98d24
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x6ae0
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x64e0
cmpl $0x0, -0x2c(%rbp)
jne 0x98d57
jmp 0x98d2c
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x98d55
movq -0x10(%rbp), %rdi
addq $0xb8, %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
callq 0x6b90
jmp 0x98d2c
jmp 0x98d57
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x98d8d
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 0x98da1
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 0x98dea
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 0x98dea
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 0x6040
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0x64e0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)