// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_env_copy2
int ESECT
mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
{
	int rc, len;
	char *lpath;
	HANDLE newfd = INVALID_HANDLE_VALUE;

	if (env->me_flags & MDB_NOSUBDIR) {
		lpath = (char *)path;
	} else {
		len = strlen(path);
		len += sizeof(DATANAME);
		lpath = malloc(len);
		if (!lpath)
			return ENOMEM;
		sprintf(lpath, "%s" DATANAME, path);
	}

	/* The destination path must exist, but the destination file must not.
	 * We don't want the OS to cache the writes, since the source data is
	 * already in the OS cache.
	 */
#ifdef _WIN32
	newfd = CreateFileA(lpath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
				FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH, NULL);
#else
	newfd = open(lpath, O_WRONLY|O_CREAT|O_EXCL, 0666);
#endif
	if (newfd == INVALID_HANDLE_VALUE) {
		rc = ErrCode();
		goto leave;
	}

	if (env->me_psize >= env->me_os_psize) {
#ifdef O_DIRECT
	/* Set O_DIRECT if the file system supports it */
	if ((rc = fcntl(newfd, F_GETFL)) != -1)
		(void) fcntl(newfd, F_SETFL, rc | O_DIRECT);
#endif
#ifdef F_NOCACHE	/* __APPLE__ */
	rc = fcntl(newfd, F_NOCACHE, 1);
	if (rc) {
		rc = ErrCode();
		goto leave;
	}
#endif
	}

	rc = mdb_env_copyfd2(env, newfd, flags);

leave:
	if (!(env->me_flags & MDB_NOSUBDIR))
		free(lpath);
	if (newfd != INVALID_HANDLE_VALUE)
		if (close(newfd) < 0 && rc == MDB_SUCCESS)
			rc = ErrCode();

	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x14f25
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x14f72
movq -0x18(%rbp), %rdi
callq 0x2100
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rax
addq $0xa, %rax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rdi
callq 0x2260
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x14f5c
movl $0xc, -0x4(%rbp)
jmp 0x15034
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x24be(%rip), %rsi      # 0x17429
movb $0x0, %al
callq 0x2360
movq -0x30(%rbp), %rdi
movl $0xc1, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x2300
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0x14f9c
callq 0x2050
movl (%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0x14fef
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jb 0x14fdd
movl -0x34(%rbp), %edi
movl $0x3, %esi
movb $0x0, %al
callq 0x20b0
movl %eax, -0x20(%rbp)
cmpl $-0x1, %eax
je 0x14fdb
movl -0x34(%rbp), %edi
movl -0x20(%rbp), %edx
orl $0x4000, %edx           # imm = 0x4000
movl $0x4, %esi
movb $0x0, %al
callq 0x20b0
jmp 0x14fdd
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x147b0
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
jne 0x15009
movq -0x30(%rbp), %rdi
callq 0x2030
cmpl $-0x1, -0x34(%rbp)
je 0x1502e
movl -0x34(%rbp), %edi
callq 0x2190
cmpl $0x0, %eax
jge 0x1502c
cmpl $0x0, -0x20(%rbp)
jne 0x1502c
callq 0x2050
movl (%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0x1502e
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)