// File: readmapping[P]alfalfa/src/kthread.c
// Function: kts_worker
static void *kts_worker(void *data)
{
	kts_worker_t *w = (kts_worker_t*)data;
	for (;;) {
		int i, to_sync, n_slots;
		// update the task and slot information
		pthread_mutex_lock(&w->t->lock);
		while (w->i >= w->t->n_slots && !w->t->to_sync)
			pthread_cond_wait(&w->t->cv, &w->t->lock);
		to_sync = w->t->to_sync, n_slots = w->t->n_slots;
		pthread_mutex_unlock(&w->t->lock);
		// process the pending slot if there is any
		if (w->pending >= 0 && w->pending < n_slots) {
			process_slot(w->t, w->pending, w - w->t->w);
			w->pending = -1;
		}
		// process slots assigned to the current worker
		for (;;) {
			i = __sync_fetch_and_add(&w->i, w->t->n_threads);
			if (i >= n_slots) break;
			process_slot(w->t, i, w - w->t->w);
		}
		// steal slots from other workers
		for (;;) {
			int min = 0x7fffffff, min_i = 0;
			for (i = 0; i < w->t->n_threads; ++i)
				if (min > w->t->w[i].i) min = w->t->w[i].i, min_i = i;
			i = __sync_fetch_and_add(&w->t->w[min_i].i, w->t->n_threads);
			if (i >= n_slots) {
				w->pending = i;
				break;
			} else process_slot(w->t, i, w - w->t->w);
		}
		if (to_sync) break;
	}
	pthread_exit(0);
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq $0x6018, %rdi           # imm = 0x6018
callq 0x85f0
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rdx
xorl %eax, %eax
cmpl 0x14(%rdx), %ecx
movb %al, 0x3(%rsp)
jl 0x78437
movq 0x18(%rsp), %rax
movq (%rax), %rax
cmpl $0x0, 0xc(%rax)
setne %al
xorb $-0x1, %al
movb %al, 0x3(%rsp)
movb 0x3(%rsp), %al
testb $0x1, %al
jne 0x78441
jmp 0x78466
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq $0x6040, %rdi           # imm = 0x6040
movq 0x18(%rsp), %rax
movq (%rax), %rsi
addq $0x6018, %rsi           # imm = 0x6018
callq 0x8650
jmp 0x78407
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq $0x6018, %rdi           # imm = 0x6018
callq 0x82a0
movq 0x18(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jl 0x784eb
movq 0x18(%rsp), %rax
movl 0x18(%rax), %eax
cmpl 0xc(%rsp), %eax
jge 0x784eb
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movl 0x18(%rax), %esi
movq 0x18(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl %eax, %edx
callq 0x78890
movq 0x18(%rsp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
jmp 0x784ed
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movl 0x8(%rax), %eax
lock
xaddl %eax, 0x8(%rcx)
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0xc(%rsp), %eax
jl 0x7850d
jmp 0x78539
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0x14(%rsp), %esi
movq 0x18(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl %eax, %edx
callq 0x78890
jmp 0x784ed
jmp 0x7853b
movl $0x7fffffff, 0x8(%rsp)  # imm = 0x7FFFFFFF
movl $0x0, 0x4(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
cmpl 0x8(%rcx), %eax
jge 0x785b9
movl 0x8(%rsp), %eax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
movslq 0x14(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jle 0x785aa
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movslq 0x14(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, 0x8(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x4(%rsp)
jmp 0x785ac
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x78553
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rcx
movslq 0x4(%rsp), %rdx
shlq $0x5, %rdx
movl 0x8(%rax), %eax
lock
xaddl %eax, 0x8(%rcx,%rdx)
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0xc(%rsp), %eax
jl 0x785f2
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x18(%rax)
jmp 0x78621
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0x14(%rsp), %esi
movq 0x18(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
sarq $0x5, %rax
movl %eax, %edx
callq 0x78890
jmp 0x7853b
cmpl $0x0, 0x10(%rsp)
je 0x7862a
jmp 0x7862f
jmp 0x783f3
xorl %eax, %eax
movl %eax, %edi
callq 0x8740
nopl (%rax,%rax)