// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb.c
// Function: mdb_rebalance
static int
mdb_rebalance(MDB_cursor *mc)
{
	MDB_node	*node;
	int rc;
	unsigned int ptop, minkeys;
	MDB_cursor	mn;
	indx_t oldki;

	minkeys = 1 + (IS_BRANCH(mc->mc_pg[mc->mc_top]));
	DPRINTF(("rebalancing %s page %"Z"u (has %u keys, %.1f%% full)",
	    IS_LEAF(mc->mc_pg[mc->mc_top]) ? "leaf" : "branch",
	    mdb_dbg_pgno(mc->mc_pg[mc->mc_top]), NUMKEYS(mc->mc_pg[mc->mc_top]),
		(float)PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) / 10));

	if (PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) >= FILL_THRESHOLD &&
		NUMKEYS(mc->mc_pg[mc->mc_top]) >= minkeys) {
		DPRINTF(("no need to rebalance page %"Z"u, above fill threshold",
		    mdb_dbg_pgno(mc->mc_pg[mc->mc_top])));
		return MDB_SUCCESS;
	}

	if (mc->mc_snum < 2) {
		MDB_page *mp = mc->mc_pg[0];
		if (IS_SUBP(mp)) {
			DPUTS("Can't rebalance a subpage, ignoring");
			return MDB_SUCCESS;
		}
		if (NUMKEYS(mp) == 0) {
			DPUTS("tree is completely empty");
			mc->mc_db->md_root = P_INVALID;
			mc->mc_db->md_depth = 0;
			mc->mc_db->md_leaf_pages = 0;
			rc = mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
			if (rc)
				return rc;
			/* Adjust cursors pointing to mp */
			mc->mc_snum = 0;
			mc->mc_top = 0;
			mc->mc_flags &= ~C_INITIALIZED;
			{
				MDB_cursor *m2, *m3;
				MDB_dbi dbi = mc->mc_dbi;

				for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
					if (mc->mc_flags & C_SUB)
						m3 = &m2->mc_xcursor->mx_cursor;
					else
						m3 = m2;
					if (m3->mc_snum < mc->mc_snum) continue;
					if (m3->mc_pg[0] == mp) {
						m3->mc_snum = 0;
						m3->mc_top = 0;
						m3->mc_flags &= ~C_INITIALIZED;
					}
				}
			}
		} else if (IS_BRANCH(mp) && NUMKEYS(mp) == 1) {
			int i;
			DPUTS("collapsing root page!");
			rc = mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
			if (rc)
				return rc;
			mc->mc_db->md_root = NODEPGNO(NODEPTR(mp, 0));
			rc = mdb_page_get(mc->mc_txn,mc->mc_db->md_root,&mc->mc_pg[0],NULL);
			if (rc)
				return rc;
			mc->mc_db->md_depth--;
			mc->mc_db->md_branch_pages--;
			mc->mc_ki[0] = mc->mc_ki[1];
			for (i = 1; i<mc->mc_db->md_depth; i++) {
				mc->mc_pg[i] = mc->mc_pg[i+1];
				mc->mc_ki[i] = mc->mc_ki[i+1];
			}
			{
				/* Adjust other cursors pointing to mp */
				MDB_cursor *m2, *m3;
				MDB_dbi dbi = mc->mc_dbi;

				for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
					if (mc->mc_flags & C_SUB)
						m3 = &m2->mc_xcursor->mx_cursor;
					else
						m3 = m2;
					if (m3 == mc || m3->mc_snum < mc->mc_snum) continue;
					if (m3->mc_pg[0] == mp) {
						for (i=0; i<m3->mc_snum; i++) {
							m3->mc_pg[i] = m3->mc_pg[i+1];
							m3->mc_ki[i] = m3->mc_ki[i+1];
						}
						m3->mc_snum--;
						m3->mc_top--;
					}
				}
			}
		} else
			DPUTS("root page doesn't need rebalancing");
		return MDB_SUCCESS;
	}

	/* The parent (branch page) must have at least 2 pointers,
	 * otherwise the tree is invalid.
	 */
	ptop = mc->mc_top-1;
	mdb_cassert(mc, NUMKEYS(mc->mc_pg[ptop]) > 1);

	/* Leaf page fill factor is below the threshold.
	 * Try to move keys from left or right neighbor, or
	 * merge with a neighbor page.
	 */

	/* Find neighbors.
	 */
	mdb_cursor_copy(mc, &mn);
	mn.mc_xcursor = NULL;

	oldki = mc->mc_ki[mc->mc_top];
	if (mc->mc_ki[ptop] == 0) {
		/* We're the leftmost leaf in our parent.
		 */
		DPUTS("reading right neighbor");
		mn.mc_ki[ptop]++;
		node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
		rc = mdb_page_get(mc->mc_txn,NODEPGNO(node),&mn.mc_pg[mn.mc_top],NULL);
		if (rc)
			return rc;
		mn.mc_ki[mn.mc_top] = 0;
		mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]);
	} else {
		/* There is at least one neighbor to the left.
		 */
		DPUTS("reading left neighbor");
		mn.mc_ki[ptop]--;
		node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
		rc = mdb_page_get(mc->mc_txn,NODEPGNO(node),&mn.mc_pg[mn.mc_top],NULL);
		if (rc)
			return rc;
		mn.mc_ki[mn.mc_top] = NUMKEYS(mn.mc_pg[mn.mc_top]) - 1;
		mc->mc_ki[mc->mc_top] = 0;
	}

	DPRINTF(("found neighbor page %"Z"u (%u keys, %.1f%% full)",
	    mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]),
		(float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10));

	/* If the neighbor page is above threshold and has enough keys,
	 * move one key from it. Otherwise we should try to merge them.
	 * (A branch page must never have less than 2 keys.)
	 */
	minkeys = 1 + (IS_BRANCH(mn.mc_pg[mn.mc_top]));
	if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys) {
		rc = mdb_node_move(&mn, mc);
		if (mc->mc_ki[ptop]) {
			oldki++;
		}
	} else {
		if (mc->mc_ki[ptop] == 0) {
			rc = mdb_page_merge(&mn, mc);
		} else {
			MDB_cursor dummy;
			oldki += NUMKEYS(mn.mc_pg[mn.mc_top]);
			mn.mc_ki[mn.mc_top] += mc->mc_ki[mn.mc_top] + 1;
			/* We want mdb_rebalance to find mn when doing fixups */
			if (mc->mc_flags & C_SUB) {
				dummy.mc_next = mc->mc_txn->mt_cursors[mc->mc_dbi];
				mc->mc_txn->mt_cursors[mc->mc_dbi] = &dummy;
				dummy.mc_xcursor = (MDB_xcursor *)&mn;
			} else {
				mn.mc_next = mc->mc_txn->mt_cursors[mc->mc_dbi];
				mc->mc_txn->mt_cursors[mc->mc_dbi] = &mn;
			}
			rc = mdb_page_merge(mc, &mn);
			if (mc->mc_flags & C_SUB)
				mc->mc_txn->mt_cursors[mc->mc_dbi] = dummy.mc_next;
			else
				mc->mc_txn->mt_cursors[mc->mc_dbi] = mn.mc_next;
			mdb_cursor_copy(&mn, mc);
		}
		mc->mc_flags &= ~C_EOF;
	}
	mc->mc_ki[mc->mc_top] = oldki;
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x380, %rsp            # imm = 0x380
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xa(%rax), %eax
andl $0x1, %eax
cmpl $0x1, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl 0x10(%rax), %eax
subl $0x10, %eax
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movzwl 0x42(%rdx), %edx
movq 0x48(%rcx,%rdx,8), %rcx
movzwl 0xe(%rcx), %ecx
movq -0x10(%rbp), %rdx
movq -0x10(%rbp), %rsi
movzwl 0x42(%rsi), %esi
movq 0x48(%rdx,%rsi,8), %rdx
movzwl 0xc(%rdx), %edx
subl %edx, %ecx
movzwl %cx, %ecx
subl %ecx, %eax
movl %eax, %eax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0x10(%rcx), %ecx
subl $0x10, %ecx
movl %ecx, %ecx
cqto
idivq %rcx
cmpq $0xfa, %rax
jl 0x10660
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
cmpl -0x24(%rbp), %eax
jb 0x10660
movl $0x0, -0x4(%rbp)
jmp 0x11069
movq -0x10(%rbp), %rax
movzwl 0x40(%rax), %eax
cmpl $0x2, %eax
jge 0x10b5c
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x40, %eax
cmpl $0x40, %eax
jne 0x1069f
movl $0x0, -0x4(%rbp)
jmp 0x11069
movq -0x1c0(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
cmpl $0x0, %eax
jne 0x10813
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq $-0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw $0x0, 0x6(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x28, %rdi
movq -0x1c0(%rbp), %rax
movq (%rax), %rsi
callq 0x12860
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x10715
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11069
movq -0x10(%rbp), %rax
movw $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x42(%rax)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
andl $-0x2, %ecx
movl %ecx, 0x44(%rax)
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x1d4(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movl -0x1d4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1c8(%rbp)
cmpq $0x0, -0x1c8(%rbp)
je 0x1080e
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x10791
movq -0x1c8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1d0(%rbp)
jmp 0x1079f
movq -0x1c8(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
movzwl 0x40(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jge 0x107b8
jmp 0x107f8
movq -0x1d0(%rbp), %rax
movq 0x48(%rax), %rax
cmpq -0x1c0(%rbp), %rax
jne 0x107f6
movq -0x1d0(%rbp), %rax
movw $0x0, 0x40(%rax)
movq -0x1d0(%rbp), %rax
movw $0x0, 0x42(%rax)
movq -0x1d0(%rbp), %rax
movl 0x44(%rax), %ecx
andl $-0x2, %ecx
movl %ecx, 0x44(%rax)
jmp 0x107f8
movq -0x1c8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1c8(%rbp)
jmp 0x10760
jmp 0x10b50
movq -0x1c0(%rbp), %rax
movzwl 0xa(%rax), %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0x10b4c
movq -0x1c0(%rbp), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
cmpl $0x1, %eax
jne 0x10b4c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x28, %rdi
movq -0x1c0(%rbp), %rax
movq (%rax), %rsi
callq 0x12860
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x10872
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11069
movq -0x1c0(%rbp), %rax
movq -0x1c0(%rbp), %rcx
movzwl 0x10(%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx), %eax
movl %eax, %ecx
movq -0x1c0(%rbp), %rax
movq -0x1c0(%rbp), %rdx
movzwl 0x10(%rdx), %edx
movslq %edx, %rdx
movzwl 0x2(%rax,%rdx), %eax
shlq $0x10, %rax
orq %rax, %rcx
movq -0x1c0(%rbp), %rax
movq -0x1c0(%rbp), %rdx
movzwl 0x10(%rdx), %edx
movslq %edx, %rdx
movzwl 0x4(%rax,%rdx), %eax
shlq $0x20, %rax
orq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rsi
movq -0x10(%rbp), %rdx
addq $0x48, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x10914
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11069
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw 0x6(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x6(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movw 0x14a(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x148(%rax)
movl $0x1, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movzwl 0x6(%rcx), %ecx
cmpl %ecx, %eax
jge 0x109d3
movq -0x10(%rbp), %rax
movl -0x1d8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x48(%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
movq %rdx, 0x48(%rax,%rcx,8)
movq -0x10(%rbp), %rax
movl -0x1d8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw 0x148(%rax,%rcx,2), %dx
movq -0x10(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
movw %dx, 0x148(%rax,%rcx,2)
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x1095c
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x1ec(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movl -0x1ec(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1e0(%rbp)
cmpq $0x0, -0x1e0(%rbp)
je 0x10b4a
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x10a2e
movq -0x1e0(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1e8(%rbp)
jmp 0x10a3c
movq -0x1e0(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq -0x1e8(%rbp), %rax
cmpq -0x10(%rbp), %rax
je 0x10a60
movq -0x1e8(%rbp), %rax
movzwl 0x40(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jge 0x10a65
jmp 0x10b34
movq -0x1e8(%rbp), %rax
movq 0x48(%rax), %rax
cmpq -0x1c0(%rbp), %rax
jne 0x10b32
movl $0x0, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
movq -0x1e8(%rbp), %rcx
movzwl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jge 0x10b0c
movq -0x1e8(%rbp), %rax
movl -0x1d8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x48(%rax,%rcx,8), %rdx
movq -0x1e8(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
movq %rdx, 0x48(%rax,%rcx,8)
movq -0x1e8(%rbp), %rax
movl -0x1d8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw 0x148(%rax,%rcx,2), %dx
movq -0x1e8(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
movw %dx, 0x148(%rax,%rcx,2)
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x10a87
movq -0x1e8(%rbp), %rax
movw 0x40(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x40(%rax)
movq -0x1e8(%rbp), %rax
movw 0x42(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x42(%rax)
jmp 0x10b34
movq -0x1e0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1e0(%rbp)
jmp 0x109fd
jmp 0x10b4e
jmp 0x10b4e
jmp 0x10b50
movl $0x0, -0x4(%rbp)
jmp 0x11069
movq -0x10(%rbp), %rax
movzwl 0x42(%rax), %eax
subl $0x1, %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
cmpl $0x1, %eax
jbe 0x10b86
jmp 0x10bb2
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0x7043(%rip), %rsi      # 0x17bdc
leaq 0x7059(%rip), %rdx      # 0x17bf9
leaq 0x6868(%rip), %rcx      # 0x1740f
movl $0x1f33, %r8d           # imm = 0x1F33
callq 0x133a0
movq -0x10(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x11080
movq $0x0, -0x1a0(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw 0x148(%rax,%rcx,2), %ax
movw %ax, -0x1b2(%rbp)
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x10ce9
movl -0x20(%rbp), %eax
movw -0x68(%rbp,%rax,2), %cx
addw $0x1, %cx
movw %cx, -0x68(%rbp,%rax,2)
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movl -0x20(%rbp), %edx
movq 0x48(%rcx,%rdx,8), %rcx
movl -0x20(%rbp), %edx
movzwl -0x68(%rbp,%rdx,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %eax
shlq $0x10, %rax
orq %rax, %rsi
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %eax
shlq $0x20, %rax
orq %rax, %rsi
leaq -0x1b0(%rbp), %rdx
addq $0x48, %rdx
movzwl -0x16e(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x10ca5
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11069
movzwl -0x16e(%rbp), %eax
movw $0x0, -0x68(%rbp,%rax,2)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movq 0x48(%rax,%rcx,8), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw %dx, 0x148(%rax,%rcx,2)
jmp 0x10dce
movl -0x20(%rbp), %eax
movw -0x68(%rbp,%rax,2), %cx
addw $-0x1, %cx
movw %cx, -0x68(%rbp,%rax,2)
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movq 0x48(%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movl -0x20(%rbp), %edx
movq 0x48(%rcx,%rdx,8), %rcx
movl -0x20(%rbp), %edx
movzwl -0x68(%rbp,%rdx,2), %edx
movzwl 0x10(%rcx,%rdx,2), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %eax
shlq $0x10, %rax
orq %rax, %rsi
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %eax
shlq $0x20, %rax
orq %rax, %rsi
leaq -0x1b0(%rbp), %rdx
addq $0x48, %rdx
movzwl -0x16e(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xa740
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x10d8e
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11069
movzwl -0x16e(%rbp), %eax
movq -0x168(%rbp,%rax,8), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
subl $0x1, %eax
movw %ax, %cx
movzwl -0x16e(%rbp), %eax
movw %cx, -0x68(%rbp,%rax,2)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw $0x0, 0x148(%rax,%rcx,2)
movzwl -0x16e(%rbp), %eax
movq -0x168(%rbp,%rax,8), %rax
movzwl 0xa(%rax), %eax
andl $0x1, %eax
cmpl $0x1, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl 0x10(%rax), %eax
subl $0x10, %eax
movzwl -0x16e(%rbp), %ecx
movq -0x168(%rbp,%rcx,8), %rcx
movzwl 0xe(%rcx), %ecx
movzwl -0x16e(%rbp), %edx
movq -0x168(%rbp,%rdx,8), %rdx
movzwl 0xc(%rdx), %edx
subl %edx, %ecx
movzwl %cx, %ecx
subl %ecx, %eax
movl %eax, %eax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0x10(%rcx), %ecx
subl $0x10, %ecx
movl %ecx, %ecx
cqto
idivq %rcx
cmpq $0xfa, %rax
jl 0x10eb7
movzwl -0x16e(%rbp), %eax
movq -0x168(%rbp,%rax,8), %rax
movzwl 0xc(%rax), %eax
subl $0x10, %eax
shrl %eax
cmpl -0x24(%rbp), %eax
jbe 0x10eb7
movq -0x10(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x11160
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
cmpw $0x0, 0x148(%rax,%rcx,2)
je 0x10eb2
movw -0x1b2(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1b2(%rbp)
jmp 0x11048
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movzwl 0x148(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x10ee3
movq -0x10(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x11ba0
movl %eax, -0x1c(%rbp)
jmp 0x1103b
movzwl -0x16e(%rbp), %eax
movq -0x168(%rbp,%rax,8), %rax
movzwl 0xc(%rax), %ecx
subl $0x10, %ecx
shrl %ecx
movzwl -0x1b2(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x1b2(%rbp)
movq -0x10(%rbp), %rax
movzwl -0x16e(%rbp), %ecx
movzwl 0x148(%rax,%rcx,2), %edx
addl $0x1, %edx
movzwl -0x16e(%rbp), %eax
movzwl -0x68(%rbp,%rax,2), %ecx
addl %edx, %ecx
movw %cx, -0x68(%rbp,%rax,2)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x10f8f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x378(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
leaq -0x378(%rbp), %rdx
movq %rdx, (%rax,%rcx,8)
leaq -0x1b0(%rbp), %rax
movq %rax, -0x368(%rbp)
jmp 0x10fcb
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1b0(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
leaq -0x1b0(%rbp), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x11ba0
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1100d
movq -0x378(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0x1102b
movq -0x1b0(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x11080
movq -0x10(%rbp), %rax
movl 0x44(%rax), %ecx
andl $-0x3, %ecx
movl %ecx, 0x44(%rax)
movw -0x1b2(%rbp), %dx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x42(%rcx), %ecx
movw %dx, 0x148(%rax,%rcx,2)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x380, %rsp            # imm = 0x380
popq %rbp
retq
nopw %cs:(%rax,%rax)