// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %r14
movq (%rsi), %rsi
cmpq $-0x1, %rsi
je 0xe087
movl %edx, %ebp
movq %rdi, %r15
movq 0x80(%rdi), %r12
leaq 0xc8(%rsp), %rdx
movl $0x1, -0x8(%rdx)
movq %r12, -0x30(%rdx)
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x789a
testl %eax, %eax
jne 0xe082
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xa809
testl %eax, %eax
je 0xe09f
movl %eax, %r13d
jmp 0xe08a
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x78(%r15), %rbx
movzwl 0xc0(%rsp), %edi
imull 0x10(%rbx), %edi
callq 0x2270
testq %rax, %rax
je 0xe60e
movq %rax, %r13
movq %r12, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movl %ebp, 0x1c(%rsp)
movzwl 0xc2(%rsp), %r14d
movq %rax, %rbp
testq %r14, %r14
je 0xe10e
xorl %r12d, %r12d
movq %r13, %rbp
movq 0xc8(%rsp,%r12,8), %rsi
movl 0x10(%rbx), %edx
movq %rbp, %rdi
callq 0xaa49
movq %rbp, 0xc8(%rsp,%r12,8)
movl 0x10(%rbx), %eax
addq %rax, %rbp
incq %r12
cmpq %r12, %r14
jne 0xe0e5
movq %r13, 0x28(%rsp)
cmpw $0x0, 0xc0(%rsp)
je 0xe619
movl 0xac(%r15), %eax
movl %eax, 0x4(%rsp)
andl $0x4, 0x1c(%rsp)
xorl %r13d, %r13d
leaq 0x10(%rsp), %r14
movq %r15, 0x8(%rsp)
movzwl 0xc2(%rsp), %eax
movq 0xc8(%rsp,%rax,8), %rdx
movq %rdx, 0x10(%rsp)
movzwl 0xc(%rdx), %ecx
addl $-0x10, %ecx
movl %ecx, %ebx
shrl %ebx
movzwl 0xa(%rdx), %edx
testb $0x2, %dl
jne 0xe21e
movzwl 0x1c8(%rsp,%rax,2), %ecx
incl %ecx
movw %cx, 0x1c8(%rsp,%rax,2)
movzwl %cx, %eax
cmpl %eax, %ebx
jbe 0xe4af
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %rax
movzwl 0xc2(%rsp), %ecx
movzwl 0x1c8(%rsp,%rcx,2), %ecx
movzwl 0x10(%rax,%rcx,2), %ecx
movl (%rax,%rcx), %edx
movzwl 0x4(%rax,%rcx), %esi
shlq $0x20, %rsi
orq %rdx, %rsi
movq %rbx, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x789a
movl %eax, %r13d
testl %eax, %eax
jne 0xe4d6
movzwl 0xc2(%rsp), %eax
incl %eax
movw %ax, 0xc2(%rsp)
incw 0xc0(%rsp)
movzwl %ax, %eax
movw $0x0, 0x1c8(%rsp,%rax,2)
movq 0x10(%rsp), %rsi
testb $0x1, 0xa(%rsi)
je 0xe4e0
movq 0xc8(%rsp,%rax,8), %rdi
movq 0x78(%r15), %rax
movl 0x10(%rax), %edx
callq 0xaa49
jmp 0xe18e
andl $0x20, %edx
orl 0x1c(%rsp), %edx
jne 0xe4af
cmpl $0x2, %ecx
jb 0xe4af
cmpl $0x1, %ebx
adcl $0x0, %ebx
xorl %r12d, %r12d
movq %rbx, 0x48(%rsp)
movq 0x10(%rsp), %rsi
movzwl 0x10(%rsi,%r12,2), %eax
leaq (%rsi,%rax), %r14
movzwl 0x4(%rsi,%rax), %eax
testb $0x1, %al
jne 0xe32e
testb $0x2, %al
je 0xe4a3
cmpq %rbp, %rsi
je 0xe298
movzwl 0xc2(%rsp), %eax
movq %rbp, 0xc8(%rsp,%rax,8)
movq 0x78(%r15), %rax
movl 0x10(%rax), %edx
movq %rbp, %rdi
callq 0xaa49
movq %rbp, 0x10(%rsp)
movzwl 0x10(%rbp,%r12,2), %r14d
addq %rbp, %r14
movzwl 0x6(%r14), %eax
movups 0x8(%r14,%rax), %xmm0
movups 0x18(%r14,%rax), %xmm1
movups 0x28(%r14,%rax), %xmm2
movaps %xmm2, 0x70(%rsp)
movaps %xmm1, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0xac(%r15)
movzwl 0x4(%r14), %edx
andl $0x4, %edx
movq %r15, %rdi
leaq 0x78(%rsp), %rsi
callq 0xe020
movl %eax, %r13d
movl $0xa, %edx
testl %eax, %eax
jne 0xe320
movl 0xac(%r15), %eax
movl %eax, 0x4(%rsp)
movzwl 0x6(%r14), %eax
addq $0x8, %r14
movaps 0x50(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
movaps 0x70(%rsp), %xmm2
movups %xmm2, 0x20(%r14,%rax)
movups %xmm1, 0x10(%r14,%rax)
movups %xmm0, (%r14,%rax)
xorl %edx, %edx
testl %r13d, %r13d
je 0xe4a3
jmp 0xe5e3
cmpq %rbp, %rsi
je 0xe360
movzwl 0xc2(%rsp), %eax
movq %rbp, 0xc8(%rsp,%rax,8)
movq 0x78(%r15), %rax
movl 0x10(%rax), %edx
movq %rbp, %rdi
callq 0xaa49
movq %rbp, 0x10(%rsp)
movzwl 0x10(%rbp,%r12,2), %r14d
addq %rbp, %r14
movzwl 0x6(%r14), %eax
movq 0x8(%r14,%rax), %rsi
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rdx
xorl %ecx, %ecx
callq 0x789a
movl $0xa, %edx
testl %eax, %eax
je 0xe38e
movl %eax, %r13d
jmp 0xe496
movslq 0x4(%rsp), %rax
cmpl $0x100000, 0x88(%r15,%rax,4) # imm = 0x100000
jl 0xe3c1
movq %r15, %rdi
movl $0x1, %esi
callq 0xe63c
testl %eax, %eax
jne 0xe466
movl 0xac(%r15), %eax
movl %eax, 0x4(%rsp)
movslq 0x4(%rsp), %rbx
movq 0x58(%r15,%rbx,8), %rcx
movq %rcx, 0x40(%rsp)
movq 0x8(%rsp), %rax
movslq 0x88(%rax,%rbx,4), %r15
leaq (%rcx,%r15), %rdi
movq 0x50(%rsp), %r13
movq 0x8(%rsp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq %rdi, 0x38(%rsp)
movq %r13, %rsi
callq 0x2240
movq 0x8(%rsp), %rax
movq 0x98(%rax), %rax
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%r15)
movq 0x8(%rsp), %r15
movl 0xc(%r13), %eax
addq %rax, 0x98(%r15)
movq 0x78(%r15), %rax
movl 0x10(%rax), %ecx
addl %ecx, 0x88(%r15,%rbx,4)
movl 0xc(%r13), %ecx
cmpl $0x2, %ecx
jb 0xe47d
decl %ecx
imull 0x10(%rax), %ecx
movl %ecx, 0x90(%r15,%rbx,4)
movl 0x10(%rax), %eax
addq %rax, %r13
movq %r13, 0x68(%r15,%rbx,8)
movq %r15, %rdi
movl $0x1, %esi
callq 0xe63c
testl %eax, %eax
je 0xe472
movl %eax, %r13d
xorl %ebx, %ebx
movl $0xa, %edx
jmp 0xe496
movl 0xac(%r15), %eax
movl %eax, 0x4(%rsp)
movzwl 0x6(%r14), %eax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%r14,%rax)
movb $0x1, %bl
xorl %r13d, %r13d
xorl %edx, %edx
testb %bl, %bl
movq 0x48(%rsp), %rbx
je 0xe5e3
incq %r12
cmpq %r12, %rbx
jne 0xe242
movslq 0x4(%rsp), %rax
cmpl $0x100000, 0x88(%r15,%rax,4) # imm = 0x100000
jl 0xe500
movq %r15, %rdi
movl $0x1, %esi
callq 0xe63c
testl %eax, %eax
je 0xe4f2
movl %eax, %r13d
movl $0xa, %edx
jmp 0xe5e3
movq %rsi, 0xc8(%rsp,%rax,8)
movl $0x5, %edx
jmp 0xe5e3
movl 0xac(%r15), %eax
movl %eax, 0x4(%rsp)
xorl %r13d, %r13d
movslq 0x4(%rsp), %r14
movq 0x8(%rsp), %rax
movq 0x58(%rax,%r14,8), %r15
movq 0x8(%rsp), %rax
movslq 0x88(%rax,%r14,4), %r12
leaq (%r15,%r12), %rbx
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq %rbx, %rdi
callq 0xaa49
movq 0x8(%rsp), %rax
movq 0x98(%rax), %rax
leaq 0x1(%rax), %rcx
movq 0x8(%rsp), %rdx
movq %rcx, 0x98(%rdx)
movq %rax, (%r15,%r12)
movq 0x8(%rsp), %r15
movq 0x78(%r15), %rax
movl 0x10(%rax), %eax
addl %eax, 0x88(%r15,%r14,4)
movzwl 0xc2(%rsp), %eax
testq %rax, %rax
je 0xe5d3
leal -0x1(%rax), %ecx
movq 0xc8(%rsp,%rcx,8), %rdx
movzwl 0x1c8(%rsp,%rcx,2), %ecx
movzwl 0x10(%rdx,%rcx,2), %ecx
movzwl (%rbx), %esi
movw %si, (%rdx,%rcx)
movzwl 0x2(%rbx), %esi
movw %si, 0x2(%rdx,%rcx)
movzwl 0x4(%rbx), %esi
movw %si, 0x4(%rdx,%rcx)
movzwl 0xc0(%rsp), %ecx
xorl %edx, %edx
testw %cx, %cx
je 0xe5e3
decw %cx
movw %cx, 0xc0(%rsp)
je 0xe5e3
decl %eax
movw %ax, 0xc2(%rsp)
jmp 0xe5e3
movq (%rbx), %rax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movl $0x6, %edx
cmpl $0x4, %edx
jle 0xe5f4
cmpl $0x5, %edx
leaq 0x10(%rsp), %r14
je 0xe5fd
jmp 0xe61e
testl %edx, %edx
leaq 0x10(%rsp), %r14
jne 0xe637
cmpw $0x0, 0xc0(%rsp)
jne 0xe13f
jmp 0xe628
movl $0xc, %r13d
jmp 0xe08a
xorl %r13d, %r13d
jmp 0xe628
cmpl $0x6, %edx
je 0xe628
cmpl $0xa, %edx
jne 0xe637
movq 0x28(%rsp), %rdi
callq 0x2030
jmp 0xe08a
jmp 0xe08a