// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_node_add
static int
mdb_node_add(MDB_cursor *mc, indx_t indx,
    MDB_val *key, MDB_val *data, pgno_t pgno, unsigned int flags)
{
	unsigned int	 i;
	size_t		 node_size = NODESIZE;
	ssize_t		 room;
	indx_t		 ofs;
	MDB_node	*node;
	MDB_page	*mp = mc->mc_pg[mc->mc_top];
	MDB_page	*ofp = NULL;		/* overflow page */
	void		*ndata;
	DKBUF;

	mdb_cassert(mc, mp->mp_upper >= mp->mp_lower);

	DPRINTF(("add to %s %spage %"Z"u index %i, data size %"Z"u key size %"Z"u [%s]",
	    IS_LEAF(mp) ? "leaf" : "branch",
		IS_SUBP(mp) ? "sub-" : "",
		mdb_dbg_pgno(mp), indx, data ? data->mv_size : 0,
		key ? key->mv_size : 0, key ? DKEY(key) : "null"));

	if (IS_LEAF2(mp)) {
		/* Move higher keys up one slot. */
		int ksize = mc->mc_db->md_pad, dif;
		char *ptr = LEAF2KEY(mp, indx, ksize);
		dif = NUMKEYS(mp) - indx;
		if (dif > 0)
			memmove(ptr+ksize, ptr, dif*ksize);
		/* insert new key */
		memcpy(ptr, key->mv_data, ksize);

		/* Just using these for counting */
		mp->mp_lower += sizeof(indx_t);
		mp->mp_upper -= ksize - sizeof(indx_t);
		return MDB_SUCCESS;
	}

	room = (ssize_t)SIZELEFT(mp) - (ssize_t)sizeof(indx_t);
	if (key != NULL)
		node_size += key->mv_size;
	if (IS_LEAF(mp)) {
		mdb_cassert(mc, key && data);
		if (F_ISSET(flags, F_BIGDATA)) {
			/* Data already on overflow page. */
			node_size += sizeof(pgno_t);
		} else if (node_size + data->mv_size > mc->mc_txn->mt_env->me_nodemax) {
			int ovpages = OVPAGES(data->mv_size, mc->mc_txn->mt_env->me_psize);
			int rc;
			/* Put data on overflow page. */
			DPRINTF(("data size is %"Z"u, node would be %"Z"u, put data on overflow page",
			    data->mv_size, node_size+data->mv_size));
			node_size = EVEN(node_size + sizeof(pgno_t));
			if ((ssize_t)node_size > room)
				goto full;
			if ((rc = mdb_page_new(mc, P_OVERFLOW, ovpages, &ofp)))
				return rc;
			DPRINTF(("allocated overflow page %"Z"u", ofp->mp_pgno));
			flags |= F_BIGDATA;
			goto update;
		} else {
			node_size += data->mv_size;
		}
	}
	node_size = EVEN(node_size);
	if ((ssize_t)node_size > room)
		goto full;

update:
	/* Move higher pointers up one slot. */
	for (i = NUMKEYS(mp); i > indx; i--)
		mp->mp_ptrs[i] = mp->mp_ptrs[i - 1];

	/* Adjust free space offsets. */
	ofs = mp->mp_upper - node_size;
	mdb_cassert(mc, ofs >= mp->mp_lower + sizeof(indx_t));
	mp->mp_ptrs[indx] = ofs;
	mp->mp_upper = ofs;
	mp->mp_lower += sizeof(indx_t);

	/* Write the node data. */
	node = NODEPTR(mp, indx);
	node->mn_ksize = (key == NULL) ? 0 : key->mv_size;
	node->mn_flags = flags;
	if (IS_LEAF(mp))
		SETDSZ(node,data->mv_size);
	else
		SETPGNO(node,pgno);

	if (key)
		memcpy(NODEKEY(node), key->mv_data, key->mv_size);

	if (IS_LEAF(mp)) {
		ndata = NODEDATA(node);
		if (ofp == NULL) {
			if (F_ISSET(flags, F_BIGDATA))
				memcpy(ndata, data->mv_data, sizeof(pgno_t));
			else if (F_ISSET(flags, MDB_RESERVE))
				data->mv_data = ndata;
			else
				memcpy(ndata, data->mv_data, data->mv_size);
		} else {
			memcpy(ndata, &ofp->mp_pgno, sizeof(pgno_t));
			ndata = METADATA(ofp);
			if (F_ISSET(flags, MDB_RESERVE))
				data->mv_data = ndata;
			else
				memcpy(ndata, data->mv_data, data->mv_size);
		}
	}

	return MDB_SUCCESS;

full:
	DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
		mdb_dbg_pgno(mp), NUMKEYS(mp)));
	DPRINTF(("upper-lower = %u - %u = %"Z"d", mp->mp_upper,mp->mp_lower,room));
	DPRINTF(("node size = %"Z"u", node_size));
	mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
	return MDB_PAGE_FULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq $0x8, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xe(%rax), %eax
