// File: /workspace/github_cpp_makefile_star2_O1/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_freelist_save
static int
mdb_freelist_save(MDB_txn *txn)
{
	/* env->me_pghead[] can grow and shrink during this call.
	 * env->me_pglast and txn->mt_free_pgs[] can only grow.
	 * Page numbers cannot disappear from txn->mt_free_pgs[].
	 */
	MDB_cursor mc;
	MDB_env	*env = txn->mt_env;
	int rc, maxfree_1pg = env->me_maxfree_1pg, more = 1;
	txnid_t	pglast = 0, head_id = 0;
	pgno_t	freecnt = 0, *free_pgs, *mop;
	ssize_t	head_room = 0, total_room = 0, mop_len, clean_limit;

	mdb_cursor_init(&mc, txn, FREE_DBI, NULL);

	if (env->me_pghead) {
		/* Make sure first page of freeDB is touched and on freelist */
		rc = mdb_page_search(&mc, NULL, MDB_PS_FIRST|MDB_PS_MODIFY);
		if (rc && rc != MDB_NOTFOUND)
			return rc;
	}

	if (!env->me_pghead && txn->mt_loose_pgs) {
		/* Put loose page numbers in mt_free_pgs, since
		 * we may be unable to return them to me_pghead.
		 */
		MDB_page *mp = txn->mt_loose_pgs;
		if ((rc = mdb_midl_need(&txn->mt_free_pgs, txn->mt_loose_count)) != 0)
			return rc;
		for (; mp; mp = NEXT_LOOSE_PAGE(mp))
			mdb_midl_xappend(txn->mt_free_pgs, mp->mp_pgno);
		txn->mt_loose_pgs = NULL;
		txn->mt_loose_count = 0;
	}

	/* MDB_RESERVE cancels meminit in ovpage malloc (when no WRITEMAP) */
	clean_limit = (env->me_flags & (MDB_NOMEMINIT|MDB_WRITEMAP))
		? SSIZE_MAX : maxfree_1pg;

	for (;;) {
		/* Come back here after each Put() in case freelist changed */
		MDB_val key, data;
		pgno_t *pgs;
		ssize_t j;

		/* If using records from freeDB which we have not yet
		 * deleted, delete them and any we reserved for me_pghead.
		 */
		while (pglast < env->me_pglast) {
			rc = mdb_cursor_first(&mc, &key, NULL);
			if (rc)
				return rc;
			pglast = head_id = *(txnid_t *)key.mv_data;
			total_room = head_room = 0;
			mdb_tassert(txn, pglast <= env->me_pglast);
			rc = mdb_cursor_del(&mc, 0);
			if (rc)
				return rc;
		}

		/* Save the IDL of pages freed by this txn, to a single record */
		if (freecnt < txn->mt_free_pgs[0]) {
			if (!freecnt) {
				/* Make sure last page of freeDB is touched and on freelist */
				rc = mdb_page_search(&mc, NULL, MDB_PS_LAST|MDB_PS_MODIFY);
				if (rc && rc != MDB_NOTFOUND)
					return rc;
			}
			free_pgs = txn->mt_free_pgs;
			/* Write to last page of freeDB */
			key.mv_size = sizeof(txn->mt_txnid);
			key.mv_data = &txn->mt_txnid;
			do {
				freecnt = free_pgs[0];
				data.mv_size = MDB_IDL_SIZEOF(free_pgs);
				rc = mdb_cursor_put(&mc, &key, &data, MDB_RESERVE);
				if (rc)
					return rc;
				/* Retry if mt_free_pgs[] grew during the Put() */
				free_pgs = txn->mt_free_pgs;
			} while (freecnt < free_pgs[0]);
			mdb_midl_sort(free_pgs);
			memcpy(data.mv_data, free_pgs, data.mv_size);
#if (MDB_DEBUG) > 1
			{
				unsigned int i = free_pgs[0];
				DPRINTF(("IDL write txn %"Z"u root %"Z"u num %u",
					txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, i));
				for (; i; i--)
					DPRINTF(("IDL %"Z"u", free_pgs[i]));
			}
#endif
			continue;
		}

		mop = env->me_pghead;
		mop_len = (mop ? mop[0] : 0) + txn->mt_loose_count;

		/* Reserve records for me_pghead[]. Split it if multi-page,
		 * to avoid searching freeDB for a page range. Use keys in
		 * range [1,me_pglast]: Smaller than txnid of oldest reader.
		 */
		if (total_room >= mop_len) {
			if (total_room == mop_len || --more < 0)
				break;
		} else if (head_room >= maxfree_1pg && head_id > 1) {
			/* Keep current record (overflow page), add a new one */
			head_id--;
			head_room = 0;
		}
		/* (Re)write {key = head_id, IDL length = head_room} */
		total_room -= head_room;
		head_room = mop_len - total_room;
		if (head_room > maxfree_1pg && head_id > 1) {
			/* Overflow multi-page for part of me_pghead */
			head_room /= head_id; /* amortize page sizes */
			head_room += maxfree_1pg - head_room % (maxfree_1pg + 1);
		} else if (head_room < 0) {
			/* Rare case, not bothering to delete this record */
			head_room = 0;
		}
		key.mv_size = sizeof(head_id);
		key.mv_data = &head_id;
		data.mv_size = (head_room + 1) * sizeof(pgno_t);
		rc = mdb_cursor_put(&mc, &key, &data, MDB_RESERVE);
		if (rc)
			return rc;
		/* IDL is initially empty, zero out at least the length */
		pgs = (pgno_t *)data.mv_data;
		j = head_room > clean_limit ? head_room : 0;
		do {
			pgs[j] = 0;
		} while (--j >= 0);
		total_room += head_room;
	}

	/* Return loose page numbers to me_pghead, though usually none are
	 * left at this point.  The pages themselves remain in dirty_list.
	 */
	if (txn->mt_loose_pgs) {
		MDB_page *mp = txn->mt_loose_pgs;
		unsigned count = txn->mt_loose_count;
		MDB_IDL loose;
		/* Room for loose pages + temp IDL with same */
		if ((rc = mdb_midl_need(&env->me_pghead, 2*count+1)) != 0)
			return rc;
		mop = env->me_pghead;
		loose = mop + MDB_IDL_ALLOCLEN(mop) - count;
		for (count = 0; mp; mp = NEXT_LOOSE_PAGE(mp))
			loose[ ++count ] = mp->mp_pgno;
		loose[0] = count;
		mdb_midl_sort(loose);
		mdb_midl_xmerge(mop, loose);
		txn->mt_loose_pgs = NULL;
		txn->mt_loose_count = 0;
		mop_len = mop[0];
	}

	/* Fill in the reserved me_pghead records */
	rc = MDB_SUCCESS;
	if (mop_len) {
		MDB_val key, data;

		mop += mop_len;
		rc = mdb_cursor_first(&mc, &key, &data);
		for (; !rc; rc = mdb_cursor_next(&mc, &key, &data, MDB_NEXT)) {
			txnid_t id = *(txnid_t *)key.mv_data;
			ssize_t	len = (ssize_t)(data.mv_size / sizeof(MDB_ID)) - 1;
			MDB_ID save;

			mdb_tassert(txn, len >= 0 && id <= env->me_pglast);
			key.mv_data = &id;
			if (len > mop_len) {
				len = mop_len;
				data.mv_size = (len + 1) * sizeof(MDB_ID);
			}
			data.mv_data = mop -= len;
			save = mop[0];
			mop[0] = len;
			rc = mdb_cursor_put(&mc, &key, &data, MDB_CURRENT);
			mop[0] = save;
			if (rc || !(mop_len -= len))
				break;
		}
	}
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %r12
movq 0x20(%rdi), %r15
movslq 0xd8(%r15), %r13
movq $0x0, 0x40(%rsp)
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4177
cmpq $0x0, 0xb0(%r15)
je 0x514b
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0xa112
testl %eax, %eax
je 0x514b
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x5683
leaq 0xb0(%r15), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, (%rax)
je 0x551c
movl %eax, 0x4(%rsp)
movl 0xc(%r15), %eax
movl %eax, 0x5c(%rsp)
movq %r13, 0x50(%rsp)
leal 0x1(%r13), %eax
cltq
movq %rax, 0x68(%rsp)
movq %r12, 0x18(%rsp)
leaq 0x18(%r12), %rax
movq %rax, 0x70(%rsp)
movl $0x1, %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq 0xb8(%r15), %rbx
jae 0x5218
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x67c9
movl $0x1, %ebp
testl %eax, %eax
jne 0x5206
movq 0x38(%rsp), %rax
movq (%rax), %rbx
movq %rbx, 0x40(%rsp)
cmpq 0xb8(%r15), %rbx
ja 0x5695
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x8e3e
movl $0x0, %r14d
movl $0x0, %r13d
testl %eax, %eax
je 0x51a1
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %eax, 0x4(%rsp)
testl %ebp, %ebp
je 0x51a1
cmpl $0x8, %ebp
je 0x51a1
jmp 0x5460
movq 0x18(%rsp), %rbp
movq 0x28(%rbp), %rax
cmpq (%rax), %r12
jae 0x52ae
testq %r12, %r12
je 0x52cf
movq 0x28(%rbp), %rbp
movq $0x8, 0x30(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x38(%rsp)
movq (%rbp), %r12
leaq 0x8(,%r12,8), %rax
movq %rax, 0x20(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x10000, %ecx          # imm = 0x10000
callq 0x427c
testl %eax, %eax
jne 0x52c5
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rbp
cmpq (%rbp), %r12
jb 0x524a
movq %rbp, %rdi
callq 0xc17a
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x2240
movl $0x8, %ebp
jmp 0x520a
movq 0x60(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
movq %rax, 0x48(%rsp)
je 0x5303
movq (%rax), %rax
jmp 0x5305
movl $0x1, %ebp
jmp 0x5206
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xa112
testl %eax, %eax
je 0x5233
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
je 0x5233
movl $0x1, %ebp
xorl %r12d, %r12d
jmp 0x5206
xorl %eax, %eax
movslq 0x38(%rbp), %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
cmpq %rcx, %r13
jge 0x5345
cmpq 0x50(%rsp), %r14
setge %cl
movq 0x40(%rsp), %rax
cmpq $0x2, %rax
setae %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x533c
decq %rax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movl %eax, %ecx
jmp 0x5360
movl $0x7, %ebp
je 0x520a
movq 0x8(%rsp), %rax
leal -0x1(%rax), %ecx
testl %eax, %eax
jle 0x5454
movl %ecx, 0x8(%rsp)
subq %r14, %r13
movq 0x10(%rsp), %r14
subq %r13, %r14
movq 0x50(%rsp), %rsi
cmpq %rsi, %r14
setg %al
movq 0x40(%rsp), %rcx
cmpq $0x2, %rcx
setae %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x53a7
movq %r14, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
cqto
idivq 0x68(%rsp)
addq %rsi, %r14
subq %rdx, %r14
jmp 0x53b3
testq %r14, %r14
movl $0x0, %eax
cmovleq %rax, %r14
movq $0x8, 0x30(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x8(,%r14,8), %rax
movq %rax, 0x20(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x10000, %ecx          # imm = 0x10000
callq 0x427c
testl %eax, %eax
je 0x5406
movl $0x1, %ebp
movl 0x8(%rsp), %ecx
movq %rcx, 0x8(%rsp)
jmp 0x5206
xorl %ebp, %ebp
cmpq 0x50(%rsp), %r14
movl $0x0, %eax
cmovgq %r14, %rax
testl $0x1080000, 0x5c(%rsp)  # imm = 0x1080000
cmovneq %rbp, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
andq %rax, %rcx
leaq (,%rcx,8), %rdi
addq 0x28(%rsp), %rdi
subq %rcx, %rax
leaq 0x8(,%rax,8), %rdx
xorl %esi, %esi
callq 0x2190
addq %r14, %r13
movl 0x8(%rsp), %eax
jmp 0x5456
movl %ecx, %eax
movq %rax, 0x8(%rsp)
jmp 0x520a
cmpl $0x7, %ebp
jne 0x5513
movq 0x18(%rsp), %rbp
movq 0x30(%rbp), %rbx
testq %rbx, %rbx
je 0x5585
movl 0x38(%rbp), %r13d
leal 0x1(,%r13,2), %esi
movq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0xbf73
movl %eax, %r14d
testl %eax, %eax
movq 0x48(%rsp), %rcx
jne 0x5508
movq (%r12), %rcx
movq -0x8(%rcx), %rax
movq %rcx, 0x48(%rsp)
leaq (%rcx,%rax,8), %r12
shlq $0x3, %r13
subq %r13, %r12
xorl %eax, %eax
movq (%rbx), %rcx
incl %eax
movq %rcx, (%r12,%rax,8)
movq 0x30(%rbx), %rbx
testq %rbx, %rbx
jne 0x54ba
movl %eax, %eax
movq %rax, (%r12)
movq %r12, %rdi
callq 0xc17a
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc130
movq %rbx, %rcx
movq $0x0, 0x30(%rbp)
movl $0x0, 0x38(%rbp)
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
movl 0x4(%rsp), %eax
testl %r14d, %r14d
jne 0x5683
jmp 0x558a
movl 0x4(%rsp), %eax
jmp 0x5683
movq 0x30(%r12), %rbx
testq %rbx, %rbx
je 0x567c
leaq 0x28(%r12), %r14
movl 0x38(%r12), %esi
movq %r14, %rdi
callq 0xbf73
testl %eax, %eax
jne 0x5683
movl %eax, %esi
movq (%r14), %rax
movq (%rax), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rax)
movq (%rbx), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq 0x30(%rbx), %rbx
testq %rbx, %rbx
jne 0x5549
movq $0x0, 0x30(%r12)
movl $0x0, 0x38(%r12)
movl %esi, %eax
testl %esi, %esi
jne 0x5683
jmp 0x5161
movq 0x48(%rsp), %rcx
cmpq $0x0, 0x10(%rsp)
je 0x5681
movq %rcx, %rbx
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x67c9
testl %eax, %eax
jne 0x5683
movq 0x10(%rsp), %rax
leaq (%rbx,%rax,8), %rbx
leaq 0x30(%rsp), %r13
leaq 0x20(%rsp), %r12
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x78(%rsp)
movq 0x20(%rsp), %rbp
cmpq $0x8, %rbp
jb 0x56b6
cmpq 0xb8(%r15), %rax
ja 0x56b6
shrq $0x3, %rbp
decq %rbp
leaq 0x78(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq 0x10(%rsp), %rbp
jle 0x561e
movq 0x10(%rsp), %rbp
leaq 0x8(,%rbp,8), %rax
movq %rax, 0x20(%rsp)
leaq (,%rbp,8), %rax
subq %rax, %rbx
movq %rbx, 0x28(%rsp)
movq (%rbx), %r14
movq %rbp, (%rbx)
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movl $0x40, %ecx
callq 0x427c
movq %r14, (%rbx)
testl %eax, %eax
jne 0x5683
subq %rbp, 0x10(%rsp)
je 0x5683
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movl $0x8, %ecx
callq 0x68e8
testl %eax, %eax
je 0x55cb
jmp 0x5683
jmp 0x5161
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0xa2a2(%rip), %rsi      # 0xf947
leaq 0xa2b4(%rip), %rdx      # 0xf960
movl $0xc11, %ecx            # imm = 0xC11
callq 0xc43e
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0xa2ac(%rip), %rsi      # 0xf972
leaq 0xa293(%rip), %rdx      # 0xf960
movl $0xc85, %ecx            # imm = 0xC85
callq 0xc43e