// File: 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
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x468(%rbp)
movq $0x0, -0x470(%rbp)
movq $0x1, -0x480(%rbp)
movl $0x0, -0x484(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0x70c7
jmp 0x7029
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl -0x38(%rbp), %eax
jg 0x70c2
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0xc000, %eax           # imm = 0xC000
cmpl $0x0, %eax
je 0x70ae
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %ecx
andl $0xffff7fff, %ecx       # imm = 0xFFFF7FFF
movw %cx, 0xa(%rax)
movq -0x28(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
movl %ecx, %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x28(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x7029
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %ecx
andl $-0x11, %ecx
movw %cx, 0xa(%rax)
jmp 0x7029
jmp 0x73d9
jmp 0x70c9
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl -0x38(%rbp), %eax
jg 0x718b
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0xc000, %eax           # imm = 0xC000
cmpl $0x0, %eax
je 0x712e
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %ecx
andl $0xffff7fff, %ecx       # imm = 0xFFFF7FFF
movw %cx, 0xa(%rax)
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, (%rax)
jmp 0x70c9
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %ecx
andl $-0x11, %ecx
movw %cx, 0xa(%rax)
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x50(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x48(%rbp)
movq -0x60(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x4, %eax
cmpl $0x4, %eax
jne 0x7189
movq -0x60(%rbp), %rax
movl 0xc(%rax), %eax
imulq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x718b
movq -0x50(%rbp), %rax
cmpq -0x480(%rbp), %rax
jne 0x71ba
cmpl $0x40, -0x484(%rbp)
je 0x71ba
movq -0x470(%rbp), %rax
addq -0x48(%rbp), %rax
movl $0x80000000, %ecx       # imm = 0x80000000
cmpq %rcx, %rax
jbe 0x72c3
cmpl $0x0, -0x484(%rbp)
je 0x72a0
jmp 0x71c9
cmpl $0x1, -0x484(%rbp)
jne 0x71fb
movq -0x20(%rbp), %rax
movl (%rax), %edi
movq -0x460(%rbp), %rsi
movq -0x470(%rbp), %rdx
movq -0x468(%rbp), %rcx
callq 0x2290
movq %rax, -0x478(%rbp)
jmp 0x7253
jmp 0x71fd
movq -0x20(%rbp), %rax
movl (%rax), %edi
movq -0x468(%rbp), %rsi
xorl %edx, %edx
callq 0x2160
cmpq $-0x1, %rax
jne 0x7234
callq 0x2050
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jne 0x7229
jmp 0x71fd
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x740b
movq -0x20(%rbp), %rax
movl (%rax), %edi
leaq -0x460(%rbp), %rsi
movl -0x484(%rbp), %edx
callq 0x2090
movq %rax, -0x478(%rbp)
movq -0x478(%rbp), %rax
cmpq -0x470(%rbp), %rax
je 0x7296
cmpq $0x0, -0x478(%rbp)
jge 0x7284
callq 0x2050
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jne 0x7282
jmp 0x71c9
jmp 0x728b
movl $0x5, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x740b
movl $0x0, -0x484(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0x72ad
jmp 0x7331
movq -0x50(%rbp), %rax
movq %rax, -0x468(%rbp)
movq $0x0, -0x470(%rbp)
movq -0x50(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x480(%rbp)
movq -0x48(%rbp), %rcx
movslq -0x484(%rbp), %rdx
leaq -0x460(%rbp), %rax
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rcx
movslq -0x484(%rbp), %rdx
leaq -0x460(%rbp), %rax
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
addq -0x470(%rbp), %rax
movq %rax, -0x470(%rbp)
movl -0x484(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x484(%rbp)
jmp 0x70c9
movl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl -0x38(%rbp), %eax
jg 0x73d7
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0x73c5
movq -0x28(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
movl %ecx, %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x28(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movl -0x30(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
jmp 0x7337
movq -0x20(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0xee40
jmp 0x7337
jmp 0x73d9
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %ecx
subl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x80(%rax), %ecx
movl %ecx, 0x80(%rax)
movl -0x30(%rbp), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
nopw (%rax,%rax)