// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_env_copythr
static THREAD_RET ESECT
mdb_env_copythr(void *arg)
{
	mdb_copy *my = arg;
	char *ptr;
	int toggle = 0, wsize, rc;
#ifdef _WIN32
	DWORD len;
#define DO_WRITE(rc, fd, ptr, w2, len)	rc = WriteFile(fd, ptr, w2, &len, NULL)
#else
	int len;
#define DO_WRITE(rc, fd, ptr, w2, len)	len = write(fd, ptr, w2); rc = (len >= 0)
#endif

	pthread_mutex_lock(&my->mc_mutex);
	my->mc_new = 0;
	pthread_cond_signal(&my->mc_cond);
	for(;;) {
		while (!my->mc_new)
			pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
		if (my->mc_new < 0) {
			my->mc_new = 0;
			break;
		}
		my->mc_new = 0;
		wsize = my->mc_wlen[toggle];
		ptr = my->mc_wbuf[toggle];
again:
		while (wsize > 0) {
			DO_WRITE(rc, my->mc_fd, ptr, wsize, len);
			if (!rc) {
				rc = ErrCode();
				break;
			} else if (len > 0) {
				rc = MDB_SUCCESS;
				ptr += len;
				wsize -= len;
				continue;
			} else {
				rc = EIO;
				break;
			}
		}
		if (rc) {
			my->mc_status = rc;
			break;
		}
		/* If there's an overflow page tail, write it too */
		if (my->mc_olen[toggle]) {
			wsize = my->mc_olen[toggle];
			ptr = my->mc_over[toggle];
			my->mc_olen[toggle] = 0;
			goto again;
		}
		my->mc_wlen[toggle] = 0;
		toggle ^= 1;
		pthread_cond_signal(&my->mc_cond);
	}
	pthread_cond_signal(&my->mc_cond);
	pthread_mutex_unlock(&my->mc_mutex);
	return (THREAD_RET)0;
#undef DO_WRITE
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2400
movq -0x10(%rbp), %rax
movl $0x0, 0xa8(%rax)
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0x21e0
jmp 0x15e81
movq -0x10(%rbp), %rax
movl 0xa8(%rax), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x15e99
jmp 0x15eac
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
movq -0x10(%rbp), %rsi
callq 0x20e0
jmp 0x15e81
movq -0x10(%rbp), %rax
movl 0xa8(%rax), %eax
cmpl $0x0, %eax
jge 0x15ece
movq -0x10(%rbp), %rax
movl $0x0, 0xa8(%rax)
jmp 0x16004
movq -0x10(%rbp), %rax
movl $0x0, 0xa8(%rax)
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl 0x88(%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x58(%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
jmp 0x15f01
cmpl $0x0, -0x20(%rbp)
jle 0x15f74
movq -0x10(%rbp), %rax
movl 0xa0(%rax), %edi
movq -0x18(%rbp), %rsi
movslq -0x20(%rbp), %rdx
callq 0x20c0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x15f42
callq 0x2050
movl (%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x15f74
cmpl $0x0, -0x28(%rbp)
jle 0x15f6d
movl $0x0, -0x24(%rbp)
movl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movl -0x28(%rbp), %ecx
movl -0x20(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x20(%rbp)
jmp 0x15f01
movl $0x5, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x15f89
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xa4(%rax)
jmp 0x16004
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x0, 0x90(%rax,%rcx,4)
je 0x15fd6
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl 0x90(%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x68(%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl $0x0, 0x90(%rax,%rcx,4)
jmp 0x15eff
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl $0x0, 0x88(%rax,%rcx,4)
movl -0x1c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0x21e0
jmp 0x15e7f
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0x21e0
movq -0x10(%rbp), %rdi
callq 0x2250
xorl %eax, %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)