// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq (%rsi), %rsi
cmpq $-0x1, %rsi
je 0xd174
movl %edx, %r14d
movq %rdi, %r15
movq 0x80(%rdi), %r13
leaq 0xc8(%rsp), %rdx
movl $0x1, -0x8(%rdx)
movq %r13, -0x30(%rdx)
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x6fba
testl %eax, %eax
jne 0xd16f
leaq 0x80(%rsp), %rdi
pushq $0x4
popq %rdx
xorl %esi, %esi
callq 0x9bfa
testl %eax, %eax
je 0xd18c
movl %eax, %r12d
jmp 0xd177
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x30(%rsp)
movq 0x78(%r15), %rbp
movl 0x10(%rbp), %r12d
movzwl 0xc0(%rsp), %eax
movl %r12d, %edi
movl %eax, 0x20(%rsp)
imull %eax, %edi
callq 0x2270
testq %rax, %rax
je 0xd6a3
movq %r15, 0x8(%rsp)
movl %r14d, 0x4(%rsp)
movq %rbx, 0x48(%rsp)
movzwl 0xc2(%rsp), %ebx
movzwl %bx, %r14d
xorl %r15d, %r15d
movq %rax, 0x10(%rsp)
movq %rax, %r13
cmpq %r15, %r14
je 0xd20b
movq 0xc8(%rsp,%r15,8), %rsi
movq %r13, %rdi
movl %r12d, %edx
callq 0x9dfd
movq %r13, 0xc8(%rsp,%r15,8)
movl 0x10(%rbp), %r12d
addq %r12, %r13
incq %r15
jmp 0xd1df
movq 0x8(%rsp), %r15
movl 0xac(%r15), %eax
movl %eax, (%rsp)
movl 0x4(%rsp), %r14d
andl $0x4, %r14d
movl 0x20(%rsp), %ecx
xorl %r12d, %r12d
testw %cx, %cx
je 0xd694
movzwl %bx, %ecx
movq 0xc8(%rsp,%rcx,8), %rbp
movq %rbp, 0x18(%rsp)
movzwl 0xc(%rbp), %eax
addl $-0x10, %eax
shrl %eax
movzwl 0xa(%rbp), %edx
testb $0x2, %dl
jne 0xd317
movzwl 0x1c8(%rsp,%rcx,2), %edx
incl %edx
movw %dx, 0x1c8(%rsp,%rcx,2)
movzwl %dx, %ecx
cmpl %ecx, %eax
jbe 0xd323
movzwl %dx, %eax
movzwl 0x10(%rbp,%rax,2), %eax
movl (%rbp,%rax), %ecx
movzwl 0x4(%rbp,%rax), %esi
shlq $0x20, %rsi
orq %rcx, %rsi
movq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x6fba
testl %eax, %eax
jne 0xd691
movq 0xc0(%rsp), %xmm0
psubw 0x1088(%rip), %xmm0     # 0xe340
pextrw $0x1, %xmm0, %ebx
movd %xmm0, 0xc0(%rsp)
andw $0x0, 0x1c8(%rsp,%rbx,2)
movq 0x18(%rsp), %rbp
testb $0x1, 0xa(%rbp)
je 0xd306
movq 0xc8(%rsp,%rbx,8), %rdi
movq 0x78(%r15), %rax
movl 0x10(%rax), %edx
movq %rbp, %rsi
callq 0x9dfd
movzwl 0xc2(%rsp), %eax
movzwl 0x1c8(%rsp,%rax,2), %edx
jmp 0xd276
movq %rbp, 0xc8(%rsp,%rbx,8)
movd %xmm0, %ecx
jmp 0xd227
andl $0x20, %edx
orl %r14d, %edx
je 0xd43e
movl %r14d, 0x4(%rsp)
movslq (%rsp), %rbx
movl 0x88(%r15,%rbx,4), %eax
cmpl $0x100000, %eax         # imm = 0x100000
jl 0xd363
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0xd6ba
testl %eax, %eax
jne 0xd691
movl 0xac(%r15), %eax
movl %eax, (%rsp)
movslq %eax, %rbx
movl 0x88(%r15,%rbx,4), %eax
movq 0x58(%r15,%rbx,8), %r14
movslq %eax, %r15
leaq (%r14,%r15), %rbp
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq %rbp, %rdi
callq 0x9dfd
movq 0x8(%rsp), %rax
movq 0x98(%rax), %rax
leaq 0x1(%rax), %rcx
movq 0x8(%rsp), %rdx
movq %rcx, 0x98(%rdx)
movq %rax, (%r14,%r15)
movq 0x8(%rsp), %r15
movq 0x78(%r15), %rax
movl 0x10(%rax), %eax
addl %eax, 0x88(%r15,%rbx,4)
movzwl 0xc2(%rsp), %ebx
testq %rbx, %rbx
je 0xd6ac
movl %ebx, %eax
decl %eax
movq 0xc8(%rsp,%rax,8), %rcx
movzwl 0x1c8(%rsp,%rax,2), %eax
movzwl 0x10(%rcx,%rax,2), %eax
movzwl (%rbp), %edx
movw %dx, (%rcx,%rax)
movzwl 0x2(%rbp), %edx
movw %dx, 0x2(%rcx,%rax)
movzwl 0x4(%rbp), %edx
movw %dx, 0x4(%rcx,%rax)
movzwl 0xc0(%rsp), %eax
xorl %ecx, %ecx
testw %ax, %ax
movl 0x4(%rsp), %r14d
je 0xd227
xorl %ecx, %ecx
decw %ax
movw %ax, 0xc0(%rsp)
je 0xd227
decl %ebx
movw %bx, 0xc2(%rsp)
movw $0x1, %cx
jmp 0xd227
movl %eax, %ebp
xorl %ebx, %ebx
movl %r14d, 0x4(%rsp)
movq %rbp, 0x28(%rsp)
cmpq %rbx, %rbp
je 0xd323
movq 0x18(%rsp), %rsi
movzwl 0x10(%rsi,%rbx,2), %eax
leaq (%rsi,%rax), %rcx
movzwl 0x4(%rsi,%rax), %eax
testb $0x1, %al
jne 0xd531
testb $0x2, %al
je 0xd689
cmpq %r13, %rsi
je 0xd4aa
movzwl 0xc2(%rsp), %eax
movq %r13, 0xc8(%rsp,%rax,8)
movq 0x78(%r15), %rax
movl 0x10(%rax), %edx
movq %r13, %rdi
callq 0x9dfd
movq %r13, 0x18(%rsp)
movzwl 0x10(%r13,%rbx,2), %ecx
addq %r13, %rcx
movzwl 0x6(%rcx), %eax
movups 0x8(%rcx,%rax), %xmm0
movups 0x18(%rcx,%rax), %xmm1
movups 0x28(%rcx,%rax), %xmm2
movaps %xmm2, 0x70(%rsp)
movaps %xmm1, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl (%rsp), %eax
movl %eax, 0xac(%r15)
movq %rcx, %rbp
movzwl 0x4(%rcx), %edx
andl $0x4, %edx
movq %r15, %rdi
leaq 0x78(%rsp), %rsi
callq 0xd10e
testl %eax, %eax
jne 0xd691
movl 0xac(%r15), %eax
movl %eax, (%rsp)
movzwl 0x6(%rbp), %eax
addq $0x8, %rbp
movdqa 0x50(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
movaps 0x70(%rsp), %xmm2
movups %xmm2, 0x20(%rbp,%rax)
movups %xmm1, 0x10(%rbp,%rax)
movdqu %xmm0, (%rbp,%rax)
movq 0x28(%rsp), %rbp
jmp 0xd689
movq %rbx, 0x20(%rsp)
cmpq %r13, %rsi
je 0xd56d
movzwl 0xc2(%rsp), %eax
movq %r13, 0xc8(%rsp,%rax,8)
movq 0x78(%r15), %rax
movl 0x10(%rax), %edx
movq %r13, %rdi
callq 0x9dfd
movq %r13, 0x18(%rsp)
movq 0x20(%rsp), %rax
movzwl 0x10(%r13,%rax,2), %ecx
addq %r13, %rcx
movzwl 0x6(%rcx), %eax
movq %rcx, 0x40(%rsp)
movq 0x8(%rcx,%rax), %rsi
movq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rdx
xorl %ecx, %ecx
callq 0x6fba
testl %eax, %eax
jne 0xd691
movslq (%rsp), %rbx
movl 0x88(%r15,%rbx,4), %eax
cmpl $0x100000, %eax         # imm = 0x100000
jl 0xd5cf
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0xd6ba
testl %eax, %eax
jne 0xd691
movl 0xac(%r15), %eax
movl %eax, (%rsp)
movslq %eax, %rbx
movl 0x88(%r15,%rbx,4), %eax
movq 0x58(%r15,%rbx,8), %r14
movslq %eax, %r15
leaq (%r14,%r15), %rdi
movq 0x50(%rsp), %rbp
movq 0x8(%rsp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq %rdi, 0x38(%rsp)
movq %rbp, %rsi
callq 0x2240
movq 0x8(%rsp), %rax
movq 0x98(%rax), %rax
movq %rax, (%r14,%r15)
movq 0x8(%rsp), %r15
movl 0x4(%rsp), %r14d
movl 0xc(%rbp), %ecx
addq %rax, %rcx
movq %rcx, 0x98(%r15)
movq 0x78(%r15), %rax
movl 0x10(%rax), %ecx
addl %ecx, 0x88(%r15,%rbx,4)
movl 0xc(%rbp), %ecx
cmpl $0x2, %ecx
jb 0xd669
decl %ecx
imull 0x10(%rax), %ecx
movl %ecx, 0x90(%r15,%rbx,4)
movl 0x10(%rax), %eax
addq %rax, %rbp
movq %rbp, 0x68(%r15,%rbx,8)
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0xd6ba
testl %eax, %eax
jne 0xd691
movl 0xac(%r15), %eax
movl %eax, (%rsp)
movq 0x40(%rsp), %rdx
movzwl 0x6(%rdx), %eax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%rdx,%rax)
movq 0x28(%rsp), %rbp
movq 0x20(%rsp), %rbx
incq %rbx
jmp 0xd44c
movl %eax, %r12d
movq 0x10(%rsp), %rdi
callq 0x2030
jmp 0xd177
pushq $0xc
popq %r12
jmp 0xd177
movq (%rbp), %rax
movq 0x48(%rsp), %rcx
movq %rax, (%rcx)
jmp 0xd694