// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
pushq %rax
movq %rdi, %rbx
movl $0x6018, %r14d          # imm = 0x6018
movq (%rbx), %rdi
addq %r14, %rdi
callq 0x74a0
movq (%rbx), %rsi
movl 0x14(%rsi), %ebp
cmpl %ebp, 0x8(%rbx)
jl 0x3fb12
cmpl $0x0, 0xc(%rsi)
jne 0x3fb12
leaq 0x6040(%rsi), %rdi
addq $0x6018, %rsi           # imm = 0x6018
callq 0x74f0
jmp 0x3faec
movl 0xc(%rsi), %r15d
addq $0x6018, %rsi           # imm = 0x6018
movq %rsi, %rdi
callq 0x7220
movl 0x18(%rbx), %esi
testl %esi, %esi
js 0x3fb91
cmpl %ebp, %esi
jge 0x3fb91
movq (%rbx), %rcx
leaq 0x18(%rcx), %rax
movl 0x18(%rcx), %edx
cmpl %esi, %edx
jg 0x3fb4b
subl %edx, %esi
movl 0x18(%rax), %edx
addq $0x18, %rax
cmpl %edx, %esi
jge 0x3fb3e
movq %rbx, %rdx
subq (%rcx), %rdx
shrq $0x5, %rdx
movq 0x10(%rax), %rdi
callq *0x8(%rax)
movl $0xffffffff, 0x18(%rbx) # imm = 0xFFFFFFFF
jmp 0x3fb91
movq (%rbx), %rcx
leaq 0x18(%rcx), %rax
movl 0x18(%rcx), %edx
cmpl %esi, %edx
jg 0x3fb80
subl %edx, %esi
movl 0x18(%rax), %edx
addq $0x18, %rax
cmpl %edx, %esi
jge 0x3fb73
movq %rbx, %rdx
subq (%rcx), %rdx
shrq $0x5, %rdx
movq 0x10(%rax), %rdi
callq *0x8(%rax)
movq (%rbx), %rax
movl 0x8(%rax), %esi
lock
xaddl %esi, 0x8(%rbx)
cmpl %ebp, %esi
jl 0x3fb65
jmp 0x3fbce
movq (%rbx), %rcx
leaq 0x18(%rcx), %rax
movl 0x18(%rcx), %edx
cmpl %esi, %edx
jg 0x3fbbd
subl %edx, %esi
movl 0x18(%rax), %edx
addq $0x18, %rax
cmpl %edx, %esi
jge 0x3fbb0
movq %rbx, %rdx
subq (%rcx), %rdx
shrq $0x5, %rdx
movq 0x10(%rax), %rdi
callq *0x8(%rax)
movq (%rbx), %rax
movslq 0x8(%rax), %rsi
movq (%rax), %rax
testq %rsi, %rsi
jle 0x3fc0a
leaq 0x8(%rax), %rcx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rcx), %r9d
cmpl %r9d, %edx
cmovgel %r9d, %edx
cmovgl %edi, %r8d
incq %rdi
addq $0x20, %rcx
cmpq %rdi, %rsi
jne 0x3fbeb
movslq %r8d, %rcx
jmp 0x3fc0c
xorl %ecx, %ecx
shlq $0x5, %rcx
lock
xaddl %esi, 0x8(%rax,%rcx)
cmpl %ebp, %esi
jl 0x3fba2
movl %esi, 0x18(%rbx)
testl %r15d, %r15d
je 0x3fae1
xorl %edi, %edi
callq 0x7610