// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/lbs/worker.c
// Function: workthread
static void *
workthread(void * cookie)
{
	struct workctl * ctl = cookie;
	int rc;

	/* Grab the mutex. */
	if ((rc = pthread_mutex_lock(&ctl->mtx)) != 0) {
		warn0("pthread_mutex_lock: %s", strerror(rc));
		exit(1);
	}

	/* Infinite loop doing work until told to suicide. */
	do {
		/*
		 * Sleep until we have work which we haven't done yet or
		 * we need to kill ourself.
		 */
		while (((ctl->haswork == 0) || (ctl->workdone != 0)) &&
		    (ctl->suicide == 0)) {
			/* Sleep until we're woken up. */
			if ((rc = pthread_cond_wait(&ctl->cv,
			    &ctl->mtx)) != 0) {
				warn0("pthread_cond_wait: %s", strerror(rc));
				exit(1);
			}
		}

		/* If we need to kill ourself, stop looping. */
		if (ctl->suicide)
			break;

		/* Do the work. */
		switch (ctl->op) {
		case 0:	/* Read */
			if ((ctl->nblks = (size_t)storage_read(ctl->sstate,
			    ctl->blkno, ctl->buf)) == (size_t)(-1)) {
				warnp("Failure reading block");
				exit(1);
			}
			break;
		case 1:	/* Write */
			if (storage_write(ctl->sstate,
			    ctl->blkno, ctl->nblks, ctl->buf)) {
				warnp("Failure writing blocks");
				exit(1);
			}
			break;
		case 2:	/* Delete */
			if (storage_delete(ctl->sstate, ctl->blkno)) {
				warnp("Failure deleting blocks");
				exit(1);
			}
			break;
		default:
			warn0("Invalid op: %d", ctl->op);
		}

		/* We've done the work; notify the master thread. */
		ctl->workdone = 1;
		if (noeintr_write(ctl->wakeupsock, &ctl->ID, sizeof(size_t)) <
		    (ssize_t)sizeof(size_t)) {
			warnp("Error writing to wakeup socket");
			exit(1);
		}
	} while (1);

	/* Release the mutex and die. */
	if ((rc = pthread_mutex_unlock(&ctl->mtx)) != 0) {
		warn0("pthread_mutex_unlock: %s", strerror(rc));
		exit(1);
	}
	return (NULL);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
callq 0x2550
testl %eax, %eax
jne 0x4230
leaq 0x30(%rbx), %r14
leaq 0x70(%rbx), %r15
pushq $0x8
popq %r12
leaq 0x8861(%rip), %r13      # 0xc8fc
cmpl $0x0, 0x60(%rbx)
je 0x40a7
cmpl $0x0, 0x64(%rbx)
je 0x40c5
cmpl $0x0, 0x68(%rbx)
jne 0x419d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2170
testl %eax, %eax
je 0x409b
jmp 0x41b9
cmpl $0x0, 0x68(%rbx)
jne 0x419d
movl 0x88(%rbx), %esi
cmpl $0x2, %esi
je 0x413a
cmpl $0x1, %esi
je 0x4110
testl %esi, %esi
jne 0x4156
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
movq 0xa0(%rbx), %rdx
callq 0x480a
cltq
movq %rax, 0x98(%rbx)
cmpl $-0x1, %eax
jne 0x4168
jmp 0x41f0
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
movq 0x98(%rbx), %rdx
movq 0xa0(%rbx), %rcx
callq 0x495f
testl %eax, %eax
je 0x4168
jmp 0x4203
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
callq 0x4b62
testl %eax, %eax
je 0x4168
jmp 0x4216
movq %r13, %rdi
xorl %eax, %eax
callq 0x9ec8
callq 0x20a0
andl $0x0, (%rax)
movl $0x1, 0x64(%rbx)
movl 0x78(%rbx), %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x8d94
cmpq $0x7, %rax
jg 0x409b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x41e1
leaq 0x8773(%rip), %rdi      # 0xc90b
jmp 0x4227
movq %rbx, %rdi
callq 0x2370
testl %eax, %eax
jne 0x4240
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl %eax, %edi
callq 0x2520
leaq 0x86da(%rip), %rdi      # 0xc8a1
movq %rax, %rsi
xorl %eax, %eax
callq 0x9ec8
callq 0x20a0
andl $0x0, (%rax)
pushq $0x1
popq %rdi
callq 0x24c0
movq %rax, %rbx
leaq 0x8720(%rip), %rdi      # 0xc90b
jmp 0x4275
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x4253
leaq 0x86b6(%rip), %rdi      # 0xc8b7
jmp 0x4227
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x425f
leaq 0x86b9(%rip), %rdi      # 0xc8cd
jmp 0x4227
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x426b
leaq 0x86bd(%rip), %rdi      # 0xc8e4
xorl %eax, %eax
callq 0x9ec8
jmp 0x41d9
movl %eax, %edi
callq 0x2520
leaq 0x84c4(%rip), %rdi      # 0xc702
jmp 0x41c7
movl %eax, %edi
callq 0x2520
leaq 0x84f4(%rip), %rdi      # 0xc742
jmp 0x41c7
movq %rax, %rbx
leaq 0x865a(%rip), %rdi      # 0xc8b7
jmp 0x4275
movq %rax, %rbx
leaq 0x8664(%rip), %rdi      # 0xc8cd
jmp 0x4275
movq %rax, %rbx
leaq 0x866f(%rip), %rdi      # 0xc8e4
xorl %eax, %eax
callq 0x9d39
andl $0x0, (%rbx)
jmp 0x41d9