// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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), %r15
leaq 0x30(%r15), %rbx
leaq 0x58(%r15), %r13
leaq 0x18(%rdi), %r12
leaq 0xb8(%r15), %rax
movq %rax, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movq %rbx, %rdi
callq 0x47a0
movq 0x10(%r15), %rbp
movl 0xc(%r15), %eax
testq %rbp, %rbp
jne 0x34cea
testl %eax, %eax
jne 0x34dd4
incl 0x8(%r15)
cmpl $0x0, 0x4(%r15)
jne 0x34c64
movq %r13, %rdi
callq 0x4040
movl 0xf0(%r15), %ecx
cmpl $-0x1, %ecx
sete %dl
movl 0x8(%r14), %eax
cmpl %eax, %ecx
setg %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0x34c88
movl %eax, 0xf0(%r15)
cltq
movq 0xe8(%r15), %rcx
movl $0x1, (%rcx,%rax,4)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x47f0
movq 0xe8(%r15), %rax
movslq 0x8(%r14), %rcx
andl $0x0, (%rax,%rcx,4)
orl $-0x1, 0xf0(%r15)
movl 0x20(%r15), %ecx
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x34ce1
cmpl $0x0, (%rax,%rdx,4)
jne 0x34cda
incq %rdx
jmp 0x34cca
movl %edx, 0xf0(%r15)
decl 0x8(%r15)
jmp 0x34c38
testl %eax, %eax
jne 0x34dd4
movq 0x10(%rbp), %rax
movq %rax, 0x10(%r15)
testq %rax, %rax
jne 0x34d04
andq $0x0, 0x18(%r15)
movl 0x4(%r15), %ecx
leal -0x1(%rcx), %eax
movl %eax, 0x4(%r15)
cmpl (%r15), %ecx
jl 0x34d22
movq 0x10(%rsp), %rdi
callq 0x4040
movl 0x4(%r15), %eax
testl %eax, %eax
jne 0x34d2e
movq %r13, %rdi
callq 0x4040
movq %rbx, %rdi
callq 0x43f0
movq 0x8(%rbp), %rdi
callq *(%rbp)
movq 0x20(%rbp), %r13
testq %r13, %r13
je 0x34dc2
pushq $0x18
popq %rdi
movq %rax, 0x8(%rsp)
callq 0x46f0
movq 0x8(%rsp), %rcx
testq %rax, %rax
je 0x34dc2
andq $0x0, (%rax)
movq %rcx, 0x10(%rax)
movq %rax, %rcx
movq %rax, 0x18(%rsp)
movl 0x28(%rbp), %eax
movl %eax, 0x8(%rcx)
leaq 0x20(%r13), %rdi
movq %rdi, 0x8(%rsp)
callq 0x47a0
movq 0x8(%r13), %rax
testq %rax, %rax
cmoveq %r13, %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movq %rcx, 0x8(%r13)
movq 0x18(%r13), %xmm0
paddd 0x11a8a(%rip), %xmm0    # 0x46830
movq %xmm0, 0x18(%r13)
addq $0x48, %r13
movq %r13, %rdi
callq 0x4040
movq 0x8(%rsp), %rdi
callq 0x43f0
movq %rbp, %rdi
callq 0x4600
movq 0x20(%rsp), %r13
jmp 0x34c30
movq %rbx, %rdi
callq 0x43f0
xorl %edi, %edi
callq 0x4960