// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x90(%rbp)
movq %rdi, -0x88(%rbp)
movq %rsi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0xc334(%rip), %rax      # 0x14960
movq %rax, -0x30(%rbp)
movq 0xc331(%rip), %rax      # 0x14968
movq %rax, -0x28(%rbp)
movq 0xc32e(%rip), %rax      # 0x14970
movq %rax, -0x20(%rbp)
movl 0xc32c(%rip), %eax      # 0x14978
movl %eax, -0x18(%rbp)
movl $0x1, -0x34(%rbp)
cmpl $0x7, -0x34(%rbp)
jge 0x8711
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x80(%rbp), %rdi
addq $0x800, %rdi            # imm = 0x800
movl -0x38(%rbp), %eax
subl $0x1, %eax
cltq
movslq -0x30(%rbp,%rax,4), %rax
imulq $0x12c, %rax, %rax      # imm = 0x12C
addq %rax, %rdi
callq 0x65e0
movq -0x80(%rbp), %rdi
movl %eax, -0x94(%rbp)
addq $0x800, %rdi            # imm = 0x800
movslq -0x38(%rbp), %rax
movslq -0x30(%rbp,%rax,4), %rax
imulq $0x12c, %rax, %rax      # imm = 0x12C
addq %rax, %rdi
callq 0x65e0
movl %eax, %ecx
movl -0x94(%rbp), %eax
cmpl %ecx, %eax
jg 0x86c4
jmp 0x8703
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
cltq
movl -0x30(%rbp,%rax,4), %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
cltq
movl -0x30(%rbp,%rax,4), %ecx
movslq -0x38(%rbp), %rax
movl %ecx, -0x30(%rbp,%rax,4)
movl -0x3c(%rbp), %ecx
movl -0x38(%rbp), %eax
addl $0x1, %eax
cltq
movl %ecx, -0x30(%rbp,%rax,4)
cmpl $0x0, -0x38(%rbp)
jg 0x8666
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x8656
movq -0x90(%rbp), %rdi
callq 0xf770
movq -0x80(%rbp), %rdi
movl $0x0, -0x40(%rbp)
addq $0x800, %rdi            # imm = 0x800
addq $0xa8c, %rdi            # imm = 0xA8C
callq 0x6400
movq -0x80(%rbp), %rdi
shll $0x4, %eax
movl %eax, -0xa4(%rbp)
addq $0x800, %rdi            # imm = 0x800
addq $0xbb8, %rdi            # imm = 0xBB8
callq 0x6400
movq -0x90(%rbp), %rdi
movl %eax, %ecx
movl -0xa4(%rbp), %eax
addl $0x1, %ecx
orl %ecx, %eax
movb %al, -0x9d(%rbp)
movl -0x40(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
callq 0xf7b0
movb -0x9d(%rbp), %cl
movq -0x80(%rbp), %rdi
movb %cl, (%rax)
addq $0x800, %rdi            # imm = 0x800
addq $0xce4, %rdi            # imm = 0xCE4
callq 0x6400
movq -0x80(%rbp), %rdi
shll $0x4, %eax
movl %eax, -0x9c(%rbp)
addq $0x800, %rdi            # imm = 0x800
addq $0xe10, %rdi            # imm = 0xE10
callq 0x6400
movq -0x90(%rbp), %rdi
movl %eax, %ecx
movl -0x9c(%rbp), %eax
orl %ecx, %eax
movb %al, -0x95(%rbp)
movl -0x40(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
callq 0xf7b0
movb -0x95(%rbp), %dl
movq %rax, %rcx
movq -0x80(%rbp), %rax
movb %dl, (%rcx)
movl $0x5, -0x44(%rbp)
movl 0x1908(%rax), %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x7, -0x4c(%rbp)
jge 0x8965
movq -0x80(%rbp), %rax
addq $0x800, %rax            # imm = 0x800
movslq -0x4c(%rbp), %rcx
movslq -0x30(%rbp,%rcx,4), %rcx
imulq $0x12c, %rcx, %rcx      # imm = 0x12C
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x6440
movl %eax, -0x5c(%rbp)
movl $0xa, -0x60(%rbp)
movl -0x48(%rbp), %eax
shll $0x6, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x5c(%rbp)
jle 0x8894
movl -0x5c(%rbp), %edx
movl -0x60(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal -0x1(%rax,%rcx), %eax
movl %eax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x6500
movl %eax, %ecx
movl %ecx, -0x66(%rbp)
shrq $0x20, %rax
movw %ax, -0x62(%rbp)
movzbl -0x61(%rbp), %eax
addl $0x1, %eax
orl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x60(%rbp), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
shll $0x4, %eax
movl %eax, -0x48(%rbp)
movl -0x5c(%rbp), %eax
orl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl $0x1, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x88f6
movl -0x48(%rbp), %eax
addl %eax, %eax
movl %eax, -0x48(%rbp)
movq -0x58(%rbp), %rdi
movl -0x6c(%rbp), %esi
callq 0x6500
movl %eax, %ecx
movl %ecx, -0x72(%rbp)
shrq $0x20, %rax
movw %ax, -0x6e(%rbp)
movzbl -0x72(%rbp), %eax
sarl %eax
orl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x88b6
movl $0x15, %eax
subl -0x60(%rbp), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x15, %ecx
subl -0x60(%rbp), %ecx
movl -0x48(%rbp), %eax
shll %cl, %eax
movl %eax, -0x48(%rbp)
movq -0x90(%rbp), %rdi
movl -0x44(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
movl -0x44(%rbp), %ecx
sarl %cl, %eax
andl $0xff, %eax
movb %al, -0xa5(%rbp)
movl -0x40(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
callq 0xf7b0
movb -0xa5(%rbp), %cl
movb %cl, (%rax)
cmpl $0x8, -0x44(%rbp)
jge 0x8914
jmp 0x8957
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8811
cmpl $0x0, -0x44(%rbp)
jle 0x8990
movq -0x90(%rbp), %rdi
movl -0x48(%rbp), %eax
andl $0xff, %eax
movb %al, -0xa6(%rbp)
movl -0x40(%rbp), %esi
callq 0xf7b0
movb -0xa6(%rbp), %cl
movb %cl, (%rax)
movq -0x88(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq