// File: /workspace/github_cpp_makefile_star2_O3/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), %r14
movq 0x20(%r14), %rcx
movq 0xb0(%rcx), %r15
testq %r15, %r15
je 0x6fa2
movl (%r15), %edi
jmp 0x6fa4
xorl %edi, %edi
cmpl $0x1, %esi
jne 0x6fc6
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x6fc6
movq 0x30(%rax), %rcx
movq %rcx, 0x30(%r14)
decl 0x38(%r14)
movq %rax, (%rdx)
jmp 0x7334
movq $0x0, (%rdx)
cmpl $0x0, 0x80(%r14)
movq %r14, 0x18(%rsp)
je 0x72b9
movq %rdx, 0x40(%rsp)
imull $0x3c, %esi, %eax
movl %eax, 0x2c(%rsp)
movq %rcx, 0x8(%rsp)
leaq 0xb0(%rcx), %rax
movq %rax, 0x38(%rsp)
leal -0x1(%rsi), %r13d
movl %r13d, %r8d
movl $0x1, %ebx
movq %rsi, 0x30(%rsp)
subl %esi, %ebx
xorl %r12d, %r12d
movb $0x1, %al
xorl %r9d, %r9d
movl $0x0, 0x4(%rsp)
movq %r8, 0x48(%rsp)
cmpl %r13d, %edi
movq %rdi, 0x20(%rsp)
jbe 0x7063
movl %edi, %ebp
movl %ebp, %ecx
movq (%r15,%rcx,8), %r14
leal (%rbx,%rbp), %ecx
leaq (%r14,%r8), %rdx
cmpq %rdx, (%r15,%rcx,8)
je 0x7289
decl %ebp
cmpl %r13d, %ebp
ja 0x7031
cmpl $0x0, 0x2c(%rsp)
movq 0x18(%rsp), %r14
jle 0x7358
decl 0x2c(%rsp)
testb $0x1, %al
je 0x70ba
movq 0x8(%rsp), %rcx
movq 0xb8(%rcx), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rcx), %r12
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x40be
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x70c1
leaq 0x10(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq $0x8, 0x50(%rsp)
movl $0x11, %r9d
movq 0x20(%rsp), %rdi
jmp 0x70cb
movq 0x10(%rsp), %rax
jmp 0x70cb
xorl %eax, %eax
movq 0x20(%rsp), %rdi
xorl %r9d, %r9d
incq %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r12
ja 0x7145
cmpl $0x0, 0x4(%rsp)
jne 0x7134
movq 0x18(%r14), %r12
movq 0x20(%r14), %rcx
movq 0x40(%rcx), %rdx
decq %r12
testq %rdx, %rdx
je 0x7128
movslq 0x38(%rdx), %rsi
testq %rsi, %rsi
jle 0x7128
leaq 0x1(%rsi), %rcx
shlq $0x6, %rsi
addq %rsi, %rdx
addq $0x48, %rdx
cmpl $0x0, (%rdx)
je 0x711b
movq -0x8(%rdx), %rsi
cmpq %rsi, %r12
cmovaeq %rsi, %r12
decq %rcx
addq $-0x40, %rdx
cmpq $0x1, %rcx
ja 0x710b
movq 0x8(%rsp), %rcx
movq %r12, 0xa8(%rcx)
movl $0x1, 0x4(%rsp)
cmpq %rax, %r12
jbe 0x7358
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
movl %r9d, %ecx
callq 0x5fd1
testl %eax, %eax
jne 0x7348
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r12
ja 0x71e5
cmpl $0x0, 0x4(%rsp)
movq 0x20(%rsp), %rdi
jne 0x71d4
movq 0x18(%r14), %r12
movq 0x20(%r14), %rcx
movq 0x40(%rcx), %rdx
decq %r12
testq %rdx, %rdx
je 0x71c8
movslq 0x38(%rdx), %rsi
testq %rsi, %rsi
jle 0x71c8
leaq 0x1(%rsi), %rcx
shlq $0x6, %rsi
addq %rsi, %rdx
addq $0x48, %rdx
cmpl $0x0, (%rdx)
je 0x71bb
movq -0x8(%rdx), %rsi
cmpq %rsi, %r12
cmovaeq %rsi, %r12
decq %rcx
addq $-0x40, %rdx
cmpq $0x1, %rcx
ja 0x71ab
movq 0x8(%rsp), %rcx
movq %r12, 0xa8(%rcx)
movl $0x1, 0x4(%rsp)
cmpq %rax, %r12
jbe 0x7358
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 %r14, %rdi
leaq 0x60(%rsp), %rdx
callq 0x645b
testl %eax, %eax
jne 0x7336
movq 0x68(%rsp), %rbp
movl (%rbp), %esi
testq %r15, %r15
je 0x7241
movq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0xba6f
testl %eax, %eax
jne 0x72be
movq (%r15), %r15
jmp 0x7258
movl %esi, %edi
callq 0xb9fb
movq %rax, %r15
movq 0x38(%rsp), %rax
movq %r15, (%rax)
testq %r15, %r15
je 0x72b2
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0xb8(%rcx)
movq %r15, %rdi
movq %rbp, %rsi
callq 0xbcc1
movl (%r15), %edi
movl $0x8, %r9d
xorl %eax, %eax
movq 0x48(%rsp), %r8
jmp 0x7025
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %rdx
testb $0x8, 0xe(%rdx)
jne 0x72c9
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x76c4
movq 0x20(%rsp), %rdi
movq %rax, %r13
testq %rax, %rax
jne 0x72d5
movl $0xc, %eax
jmp 0x72be
movl $0xffff87bc, %eax       # imm = 0xFFFF87BC
movq 0x18(%rsp), %rcx
orb $0x2, 0x7c(%rcx)
jmp 0x7336
movl 0x10(%rdx), %r13d
imulq %r14, %r13
addq 0x38(%rdx), %r13
testl %ebp, %ebp
je 0x730c
subl %ebx, %edi
movq %rdi, (%r15)
movl %ebp, %eax
subl %ebx, %eax
cmpl %edi, %eax
jae 0x731b
movl %eax, %eax
subq %rax, %rdi
leaq (%r15,%rax,8), %rax
addq $0x8, %rax
incl %ebp
xorl %ecx, %ecx
leal (%rcx,%rbp), %edx
movq (%r15,%rdx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rdi
jne 0x72f7
jmp 0x731b
movslq %ebx, %rax
addq %r14, %rax
movq 0x18(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %r14, (%r13)
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xa956
movq 0x40(%rsp), %rax
movq %r13, (%rax)
xorl %eax, %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 0x20(%rsp), %rdi
jne 0x72be
movq 0x10(%r14), %r14
movq 0x30(%rsp), %rbx
movslq %ebx, %rcx
addq %r14, %rcx
movl $0xffff87b8, %eax       # imm = 0xFFFF87B8
xorl %ebp, %ebp
movq 0x8(%rsp), %rdx
cmpq 0x80(%rdx), %rcx
jb 0x7293
jmp 0x72be