movq -0x60(%rbp), %rcx
movzwl 0xc(%rcx), %ecx
cmpl %ecx, %eax
jl 0xc631
jmp 0xc65d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0xb506(%rip), %rsi      # 0x17b4a
leaq 0xb51c(%rip), %rdx      # 0x17b67
leaq 0xadbd(%rip), %rcx      # 0x1740f
movl $0x1b7e, %r8d           # imm = 0x1B7E
callq 0x133a0
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x20, %eax
cmpl $0x20, %eax
jne 0xc71a
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x60(%rbp), %rax
addq $0x10, %rax
movzwl -0x12(%rbp), %ecx
imull -0x74(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
movzwl -0x12(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x78(%rbp)
jle 0xc6d2
movq -0x80(%rbp), %rdi
movslq -0x74(%rbp), %rax
addq %rax, %rdi
movq -0x80(%rbp), %rsi
movl -0x78(%rbp), %eax
imull -0x74(%rbp), %eax
movslq %eax, %rdx
callq 0x22c0
movq -0x80(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
movslq -0x74(%rbp), %rdx
callq 0x2230
movq -0x60(%rbp), %rax
movzwl 0xc(%rax), %ecx
addq $0x2, %rcx
movw %cx, 0xc(%rax)
movslq -0x74(%rbp), %rdx
subq $0x2, %rdx
movq -0x60(%rbp), %rax
movzwl 0xe(%rax), %ecx
subq %rdx, %rcx
movw %cx, 0xe(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xcb5c
movq -0x60(%rbp), %rax
movzwl 0xe(%rax), %eax
movq -0x60(%rbp), %rcx
movzwl 0xc(%rcx), %ecx
subl %ecx, %eax
movzwl %ax, %eax
subq $0x2, %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xc74d
movq -0x20(%rbp), %rax
movq (%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
jne 0xc87a
cmpq $0x0, -0x20(%rbp)
je 0xc771
cmpq $0x0, -0x28(%rbp)
je 0xc771
jmp 0xc79d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0xb3f0(%rip), %rsi      # 0x17b74
leaq 0xb3dc(%rip), %rdx      # 0x17b67
leaq 0xac7d(%rip), %rcx      # 0x1740f
movl $0x1b9a, %r8d           # imm = 0x1B9A
callq 0x133a0
movl -0x34(%rbp), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0xc7b9
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
jmp 0xc878
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
addq (%rcx), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0xdc(%rcx), %ecx
cmpq %rcx, %rax
jbe 0xc867
movq -0x28(%rbp), %rax
movq (%rax), %rax
addq $0xf, %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0x10(%rcx), %ecx
xorl %edx, %edx
divq %rcx
addq $0x1, %rax
movl %eax, -0x84(%rbp)
movq -0x40(%rbp), %rax
addq $0x8, %rax
addq $0x1, %rax
andq $-0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jle 0xc82b
jmp 0xcb44
movq -0x10(%rbp), %rdi
movl -0x84(%rbp), %edx
movl $0x4, %esi
leaq -0x68(%rbp), %rcx
callq 0x9a40
movl %eax, -0x88(%rbp)
cmpl $0x0, %eax
je 0xc85c
movl -0x88(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xcb5c
movl -0x34(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xc89b
movq -0x28(%rbp), %rax
movq (%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0xc878
jmp 0xc87a
movq -0x40(%rbp), %rax
addq $0x1, %rax
andq $-0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jle 0xc899
jmp 0xcb44
jmp 0xc89b
movq -0x60(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jbe 0xc8de
movq -0x60(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movw 0x10(%rax,%rcx,2), %dx
movq -0x60(%rbp), %rax
movl -0x38(%rbp), %ecx
movw %dx, 0x10(%rax,%rcx,2)
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xc8ab
movq -0x60(%rbp), %rax
movzwl 0xe(%rax), %eax
subq -0x40(%rbp), %rax
movw %ax, -0x4a(%rbp)
movzwl -0x4a(%rbp), %eax
movq -0x60(%rbp), %rcx
movzwl 0xc(%rcx), %ecx
addq $0x2, %rcx
cmpq %rcx, %rax
jb 0xc905
jmp 0xc931
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0xb268(%rip), %rsi      # 0x17b80
leaq 0xb248(%rip), %rdx      # 0x17b67
leaq 0xaae9(%rip), %rcx      # 0x1740f
movl $0x1bbb, %r8d           # imm = 0x1BBB
callq 0x133a0
movw -0x4a(%rbp), %dx
movq -0x60(%rbp), %rax
movzwl -0x12(%rbp), %ecx
movw %dx, 0x10(%rax,%rcx,2)
movw -0x4a(%rbp), %cx
movq -0x60(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x60(%rbp), %rax
movzwl 0xc(%rax), %ecx
addq $0x2, %rcx
movw %cx, 0xc(%rax)
movq -0x60(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl -0x12(%rbp), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xc98b
xorl %eax, %eax
movq %rax, -0x90(%rbp)
jmp 0xc999
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, 0x6(%rax)
movl -0x34(%rbp), %eax
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
jne 0xc9fa
jmp 0xc9cb
movq -0x28(%rbp), %rax
movq (%rax), %rax
andq $0xffff, %rax           # imm = 0xFFFF
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
shrq $0x10, %rax
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xca38
jmp 0xc9fc
movq -0x30(%rbp), %rax
andq $0xffff, %rax           # imm = 0xFFFF
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, (%rax)
movq -0x30(%rbp), %rax
shrq $0x10, %rax
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x30(%rbp), %rax
shrq $0x20, %rax
movw %ax, %cx
movq -0x58(%rbp), %rax
movw %cx, 0x4(%rax)
jmp 0xca38
cmpq $0x0, -0x20(%rbp)
je 0xca5b
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
callq 0x2230
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x2, %eax
cmpl $0x2, %eax
jne 0xcb3b
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq -0x58(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xcae8
movl -0x34(%rbp), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0xcaaf
movq -0x70(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
jmp 0xcae6
movl -0x34(%rbp), %eax
andl $0x10000, %eax          # imm = 0x10000
cmpl $0x10000, %eax          # imm = 0x10000
jne 0xcacc
movq -0x70(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0xcae4
movq -0x70(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
callq 0x2230
jmp 0xcae6
jmp 0xcb39
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq -0x68(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x70(%rbp)
movl -0x34(%rbp), %eax
andl $0x10000, %eax          # imm = 0x10000
cmpl $0x10000, %eax          # imm = 0x10000
jne 0xcb1f
movq -0x70(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0xcb37
movq -0x70(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
callq 0x2230
jmp 0xcb39
jmp 0xcb3b
movl $0x0, -0x4(%rbp)
jmp 0xcb5c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl $0xffff87be, -0x4(%rbp) # imm = 0xFFFF87BE
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)