// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movups 0x5b44(%rip), %xmm0     # 0xaa3c
movups %xmm0, 0x2c(%rsp)
movaps 0x5b2c(%rip), %xmm0     # 0xaa30
movaps %xmm0, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
leaq 0x800(%rsi), %r13
movl $0x1, %eax
movl $0x2, %r15d
movq %rax, 0x10(%rsp)
movq %r15, %r12
movslq 0x18(%rsp,%r12,4), %r14
imulq $0x12c, %r14, %rdi      # imm = 0x12C
addq %r13, %rdi
callq 0x3b50
movl %eax, %ebp
movslq 0x1c(%rsp,%r12,4), %rbx
imulq $0x12c, %rbx, %rdi      # imm = 0x12C
addq %r13, %rdi
callq 0x3b50
cmpl %eax, %ebp
jle 0x4f69
movl %ebx, 0x18(%rsp,%r12,4)
movl %r14d, 0x1c(%rsp,%r12,4)
decq %r12
cmpq $0x1, %r12
jg 0x4f28
movq 0x10(%rsp), %rax
incq %rax
incq %r15
cmpq $0x7, %rax
jne 0x4f20
xorps %xmm0, %xmm0
movq 0x18(%rsp), %rbx
movups %xmm0, (%rbx)
movq $0x0, 0xd(%rbx)
movq 0x8(%rsp), %r14
leaq 0x128c(%r14), %rdi
callq 0x3aae
movl %eax, %ebp
shll $0x4, %ebp
leaq 0x13b8(%r14), %rdi
callq 0x3aae
incl %eax
orl %ebp, %eax
movb %al, (%rbx)
leaq 0x14e4(%r14), %rdi
callq 0x3aae
movl %eax, %ebp
shll $0x4, %ebp
leaq 0x1610(%r14), %rdi
callq 0x3aae
orl %ebp, %eax
movb %al, 0x1(%rbx)
movl 0x1908(%r14), %ebx
movl $0x5, %esi
movl $0x2, %r15d
xorl %ecx, %ecx
movl %esi, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
movslq 0x20(%rsp,%rcx,4), %rax
imulq $0x12c, %rax, %r14      # imm = 0x12C
addq %r13, %r14
movq %r14, %rdi
callq 0x3abe
movl %eax, %ebp
shll $0x6, %ebx
testl %eax, %eax
jle 0x5036
movq %r14, %rdi
xorl %esi, %esi
callq 0x3afc
shrq $0x28, %rax
movzbl %al, %eax
incl %eax
orl %eax, %ebx
movl $0xc, %eax
subl %ebp, %eax
movl %eax, 0x4(%rsp)
jmp 0x503e
movl $0xb, 0x4(%rsp)
shll $0x4, %ebx
orl %ebp, %ebx
cmpl $0x2, %ebp
jl 0x506e
movl $0x1, %r12d
addl %ebx, %ebx
movq %r14, %rdi
movl %r12d, %esi
callq 0x3afc
shrl %eax
andl $0x7f, %eax
orl %ebx, %eax
incl %r12d
movl %eax, %ebx
cmpl %r12d, %ebp
jne 0x504e
jmp 0x5070
movl %ebx, %eax
movl %eax, %ebx
movl 0x4(%rsp), %ecx
shll %cl, %ebx
movq %r15, %rdi
shlq $0x20, %rdi
movslq %r15d, %r15
movl 0x8(%rsp), %eax
addl $0x1d, %eax
movl %eax, %edx
movq 0x18(%rsp), %rax
movabsq $0x100000000, %r8       # imm = 0x100000000
addq %r8, %rdi
leal -0x10(%rdx), %ecx
movl %ebx, %esi
sarl %cl, %esi
movb %sil, (%rax,%r15)
incq %r15
addl $-0x8, %edx
cmpl $0xf, %edx
jg 0x509a
movq 0x10(%rsp), %rcx
incq %rcx
leal -0x8(%rdx), %esi
cmpq $0x7, %rcx
jne 0x4feb
cmpl $0x8, %edx
jle 0x50d4
sarq $0x20, %rdi
movb %bl, (%rax,%rdi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop