// File: /workspace/github_cpp_makefile_star2_O1/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 0x5906
movq %rdi, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movl 0x10(%rax), %r10d
movslq %r11d, %rax
movq %rax, 0x38(%rsp)
movl $0x1, %eax
subl %r11d, %eax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movl $0x1, %eax
movl %esi, 0x34(%rsp)
movl %esi, %ebp
xorl %r12d, %r12d
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r9, 0x20(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x40(%rsp)
cmpl %r11d, %ebp
setl %r8b
jge 0x57ec
movslq %ebp, %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, %rdx
shlq $0x4, %rdx
leaq (%r9,%rdx), %rcx
movq 0x8(%r9,%rdx), %rbx
movzwl 0xa(%rbx), %edx
cmpl $0x4000, %edx           # imm = 0x4000
jb 0x57cd
movl %ebp, %r8d
addq 0x48(%rsp), %r8
xorl %edi, %edi
andl $0x7fff, %edx           # imm = 0x7FFF
movw %dx, 0xa(%rbx)
movq $0x0, (%rcx)
movl %r8d, %edx
addl %edi, %edx
je 0x57fa
movq 0x18(%rcx), %rbx
movzwl 0xa(%rbx), %edx
addq $0x10, %rcx
incq %rdi
cmpl $0x4000, %edx           # imm = 0x4000
jae 0x578b
leaq (%rsi,%rdi), %r8
cmpq 0x38(%rsp), %r8
setl %r8b
addq %rsi, %rdi
incq %rdi
movl %edi, %ebp
movl %edx, %esi
andl $0x3fef, %esi           # imm = 0x3FEF
movq (%rcx), %r12
imulq %r10, %r12
movw %si, 0xa(%rbx)
testb $0x4, %dl
jne 0x57f0
movq %r10, %r14
jmp 0x580f
incl %ebp
jmp 0x580f
movl 0xc(%rbx), %r14d
imulq %r10, %r14
jmp 0x580f
leaq (%rsi,%rdi), %rcx
incq %rcx
cmpq 0x38(%rsp), %rcx
setl %r8b
addl %edi, %ebp
addl $0x2, %ebp
cmpq %rax, %r12
jne 0x582c
cmpl $0x40, %r13d
je 0x582c
leaq (%r14,%r15), %rax
movl $0x80000001, %ecx       # imm = 0x80000001
cmpq %rcx, %rax
jb 0x58e3
testl %r13d, %r13d
je 0x58cf
movb %r8b, 0x7(%rsp)
cmpl $0x1, %r13d
jne 0x586b
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x50(%rsp), %rsi
movq %r15, %rdx
movq 0x28(%rsp), %rcx
callq 0x22a0
jmp 0x5898
callq 0x2050
movl (%rax), %eax
cmpl $0x4, %eax
jne 0x598a
movq 0x8(%rsp), %rax
movl (%rax), %edi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
callq 0x2170
cmpq $-0x1, %rax
je 0x585b
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 0x40(%rsp), %r10
movb 0x7(%rsp), %r8b
cmpq %r15, %rax
je 0x58cf
testq %rax, %rax
jns 0x5a37
callq 0x2050
movl (%rax), %eax
cmpl $0x4, %eax
je 0x583a
jmp 0x598a
testb %r8b, %r8b
je 0x599c
xorl %r15d, %r15d
movq %r12, 0x28(%rsp)
xorl %r13d, %r13d
leaq (%r12,%r14), %rax
movslq %r13d, %r13
movq %r13, %rcx
shlq $0x4, %rcx
movq %r14, 0x58(%rsp,%rcx)
movq %rbx, 0x50(%rsp,%rcx)
addq %r14, %r15
incl %r13d
jmp 0x5751
cmpl %esi, %r11d
jle 0x5979
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 0x594c
incq %rcx
andl $0x3fef, %r8d           # imm = 0x3FEF
movw %r8w, 0xa(%r10)
addq $0x10, %r14
cmpq %rax, %rcx
jl 0x5921
jmp 0x5975
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 0x5914
incl %ecx
movl %ecx, %esi
jmp 0x597b
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 0x34(%rsp), %esi
cmpl %esi, %r11d
jle 0x5a41
movslq %r11d, %r14
leaq 0x10(%r9), %r15
movl %esi, %ebx
movq 0x8(%rsp), %rbp
movslq %esi, %r12
movq %r12, %r13
shlq $0x4, %r13
addq %r15, %r13
movq 0x8(%r13), %rdi
cmpq $0x0, (%r13)
je 0x5a0b
testb $0x4, 0xa(%rdi)
je 0x59db
cmpl $0x1, 0xc(%rdi)
jne 0x59ee
movq 0xc0(%rbp), %rax
movq %rax, (%rdi)
movq %rdi, 0xc0(%rbp)
jmp 0x59fd
callq 0x2030
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %r9
incq %r12
addq $0x10, %r13
cmpq %r14, %r12
jl 0x59c4
jmp 0x5a32
leal 0x1(%r12), %esi
incl %ebx
movq %rbx, %rax
shlq $0x4, %rax
movups (%r13), %xmm0
movups %xmm0, (%r9,%rax)
movq (%rdi), %rcx
movq %rcx, (%r9,%rax)
cmpl %r11d, %esi
jl 0x59b7
incl %r12d
movl %r12d, %esi
jmp 0x5a43
movl $0x5, %eax
jmp 0x598a
movl %esi, %ebx
movq 0x10(%rsp), %rdi
jmp 0x597b