// File: /workspace/github_cpp_makefile_star2_O1/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 $0x208, %rsp            # imm = 0x208
movq 0x18(%rdi), %r12
movq 0x20(%r12), %rcx
movq 0xb0(%rcx), %r15
testq %r15, %r15
je 0x733a
movl (%r15), %edi
jmp 0x733c
xorl %edi, %edi
cmpl $0x1, %esi
jne 0x7361
movq 0x30(%r12), %rax
testq %rax, %rax
je 0x7361
movq 0x30(%rax), %rcx
movq %rcx, 0x30(%r12)
decl 0x38(%r12)
movq %rax, (%rdx)
jmp 0x7759
movq $0x0, (%rdx)
cmpl $0x0, 0x80(%r12)
je 0x769c
movq %rdx, 0x50(%rsp)
movq %r12, 0x28(%rsp)
imull $0x3c, %esi, %eax
movq %rax, 0x40(%rsp)
movq %rcx, 0x20(%rsp)
leaq 0xb0(%rcx), %rax
movq %rax, 0x38(%rsp)
leal -0x1(%rsi), %ebp
movl %ebp, %eax
movq %rax, 0x58(%rsp)
movl $0x1, %r14d
movq %rsi, 0x30(%rsp)
subl %esi, %r14d
movq $0x0, 0x18(%rsp)
movb $0x1, %cl
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
cmpl %ebp, %edi
jbe 0x740f
movl %edi, %r13d
movq 0x58(%rsp), %rsi
movl %r13d, %eax
movq (%r15,%rax,8), %rbx
leal (%r14,%r13), %eax
leaq (%rbx,%rsi), %rdx
cmpq %rdx, (%r15,%rax,8)
je 0x7508
decl %r13d
cmpl %ebp, %r13d
ja 0x73d4
movq 0x40(%rsp), %rax
leal -0x1(%rax), %edx
testl %eax, %eax
jle 0x75ab
movl %ebp, %r13d
movl %edx, %eax
movq %rax, 0x40(%rsp)
testb $0x1, %cl
movq %rdi, 0x8(%rsp)
je 0x7475
movq 0x20(%rsp), %rcx
movq 0xb8(%rcx), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rcx), %rax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x80(%rsp), %rdi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4177
cmpq $0x0, 0x10(%rsp)
je 0x7470
leaq 0x10(%rsp), %rax
movq %rax, 0x68(%rsp)
movq $0x8, 0x60(%rsp)
movl $0x11, %r12d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
incq %rax
movq %rax, 0x10(%rsp)
cmpq %rax, 0x18(%rsp)
ja 0x74d0
cmpl $0x0, 0x4(%rsp)
jne 0x74b3
movq 0x28(%rsp), %rdi
callq 0xad8e
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rcx, 0xa8(%rax)
movl $0x3, %esi
movl $0x1, 0x4(%rsp)
movq 0x18(%rsp), %rcx
cmpq 0x10(%rsp), %rcx
jbe 0x75f6
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
movl %r12d, %ecx
callq 0x62d0
testl %eax, %eax
je 0x7512
movl %eax, (%rsp)
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
movq 0x8(%rsp), %rdi
jne 0x75a4
movl $0x3, %esi
jmp 0x75f6
movl $0x8, %esi
jmp 0x75f6
movq 0x68(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, 0x18(%rsp)
ja 0x7563
cmpl $0x0, 0x4(%rsp)
jne 0x754b
movq 0x28(%rsp), %rdi
callq 0xad8e
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rcx, 0xa8(%rax)
movl $0x1, 0x4(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
jbe 0x75e5
movzwl 0xc2(%rsp), %eax
movq 0xc8(%rsp,%rax,8), %rcx
movzwl 0x1c8(%rsp,%rax,2), %eax
movzwl 0x10(%rcx,%rax,2), %esi
addq %rcx, %rsi
movq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x6780
testl %eax, %eax
je 0x75bc
movl $0x1, %esi
movl %eax, (%rsp)
movl %eax, 0x4c(%rsp)
jmp 0x75f1
movl $0x2, %esi
jmp 0x75f6
movl $0x3, %esi
movl %ebp, %r13d
movl %edx, %ecx
movq %rcx, 0x40(%rsp)
jmp 0x75f6
movq 0x78(%rsp), %r12
movl (%r12), %r13d
testq %r15, %r15
movq 0x38(%rsp), %rdi
je 0x7608
movl %r13d, %esi
callq 0xbf73
testl %eax, %eax
je 0x7631
movl %eax, (%rsp)
movl $0x2, %esi
jmp 0x75f1
movl $0x0, (%rsp)
movl $0x3, %esi
movq 0x8(%rsp), %rdi
movl $0x8, %r12d
xorl %ecx, %ecx
testl %esi, %esi
je 0x73c8
jmp 0x7663
movl %r13d, %edi
callq 0xbeff
movq %rax, %r15
movq 0x38(%rsp), %rax
movq %r15, (%rax)
testq %r15, %r15
jne 0x7639
movl $0xc, (%rsp)
movl $0x2, %esi
xorl %r15d, %r15d
jmp 0x75f1
movq 0x38(%rsp), %rax
movq (%rax), %r15
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rax, 0xb8(%rcx)
movq %r15, %rdi
movq %r12, %rsi
callq 0xc130
movl (%r15), %edi
xorl %esi, %esi
movl $0x0, (%rsp)
jmp 0x75f6
cmpl $0x2, %esi
je 0x76a3
cmpl $0x8, %esi
movq 0x28(%rsp), %r12
je 0x76ad
cmpl $0x3, %esi
movq 0x20(%rsp), %rcx
jne 0x76e2
movq 0x10(%r12), %rbx
movslq 0x30(%rsp), %rax
addq %rbx, %rax
movl $0xffff87b8, %edx       # imm = 0xFFFF87B8
xorl %r13d, %r13d
cmpq 0x80(%rcx), %rax
jb 0x76b2
jmp 0x76d8
movl $0xffff87bc, %edx       # imm = 0xFFFF87BC
jmp 0x76d8
movq 0x28(%rsp), %r12
movl (%rsp), %edx
jmp 0x76d8
movq 0x20(%rsp), %rcx
testb $0x8, 0xe(%rcx)
jne 0x76e8
movq %rdi, %r14
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x7afe
movq %r14, %rdi
movq %rax, %r14
testq %rax, %rax
jne 0x76f4
movl $0xc, %edx
orb $0x2, 0x7c(%r12)
movl %edx, %eax
jmp 0x775b
movl 0x4c(%rsp), %eax
jmp 0x775b
movl 0x10(%rcx), %r14d
imulq %rbx, %r14
addq 0x38(%rcx), %r14
testl %r13d, %r13d
je 0x7736
movq 0x30(%rsp), %rcx
subl %ecx, %edi
movq %rdi, (%r15)
movl %r13d, %eax
subl %ecx, %eax
cmpl %edi, %eax
jae 0x7743
movl %eax, %ecx
movl %edi, %eax
subq %rcx, %rax
leaq (%r15,%rcx,8), %rcx
addq $0x8, %rcx
incl %r13d
xorl %edx, %edx
leal (%rdx,%r13), %esi
movq (%r15,%rsi,8), %rsi
movq %rsi, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x7720
jmp 0x7743
movslq 0x30(%rsp), %rax
addq %rbx, %rax
movq %rax, 0x10(%r12)
movq %rbx, (%r14)
movq %r12, %rdi
movq %r14, %rsi
callq 0xadd8
movq 0x50(%rsp), %rax
movq %r14, (%rax)
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq