// File: 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
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
imull $0x3c, -0x14(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0xb0(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x9ffb
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x240(%rbp)
jmp 0xa006
xorl %eax, %eax
movq %rax, -0x240(%rbp)
jmp 0xa006
movq -0x240(%rbp), %rax
movl %eax, -0x54(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x58(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x204(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0xa07c
movq -0x30(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0xa07c
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x30(%rbp), %rax
movl 0x38(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0xa49c
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jne 0xa0a0
movl $0xffff87bc, -0x24(%rbp) # imm = 0xFFFF87BC
jmp 0xa489
movl $0x0, -0x74(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jbe 0xa10d
movl -0x54(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
subl -0x58(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x40(%rbp), %rcx
movl -0x58(%rbp), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jne 0xa0e8
jmp 0xa3a1
jmp 0xa0ea
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
cmpl -0x58(%rbp), %eax
ja 0xa0b5
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, %eax
jge 0xa10b
jmp 0xa368
jmp 0xa10d
cmpl $0x0, -0x74(%rbp)
jne 0xa16d
movq -0x38(%rbp), %rax
movq 0xb8(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rsi
leaq -0x200(%rbp), %rdi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x4d40
cmpq $0x0, -0x70(%rbp)
je 0xa16b
movl $0x11, -0x74(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x210(%rbp)
movq $0x8, -0x218(%rbp)
jmp 0xa16d
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0xa1c3
cmpl $0x0, -0x204(%rbp)
jne 0xa1b2
movq -0x30(%rbp), %rdi
callq 0x10330
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0xa8(%rax)
movl $0x1, -0x204(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0xa1c1
jmp 0xa368
jmp 0xa1c3
movl -0x74(%rbp), %ecx
leaq -0x200(%rbp), %rdi
leaq -0x218(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x8150
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0xa1f9
cmpl $0xffff87b2, -0x24(%rbp) # imm = 0xFFFF87B2
jne 0xa1f4
jmp 0xa368
jmp 0xa489
movq -0x210(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0xa251
cmpl $0x0, -0x204(%rbp)
jne 0xa240
movq -0x30(%rbp), %rdi
callq 0x10330
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0xa8(%rax)
movl $0x1, -0x204(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0xa24f
jmp 0xa368
jmp 0xa251
movzwl -0x1be(%rbp), %eax
movq -0x1b8(%rbp,%rax,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x60(%rbp), %rcx
movzwl -0x1be(%rbp), %edx
movzwl -0xb8(%rbp,%rdx,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x230(%rbp)
movq -0x30(%rbp), %rdi
movq -0x230(%rbp), %rsi
leaq -0x228(%rbp), %rdx
callq 0x8a10
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
je 0xa2b7
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa49c
movq -0x220(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x4c(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xa304
movl -0x4c(%rbp), %edi
callq 0x126b0
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rcx
movq %rax, 0xb0(%rcx)
cmpq $0x0, %rax
jne 0xa302
movl $0xc, -0x24(%rbp)
jmp 0xa489
jmp 0xa333
movq -0x38(%rbp), %rdi
addq $0xb0, %rdi
movl -0x4c(%rbp), %esi
callq 0x127b0
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
je 0xa324
jmp 0xa489
movq -0x38(%rbp), %rax
movq 0xb0(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x70(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x48(%rbp), %rdi
movq -0x238(%rbp), %rsi
callq 0x12b00
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x54(%rbp)
movl $0x8, -0x74(%rbp)
jmp 0xa0a7
movl $0x0, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq -0x38(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jb 0xa39f
movl $0xffff87b8, -0x24(%rbp) # imm = 0xFFFF87B8
jmp 0xa489
jmp 0xa3a1
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0xa3cf
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
jmp 0xa3f3
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xaae0
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
jne 0xa3f1
movl $0xc, -0x24(%rbp)
jmp 0xa489
jmp 0xa3f3
cmpl $0x0, -0x4c(%rbp)
je 0xa44a
movl -0x14(%rbp), %ecx
movl -0x54(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x54(%rbp)
movl %eax, %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movl -0x4c(%rbp), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0xa448
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x4c(%rbp)
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x50(%rbp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0xa418
jmp 0xa45d
movq -0x40(%rbp), %rcx
movslq -0x14(%rbp), %rax
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x103e0
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0xa49c
movq -0x30(%rbp), %rax
movl 0x7c(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x7c(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopl (%rax,%rax)