// File: /workspace/github_cpp_makefile_star2_O3/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
movq 0x20(%rdi), %rax
movq 0x48(%rdi), %r9
movq (%r9), %r11
testb $0x8, 0xe(%rax)
jne 0x56ed
movq %rdi, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movl 0x10(%rax), %r8d
movslq %r11d, %rax
movq %rax, 0x28(%rsp)
leal 0x1(%r11), %eax
movl %eax, 0x40(%rsp)
movl $0x1, %eax
subl %r11d, %eax
movl %eax, 0x44(%rsp)
xorl %ebp, %ebp
movl $0x1, %eax
movl $0x80000001, %edx       # imm = 0x80000001
movl %esi, 0x3c(%rsp)
movl %esi, %r14d
xorl %r12d, %r12d
xorl %ebx, %ebx
movq $0x0, 0x30(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r9, 0x20(%rsp)
movq %r11, 0x18(%rsp)
movq %r8, 0x48(%rsp)
cmpl %r11d, %r14d
jge 0x55ca
movslq %r14d, %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, %rdx
shlq $0x4, %rdx
leaq (%r9,%rdx), %rcx
movq 0x8(%r9,%rdx), %rbx
movzwl 0xa(%rbx), %edx
movb $0x1, %r10b
movl %r10d, 0x4(%rsp)
cmpl $0x4000, %edx           # imm = 0x4000
jb 0x55aa
addl 0x44(%rsp), %r14d
xorl %edi, %edi
andl $0x7fff, %edx           # imm = 0x7FFF
movw %dx, 0xa(%rbx)
movq $0x0, (%rcx)
cmpl %edi, %r14d
je 0x55e0
movq 0x18(%rcx), %rbx
movzwl 0xa(%rbx), %edx
addq $0x10, %rcx
decq %rdi
cmpl $0x4000, %edx           # imm = 0x4000
jae 0x5567
subq %rdi, %rsi
cmpq 0x28(%rsp), %rsi
setl %dil
movl %edi, 0x4(%rsp)
incq %rsi
movq %rsi, %rdi
movl %edi, %r14d
movl %edx, %esi
andl $0x3fef, %esi           # imm = 0x3FEF
movq (%rcx), %r12
imulq %r8, %r12
movw %si, 0xa(%rbx)
testb $0x4, %dl
jne 0x55d7
movq %r8, %rbp
jmp 0x55f9
incl %r14d
movl $0x0, 0x4(%rsp)
jmp 0x55fe
movl 0xc(%rbx), %ebp
imulq %r8, %rbp
jmp 0x55f9
subq %rdi, %rsi
incq %rsi
cmpq 0x28(%rsp), %rsi
setl %cl
movl %ecx, 0x4(%rsp)
movl 0x40(%rsp), %ecx
movl %ecx, %r14d
movl $0x80000001, %edx       # imm = 0x80000001
cmpq %rax, %r12
jne 0x5616
cmpl $0x40, %r13d
je 0x5616
leaq (%r15,%rbp), %rax
cmpq %rdx, %rax
jb 0x56ca
testl %r13d, %r13d
je 0x56b4
cmpl $0x1, %r13d
jne 0x5650
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x50(%rsp), %rsi
movq %r15, %rdx
movq 0x30(%rsp), %rcx
callq 0x22a0
jmp 0x567d
callq 0x2050
movl (%rax), %eax
cmpl $0x4, %eax
jne 0x5771
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x2170
cmpq $-0x1, %rax
je 0x5640
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x50(%rsp), %rsi
movl %r13d, %edx
callq 0x2090
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r11
movq 0x48(%rsp), %r8
movl $0x80000001, %edx       # imm = 0x80000001
cmpq %r15, %rax
je 0x56b4
testq %rax, %rax
jns 0x581d
callq 0x2050
movl (%rax), %eax
cmpl $0x4, %eax
je 0x561f
jmp 0x5771
cmpb $0x0, 0x4(%rsp)
je 0x5783
xorl %r15d, %r15d
movq %r12, 0x30(%rsp)
xorl %r13d, %r13d
leaq (%r12,%rbp), %rax
movslq %r13d, %r13
movq %r13, %rcx
shlq $0x4, %rcx
movq %rbp, 0x58(%rsp,%rcx)
movq %rbx, 0x50(%rsp,%rcx)
addq %rbp, %r15
incl %r13d
jmp 0x552c
cmpl %esi, %r11d
jle 0x5760
movslq %r11d, %rax
leaq 0x18(%r9), %rdx
movl %esi, %ebx
movslq %esi, %rcx
movq %rcx, %r14
shlq $0x4, %r14
addq %rdx, %r14
movq (%r14), %r10
movzwl 0xa(%r10), %r8d
cmpl $0x4000, %r8d           # imm = 0x4000
jae 0x5733
incq %rcx
andl $0x3fef, %r8d           # imm = 0x3FEF
movw %r8w, 0xa(%r10)
addq $0x10, %r14
cmpq %rax, %rcx
jl 0x5708
jmp 0x575c
leal 0x1(%rcx), %esi
andl $0x7fff, %r8d           # imm = 0x7FFF
movw %r8w, 0xa(%r10)
incl %ebx
movq %rbx, %r8
shlq $0x4, %r8
movups -0x8(%r14), %xmm0
movups %xmm0, (%r9,%r8)
cmpl %r11d, %esi
jl 0x56fb
incl %ecx
movl %ecx, %esi
jmp 0x5762
movl %esi, %ebx
subl %ebx, %esi
addl %esi, 0x80(%rdi)
movl %ebx, %eax
movq %rax, (%r9)
xorl %eax, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x3c(%rsp), %esi
cmpl %esi, %r11d
jle 0x5827
leaq 0x10(%r9), %r14
movl %esi, %ebx
movq 0x8(%rsp), %r13
movslq %esi, %r15
movq %r15, %r12
shlq $0x4, %r12
addq %r14, %r12
movq 0x8(%r12), %rdi
cmpq $0x0, (%r12)
je 0x57f2
testb $0x4, 0xa(%rdi)
je 0x57c0
cmpl $0x1, 0xc(%rdi)
jne 0x57d3
movq 0xc0(%r13), %rax
movq %rax, (%rdi)
movq %rdi, 0xc0(%r13)
jmp 0x57e2
callq 0x2030
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %r9
incq %r15
addq $0x10, %r12
cmpq 0x28(%rsp), %r15
jl 0x57a8
jmp 0x5818
leal 0x1(%r15), %esi
incl %ebx
movq %rbx, %rax
shlq $0x4, %rax
movups (%r12), %xmm0
movups %xmm0, (%r9,%rax)
movq (%rdi), %rcx
movq %rcx, (%r9,%rax)
cmpl %r11d, %esi
jl 0x579b
incl %r15d
movl %r15d, %esi
jmp 0x5829
movl $0x5, %eax
jmp 0x5771
movl %esi, %ebx
movq 0x10(%rsp), %rdi
jmp 0x5762