// File: /workspace/github_cpp_makefile_star2_O2/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 %rdx, %r14
movq %rdi, %r12
movzwl 0x42(%rdi), %eax
movq 0x48(%rdi,%rax,8), %rbp
andq $0x0, 0x10(%rsp)
movzwl 0xc(%rbp), %r10d
movzwl 0xe(%rbp), %edx
cmpw %r10w, %dx
jb 0x83c4
movl %esi, %r13d
movzwl 0xa(%rbp), %eax
testb $0x20, %al
jne 0x8165
subl %r10d, %edx
movzwl %dx, %edx
testq %r14, %r14
je 0x81c7
movq (%r14), %rsi
addq $0x8, %rsi
jmp 0x81ca
movzwl %r10w, %eax
movq 0x28(%r12), %rcx
movslq (%rcx), %rbx
movzwl %r13w, %ecx
movq %rcx, %rdx
imulq %rbx, %rdx
leaq (%rdx,%rbp), %r15
addq $0x10, %r15
addl $-0x10, %eax
shrl %eax
subl %ecx, %eax
jle 0x819f
leaq (%r15,%rbx), %rdi
imull %ebx, %eax
movslq %eax, %rdx
movq %r15, %rsi
callq 0x22e0
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 0x83b3
pushq $0x8
popq %rsi
addq $-0x2, %rdx
testb $0x2, %al
je 0x8270
testq %r14, %r14
je 0x83e5
testq %rcx, %rcx
je 0x83e5
testb $0x1, %r9b
jne 0x826c
movq (%rcx), %rax
leaq (%rax,%rsi), %r15
movq 0x18(%r12), %rdi
movq 0x20(%rdi), %rbx
movl 0xdc(%rbx), %r11d
cmpq %r11, %r15
jbe 0x8273
addq $0x9, %rsi
movq %rsi, %r15
andq $-0x2, %r15
cmpq %rdx, %r15
jg 0x8284
movq %r8, 0x18(%rsp)
movl %r9d, 0xc(%rsp)
movq %rcx, 0x20(%rsp)
addq $0xf, %rax
movl 0x10(%rbx), %ecx
xorl %edx, %edx
divq %rcx
leal 0x1(%rax), %edx
pushq $0x4
popq %rsi
leaq 0x10(%rsp), %rcx
movq %r12, %rdi
callq 0x682b
movl %eax, %ebx
testl %eax, %eax
jne 0x83b3
movl 0xc(%rsp), %r9d
orl $0x1, %r9d
movzwl 0xc(%rbp), %r10d
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
jmp 0x8292
addq $0x8, %rsi
movq %rsi, %r15
incq %r15
andq $-0x2, %r15
cmpq %rdx, %r15
jle 0x8292
movq 0x18(%r12), %rdi
orb $0x2, 0x7c(%rdi)
movl $0xffff87be, %ebx       # imm = 0xFFFF87BE
jmp 0x83b3
movl $0xfffffff0, %edx       # imm = 0xFFFFFFF0
movzwl %r10w, %r10d
leal (%r10,%rdx), %esi
shrl %esi
movzwl %r13w, %eax
cmpq %rax, %rsi
jbe 0x82bf
leal (%rdx,%rsi), %edi
addl $0xf, %edi
movzwl 0x10(%rbp,%rdi,2), %edi
movw %di, 0x10(%rbp,%rsi,2)
decq %rsi
jmp 0x82a5
movzwl 0xe(%rbp), %edx
subl %r15d, %edx
movzwl %dx, %edx
addl $0x2, %r10d
cmpl %edx, %r10d
ja 0x8406
movw %dx, 0x10(%rbp,%rax,2)
movw %dx, 0xe(%rbp)
addw $0x2, 0xc(%rbp)
leaq (%rdx,%rbp), %r15
testq %r14, %r14
je 0x82f3
movzwl (%r14), %r12d
jmp 0x82f6
xorl %r12d, %r12d
movw %r12w, 0x6(%r15)
movw %r9w, 0x4(%r15)
movzwl 0xa(%rbp), %eax
andw $0x2, %ax
jne 0x831b
movq %rcx, %r13
movq %r8, %rcx
shrq $0x20, %rcx
movw %cx, 0x4(%r15)
jmp 0x8321
movq %rcx, %r13
movq (%rcx), %r8
movl %r8d, (%r15)
testq %r14, %r14
je 0x8346
leaq 0x8(%r15), %rdi
movq (%r14), %rdx
movq 0x8(%r14), %rsi
movl %r9d, %ebx
callq 0x2240
movl %ebx, %r9d
movzwl 0xa(%rbp), %eax
andl $0x2, %eax
xorl %ebx, %ebx
testw %ax, %ax
je 0x83b3
movzwl %r12w, %eax
leaq (%r15,%rax), %rdi
addq $0x8, %rdi
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x8381
movq (%rax), %rcx
movq %rcx, (%rdi)
addq $0x10, %rax
btl $0x10, %r9d
jb 0x839d
movq (%r13), %rdx
movq 0x8(%r13), %rsi
movq %rax, %rdi
jmp 0x8396
testb $0x1, %r9b
jne 0x83a3
btl $0x10, %r9d
jb 0x83af
movq (%r13), %rdx
movq 0x8(%r13), %rsi
callq 0x2240
jmp 0x83b3
movq %rax, 0x8(%r13)
jmp 0x83b3
movq 0x8(%r13), %rax
movq (%rax), %rax
movq %rax, (%rdi)
jmp 0x83b3
movq %rdi, 0x8(%r13)
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 0x677a(%rip), %rsi      # 0xeb4e
leaq 0x6790(%rip), %rdx      # 0xeb6b
movl $0x1b7e, %ecx           # imm = 0x1B7E
callq 0xb672
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x6783(%rip), %rsi      # 0xeb78
leaq 0x676f(%rip), %rdx      # 0xeb6b
movl $0x1b9a, %ecx           # imm = 0x1B9A
callq 0xb672
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x676e(%rip), %rsi      # 0xeb84
leaq 0x674e(%rip), %rdx      # 0xeb6b
movl $0x1bbb, %ecx           # imm = 0x1BBB
callq 0xb672