// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %rbp
movq 0x20(%rdi), %r13
movslq 0xd8(%r13), %r14
movq $0x0, 0x28(%rsp)
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x40be
leaq 0xb0(%r13), %r15
cmpq $0x0, 0xb0(%r13)
je 0x4ff8
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0x9d28
testl %eax, %eax
je 0x4ff2
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x5017
cmpq $0x0, (%r15)
jne 0x5059
movq 0x30(%rbp), %rbx
testq %rbx, %rbx
je 0x5059
movq %r14, %r12
leaq 0x28(%rbp), %r14
movl 0x38(%rbp), %esi
movq %r14, %rdi
callq 0xba6f
testl %eax, %eax
je 0x5029
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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 0x502c
movq $0x0, 0x30(%rbp)
movl $0x0, 0x38(%rbp)
movq %r12, %r14
movq %r15, 0x48(%rsp)
movl 0xc(%r13), %eax
movl %eax, 0x44(%rsp)
leal 0x1(%r14), %eax
cltq
movq %rax, 0x58(%rsp)
leaq 0x18(%rbp), %rax
movq %rax, 0x60(%rsp)
movl $0x1, 0x34(%rsp)
leaq 0x70(%rsp), %r15
xorl %r8d, %r8d
xorl %edi, %edi
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq 0xb8(%r13), %r12
jae 0x50eb
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x64a4
testl %eax, %eax
jne 0x5017
movq 0x10(%rsp), %rax
movq (%rax), %r12
movq %r12, 0x28(%rsp)
cmpq 0xb8(%r13), %r12
ja 0x546a
movq %r15, %rdi
xorl %esi, %esi
callq 0x8aee
testl %eax, %eax
jne 0x5017
cmpq 0xb8(%r13), %r12
jb 0x509a
xorl %r8d, %r8d
xorl %edi, %edi
movq 0x28(%rbp), %rax
cmpq (%rax), %rbx
jae 0x51ab
movq %r8, (%rsp)
movq %rdi, 0x38(%rsp)
testq %rbx, %rbx
jne 0x5124
movq %r15, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0x9d28
testl %eax, %eax
je 0x5124
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x5017
movq %r14, 0x50(%rsp)
movq 0x28(%rbp), %rax
movq $0x8, 0x8(%rsp)
movq 0x60(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq (%rax), %rax
movq %rax, %rbx
leaq 0x8(,%rax,8), %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movl $0x10000, %ecx          # imm = 0x10000
callq 0x41b9
testl %eax, %eax
jne 0x5017
movq 0x28(%rbp), %r14
movq (%r14), %rax
cmpq %rax, %rbx
jb 0x5143
movq %r14, %rdi
callq 0xbd0b
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x2240
movq 0x50(%rsp), %r14
movq 0x38(%rsp), %rdi
movq (%rsp), %r8
jmp 0x5091
movq %r14, %rdx
movq %rbp, (%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %r14
testq %r14, %r14
movl 0x34(%rsp), %esi
je 0x51c8
movq (%r14), %rbp
jmp 0x51ca
xorl %ebp, %ebp
movq (%rsp), %rax
movslq 0x38(%rax), %rax
addq %rax, %rbp
cmpq %rbp, %rdi
jge 0x5203
movq %rdx, %r14
cmpq %rdx, %r8
setge %al
movq 0x28(%rsp), %rcx
cmpq $0x2, %rcx
setae %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x521b
decq %rcx
movq %rcx, 0x28(%rsp)
xorl %r8d, %r8d
jmp 0x521b
je 0x52fd
testl %esi, %esi
jle 0x52fd
decl %esi
movq 0x28(%rsp), %rcx
movq %rdx, %r14
subq %r8, %rdi
subq %rdi, %rbp
cmpq %r14, %rbp
setg %al
cmpq $0x2, %rcx
setae %dl
andb %al, %dl
cmpb $0x1, %dl
movl %esi, 0x34(%rsp)
movq %rdi, 0x38(%rsp)
jne 0x5258
movq %rbp, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rbp
cqto
idivq 0x58(%rsp)
addq %r14, %rbp
subq %rdx, %rbp
jmp 0x5264
testq %rbp, %rbp
movl $0x0, %eax
cmovleq %rax, %rbp
movq $0x8, 0x8(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x8(,%rbp,8), %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movl $0x10000, %ecx          # imm = 0x10000
callq 0x41b9
testl %eax, %eax
jne 0x5017
cmpq %r14, %rbp
movl $0x0, %eax
cmovgq %rbp, %rax
testl $0x1080000, 0x44(%rsp)  # imm = 0x1080000
movl $0x0, %ecx
cmovneq %rcx, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
andq %rax, %rcx
leaq (,%rcx,8), %rdi
addq 0x20(%rsp), %rdi
subq %rcx, %rax
leaq 0x8(,%rax,8), %rdx
xorl %esi, %esi
callq 0x2190
movq 0x38(%rsp), %rdi
addq %rbp, %rdi
movq %rbp, %r8
movq (%rsp), %rbp
jmp 0x5091
movq (%rsp), %rcx
movq 0x30(%rcx), %rbx
testq %rbx, %rbx
je 0x5381
movl %eax, %r12d
leal 0x1(,%r12,2), %esi
movq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0xba6f
testl %eax, %eax
jne 0x5017
movq (%r14), %r14
movq -0x8(%r14), %rax
leaq (%r14,%rax,8), %r15
shlq $0x3, %r12
subq %r12, %r15
movl $0x1, %eax
movq (%rbx), %rdx
movl %eax, %ecx
movq %rdx, (%r15,%rcx,8)
movq 0x30(%rbx), %rbx
incl %eax
testq %rbx, %rbx
jne 0x5341
movq %rcx, (%r15)
movq %r15, %rdi
callq 0xbd0b
movq %r14, %rdi
movq %r15, %rsi
callq 0xbcc1
movq (%rsp), %rax
movq $0x0, 0x30(%rax)
movl $0x0, 0x38(%rax)
movq (%r14), %rbp
testq %rbp, %rbp
je 0x5463
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x64a4
testl %eax, %eax
jne 0x5017
leaq (%r14,%rbp,8), %rbx
leaq 0x18(%rsp), %r12
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x68(%rsp)
movq 0x18(%rsp), %r14
cmpq $0x8, %r14
jb 0x5486
cmpq 0xb8(%r13), %rax
ja 0x5486
shrq $0x3, %r14
decq %r14
leaq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq %rbp, %r14
jle 0x53fe
leaq 0x8(,%rbp,8), %rax
movq %rax, 0x18(%rsp)
movq %rbp, %r14
leaq (,%r14,8), %rax
subq %rax, %rbx
movq %rbx, 0x20(%rsp)
movq (%rbx), %r15
movq %r14, (%rbx)
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r12, %rdx
movl $0x40, %ecx
callq 0x41b9
movq %r15, (%rbx)
testl %eax, %eax
jne 0x5017
subq %r14, %rbp
je 0x5017
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r12, %rdx
movl $0x8, %ecx
callq 0x65bd
testl %eax, %eax
je 0x53af
jmp 0x5017
xorl %eax, %eax
jmp 0x5017
movq 0x20(%rbp), %rdi
leaq 0xa512(%rip), %rsi      # 0xf987
leaq 0xa524(%rip), %rdx      # 0xf9a0
movl $0xc11, %ecx            # imm = 0xC11
callq 0xbfbd
movq (%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0xa51d(%rip), %rsi      # 0xf9b2
leaq 0xa504(%rip), %rdx      # 0xf9a0
movl $0xc85, %ecx            # imm = 0xC85
callq 0xbfbd