// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movq (%rdi), %r12
leaq 0x30(%r12), %rbx
leaq 0x58(%r12), %r15
leaq 0x18(%rdi), %r13
leaq 0xb8(%r12), %rax
movq %rax, 0x10(%rsp)
movq %r15, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq %rbx, %rdi
callq 0x6ae0
movq 0x10(%r12), %rbp
testq %rbp, %rbp
jne 0x6567d
cmpl $0x0, 0xc(%r12)
jne 0x65775
incl 0x8(%r12)
cmpl $0x0, 0x4(%r12)
jne 0x655ee
movq %r15, %rdi
callq 0x6040
movl 0xf0(%r12), %ecx
cmpl $-0x1, %ecx
sete %dl
movl 0x8(%r14), %eax
cmpl %eax, %ecx
setg %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0x65614
movl %eax, 0xf0(%r12)
cltq
movq 0xe8(%r12), %rcx
movl $0x1, (%rcx,%rax,4)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6b90
movq 0xe8(%r12), %rax
movslq 0x8(%r14), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0xffffffff, 0xf0(%r12) # imm = 0xFFFFFFFF
movslq 0x20(%r12), %rcx
testq %rcx, %rcx
jle 0x65673
xorl %edx, %edx
cmpl $0x0, (%rax,%rdx,4)
jne 0x6566b
incq %rdx
cmpq %rdx, %rcx
jne 0x6565b
jmp 0x65673
movl %edx, 0xf0(%r12)
decl 0x8(%r12)
jmp 0x655bf
cmpl $0x0, 0xc(%r12)
jne 0x65775
movq 0x10(%rbp), %rax
movq %rax, 0x10(%r12)
testq %rax, %rax
jne 0x656a0
movq $0x0, 0x18(%r12)
movl 0x4(%r12), %ecx
leal -0x1(%rcx), %eax
movl %eax, 0x4(%r12)
cmpl (%r12), %ecx
jl 0x656c2
movq 0x10(%rsp), %rdi
callq 0x6040
movl 0x4(%r12), %eax
testl %eax, %eax
jne 0x656ce
movq %r15, %rdi
callq 0x6040
movq %rbx, %rdi
callq 0x64f0
movq 0x8(%rbp), %rdi
callq *(%rbp)
movq 0x20(%rbp), %r13
testq %r13, %r13
je 0x6575e
movl $0x18, %edi
movq %rax, 0x8(%rsp)
callq 0x69d0
movq %rax, %r15
movq 0x8(%rsp), %rax
testq %r15, %r15
je 0x6575e
movq $0x0, (%r15)
movq %rax, 0x10(%r15)
movl 0x28(%rbp), %eax
movl %eax, 0x8(%r15)
leaq 0x20(%r13), %rdi
movq %rdi, 0x8(%rsp)
callq 0x6ae0
movq 0x8(%r13), %rax
testq %rax, %rax
cmoveq %r13, %rax
movq %r15, (%rax)
movq %r15, 0x8(%r13)
movq 0x18(%r13), %xmm0
paddd 0x3448e(%rip), %xmm0    # 0x99bd0
movq %xmm0, 0x18(%r13)
addq $0x48, %r13
movq %r13, %rdi
callq 0x6040
movq 0x8(%rsp), %rdi
callq 0x64f0
movq %rbp, %rdi
callq 0x68a0
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r13
jmp 0x655b7
movq %rbx, %rdi
callq 0x64f0
xorl %edi, %edi
callq 0x6d50