// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/htslib/cram/thread_pool.c
// Function: t_pool_worker
static void *t_pool_worker(void *arg) {
    t_pool_worker_t *w = (t_pool_worker_t *)arg;
    t_pool *p = w->p;
    t_pool_job *j;
#ifdef DEBUG_TIME
    struct timeval t1, t2, t3;
#endif

    for (;;) {
	// Pop an item off the pool queue
#ifdef DEBUG_TIME
	gettimeofday(&t1, NULL);
#endif

	pthread_mutex_lock(&p->pool_m);

#ifdef DEBUG_TIME
	gettimeofday(&t2, NULL);
	p->wait_time += TDIFF(t2,t1);
	w->wait_time += TDIFF(t2,t1);
#endif

	// If there is something on the job list and a higher priority
	// thread waiting, let it handle this instead.
//	while (p->head && p->t_stack_top != -1 && p->t_stack_top < w->idx) {
//	    pthread_mutex_unlock(&p->pool_m);
//	    pthread_cond_signal(&p->t[p->t_stack_top].pending_c);
//	    pthread_mutex_lock(&p->pool_m);
//	}

	while (!p->head && !p->shutdown) {
	    p->nwaiting++;

	    if (p->njobs == 0)
		pthread_cond_signal(&p->empty_c);
#ifdef DEBUG_TIME
	    gettimeofday(&t2, NULL);
#endif

#ifdef IN_ORDER
	    // Push this thread to the top of the waiting stack
	    if (p->t_stack_top == -1 || p->t_stack_top > w->idx)
		p->t_stack_top = w->idx;

	    p->t_stack[w->idx] = 1;
	    pthread_cond_wait(&w->pending_c, &p->pool_m);
	    p->t_stack[w->idx] = 0;

	    /* Find new t_stack_top */
	    {
		int i;
		p->t_stack_top = -1;
		for (i = 0; i < p->tsize; i++) {
		    if (p->t_stack[i]) {
			p->t_stack_top = i;
			break;
		    }
		}
	    }
#else
	    pthread_cond_wait(&p->pending_c, &p->pool_m);
#endif

#ifdef DEBUG_TIME
	    gettimeofday(&t3, NULL);
	    p->wait_time += TDIFF(t3,t2);
	    w->wait_time += TDIFF(t3,t2);
#endif
	    p->nwaiting--;
	}

	if (p->shutdown) {
#ifdef DEBUG_TIME
	    p->total_time += TDIFF(t3,t1);
#endif
#ifdef DEBUG
	    fprintf(stderr, "%d: Shutting down\n", worker_id(p));
#endif
	    pthread_mutex_unlock(&p->pool_m);
	    pthread_exit(NULL);
	}

	j = p->head;
	if (!(p->head = j->next))
	    p->tail = NULL;

	if (p->njobs-- >= p->qsize)
	    pthread_cond_signal(&p->full_c);

	if (p->njobs == 0)
	    pthread_cond_signal(&p->empty_c);

	pthread_mutex_unlock(&p->pool_m);
	    
	// We have job 'j' - now execute it.
	t_pool_add_result(j, j->func(j->arg));	
#ifdef DEBUG_TIME
	pthread_mutex_lock(&p->pool_m);
	gettimeofday(&t3, NULL);
	p->total_time += TDIFF(t3,t1);
	pthread_mutex_unlock(&p->pool_m);
#endif
	memset(j, 0xbb, sizeof(*j));
	free(j);
    }

    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
callq 0x6ae0
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x10(%rcx)
movb %al, -0x25(%rbp)
jne 0x9884c
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0x25(%rbp)
movb -0x25(%rbp), %al
testb $0x1, %al
jne 0x98858
jmp 0x9895a
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x9887c
movq -0x18(%rbp), %rdi
addq $0x58, %rdi
callq 0x6040
movq -0x18(%rbp), %rax
cmpl $-0x1, 0xf0(%rax)
je 0x9889c
movq -0x18(%rbp), %rax
movl 0xf0(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jle 0x988ad
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xf0(%rax)
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movl $0x1, (%rax,%rcx,4)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x18(%rbp), %rsi
addq $0x30, %rsi
callq 0x6b90
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movl $0xffffffff, 0xf0(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x98948
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x9893b
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xf0(%rax)
jmp 0x98948
jmp 0x9893d
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x9890b
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x9882c
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x9897a
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
callq 0x64e0
xorl %eax, %eax
movl %eax, %edi
callq 0x6d50
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x989a8
movq -0x18(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %eax
movl %eax, %edx
addl $-0x1, %edx
movl %edx, 0x4(%rcx)
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x989cf
movq -0x18(%rbp), %rdi
addq $0xb8, %rdi
callq 0x6040
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x989e6
movq -0x18(%rbp), %rdi
addq $0x58, %rdi
callq 0x6040
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
callq 0x64e0
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x99080
movq -0x20(%rbp), %rdi
movl $0xbb, %esi
movl $0x30, %edx
callq 0x63a0
movq -0x20(%rbp), %rdi
callq 0x6880
jmp 0x9881f
nopl (%rax)