// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq (%rsi), %rsi
cmpq $-0x1, %rsi
je 0xdc03
movl %edx, %ebp
movq %rdi, %r14
movq 0x80(%rdi), %r12
leaq 0xc8(%rsp), %rdx
movl $0x1, -0x8(%rdx)
movq %r12, -0x30(%rdx)
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x74c4
testl %eax, %eax
jne 0xdbff
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xa404
testl %eax, %eax
je 0xdc19
movl %eax, %ebx
jmp 0xdc05
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x20(%rsp)
movq 0x78(%r14), %r14
movl 0x10(%r14), %ebx
movzwl 0xc0(%rsp), %r13d
movl %ebx, %edi
imull %r13d, %edi
callq 0x2270
testq %rax, %rax
je 0xe11e
movl %r13d, 0x4(%rsp)
movl %ebp, 0x1c(%rsp)
movq %r15, 0x48(%rsp)
movzwl 0xc2(%rsp), %ebp
movq %rax, 0x28(%rsp)
movq %rax, %r13
testq %rbp, %rbp
je 0xdc97
xorl %r15d, %r15d
movq 0x28(%rsp), %r13
movq 0xc8(%rsp,%r15,8), %rsi
movq %r13, %rdi
movl %ebx, %edx
callq 0xa635
movq %r13, 0xc8(%rsp,%r15,8)
movl 0x10(%r14), %ebx
addq %rbx, %r13
incq %r15
cmpq %r15, %rbp
jne 0xdc6e
cmpw $0x0, 0x4(%rsp)
je 0xe133
movq 0x20(%rsp), %r14
movl 0xac(%r14), %eax
movl %eax, 0x4(%rsp)
andl $0x4, 0x1c(%rsp)
movzwl %bp, %ecx
movq 0xc8(%rsp,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movzwl 0xc(%rax), %edx
addl $-0x10, %edx
movl %edx, %ebp
shrl %ebp
movzwl 0xa(%rax), %esi
testb $0x2, %sil
jne 0xddb7
movzwl 0x1c8(%rsp,%rcx,2), %edx
incl %edx
movw %dx, 0x1c8(%rsp,%rcx,2)
movzwl %dx, %ecx
cmpl %ecx, %ebp
jbe 0xe023
movzwl 0x10(%rax,%rcx,2), %ecx
movl (%rax,%rcx), %edx
movzwl 0x4(%rax,%rcx), %esi
shlq $0x20, %rsi
orq %rdx, %rsi
movq %r12, %rdi
leaq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x74c4
testl %eax, %eax
jne 0xe11a
movq 0xc0(%rsp), %xmm0
psubw 0x1626(%rip), %xmm0     # 0xf360
pextrw $0x1, %xmm0, %ebp
movd %xmm0, 0xc0(%rsp)
movw $0x0, 0x1c8(%rsp,%rbp,2)
movq 0x8(%rsp), %rbx
testb $0x1, 0xa(%rbx)
je 0xdd9d
movq 0xc8(%rsp,%rbp,8), %rdi
movq 0x78(%r14), %rax
movl 0x10(%rax), %edx
movq %rbx, %rsi
callq 0xa635
movzwl 0xc2(%rsp), %eax
movzwl 0x1c8(%rsp,%rax,2), %eax
movzwl 0x10(%rbx,%rax,2), %eax
movl (%rbx,%rax), %ecx
movzwl 0x4(%rbx,%rax), %esi
shlq $0x20, %rsi
orq %rcx, %rsi
jmp 0xdd12
movq %rbx, 0xc8(%rsp,%rbp,8)
movd %xmm0, %eax
testw %ax, %ax
jne 0xdcb8
jmp 0xe133
andl $0x20, %esi
orl 0x1c(%rsp), %esi
jne 0xe023
cmpl $0x2, %edx
jb 0xe023
cmpl $0x1, %ebp
adcl $0x0, %ebp
xorl %ebx, %ebx
movq 0x8(%rsp), %rsi
movzwl 0x10(%rsi,%rbx,2), %eax
leaq (%rsi,%rax), %r15
movzwl 0x4(%rsi,%rax), %eax
testb $0x1, %al
jne 0xdeba
testb $0x2, %al
je 0xe017
cmpq %r13, %rsi
je 0xde2a
movzwl 0xc2(%rsp), %eax
movq %r13, 0xc8(%rsp,%rax,8)
movq 0x78(%r14), %rax
movl 0x10(%rax), %edx
movq %r13, %rdi
callq 0xa635
movq %r13, 0x8(%rsp)
movzwl 0x10(%r13,%rbx,2), %r15d
addq %r13, %r15
movq %rbx, 0x10(%rsp)
movzwl 0x6(%r15), %eax
movdqu 0x8(%r15,%rax), %xmm0
movups 0x18(%r15,%rax), %xmm1
movups 0x28(%r15,%rax), %xmm2
movaps %xmm2, 0x70(%rsp)
movaps %xmm1, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0xac(%r14)
movzwl 0x4(%r15), %edx
andl $0x4, %edx
movq %r14, %rdi
leaq 0x78(%rsp), %rsi
callq 0xdb9d
testl %eax, %eax
jne 0xe11a
movl 0xac(%r14), %eax
movl %eax, 0x4(%rsp)
movzwl 0x6(%r15), %eax
addq $0x8, %r15
movdqa 0x50(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
movaps 0x70(%rsp), %xmm2
movups %xmm2, 0x20(%r15,%rax)
movups %xmm1, 0x10(%r15,%rax)
movdqu %xmm0, (%r15,%rax)
jmp 0xe012
cmpq %r13, %rsi
je 0xdeec
movzwl 0xc2(%rsp), %eax
movq %r13, 0xc8(%rsp,%rax,8)
movq 0x78(%r14), %rax
movl 0x10(%rax), %edx
movq %r13, %rdi
callq 0xa635
movq %r13, 0x8(%rsp)
movzwl 0x10(%r13,%rbx,2), %r15d
addq %r13, %r15
movq %rbx, 0x10(%rsp)
movzwl 0x6(%r15), %eax
movq %r15, 0x40(%rsp)
movq 0x8(%r15,%rax), %rsi
movq %r12, %rdi
leaq 0x50(%rsp), %rdx
xorl %ecx, %ecx
callq 0x74c4
testl %eax, %eax
jne 0xe11a
movslq 0x4(%rsp), %rcx
movl 0x88(%r14,%rcx,4), %eax
cmpl $0x100000, %eax         # imm = 0x100000
jl 0xdf56
movq %r14, %rdi
movl $0x1, %esi
callq 0xe144
testl %eax, %eax
jne 0xe11a
movl 0xac(%r14), %eax
movl %eax, 0x4(%rsp)
movslq %eax, %rcx
movl 0x88(%r14,%rcx,4), %eax
movq %rcx, 0x38(%rsp)
movq 0x58(%r14,%rcx,8), %r15
movslq %eax, %r14
leaq (%r15,%r14), %rdi
movq 0x50(%rsp), %rbx
movq 0x20(%rsp), %rax
movq 0x78(%rax), %rax
movl 0x10(%rax), %edx
movq %rdi, 0x30(%rsp)
movq %rbx, %rsi
callq 0x2240
movq 0x38(%rsp), %rdx
movq 0x20(%rsp), %rax
movq 0x98(%rax), %rax
movq %rax, (%r15,%r14)
movq 0x20(%rsp), %r14
movl 0xc(%rbx), %ecx
addq %rax, %rcx
movq %rcx, 0x98(%r14)
movq 0x78(%r14), %rax
movl 0x10(%rax), %ecx
addl %ecx, 0x88(%r14,%rdx,4)
movl 0xc(%rbx), %ecx
cmpl $0x2, %ecx
jb 0xdffc
decl %ecx
imull 0x10(%rax), %ecx
movl %ecx, 0x90(%r14,%rdx,4)
movl 0x10(%rax), %eax
addq %rax, %rbx
movq %rbx, 0x68(%r14,%rdx,8)
movq %r14, %rdi
movl $0x1, %esi
callq 0xe144
testl %eax, %eax
jne 0xe11a
movl 0xac(%r14), %eax
movl %eax, 0x4(%rsp)
movq 0x40(%rsp), %rdx
movzwl 0x6(%rdx), %eax
movq 0x30(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%rdx,%rax)
movq 0x10(%rsp), %rbx
incq %rbx
cmpq %rbx, %rbp
jne 0xddd5
movslq 0x4(%rsp), %r15
movl 0x88(%r14,%r15,4), %eax
cmpl $0x100000, %eax         # imm = 0x100000
jl 0xe062
movq %r14, %rdi
movl $0x1, %esi
callq 0xe144
testl %eax, %eax
jne 0xe11a
movl 0xac(%r14), %eax
movl %eax, 0x4(%rsp)
movslq %eax, %r15
movl 0x88(%r14,%r15,4), %eax
movq 0x58(%r14,%r15,8), %rcx
movq %rcx, 0x10(%rsp)
movslq %eax, %rbp
leaq (%rcx,%rbp), %rbx
movq 0x8(%rsp), %rsi
movq 0x78(%r14), %rax
movl 0x10(%rax), %edx
movq %rbx, %rdi
callq 0xa635
movq 0x98(%r14), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x98(%r14)
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%rbp)
movq 0x78(%r14), %rax
movl 0x10(%rax), %eax
addl %eax, 0x88(%r14,%r15,4)
movzwl 0xc2(%rsp), %ebp
testq %rbp, %rbp
je 0xe128
leal -0x1(%rbp), %eax
movq 0xc8(%rsp,%rax,8), %rcx
movzwl 0x1c8(%rsp,%rax,2), %eax
movzwl 0x10(%rcx,%rax,2), %eax
movzwl (%rbx), %edx
movw %dx, (%rcx,%rax)
movzwl 0x2(%rbx), %edx
movw %dx, 0x2(%rcx,%rax)
movzwl 0x4(%rbx), %edx
movw %dx, 0x4(%rcx,%rax)
movzwl 0xc0(%rsp), %eax
xorl %ebx, %ebx
testw %ax, %ax
je 0xe135
decw %ax
movw %ax, 0xc0(%rsp)
je 0xe135
decl %ebp
movw %bp, 0xc2(%rsp)
jmp 0xdcb8
movl %eax, %ebx
jmp 0xe135
movl $0xc, %ebx
jmp 0xdc05
movq (%rbx), %rax
movq 0x48(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
callq 0x2030
jmp 0xdc05