// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, 0x10(%rsp)
movups 0x507c(%rip), %xmm0     # 0x9a1c
movups %xmm0, 0x3c(%rsp)
movaps 0x5064(%rip), %xmm0     # 0x9a10
movaps %xmm0, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
leaq 0x800(%rsi), %rax
movq %rax, 0x28(%rsp)
pushq $0x1
popq %rax
pushq $0x2
popq %r15
movq 0x28(%rsp), %rbx
cmpq $0x7, %rax
je 0x4a2a
movq %rax, 0x20(%rsp)
movq %r15, %r12
movslq 0x28(%rsp,%r12,4), %r13
imulq $0x12c, %r13, %rdi      # imm = 0x12C
addq %rbx, %rdi
callq 0x3916
movl %eax, %ebp
movslq 0x2c(%rsp,%r12,4), %r14
imulq $0x12c, %r14, %rdi      # imm = 0x12C
addq %rbx, %rdi
callq 0x3916
cmpl %eax, %ebp
jle 0x4a1d
movl %r14d, 0x28(%rsp,%r12,4)
movl %r13d, 0x2c(%rsp,%r12,4)
decq %r12
cmpq $0x1, %r12
ja 0x49dc
movq 0x20(%rsp), %rax
incq %rax
incq %r15
jmp 0x49ce
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x82ae
movq 0x18(%rsp), %r14
leaq 0x128c(%r14), %rdi
callq 0x3874
movl %eax, %ebp
shll $0x4, %ebp
leaq 0x13b8(%r14), %rdi
callq 0x3874
incl %eax
orl %ebp, %eax
movb %al, (%rbx)
leaq 0x14e4(%r14), %rdi
callq 0x3874
movl %eax, %ebp
shll $0x4, %ebp
leaq 0x1610(%r14), %rdi
callq 0x3874
orl %ebp, %eax
movb %al, 0x1(%rbx)
movl 0x1908(%r14), %ebx
pushq $0x2
popq %r13
pushq $0x5
popq %r15
xorl %eax, %eax
movslq %r13d, %r14
cmpq $0x7, %rax
je 0x4b54
movq %rax, 0x18(%rsp)
movslq 0x30(%rsp,%rax,4), %rax
imulq $0x12c, %rax, %r12      # imm = 0x12C
addq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x3884
movl %eax, %ebp
shll $0x6, %ebx
testl %eax, %eax
movq %r12, 0x20(%rsp)
jle 0x4ae7
movq %r12, %rdi
xorl %esi, %esi
callq 0x38c2
shrq $0x28, %rax
movzbl %al, %eax
incl %eax
orl %eax, %ebx
pushq $0xc
popq %rax
subl %ebp, %eax
jmp 0x4aea
pushq $0xb
popq %rax
movl %eax, 0xc(%rsp)
shll $0x4, %ebx
orl %ebp, %ebx
pushq $0x1
popq %r12
cmpl %ebp, %r12d
jge 0x4b19
addl %ebx, %ebx
movq 0x20(%rsp), %rdi
movl %r12d, %esi
callq 0x38c2
shrl %eax
andl $0x7f, %eax
orl %ebx, %eax
incl %r12d
movl %eax, %ebx
jmp 0x4af7
movl 0xc(%rsp), %ecx
shll %cl, %ebx
addq 0x10(%rsp), %r14
addl $0x1d, %r15d
leal -0x10(%r15), %ecx
movl %ebx, %eax
sarl %cl, %eax
movb %al, (%r14)
incq %r14
incl %r13d
addl $-0x8, %r15d
cmpl $0xf, %r15d
jg 0x4b28
movq 0x18(%rsp), %rax
incq %rax
addl $-0x8, %r15d
jmp 0x4a92
testl %r15d, %r15d
movq 0x10(%rsp), %rax
jle 0x4b62
movb %bl, (%rax,%r14)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop