// File: /workspace/github_cpp_makefile_star2_O2/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_page_flush
static int
mdb_page_flush(MDB_txn *txn, int keep)
{
	MDB_env		*env = txn->mt_env;
	MDB_ID2L	dl = txn->mt_u.dirty_list;
	unsigned	psize = env->me_psize, j;
	int			i, pagecount = dl[0].mid, rc;
	size_t		size = 0, pos = 0;
	pgno_t		pgno = 0;
	MDB_page	*dp = NULL;
#ifdef _WIN32
	OVERLAPPED	ov;
#else
	struct iovec iov[MDB_COMMIT_PAGES];
	ssize_t		wpos = 0, wsize = 0, wres;
	size_t		next_pos = 1; /* impossible pos, so pos != next_pos */
	int			n = 0;
#endif

	j = i = keep;

	if (env->me_flags & MDB_WRITEMAP) {
		/* Clear dirty flags */
		while (++i <= pagecount) {
			dp = dl[i].mptr;
			/* Don't flush this page yet */
			if (dp->mp_flags & (P_LOOSE|P_KEEP)) {
				dp->mp_flags &= ~P_KEEP;
				dl[++j] = dl[i];
				continue;
			}
			dp->mp_flags &= ~P_DIRTY;
		}
		goto done;
	}

	/* Write the pages */
	for (;;) {
		if (++i <= pagecount) {
			dp = dl[i].mptr;
			/* Don't flush this page yet */
			if (dp->mp_flags & (P_LOOSE|P_KEEP)) {
				dp->mp_flags &= ~P_KEEP;
				dl[i].mid = 0;
				continue;
			}
			pgno = dl[i].mid;
			/* clear dirty flag */
			dp->mp_flags &= ~P_DIRTY;
			pos = pgno * psize;
			size = psize;
			if (IS_OVERFLOW(dp)) size *= dp->mp_pages;
		}
#ifdef _WIN32
		else break;

		/* Windows actually supports scatter/gather I/O, but only on
		 * unbuffered file handles. Since we're relying on the OS page
		 * cache for all our data, that's self-defeating. So we just
		 * write pages one at a time. We use the ov structure to set
		 * the write offset, to at least save the overhead of a Seek
		 * system call.
		 */
		DPRINTF(("committing page %"Z"u", pgno));
		memset(&ov, 0, sizeof(ov));
		ov.Offset = pos & 0xffffffff;
		ov.OffsetHigh = pos >> 16 >> 16;
		if (!WriteFile(env->me_fd, dp, size, NULL, &ov)) {
			rc = ErrCode();
			DPRINTF(("WriteFile: %d", rc));
			return rc;
		}
#else
		/* Write up to MDB_COMMIT_PAGES dirty pages at a time. */
		if (pos!=next_pos || n==MDB_COMMIT_PAGES || wsize+size>MAX_WRITE) {
			if (n) {
retry_write:
				/* Write previous page(s) */
#ifdef MDB_USE_PWRITEV
				wres = pwritev(env->me_fd, iov, n, wpos);
#else
				if (n == 1) {
					wres = pwrite(env->me_fd, iov[0].iov_base, wsize, wpos);
				} else {
retry_seek:
					if (lseek(env->me_fd, wpos, SEEK_SET) == -1) {
						rc = ErrCode();
						if (rc == EINTR)
							goto retry_seek;
						DPRINTF(("lseek: %s", strerror(rc)));
						return rc;
					}
					wres = writev(env->me_fd, iov, n);
				}
#endif
				if (wres != wsize) {
					if (wres < 0) {
						rc = ErrCode();
						if (rc == EINTR)
							goto retry_write;
						DPRINTF(("Write error: %s", strerror(rc)));
					} else {
						rc = EIO; /* TODO: Use which error code? */
						DPUTS("short write, filesystem full?");
					}
					return rc;
				}
				n = 0;
			}
			if (i > pagecount)
				break;
			wpos = pos;
			wsize = 0;
		}
		DPRINTF(("committing page %"Z"u", pgno));
		next_pos = pos + size;
		iov[n].iov_len = size;
		iov[n].iov_base = (char *)dp;
		wsize += size;
		n++;
#endif	/* _WIN32 */
	}

	/* MIPS has cache coherency issues, this is a no-op everywhere else
	 * Note: for any size >= on-chip cache size, entire on-chip cache is
	 * flushed.
	 */
	CACHEFLUSH(env->me_map, txn->mt_next_pgno * env->me_psize, DCACHE);

	for (i = keep; ++i <= pagecount; ) {
		dp = dl[i].mptr;
		/* This is a page we skipped above */
		if (!dl[i].mid) {
			dl[++j] = dl[i];
			dl[j].mid = dp->mp_pgno;
			continue;
		}
		mdb_dpage_free(env, dp);
	}

done:
	i--;
	txn->mt_dirty_room += i - j;
	dl[0].mid = j;
	return MDB_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movl %esi, %r12d
movq 0x20(%rdi), %rcx
movq 0x48(%rdi), %r13
movq (%r13), %rax
testb $0x8, 0xe(%rcx)
jne 0x549d
movq %rdi, 0x38(%rsp)
movq %rcx, 0x8(%rsp)
movl 0x10(%rcx), %edi
movslq %eax, %r14
movq %r13, 0x40(%rsp)
leaq 0x10(%r13), %rax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
pushq $0x1
popq %rax
movl $0x80000001, %r8d       # imm = 0x80000001
movq %r12, 0x48(%rsp)
movl %r12d, %ebx
xorl %r9d, %r9d
xorl %r15d, %r15d
movq $0x0, 0x18(%rsp)
xorl %r10d, %r10d
xorl %r12d, %r12d
movq %rdi, 0x20(%rsp)
movslq %ebx, %rbp
leal 0x1(%rbp), %ebx
movq %rbp, %rcx
shlq $0x4, %rcx
addq 0x30(%rsp), %rcx
cmpq %r14, %rbp
jge 0x538e
movq 0x8(%rcx), %r15
movzwl 0xa(%r15), %edx
cmpw $0x4000, %dx            # imm = 0x4000
jb 0x5368
andl $0x7fff, %edx           # imm = 0x7FFF
movw %dx, 0xa(%r15)
andq $0x0, (%rcx)
incl %ebx
addq $0x10, %rcx
incq %rbp
jmp 0x5339
movl %edx, %esi
andl $0x3fef, %esi           # imm = 0x3FEF
movq (%rcx), %r9
imulq %rdi, %r9
movw %si, 0xa(%r15)
testb $0x4, %dl
jne 0x5386
movq %rdi, %r13
jmp 0x538e
movl 0xc(%r15), %r13d
imulq %rdi, %r13
cmpq %rax, %r9
jne 0x53a6
cmpl $0x40, %r12d
je 0x53a6
leaq (%r10,%r13), %rax
cmpq %r8, %rax
jb 0x547a
testl %r12d, %r12d
je 0x5466
movq %r10, 0x10(%rsp)
movq %r9, 0x28(%rsp)
cmpl $0x1, %r12d
jne 0x5401
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
callq 0x22b0
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rdi
movl $0x80000001, %r8d       # imm = 0x80000001
movq 0x28(%rsp), %r9
jmp 0x5443
callq 0x2050
movl (%rax), %eax
cmpl $0x4, %eax
jne 0x5580
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x2170
cmpq $-0x1, %rax
je 0x53f1
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x50(%rsp), %rsi
movl %r12d, %edx
callq 0x2090
movq 0x20(%rsp), %rdi
movl $0x80000001, %r8d       # imm = 0x80000001
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %rcx
cmpq %rcx, %rax
je 0x5466
testq %rax, %rax
jns 0x5592
callq 0x2050
movl (%rax), %eax
cmpl $0x4, %eax
je 0x53b9
jmp 0x5580
cmpq %r14, %rbp
jge 0x5508
xorl %r10d, %r10d
movq %r9, 0x18(%rsp)
xorl %r12d, %r12d
leaq (%r9,%r13), %rax
movslq %r12d, %r12
movq %r12, %rcx
shlq $0x4, %rcx
movq %r13, 0x58(%rsp,%rcx)
movq %r15, 0x50(%rsp,%rcx)
addq %r13, %r10
incl %r12d
jmp 0x5327
cltq
leaq 0x18(%r13), %rcx
movl %r12d, %edx
movslq %edx, %rbx
leal 0x1(%rbx), %edx
movq %rbx, %rsi
shlq $0x4, %rsi
addq %rcx, %rsi
cmpq %rax, %rbx
jge 0x556e
movq (%rsi), %r9
movzwl 0xa(%r9), %r8d
cmpw $0x4000, %r8w           # imm = 0x4000
jae 0x54e6
andl $0x3fef, %r8d           # imm = 0x3FEF
movw %r8w, 0xa(%r9)
incl %edx
addq $0x10, %rsi
incq %rbx
jmp 0x54b6
andl $0x7fff, %r8d           # imm = 0x7FFF
movw %r8w, 0xa(%r9)
incl %r12d
movq %r12, %r8
shlq $0x4, %r8
movups -0x8(%rsi), %xmm0
movups %xmm0, (%r13,%r8)
jmp 0x54a6
movq 0x48(%rsp), %r12
movl %r12d, %ebp
movq 0x40(%rsp), %r13
movslq %ebp, %rbx
leal 0x1(%rbx), %ebp
movq %rbx, %r15
shlq $0x4, %r15
addq 0x30(%rsp), %r15
cmpq %r14, %rbx
jge 0x5569
movq 0x8(%r15), %rsi
cmpq $0x0, (%r15)
je 0x554b
movq 0x8(%rsp), %rdi
callq 0x9564
incl %ebp
addq $0x10, %r15
incq %rbx
jmp 0x5527
incl %r12d
movq %r12, %rax
shlq $0x4, %rax
movups (%r15), %xmm0
movups %xmm0, (%r13,%rax)
movq (%rsi), %rcx
movq %rcx, (%r13,%rax)
jmp 0x5515
movq 0x38(%rsp), %rdi
subl %r12d, %ebx
addl %ebx, 0x80(%rdi)
movl %r12d, %eax
movq %rax, (%r13)
xorl %eax, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x5
popq %rax
jmp 0x5580