// File: /workspace/github_cpp_makefile_star2_O3/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
movq $0x0, 0x10(%rsp)
movzwl 0xc(%rbp), %r10d
movzwl 0xe(%rbp), %edx
cmpw %r10w, %dx
jb 0x8a8b
movl %esi, %r13d
movzwl 0xa(%rbp), %eax
testb $0x20, %al
jne 0x882c
subl %r10d, %edx
movzwl %dx, %edx
testq %r14, %r14
je 0x888e
movq (%r14), %rsi
addq $0x8, %rsi
jmp 0x8893
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 0x8866
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 0x8a7a
movl $0x8, %esi
addq $-0x2, %rdx
testb $0x2, %al
je 0x893f
testq %r14, %r14
je 0x8aac
testq %rcx, %rcx
je 0x8aac
testb $0x1, %r9b
jne 0x893b
movq (%rcx), %rax
leaq (%rax,%rsi), %r15
movq 0x18(%r12), %rdi
movq 0x20(%rdi), %rbx
movl 0xdc(%rbx), %r11d
cmpq %r11, %r15
jbe 0x8942
addq $0x9, %rsi
movq %rsi, %r15
andq $-0x2, %r15
cmpq %rdx, %r15
jg 0x8953
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
leaq 0x10(%rsp), %rcx
movq %r12, %rdi
movl $0x4, %esi
callq 0x6d12
movl %eax, %ebx
testl %eax, %eax
jne 0x8a7a
movl 0xc(%rsp), %r9d
orl $0x1, %r9d
movzwl 0xc(%rbp), %r10d
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
jmp 0x8961
addq $0x8, %rsi
movq %rsi, %r15
incq %r15
andq $-0x2, %r15
cmpq %rdx, %r15
jle 0x8961
movq 0x18(%r12), %rdi
orb $0x2, 0x7c(%rdi)
movl $0xffff87be, %ebx       # imm = 0xFFFF87BE
jmp 0x8a7a
movzwl %r10w, %edi
leal -0x10(%rdi), %edx
shrl %edx
movzwl %r13w, %eax
cmpl %eax, %edx
jbe 0x8988
movzwl 0xe(%rbp,%rdx,2), %esi
movw %si, 0x10(%rbp,%rdx,2)
leaq -0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq %rax, %rsi
ja 0x8972
movzwl 0xe(%rbp), %edx
subl %r15d, %edx
movzwl %dx, %edx
addl $0x2, %edi
cmpl %edx, %edi
ja 0x8acd
movw %dx, 0x10(%rbp,%rax,2)
movw %dx, 0xe(%rbp)
addw $0x2, 0xc(%rbp)
leaq (%rdx,%rbp), %r15
testq %r14, %r14
je 0x89ba
movzwl (%r14), %r12d
jmp 0x89bd
xorl %r12d, %r12d
movw %r12w, 0x6(%r15)
movw %r9w, 0x4(%r15)
movzwl 0xa(%rbp), %eax
andw $0x2, %ax
jne 0x89e2
movq %rcx, %r13
movq %r8, %rcx
shrq $0x20, %rcx
movw %cx, 0x4(%r15)
jmp 0x89e8
movq %rcx, %r13
movq (%rcx), %r8
movl %r8d, (%r15)
testq %r14, %r14
je 0x8a0d
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 0x8a7a
movzwl %r12w, %eax
leaq (%r15,%rax), %rdi
addq $0x8, %rdi
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x8a48
movq (%rax), %rcx
movq %rcx, (%rdi)
addq $0x10, %rax
btl $0x10, %r9d
jb 0x8a64
movq (%r13), %rdx
movq 0x8(%r13), %rsi
movq %rax, %rdi
jmp 0x8a5d
testb $0x1, %r9b
jne 0x8a6a
btl $0x10, %r9d
jb 0x8a76
movq (%r13), %rdx
movq 0x8(%r13), %rsi
callq 0x2240
jmp 0x8a7a
movq %rax, 0x8(%r13)
jmp 0x8a7a
movq 0x8(%r13), %rax
movq (%rax), %rax
movq %rax, (%rdi)
jmp 0x8a7a
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 0x70d3(%rip), %rsi      # 0xfb6e
leaq 0x70e9(%rip), %rdx      # 0xfb8b
movl $0x1b7e, %ecx           # imm = 0x1B7E
callq 0xbfbd
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x70dc(%rip), %rsi      # 0xfb98
leaq 0x70c8(%rip), %rdx      # 0xfb8b
movl $0x1b9a, %ecx           # imm = 0x1B9A
callq 0xbfbd
movq 0x18(%r12), %rax
movq 0x20(%rax), %rdi
leaq 0x70c7(%rip), %rsi      # 0xfba4
leaq 0x70a7(%rip), %rdx      # 0xfb8b
movl $0x1bbb, %ecx           # imm = 0x1BBB
callq 0xbfbd