// File: /workspace/github_c_makefile_star2_O1/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), %rbx
leaq 0x30(%rbx), %r15
leaq 0x58(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x18(%rdi), %r12
leaq 0xb8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %r15, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, %rdi
callq 0x6ae0
movq 0x10(%rbx), %rbp
testq %rbp, %rbp
jne 0x6567c
cmpl $0x0, 0xc(%rbx)
jne 0x6567c
incl 0x8(%rbx)
cmpl $0x0, 0x4(%rbx)
jne 0x655ff
movq 0x8(%rsp), %rdi
callq 0x6040
movl 0xf0(%rbx), %eax
cmpl $-0x1, %eax
je 0x65610
cmpl 0x8(%r14), %eax
jle 0x6561a
movl 0x8(%r14), %eax
movl %eax, 0xf0(%rbx)
movq 0xe8(%rbx), %rax
movslq 0x8(%r14), %rcx
movl $0x1, (%rax,%rcx,4)
movq %r12, %rdi
movq %r15, %rsi
callq 0x6b90
movq 0xe8(%rbx), %rax
movslq 0x8(%r14), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0xffffffff, 0xf0(%rbx) # imm = 0xFFFFFFFF
movslq 0x20(%rbx), %rcx
testq %rcx, %rcx
jle 0x65674
xorl %edx, %edx
cmpl $0x0, (%rax,%rdx,4)
jne 0x6566e
incq %rdx
cmpq %rdx, %rcx
jne 0x6565e
jmp 0x65674
movl %edx, 0xf0(%rbx)
decl 0x8(%rbx)
jmp 0x655d5
cmpl $0x0, 0xc(%rbx)
jne 0x6576f
movq 0x10(%rbp), %rax
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x6569b
movq $0x0, 0x18(%rbx)
movl 0x4(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x4(%rbx)
cmpl (%rbx), %eax
jl 0x656b2
movq 0x10(%rsp), %rdi
callq 0x6040
cmpl $0x0, 0x4(%rbx)
jne 0x656c2
movq 0x8(%rsp), %rdi
callq 0x6040
movq %r15, %rdi
callq 0x64f0
movq 0x8(%rbp), %rdi
callq *(%rbp)
movq 0x20(%rbp), %r13
testq %r13, %r13
je 0x65745
movq %rax, %r12
movl $0x18, %edi
callq 0x69d0
testq %rax, %rax
je 0x65745
movq %rax, %r15
movq $0x0, (%rax)
movq %r12, 0x10(%rax)
movl 0x28(%rbp), %eax
movl %eax, 0x8(%r15)
leaq 0x20(%r13), %r12
movq %r12, %rdi
callq 0x6ae0
leaq 0x8(%r13), %rax
movq 0x8(%r13), %rcx
xorl %edx, %edx
testq %rcx, %rcx
setne %dl
cmovneq %rcx, %rax
movq %r15, (%rax)
movq %r15, (%r13,%rdx,8)
incl 0x18(%r13)
decl 0x1c(%r13)
addq $0x48, %r13
movq %r13, %rdi
callq 0x6040
movq %r12, %rdi
callq 0x64f0
movaps 0x34254(%rip), %xmm0    # 0x999a0
movups %xmm0, 0x20(%rbp)
movups %xmm0, 0x10(%rbp)
movups %xmm0, (%rbp)
movq %rbp, %rdi
callq 0x68a0
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r12
jmp 0x655cd
movq %r15, %rdi
callq 0x64f0
xorl %edi, %edi
callq 0x6d50