// File: /workspace/github_c_makefile_star2_O0/DReichLab[P]adna/kthread.c
// Function: ktp_worker
static void *ktp_worker(void *data)
{
	ktp_worker_t *w = (ktp_worker_t*)data;
	ktp_t *p = w->pl;
	while (w->step < p->n_steps) {
		// test whether we can kick off the job with this worker
		pthread_mutex_lock(&p->mutex);
		for (;;) {
			int i;
			// test whether another worker is doing the same step
			for (i = 0; i < p->n_workers; ++i) {
				if (w == &p->workers[i]) continue; // ignore itself
				if (p->workers[i].running && p->workers[i].step == w->step)
					break;
			}
			if (i == p->n_workers) break; // no other workers doing w->step; then this worker will
			pthread_cond_wait(&p->cv, &p->mutex);
		}
		w->running = 1;
		pthread_mutex_unlock(&p->mutex);

		// working on w->step
		w->data = p->func(p->shared, w->step, w->step? w->data : 0); // for the first step, input is NULL

		// update step and let other workers know
		pthread_mutex_lock(&p->mutex);
		w->step = w->step == p->n_steps - 1 || w->data? (w->step + 1) % p->n_steps : p->n_steps;
		w->running = 0;
		pthread_cond_broadcast(&p->cv);
		pthread_mutex_unlock(&p->mutex);
	}
	pthread_exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %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 -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x2977
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x2280
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x2858
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movslq -0x1c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jne 0x2811
jmp 0x284d
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0xc(%rax)
je 0x284b
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x284b
jmp 0x2858
jmp 0x284d
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x27e7
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x2866
jmp 0x2880
movq -0x18(%rbp), %rdi
addq $0x48, %rdi
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
callq 0x20c0
jmp 0x27e0
movq -0x10(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x21a0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x28d1
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x28d9
xorl %eax, %eax
movq %rax, -0x38(%rbp)
jmp 0x28d9
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %esi
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rdx
callq *%rax
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x2280
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x2922
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2939
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x18(%rbp), %rcx
cltd
idivl 0x14(%rcx)
movl %edx, -0x3c(%rbp)
jmp 0x2943
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x18(%rbp), %rdi
addq $0x48, %rdi
callq 0x2070
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x21a0
jmp 0x27bf
xorl %eax, %eax
movl %eax, %edi
callq 0x21b0