// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movq %rdi, 0x8(%rsp)
movq (%r14), %rdi
movl $0x6018, %eax           # imm = 0x6018
addq %rax, %rdi
callq 0x74a0
movq (%r14), %rsi
movl 0x14(%rsi), %r12d
cmpl %r12d, 0x8(%r14)
jl 0x3fc0e
cmpl $0x0, 0xc(%rsi)
jne 0x3fc0e
leaq 0x6040(%rsi), %rdi
addq $0x6018, %rsi           # imm = 0x6018
callq 0x74f0
jmp 0x3fbe6
movl 0xc(%rsi), %eax
movl %eax, 0x14(%rsp)
addq $0x6018, %rsi           # imm = 0x6018
movq %rsi, %rdi
callq 0x7220
movl 0x18(%r14), %ebp
testl %ebp, %ebp
js 0x3fca9
cmpl %r12d, %ebp
jge 0x3fca9
movq (%r14), %r15
subq (%r15), %r14
shrq $0x5, %r14
addq $0x28, %r15
movl %ebp, %eax
movl -0x10(%r15), %r13d
subl %r13d, %eax
jge 0x3fc58
movq (%r15), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq *-0x8(%r15)
movl %ebp, %eax
addq $0x18, %r15
cmpl %r13d, %ebp
movl %eax, %ebp
jge 0x3fc41
movq 0x8(%rsp), %r14
movl $0xffffffff, 0x18(%r14) # imm = 0xFFFFFFFF
jmp 0x3fca9
movq (%r14), %r15
subq (%r15), %r14
shrq $0x5, %r14
addq $0x28, %r15
movl %ebp, %eax
movl -0x10(%r15), %r13d
subl %r13d, %eax
jge 0x3fc99
movq (%r15), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq *-0x8(%r15)
movl %ebp, %eax
addq $0x18, %r15
cmpl %r13d, %ebp
movl %eax, %ebp
jge 0x3fc82
movq 0x8(%rsp), %r14
movq (%r14), %rax
movl 0x8(%rax), %ebp
lock
xaddl %ebp, 0x8(%r14)
cmpl %r12d, %ebp
jl 0x3fc72
movq (%r14), %rax
movslq 0x8(%rax), %r15
testq %r15, %r15
jle 0x3fcf4
movq (%rax), %rcx
addq $0x8, %rcx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx), %r8d
cmpl %r8d, %edx
cmovgel %r8d, %edx
cmovgl %esi, %edi
incq %rsi
addq $0x20, %rcx
cmpq %rsi, %r15
jne 0x3fcd6
movslq %edi, %rcx
jmp 0x3fcf6
xorl %ecx, %ecx
movq (%rax), %rax
shlq $0x5, %rcx
lock
xaddl %r15d, 0x8(%rax,%rcx)
movl %r12d, %ebx
cmpl %r12d, %r15d
jge 0x3fd46
movq (%r14), %r13
subq (%r13), %r14
shrq $0x5, %r14
addq $0x28, %r13
movl %r15d, %ebp
movl %r15d, %eax
movl -0x10(%r13), %r12d
subl %r12d, %eax
jge 0x3fd39
movq (%r13), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq *-0x8(%r13)
movl %ebp, %eax
addq $0x18, %r13
cmpl %r12d, %ebp
movl %eax, %ebp
jge 0x3fd21
jmp 0x3fd4a
movl %r15d, 0x18(%r14)
movl %ebx, %r12d
cmpl %ebx, %r15d
movq 0x8(%rsp), %r14
jl 0x3fcba
cmpl $0x0, 0x14(%rsp)
je 0x3fbd6
xorl %edi, %edi
callq 0x7610