// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r12
movzwl 0x42(%rdi), %eax
movq 0x48(%rdi,%rax,8), %rbp
movq $0x0, 0x20(%rsp)
movzwl 0xe(%rbp), %eax
cmpw 0xc(%rbp), %ax
jb 0x8ddb
movq %rdx, %r14
movl %esi, %r13d
movzwl 0xa(%rbp), %eax
testb $0x20, %al
jne 0x8b5a
movzwl 0xe(%rbp), %edx
subw 0xc(%rbp), %dx
movzwl %dx, %r10d
testq %r14, %r14
je 0x8bbc
movq (%r14), %rdx
addq $0x8, %rdx
jmp 0x8bc1
movq 0x28(%r12), %rax
movslq (%rax), %rbx
movzwl %r13w, %ecx
movq %rcx, %rax
imulq %rbx, %rax
leaq (%rax,%rbp), %r15
addq $0x10, %r15
movzwl 0xc(%rbp), %eax
addl $-0x10, %eax
shrl %eax
subl %ecx, %eax
jle 0x8b94
leaq (%r15,%rbx), %rdi
imull %ebx, %eax
movslq %eax, %rdx
movq %r15, %rsi
callq 0x22d0
movq 0x8(%r14), %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x2240
addw $0x2, 0xc(%rbp)
movzwl 0xe(%rbp), %eax
subl %ebx, %eax
addl $0x2, %eax
movw %ax, 0xe(%rbp)
xorl %ebx, %ebx
jmp 0x8dca
movl $0x8, %edx
addq $-0x2, %r10
testb $0x2, %al
je 0x8c1a
testq %r14, %r14
je 0x8dfc
testq %rcx, %rcx
je 0x8dfc
testb $0x1, %r9b
jne 0x8c16
movq (%rcx), %rax
leaq (%rax,%rdx), %r15
movq 0x18(%r12), %rsi
movq 0x20(%rsi), %rsi
movl 0xdc(%rsi), %edi
cmpq %rdi, %r15
jbe 0x8c1d
addq $0x9, %rdx
movq %rdx, %r15
andq $-0x2, %r15
cmpq %r10, %r15
jle 0x8c40
movl $0x2, %eax
jmp 0x8c9f
addq $0x8, %rdx
movq %rdx, %r15
incq %r15
andq $-0x2, %r15
cmpq %r10, %r15
jle 0x8cad
movq 0x18(%r12), %rax
orl $0x2, 0x7c(%rax)
movl $0xffff87be, %ebx       # imm = 0xFFFF87BE
jmp 0x8dca
movq %r8, 0x10(%rsp)
movl %r9d, 0xc(%rsp)
movq %rcx, 0x18(%rsp)
addq $0xf, %rax
movl 0x10(%rsi), %ecx
xorl %edx, %edx
divq %rcx
leal 0x1(%rax), %edx
leaq 0x20(%rsp), %rcx
movq %r12, %rdi
movl $0x4, %esi
callq 0x709b
testl %eax, %eax
je 0x8c87
movl %eax, %ebx
movl $0x1, %eax
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %r9d
jmp 0x8c9a
movl 0xc(%rsp), %r9d
orl $0x1, %r9d
movl $0x3, %eax
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
cmpl $0x2, %eax
je 0x8c2d
cmpl $0x3, %eax
jne 0x8dca
movzwl 0xc(%rbp), %edi
movl %edi, %edx
addl $-0x10, %edx
shrl %edx
movzwl %r13w, %eax
cmpl %eax, %edx
jbe 0x8cd2
movzwl 0xe(%rbp,%rdx,2), %esi
movw %si, 0x10(%rbp,%rdx,2)
decq %rdx
cmpq %rax, %rdx
ja 0x8cc0
movzwl 0xe(%rbp), %edx
subl %r15d, %edx
movzwl %dx, %edx
leal 0x2(%rdi), %esi
cmpl %edx, %esi
ja 0x8e1d
movw %dx, 0x10(%rbp,%rax,2)
movw %dx, 0xe(%rbp)
addw $0x2, 0xc(%rbp)
leaq (%rdx,%rbp), %r15
testq %r14, %r14
je 0x8d04
movzwl (%r14), %eax
jmp 0x8d06
xorl %eax, %eax
movw %ax, 0x6(%r15)
movw %r9w, 0x4(%r15)
testb $0x2, 0xa(%rbp)
jne 0x8d33
movq %rcx, %r12
movw %r8w, (%r15)
movl %r8d, %eax
shrl $0x10, %eax
movw %ax, 0x2(%r15)
shrq $0x20, %r8
movw %r8w, 0x4(%r15)
jmp 0x8d3b
movq %rcx, %r12
movl (%rcx), %eax
movl %eax, (%r15)
testq %r14, %r14
je 0x8d56
leaq 0x8(%r15), %rdi
movq (%r14), %rdx
movq 0x8(%r14), %rsi
movl %r9d, %ebx
callq 0x2240
movl %ebx, %r9d
xorl %ebx, %ebx
testb $0x2, 0xa(%rbp)
je 0x8dca
movzwl 0x6(%r15), %eax
leaq (%r15,%rax), %rdi
addq $0x8, %rdi
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x8d94
movq (%rax), %rcx
movq %rcx, (%rdi)
addq $0x10, %rax
btl $0x10, %r9d
jb 0x8db1
movq (%r12), %rdx
movq 0x8(%r12), %rsi
movq %rax, %rdi
jmp 0x8daa
testb $0x1, %r9b
jne 0x8db8
btl $0x10, %r9d
jb 0x8dc5
movq (%r12), %rdx
movq 0x8(%r12), %rsi
callq 0x2240
jmp 0x8dca
movq %rax, 0x8(%r12)
jmp 0x8dca
movq 0x8(%r12), %rax
movq (%rax), %rax
movq %rax, (%rdi)
jmp 0x8dca
movq %rdi, 0x8(%r12)
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x6d43(%rip), %rsi      # 0xfb2e
leaq 0x6d59(%rip), %rdx      # 0xfb4b
movl $0x1b7e, %ecx           # imm = 0x1B7E
callq 0xc43e
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x6d4c(%rip), %rsi      # 0xfb58
leaq 0x6d38(%rip), %rdx      # 0xfb4b
movl $0x1b9a, %ecx           # imm = 0x1B9A
callq 0xc43e
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x6d37(%rip), %rsi      # 0xfb64
leaq 0x6d17(%rip), %rdx      # 0xfb4b
movl $0x1bbb, %ecx           # imm = 0x1BBB
callq 0xc43e