// File: /workspace/github_c_makefile_star2_O1/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 0x48cd
leaq 0x30(%rbx), %r14
leaq 0x70(%rbx), %r15
leaq 0x9245(%rip), %r12      # 0xd940
cmpl $0x0, 0x60(%rbx)
je 0x4707
cmpl $0x0, 0x64(%rbx)
je 0x470d
cmpl $0x0, 0x68(%rbx)
je 0x475c
cmpl $0x0, 0x68(%rbx)
jne 0x4802
movl 0x88(%rbx), %esi
cmpl $0x2, %esi
je 0x479a
cmpl $0x1, %esi
je 0x4770
testl %esi, %esi
jne 0x47b6
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
movq 0xa0(%rbx), %rdx
callq 0x4eb0
cltq
movq %rax, 0x98(%rbx)
cmpl $-0x1, %eax
jne 0x47cb
jmp 0x4867
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2170
testl %eax, %eax
je 0x46fb
jmp 0x482f
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
movq 0x98(%rbx), %rdx
movq 0xa0(%rbx), %rcx
callq 0x5015
testl %eax, %eax
je 0x47cb
jmp 0x487a
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %rsi
callq 0x5220
testl %eax, %eax
je 0x47cb
jmp 0x4854
movq %r12, %rdi
xorl %eax, %eax
callq 0xad37
callq 0x20a0
movl $0x0, (%rax)
movl $0x1, 0x64(%rbx)
movl 0x78(%rbx), %edi
movl $0x8, %edx
movq %r15, %rsi
callq 0x9ab0
cmpq $0x7, %rax
jg 0x46fb
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x4820
leaq 0x9152(%rip), %rdi      # 0xd94f
jmp 0x488b
movq %rbx, %rdi
callq 0x2370
testl %eax, %eax
jne 0x48e0
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x9125(%rip), %rdi      # 0xd94f
jmp 0x48b6
movl %eax, %edi
callq 0x2520
leaq 0x90a8(%rip), %rdi      # 0xd8e5
movq %rax, %rsi
xorl %eax, %eax
callq 0xad37
callq 0x20a0
movl $0x0, (%rax)
jmp 0x48c3
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x4894
leaq 0x90c3(%rip), %rdi      # 0xd928
jmp 0x488b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x48a0
leaq 0x9083(%rip), %rdi      # 0xd8fb
jmp 0x488b
callq 0x20a0
cmpl $0x0, (%rax)
jne 0x48ac
leaq 0x9086(%rip), %rdi      # 0xd911
xorl %eax, %eax
callq 0xad37
jmp 0x48c3
movq %rax, %rbx
leaq 0x908a(%rip), %rdi      # 0xd928
jmp 0x48b6
movq %rax, %rbx
leaq 0x9051(%rip), %rdi      # 0xd8fb
jmp 0x48b6
movq %rax, %rbx
leaq 0x905b(%rip), %rdi      # 0xd911
xorl %eax, %eax
callq 0xac18
movl $0x0, (%rbx)
movl $0x1, %edi
callq 0x24c0
movl %eax, %edi
callq 0x2520
leaq 0x8e6b(%rip), %rdi      # 0xd746
jmp 0x483d
movl %eax, %edi
callq 0x2520
leaq 0x8e98(%rip), %rdi      # 0xd786
jmp 0x483d