// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_env_cwalk
static int ESECT
mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
{
	MDB_cursor mc;
	MDB_txn *txn = my->mc_txn;
	MDB_node *ni;
	MDB_page *mo, *mp, *leaf;
	char *buf, *ptr;
	int rc, toggle;
	unsigned int i;

	/* Empty DB, nothing to do */
	if (*pg == P_INVALID)
		return MDB_SUCCESS;

	mc.mc_snum = 1;
	mc.mc_top = 0;
	mc.mc_txn = txn;

	rc = mdb_page_get(my->mc_txn, *pg, &mc.mc_pg[0], NULL);
	if (rc)
		return rc;
	rc = mdb_page_search_root(&mc, NULL, MDB_PS_FIRST);
	if (rc)
		return rc;

	/* Make cursor pages writable */
	buf = ptr = malloc(my->mc_env->me_psize * mc.mc_snum);
	if (buf == NULL)
		return ENOMEM;

	for (i=0; i<mc.mc_top; i++) {
		mdb_page_copy((MDB_page *)ptr, mc.mc_pg[i], my->mc_env->me_psize);
		mc.mc_pg[i] = (MDB_page *)ptr;
		ptr += my->mc_env->me_psize;
	}

	/* This is writable space for a leaf page. Usually not needed. */
	leaf = (MDB_page *)ptr;

	toggle = my->mc_toggle;
	while (mc.mc_snum > 0) {
		unsigned n;
		mp = mc.mc_pg[mc.mc_top];
		n = NUMKEYS(mp);

		if (IS_LEAF(mp)) {
			if (!IS_LEAF2(mp) && !(flags & F_DUPDATA)) {
				for (i=0; i<n; i++) {
					ni = NODEPTR(mp, i);
					if (ni->mn_flags & F_BIGDATA) {
						MDB_page *omp;
						pgno_t pg;

						/* Need writable leaf */
						if (mp != leaf) {
							mc.mc_pg[mc.mc_top] = leaf;
							mdb_page_copy(leaf, mp, my->mc_env->me_psize);
							mp = leaf;
							ni = NODEPTR(mp, i);
						}

						memcpy(&pg, NODEDATA(ni), sizeof(pg));
						rc = mdb_page_get(txn, pg, &omp, NULL);
						if (rc)
							goto done;
						if (my->mc_wlen[toggle] >= MDB_WBUF) {
							rc = mdb_env_cthr_toggle(my, 1);
							if (rc)
								goto done;
							toggle = my->mc_toggle;
						}
						mo = (MDB_page *)(my->mc_wbuf[toggle] + my->mc_wlen[toggle]);
						memcpy(mo, omp, my->mc_env->me_psize);
						mo->mp_pgno = my->mc_next_pgno;
						my->mc_next_pgno += omp->mp_pages;
						my->mc_wlen[toggle] += my->mc_env->me_psize;
						if (omp->mp_pages > 1) {
							my->mc_olen[toggle] = my->mc_env->me_psize * (omp->mp_pages - 1);
							my->mc_over[toggle] = (char *)omp + my->mc_env->me_psize;
							rc = mdb_env_cthr_toggle(my, 1);
							if (rc)
								goto done;
							toggle = my->mc_toggle;
						}
						memcpy(NODEDATA(ni), &mo->mp_pgno, sizeof(pgno_t));
					} else if (ni->mn_flags & F_SUBDATA) {
						MDB_db db;

						/* Need writable leaf */
						if (mp != leaf) {
							mc.mc_pg[mc.mc_top] = leaf;
							mdb_page_copy(leaf, mp, my->mc_env->me_psize);
							mp = leaf;
							ni = NODEPTR(mp, i);
						}

						memcpy(&db, NODEDATA(ni), sizeof(db));
						my->mc_toggle = toggle;
						rc = mdb_env_cwalk(my, &db.md_root, ni->mn_flags & F_DUPDATA);
						if (rc)
							goto done;
						toggle = my->mc_toggle;
						memcpy(NODEDATA(ni), &db, sizeof(db));
					}
				}
			}
		} else {
			mc.mc_ki[mc.mc_top]++;
			if (mc.mc_ki[mc.mc_top] < n) {
				pgno_t pg;
again:
				ni = NODEPTR(mp, mc.mc_ki[mc.mc_top]);
				pg = NODEPGNO(ni);
				rc = mdb_page_get(txn, pg, &mp, NULL);
				if (rc)
					goto done;
				mc.mc_top++;
				mc.mc_snum++;
				mc.mc_ki[mc.mc_top] = 0;
				if (IS_BRANCH(mp)) {
					/* Whenever we advance to a sibling branch page,
					 * we must proceed all the way down to its first leaf.
					 */
					mdb_page_copy(mc.mc_pg[mc.mc_top], mp, my->mc_env->me_psize);
					goto again;
				} else
					mc.mc_pg[mc.mc_top] = mp;
				continue;
			}
		}
		if (my->mc_wlen[toggle] >= MDB_WBUF) {
			rc = mdb_env_cthr_toggle(my, 1);
			if (rc)
				goto done;
			toggle = my->mc_toggle;
		}
		mo = (MDB_page *)(my->mc_wbuf[toggle] + my->mc_wlen[toggle]);
		mdb_page_copy(mo, mp, my->mc_env->me_psize);
		mo->mp_pgno = my->mc_next_pgno++;
		my->mc_wlen[toggle] += my->mc_env->me_psize;
		if (mc.mc_top) {
			/* Update parent if there is one */
			ni = NODEPTR(mc.mc_pg[mc.mc_top-1], mc.mc_ki[mc.mc_top-1]);
			SETPGNO(ni, mo->mp_pgno);
			mdb_cursor_pop(&mc);
		} else {
			/* Otherwise we're done */
			*pg = mo->mp_pgno;
			break;
		}
	}
done:
	free(buf);
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x1b0(%rbp)
movq -0x18(%rbp), %rax
cmpq $-0x1, (%rax)
jne 0x1606e
movl $0x0, -0x4(%rbp)
jmp 0x169a4
movw $0x1, -0x168(%rbp)
movw $0x0, -0x166(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
leaq -0x1a8(%rbp), %rdx
addq $0x48, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x160d1
movl -0x1e4(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x169a4
leaq -0x1a8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x4, %edx
callq 0xf9e0
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x16103
movl -0x1e4(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x169a4
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %eax
movzwl -0x168(%rbp), %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x2260
movq %rax, -0x1e0(%rbp)
movq %rax, -0x1d8(%rbp)
cmpq $0x0, -0x1d8(%rbp)
jne 0x16145
movl $0xc, -0x4(%rbp)
jmp 0x169a4
movl $0x0, -0x1ec(%rbp)
movl -0x1ec(%rbp), %eax
movzwl -0x166(%rbp), %ecx
cmpl %ecx, %eax
jae 0x161c9
movq -0x1e0(%rbp), %rdi
movl -0x1ec(%rbp), %eax
movq -0x160(%rbp,%rax,8), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
callq 0xfd30
movq -0x1e0(%rbp), %rcx
movl -0x1ec(%rbp), %eax
movq %rcx, -0x160(%rbp,%rax,8)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %ecx
movq -0x1e0(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x1e0(%rbp)
movl -0x1ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ec(%rbp)
jmp 0x1614f
movq -0x1e0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x10(%rbp), %rax
movl 0xac(%rax), %eax
movl %eax, -0x1e8(%rbp)
movzwl -0x168(%rbp), %eax
cmpl $0x0, %eax
jle 0x1698d
movzwl -0x166(%rbp), %eax
movq -0x160(%rbp,%rax,8), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
movl %eax, -0x1f0(%rbp)
movq -0x1c8(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
jne 0x1668c
movq -0x1c8(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x20, %eax
cmpl $0x20, %eax
je 0x16687
movl -0x1c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x16687
movl $0x0, -0x1ec(%rbp)
movl -0x1ec(%rbp), %eax
cmpl -0x1f0(%rbp), %eax
jae 0x16685
movq -0x1c8(%rbp), %rax
movq -0x1c8(%rbp), %rcx
movl -0x1ec(%rbp), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x16530
movq -0x1c8(%rbp), %rax
cmpq -0x1d0(%rbp), %rax
je 0x16331
movq -0x1d0(%rbp), %rcx
movzwl -0x166(%rbp), %eax
movq %rcx, -0x160(%rbp,%rax,8)
movq -0x1d0(%rbp), %rdi
movq -0x1c8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
callq 0xfd30
movq -0x1d0(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movq -0x1c8(%rbp), %rcx
movl -0x1ec(%rbp), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movq -0x1b8(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
movslq %ecx, %rcx
movq 0x8(%rax,%rcx), %rax
movq %rax, -0x200(%rbp)
movq -0x1b0(%rbp), %rdi
movq -0x200(%rbp), %rsi
leaq -0x1f8(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x16384
jmp 0x1698f
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
cmpl $0x100000, 0x88(%rax,%rcx,4) # imm = 0x100000
jl 0x163ce
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x169b0
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x163be
jmp 0x1698f
movq -0x10(%rbp), %rax
movl 0xac(%rax), %eax
movl %eax, -0x1e8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
movq 0x58(%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movslq -0x1e8(%rbp), %rdx
movslq 0x88(%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0x2230
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rcx
movq -0x1c0(%rbp), %rax
movq %rcx, (%rax)
movq -0x1f8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x98(%rax), %rcx
movq %rcx, 0x98(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
addl 0x88(%rax,%rcx,4), %edx
movl %edx, 0x88(%rax,%rcx,4)
movq -0x1f8(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jbe 0x16507
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq -0x1f8(%rbp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
imull %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
movl %edx, 0x90(%rax,%rcx,4)
movq -0x1f8(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %eax
addq %rax, %rdx
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
movq %rdx, 0x68(%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x169b0
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x164f7
jmp 0x1698f
movq -0x10(%rbp), %rax
movl 0xac(%rax), %eax
movl %eax, -0x1e8(%rbp)
movq -0x1b8(%rbp), %rax
movq -0x1b8(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
movslq %ecx, %rcx
movq -0x1c0(%rbp), %rdx
movq (%rdx), %rdx
movq %rdx, 0x8(%rax,%rcx)
jmp 0x1666f
movq -0x1b8(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1666d
movq -0x1c8(%rbp), %rax
cmpq -0x1d0(%rbp), %rax
je 0x165bf
movq -0x1d0(%rbp), %rcx
movzwl -0x166(%rbp), %eax
movq %rcx, -0x160(%rbp,%rax,8)
movq -0x1d0(%rbp), %rdi
movq -0x1c8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
callq 0xfd30
movq -0x1d0(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movq -0x1c8(%rbp), %rcx
movl -0x1ec(%rbp), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rsi
addq $0x8, %rsi
movq -0x1b8(%rbp), %rax
movzwl 0x6(%rax), %eax
cltq
addq %rax, %rsi
leaq -0x230(%rbp), %rdi
movl $0x30, %edx
callq 0x2230
movl -0x1e8(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xac(%rax)
movq -0x10(%rbp), %rdi
leaq -0x230(%rbp), %rsi
addq $0x28, %rsi
movq -0x1b8(%rbp), %rax
movzwl 0x4(%rax), %edx
andl $0x4, %edx
callq 0x16030
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x16631
jmp 0x1698f
movq -0x10(%rbp), %rax
movl 0xac(%rax), %eax
movl %eax, -0x1e8(%rbp)
movq -0x1b8(%rbp), %rdi
addq $0x8, %rdi
movq -0x1b8(%rbp), %rax
movzwl 0x6(%rax), %eax
cltq
addq %rax, %rdi
leaq -0x230(%rbp), %rsi
movl $0x30, %edx
callq 0x2230
jmp 0x1666f
jmp 0x16671
movl -0x1ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ec(%rbp)
jmp 0x1626a
jmp 0x16687
jmp 0x167db
movzwl -0x166(%rbp), %eax
movw -0x60(%rbp,%rax,2), %cx
addw $0x1, %cx
movw %cx, -0x60(%rbp,%rax,2)
movzwl -0x166(%rbp), %eax
movzwl -0x60(%rbp,%rax,2), %eax
cmpl -0x1f0(%rbp), %eax
jae 0x167d9
jmp 0x166bb
movq -0x1c8(%rbp), %rax
movq -0x1c8(%rbp), %rcx
movzwl -0x166(%rbp), %edx
movzwl -0x60(%rbp,%rdx,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movzwl (%rax), %eax
movq -0x1b8(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
shlq $0x10, %rcx
orq %rcx, %rax
movq -0x1b8(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, -0x238(%rbp)
movq -0x1b0(%rbp), %rdi
movq -0x238(%rbp), %rsi
leaq -0x1c8(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x1674e
jmp 0x1698f
movw -0x166(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x166(%rbp)
movw -0x168(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x168(%rbp)
movzwl -0x166(%rbp), %eax
movw $0x0, -0x60(%rbp,%rax,2)
movq -0x1c8(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0x167be
movzwl -0x166(%rbp), %eax
movq -0x160(%rbp,%rax,8), %rdi
movq -0x1c8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
callq 0xfd30
jmp 0x166bb
movq -0x1c8(%rbp), %rcx
movzwl -0x166(%rbp), %eax
movq %rcx, -0x160(%rbp,%rax,8)
jmp 0x161e7
jmp 0x167db
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
cmpl $0x100000, 0x88(%rax,%rcx,4) # imm = 0x100000
jl 0x16825
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x169b0
movl %eax, -0x1e4(%rbp)
cmpl $0x0, -0x1e4(%rbp)
je 0x16815
jmp 0x1698f
movq -0x10(%rbp), %rax
movl 0xac(%rax), %eax
movl %eax, -0x1e8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
movq 0x58(%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movslq -0x1e8(%rbp), %rdx
movslq 0x88(%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rdi
movq -0x1c8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
callq 0xfd30
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x98(%rax)
movq -0x1c0(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq -0x10(%rbp), %rax
movslq -0x1e8(%rbp), %rcx
addl 0x88(%rax,%rcx,4), %edx
movl %edx, 0x88(%rax,%rcx,4)
cmpw $0x0, -0x166(%rbp)
je 0x16975
movzwl -0x166(%rbp), %eax
subl $0x1, %eax
cltq
movq -0x160(%rbp,%rax,8), %rax
movzwl -0x166(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq -0x160(%rbp,%rcx,8), %rcx
movzwl -0x166(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movzwl -0x60(%rbp,%rdx,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1b8(%rbp)
movq -0x1c0(%rbp), %rax
movq (%rax), %rax
andq $0xffff, %rax           # imm = 0xFFFF
movw %ax, %cx
movq -0x1b8(%rbp), %rax
movw %cx, (%rax)
movq -0x1c0(%rbp), %rax
movq (%rax), %rax
shrq $0x10, %rax
movw %ax, %cx
movq -0x1b8(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x1c0(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movw %ax, %cx
movq -0x1b8(%rbp), %rax
movw %cx, 0x4(%rax)
leaq -0x1a8(%rbp), %rdi
callq 0x10080
jmp 0x16988
movq -0x1c0(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x1698d
jmp 0x161e7
jmp 0x1698f
movq -0x1d8(%rbp), %rdi
callq 0x2030
movl -0x1e4(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq