// File: /workspace/github_cpp_makefile_star2_O3/ShootMe[P]Klondike-Solver/Solitaire.cpp
// Function: Solitaire::GameState()
HashKey Solitaire::GameState() {
	int order[7] = { TABLEAU1, TABLEAU2, TABLEAU3, TABLEAU4, TABLEAU5, TABLEAU6, TABLEAU7 };
	int current = 1;
	//sort the piles
	while (current < 7) {
		int search = current;

		do {
			if (piles[order[search - 1]].HighValue() <= piles[order[search]].HighValue()) {
				break;
			}

			int temp = order[--search];
			order[search] = order[search + 1];
			order[search + 1] = temp;
		} while (search > 0);

		++current;
	}

	HashKey key;
	int z = 0;
	key[z++] = (piles[FOUNDATION1C].Size() << 4) | (piles[FOUNDATION2D].Size() + 1);
	key[z++] = (piles[FOUNDATION3S].Size() << 4) | piles[FOUNDATION4H].Size();

	int bits = 5;
	int mask = roundCount;
	for (int i = 0; i < 7; ++i) {
		Pile & pile = piles[order[i]];
		int upSize = pile.UpSize();

		int added = 10;
		mask <<= 6;
		if (upSize > 0) {
			added += upSize - 1;
			mask |= pile.Up(0).Value + 1;
		}
		bits += added;
		mask <<= 4;
		mask |= upSize;
		for (int j = 1; j < upSize; ++j) {
			mask <<= 1;
			mask |= pile.Up(j).Suit >> 1;
		}

		bits += 21 - added;
		mask <<= 21 - added;
		do {
			bits -= 8;
			key[z++] = (mask >> bits) & 255;
		} while (bits >= 8);
	}
	if (bits > 0) {
		key[z] = mask & 255;
	}

	return key;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movups 0x5cda(%rip), %xmm0     # 0xaa3c
movups %xmm0, 0x2c(%rsp)
movaps 0x5cc2(%rip), %xmm0     # 0xaa30
movaps %xmm0, 0x20(%rsp)
movq %rsi, (%rsp)
leaq 0x800(%rsi), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %eax
movl $0x2, %r13d
movq 0x18(%rsp), %r12
movq %rax, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movslq 0x18(%rsp,%r13,4), %r14
imulq $0x12c, %r14, %rdi      # imm = 0x12C
addq %r12, %rdi
callq 0x3b08
movl %eax, %ebp
movslq 0x1c(%rsp,%r13,4), %r15
imulq $0x12c, %r15, %rdi      # imm = 0x12C
addq %r12, %rdi
callq 0x3b08
cmpl %eax, %ebp
jle 0x4dde
movl %r15d, 0x18(%rsp,%r13,4)
movl %r14d, 0x1c(%rsp,%r13,4)
decq %r13
cmpq $0x1, %r13
ja 0x4d9d
movq 0x10(%rsp), %rax
incq %rax
movq 0x8(%rsp), %r13
incq %r13
cmpq $0x7, %rax
jne 0x4d93
xorps %xmm0, %xmm0
movups %xmm0, 0x2(%rbx)
movl $0x0, 0x11(%rbx)
movq (%rsp), %r14
leaq 0x128c(%r14), %rdi
callq 0x3a66
movl %eax, %ebp
shll $0x4, %ebp
leaq 0x13b8(%r14), %rdi
callq 0x3a66
incl %eax
orl %ebp, %eax
movb %al, (%rbx)
leaq 0x14e4(%r14), %rdi
callq 0x3a66
movl %eax, %ebp
shll $0x4, %ebp
leaq 0x1610(%r14), %rdi
callq 0x3a66
orl %ebp, %eax
movb %al, 0x1(%rbx)
movl 0x1908(%r14), %r14d
movl $0x5, %ecx
movl $0x2, %ebp
xorl %r13d, %r13d
movq %rbx, 0x40(%rsp)
movl %ecx, 0x10(%rsp)
movslq 0x20(%rsp,%r13,4), %rax
imulq $0x12c, %rax, %r15      # imm = 0x12C
addq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x3a76
movl %eax, %r12d
testl %eax, %eax
jle 0x4ef9
movq %r15, %rdi
xorl %esi, %esi
callq 0x3ab4
movl $0xc, %ecx
subl %r12d, %ecx
shll $0xa, %r14d
shrq $0x24, %rax
andl $0xff0, %eax            # imm = 0xFF0
addl $0x10, %eax
orl %r12d, %r14d
orl %eax, %r14d
cmpl $0x1, %r12d
je 0x4f05
movl %ecx, (%rsp)
movq %r13, 0x8(%rsp)
movl $0x1, %r13d
movl %r14d, %ebx
addl %r14d, %ebx
movq %r15, %rdi
movl %r13d, %esi
callq 0x3ab4
movq %rax, %r14
shrl %r14d
andl $0x7f, %r14d
orl %ebx, %r14d
incl %r13d
cmpl %r13d, %r12d
jne 0x4ec4
movq 0x40(%rsp), %rbx
movq 0x8(%rsp), %r13
movl (%rsp), %ecx
jmp 0x4f05
shll $0xa, %r14d
orl %r12d, %r14d
movl $0xb, %ecx
shll %cl, %r14d
movq %rbp, %rax
shlq $0x20, %rax
movslq %ebp, %rbp
movl 0x10(%rsp), %ecx
addl $0x1d, %ecx
movl %ecx, %edx
movabsq $0x100000000, %rdi      # imm = 0x100000000
addq %rdi, %rax
leal -0x10(%rdx), %ecx
movl %r14d, %esi
sarl %cl, %esi
movb %sil, (%rbx,%rbp)
incq %rbp
addl $-0x8, %edx
cmpl $0xf, %edx
jg 0x4f25
incq %r13
leal -0x8(%rdx), %ecx
cmpq $0x7, %r13
jne 0x4e64
cmpl $0x8, %edx
jle 0x4f5c
sarq $0x20, %rax
movb %r14b, (%rbx,%rax)
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq