// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_cursor_last
static int
mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
{
	int		 rc;
	MDB_node	*leaf;

	if (mc->mc_xcursor)
		mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);

	if (!(mc->mc_flags & C_EOF)) {

		if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
			rc = mdb_page_search(mc, NULL, MDB_PS_LAST);
			if (rc != MDB_SUCCESS)
				return rc;
		}
		mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));

	}
	mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]) - 1;
	mc->mc_flags |= C_INITIALIZED|C_EOF;
	leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);

	if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
		key->mv_size = mc->mc_db->md_pad;
		key->mv_data = LEAF2KEY(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], key->mv_size);
		return MDB_SUCCESS;
	}

	if (data) {
		if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
			mdb_xcursor_init1(mc, leaf);
			rc = mdb_cursor_last(&mc->mc_xcursor->mx_cursor, data, NULL);
			if (rc)
				return rc;
		} else {
			if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
				return rc;
		}
	}

	MDB_GET_KEY(leaf, key);
	return MDB_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x9100
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x44(%rax), %ecx
andl $-0x4, %ecx
movl %ecx, 0x44(%rax)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x91a4
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x912f
movq -0x10(%rbp), %rax
movzwl 0x42(%rax), %eax
cmpl $0x0, %eax
je 0x9157
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xeec0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x9155
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9346
jmp 0x9157
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
jne 0x9176
jmp 0x91a2
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0xe91f(%rip), %rsi      # 0x17aa8
leaq 0xe975(%rip), %rdx      # 0x17b05
leaq 0xe278(%rip), %rcx      # 0x1740f
movl $0x17c1, %r8d           # imm = 0x17C1
callq 0x133a0
jmp 0x91a4
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
subl $0x1, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw %dx, 0x148(%rax,%rcx,2)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
orl $0x3, %ecx
movl %ecx, 0x44(%rax)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movzwl 0x42(%rdx), %edx
movq 0x48(%rcx,%rdx,8), %rcx
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rsi
movzwl 0x42(%rsi), %esi
movzwl 0x148(%rdx,%rsi,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xa(%rax), %eax
andl $0x20, %eax
cmpl $0x20, %eax
jne 0x92a2
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl (%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rcx
addq $0x10, %rcx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rdx
movzwl 0x42(%rdx), %edx
movzwl 0x148(%rax,%rdx,2), %eax
movq -0x18(%rbp), %rdx
imulq (%rdx), %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x9346
cmpq $0x0, -0x20(%rbp)
je 0x9317
movq -0x30(%rbp), %rax
movzwl 0x4(%rax), %eax
andl $0x4, %eax
cmpl $0x4, %eax
jne 0x92ee
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x8800
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x90d0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x92ec
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9346
jmp 0x9315
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x8a10
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
je 0x9313
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9346
jmp 0x9315
jmp 0x9317
cmpq $0x0, -0x18(%rbp)
je 0x933f
movq -0x30(%rbp), %rax
movzwl 0x6(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
addq $0x8, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop