// File: /workspace/github_cpp_makefile_star2_O2/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_alloc
static int
mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
{
#ifdef MDB_PARANOID	/* Seems like we can ignore this now */
	/* Get at most <Max_retries> more freeDB records once me_pghead
	 * has enough pages.  If not enough, use new pages from the map.
	 * If <Paranoid> and mc is updating the freeDB, only get new
	 * records if me_pghead is empty. Then the freelist cannot play
	 * catch-up with itself by growing while trying to save it.
	 */
	enum { Paranoid = 1, Max_retries = 500 };
#else
	enum { Paranoid = 0, Max_retries = INT_MAX /*infinite*/ };
#endif
	int rc, retry = num * 60;
	MDB_txn *txn = mc->mc_txn;
	MDB_env *env = txn->mt_env;
	pgno_t pgno, *mop = env->me_pghead;
	unsigned i, j, mop_len = mop ? mop[0] : 0, n2 = num-1;
	MDB_page *np;
	txnid_t oldest = 0, last;
	MDB_cursor_op op;
	MDB_cursor m2;
	int found_old = 0;

	/* If there are any loose pages, just use them */
	if (num == 1 && txn->mt_loose_pgs) {
		np = txn->mt_loose_pgs;
		txn->mt_loose_pgs = NEXT_LOOSE_PAGE(np);
		txn->mt_loose_count--;
		DPRINTF(("db %d use loose page %"Z"u", DDBI(mc),
				np->mp_pgno));
		*mp = np;
		return MDB_SUCCESS;
	}

	*mp = NULL;

	/* If our dirty list is already full, we can't do anything */
	if (txn->mt_dirty_room == 0) {
		rc = MDB_TXN_FULL;
		goto fail;
	}

	for (op = MDB_FIRST;; op = MDB_NEXT) {
		MDB_val key, data;
		MDB_node *leaf;
		pgno_t *idl;

		/* Seek a big enough contiguous page range. Prefer
		 * pages at the tail, just truncating the list.
		 */
		if (mop_len > n2) {
			i = mop_len;
			do {
				pgno = mop[i];
				if (mop[i-n2] == pgno+n2)
					goto search_done;
			} while (--i > n2);
			if (--retry < 0)
				break;
		}

		if (op == MDB_FIRST) {	/* 1st iteration */
			/* Prepare to fetch more and coalesce */
			last = env->me_pglast;
			oldest = env->me_pgoldest;
			mdb_cursor_init(&m2, txn, FREE_DBI, NULL);
			if (last) {
				op = MDB_SET_RANGE;
				key.mv_data = &last; /* will look up last+1 */
				key.mv_size = sizeof(last);
			}
			if (Paranoid && mc->mc_dbi == FREE_DBI)
				retry = -1;
		}
		if (Paranoid && retry < 0 && mop_len)
			break;

		last++;
		/* Do not fetch more if the record will be too recent */
		if (oldest <= last) {
			if (!found_old) {
				oldest = mdb_find_oldest(txn);
				env->me_pgoldest = oldest;
				found_old = 1;
			}
			if (oldest <= last)
				break;
		}
		rc = mdb_cursor_get(&m2, &key, NULL, op);
		if (rc) {
			if (rc == MDB_NOTFOUND)
				break;
			goto fail;
		}
		last = *(txnid_t*)key.mv_data;
		if (oldest <= last) {
			if (!found_old) {
				oldest = mdb_find_oldest(txn);
				env->me_pgoldest = oldest;
				found_old = 1;
			}
			if (oldest <= last)
				break;
		}
		np = m2.mc_pg[m2.mc_top];
		leaf = NODEPTR(np, m2.mc_ki[m2.mc_top]);
		if ((rc = mdb_node_read(txn, leaf, &data)) != MDB_SUCCESS)
			return rc;

		idl = (MDB_ID *) data.mv_data;
		i = idl[0];
		if (!mop) {
			if (!(env->me_pghead = mop = mdb_midl_alloc(i))) {
				rc = ENOMEM;
				goto fail;
			}
		} else {
			if ((rc = mdb_midl_need(&env->me_pghead, i)) != 0)
				goto fail;
			mop = env->me_pghead;
		}
		env->me_pglast = last;
#if (MDB_DEBUG) > 1
		DPRINTF(("IDL read txn %"Z"u root %"Z"u num %u",
			last, txn->mt_dbs[FREE_DBI].md_root, i));
		for (j = i; j; j--)
			DPRINTF(("IDL %"Z"u", idl[j]));
#endif
		/* Merge in descending sorted order */
		mdb_midl_xmerge(mop, idl);
		mop_len = mop[0];
	}

	/* Use new pages from the map when nothing suitable in the freeDB */
	i = 0;
	pgno = txn->mt_next_pgno;
	if (pgno + num >= env->me_maxpg) {
			DPUTS("DB size maxed out");
			rc = MDB_MAP_FULL;
			goto fail;
	}

search_done:
	if (env->me_flags & MDB_WRITEMAP) {
		np = (MDB_page *)(env->me_map + env->me_psize * pgno);
	} else {
		if (!(np = mdb_page_malloc(txn, num))) {
			rc = ENOMEM;
			goto fail;
		}
	}
	if (i) {
		mop[0] = mop_len -= num;
		/* Move any stragglers down */
		for (j = i-num; j < mop_len; )
			mop[++j] = mop[++i];
	} else {
		txn->mt_next_pgno = pgno + num;
	}
	np->mp_pgno = pgno;
	mdb_page_dirty(txn, np);
	*mp = np;

	return MDB_SUCCESS;

fail:
	txn->mt_flags |= MDB_TXN_ERROR;
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq 0x18(%rdi), %r12
movq 0x20(%r12), %rcx
movq 0xb0(%rcx), %r15
testq %r15, %r15
je 0x6aec
movl (%r15), %edi
jmp 0x6aee
xorl %edi, %edi
cmpl $0x1, %esi
jne 0x6b13
movq 0x30(%r12), %rax
testq %rax, %rax
je 0x6b13
movq 0x30(%rax), %rcx
movq %rcx, 0x30(%r12)
decl 0x38(%r12)
movq %rax, (%rdx)
jmp 0x6e20
andq $0x0, (%rdx)
cmpl $0x0, 0x80(%r12)
movq %r12, 0x20(%rsp)
je 0x6dae
movq %rdx, 0x48(%rsp)
imull $0x3c, %esi, %eax
movl %eax, 0x2c(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0xb0(%rcx), %rax
movq %rax, 0x38(%rsp)
leal -0x1(%rsi), %ebp
movl %ebp, %ebx
pushq $0x1
popq %rax
movl %eax, %r14d
subl %esi, %r14d
xorl %r13d, %r13d
movb $0x1, %al
xorl %r8d, %r8d
movl $0x0, 0x4(%rsp)
movq %rsi, 0x30(%rsp)
movslq %esi, %rcx
movq %rcx, 0x40(%rsp)
cmpl %ebp, %edi
movq %rdi, 0x8(%rsp)
jbe 0x6bba
movq %r13, %rsi
movl %edi, %r12d
movl %r12d, %ecx
movq (%r15,%rcx,8), %r13
leal (%r14,%r12), %ecx
leaq (%rbx,%r13), %rdx
cmpq %rdx, (%r15,%rcx,8)
je 0x6d82
decl %r12d
cmpl %ebp, %r12d
ja 0x6b82
cmpl $0x0, 0x2c(%rsp)
movq 0x20(%rsp), %r12
jle 0x6e42
movq %rsi, %r13
decl 0x2c(%rsp)
testb $0x1, %al
je 0x6c11
movq 0x18(%rsp), %rcx
movq 0xb8(%rcx), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rcx), %r13
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4451
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x6c18
leaq 0x10(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq $0x8, 0x50(%rsp)
pushq $0x11
popq %rcx
movl %ecx, %r8d
movq 0x8(%rsp), %rdi
jmp 0x6c22
movq 0x10(%rsp), %rax
jmp 0x6c22
xorl %eax, %eax
movq 0x8(%rsp), %rdi
xorl %r8d, %r8d
incq %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r13
ja 0x6c6d
cmpl $0x0, 0x4(%rsp)
jne 0x6c5d
movq %r12, %rdi
movq %r8, %r13
callq 0xa0ee
movq %r13, %r8
movq 0x8(%rsp), %rdi
movq %rax, %r13
movq 0x18(%rsp), %rax
movq %r13, 0xa8(%rax)
movq 0x10(%rsp), %rax
pushq $0x1
popq %rcx
movl %ecx, 0x4(%rsp)
cmpq %rax, %r13
jbe 0x6e42
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
movl %r8d, %ecx
callq 0x5b27
testl %eax, %eax
jne 0x6e36
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r13
ja 0x6cd3
cmpl $0x0, 0x4(%rsp)
jne 0x6cbe
movq %r12, %rdi
callq 0xa0ee
movq %rax, %r13
movq 0x18(%rsp), %rax
movq %r13, 0xa8(%rax)
movq 0x10(%rsp), %rax
pushq $0x1
popq %rcx
movl %ecx, 0x4(%rsp)
cmpq %rax, %r13
movq 0x8(%rsp), %rdi
jbe 0x6e42
movzwl 0xb2(%rsp), %eax
movq 0xb8(%rsp,%rax,8), %rcx
movzwl 0x1b8(%rsp,%rax,2), %eax
movzwl 0x10(%rcx,%rax,2), %esi
addq %rcx, %rsi
movq %r12, %rdi
leaq 0x60(%rsp), %rdx
callq 0x5f9f
testl %eax, %eax
jne 0x6e75
movq %r13, 0x8(%rsp)
movq 0x68(%rsp), %r13
movl (%r13), %esi
testq %r15, %r15
je 0x6d35
movq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0xb1eb
testl %eax, %eax
jne 0x6e6e
movq (%r15), %r15
jmp 0x6d4c
movl %esi, %edi
callq 0xb17a
movq %rax, %r15
movq 0x38(%rsp), %rax
movq %r15, (%rax)
testq %r15, %r15
je 0x6da9
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, 0xb8(%rcx)
movq %r15, %rdi
movq %r13, %rsi
callq 0xb390
movl (%r15), %edi
xorl %eax, %eax
pushq $0x8
popq %rcx
movl %ecx, %r8d
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %r13
jmp 0x6b6b
movq 0x18(%rsp), %rcx
testb $0x8, 0xe(%rcx)
jne 0x6dbe
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x71ca
movq 0x8(%rsp), %rdi
movq %rax, %r14
testq %rax, %rax
jne 0x6dca
pushq $0xc
popq %rdx
jmp 0x6db3
movl $0xffff87bc, %edx       # imm = 0xFFFF87BC
movq 0x20(%rsp), %rax
orb $0x2, 0x7c(%rax)
jmp 0x6e22
movl 0x10(%rcx), %r14d
imulq %r13, %r14
addq 0x38(%rcx), %r14
testl %r12d, %r12d
je 0x6df7
movq 0x30(%rsp), %rcx
subl %ecx, %edi
movq %rdi, (%r15)
movl %r12d, %eax
subl %ecx, %eax
incl %r12d
cmpq %rdi, %rax
jae 0x6e08
movl %r12d, %ecx
movq (%r15,%rcx,8), %rcx
movq %rcx, 0x8(%r15,%rax,8)
incq %rax
jmp 0x6dde
movq 0x40(%rsp), %rcx
addq %r13, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x10(%rax)
movq %r13, (%r14)
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0xa139
movq 0x48(%rsp), %rax
movq %r14, (%rax)
xorl %edx, %edx
movl %edx, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
movq 0x8(%rsp), %rdi
jne 0x6e6e
movq 0x10(%r12), %r13
movq 0x40(%rsp), %rax
addq %r13, %rax
movl $0xffff87b8, %edx       # imm = 0xFFFF87B8
xorl %r12d, %r12d
movq 0x18(%rsp), %rcx
cmpq 0x80(%rcx), %rax
jb 0x6d87
jmp 0x6db3
movl %eax, %edx
jmp 0x6db3
movl %eax, %edx
jmp 0x6e22