// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x2550
testl %eax, %eax
jne 0x48b8
leaq 0x30(%rbx), %r14
leaq 0x70(%rbx), %r15
leaq 0x91a9(%rip), %r12      # 0xd8c0
cmpl $0x0, 0x60(%rbx)
je 0x4723
cmpl $0x0, 0x64(%rbx)
je 0x4741
cmpl $0x0, 0x68(%rbx)
jne 0x481e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2170
testl %eax, %eax
je 0x4717
jmp 0x483c
cmpl $0x0, 0x68(%rbx)
jne 0x481e
movl 0x88(%rbx), %esi
cmpl $0x2, %esi
je 0x47b6
cmpl $0x1, %esi
je 0x478c
testl %esi, %esi
jne 0x47d2
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
movq 0xa0(%rbx), %rdx
callq 0x4ed8
cltq
movq %rax, 0x98(%rbx)
cmpl $-0x1, %eax
jne 0x47e7
jmp 0x4878
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
movq 0x98(%rbx), %rdx
movq 0xa0(%rbx), %rcx
callq 0x503d
testl %eax, %eax
je 0x47e7
jmp 0x488b
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
callq 0x5247
testl %eax, %eax
je 0x47e7
jmp 0x489e
movq %r12, %rdi
xorl %eax, %eax
callq 0xadf7
callq 0x20a0
movl $0x0, (%rax)
movl $0x1, 0x64(%rbx)
movl 0x78(%rbx), %edi
movl $0x8, %edx
movq %r15, %rsi
callq 0x9b94
cmpq $0x7, %rax
jg 0x4717
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x4869
leaq 0x90b6(%rip), %rdi      # 0xd8cf
jmp 0x48af
movq %rbx, %rdi
callq 0x2370
testl %eax, %eax
jne 0x48c8
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl %eax, %edi
callq 0x2520
leaq 0x901b(%rip), %rdi      # 0xd865
movq %rax, %rsi
xorl %eax, %eax
callq 0xadf7
callq 0x20a0
movl $0x0, (%rax)
movl $0x1, %edi
callq 0x24c0
movq %rax, %rbx
leaq 0x905c(%rip), %rdi      # 0xd8cf
jmp 0x48fd
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x48db
leaq 0x8ff2(%rip), %rdi      # 0xd87b
jmp 0x48af
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x48e7
leaq 0x8ff5(%rip), %rdi      # 0xd891
jmp 0x48af
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x48f3
leaq 0x8ff9(%rip), %rdi      # 0xd8a8
xorl %eax, %eax
callq 0xadf7
jmp 0x485f
movl %eax, %edi
callq 0x2520
leaq 0x8e00(%rip), %rdi      # 0xd6c6
jmp 0x484a
movl %eax, %edi
callq 0x2520
leaq 0x8e30(%rip), %rdi      # 0xd706
jmp 0x484a
movq %rax, %rbx
leaq 0x8f96(%rip), %rdi      # 0xd87b
jmp 0x48fd
movq %rax, %rbx
leaq 0x8fa0(%rip), %rdi      # 0xd891
jmp 0x48fd
movq %rax, %rbx
leaq 0x8fab(%rip), %rdi      # 0xd8a8
xorl %eax, %eax
callq 0xacd8
movl $0x0, (%rbx)
jmp 0x485f