// File: /workspace/github_cpp_makefile_star2_O1/ShootMe[P]Klondike-Solver/Solitaire.cpp
// Function: Solitaire::SolveRandom(int, int)
SolveResult Solitaire::SolveRandom(int numberOfTimesToPlay, int solutionsToFind) {
	MakeAutoMoves();
	if (movesAvailableCount == 0) { return foundationCount == 52 ? SolvedMinimal : Impossible; }

	Move bestSolution[512];
	bestSolution[0].Count = 255;
	int bestSolutionMoveCount = 512;
	int maxFoundationCount = 0;
	int bestRoundCount = 10;

	shared_ptr<MoveNode> firstNode = movesMadeCount > 0 ? make_shared<MoveNode>(movesMade[0]) : NULL;
	shared_ptr<MoveNode> node = firstNode;
	for (int i = 1; i < movesMadeCount; i++) {
		node->Parent = make_shared<MoveNode>(movesMade[i]);
		node = node->Parent;
	}

	int solutionCount = 0;
	while ((maxFoundationCount < 52 || solutionCount < solutionsToFind) && numberOfTimesToPlay-- > 0) {
		while (foundationCount < 52 && movesAvailableCount > 0 && movesMadeCount < 500 && roundCount <= 10) {
			int drawIndex = -1;
			for (int i = 0; i < movesAvailableCount; i++) {
				if (movesAvailable[i].From == WASTE) {
					drawIndex = i;
					break;
				}
			}

			if (drawIndex <= 0) {
				MakeMove(random.Next1() % movesAvailableCount);
			} else {
				int r = random.Next1() % (drawIndex + (random.Next1() & 1) + (movesAvailableCount >> 3));
				if (r >= drawIndex) {
					MakeMove((random.Next1() % (movesAvailableCount - drawIndex)) + drawIndex);
				} else {
					MakeMove(r);
				}
			}
			UpdateAvailableMoves();
		}

		if (foundationCount == 52 || foundationCount > maxFoundationCount) {
			int movesTotal = MovesMadeNormalizedCount();
			if (foundationCount == 52) { solutionCount++; }
			if (foundationCount < 52 || movesTotal < bestSolutionMoveCount) {
				if (foundationCount == 52) {
					bestRoundCount = roundCount;
					bestSolutionMoveCount = movesTotal;
				}
				maxFoundationCount = foundationCount;

				//Save solution
				for (int i = 0; i < movesMadeCount; i++) {
					bestSolution[i] = movesMade[i];
				}
				bestSolution[movesMadeCount].Count = 255;
			}
		}

		//Reset game
		node = firstNode;
		ResetGame(drawCount);
		while (node != NULL) {
			MakeMove(node->Value);
			node = node->Parent;
		}
		UpdateAvailableMoves();
	}

	//Reset game to best solution found
	ResetGame(drawCount);
	for (int i = 0; bestSolution[i].Count < 255; i++) {
		MakeMove(bestSolution[i]);
	}
	return foundationCount == 52 ? SolvedMayNotBeMinimal : CouldNotComplete;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movl %edx, 0x3c(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
callq 0x5658
cmpl $0x0, 0x1910(%rbx)
je 0x56ff
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x50, %rdi
callq 0x373c
addq $0x4, %r14
cmpq $0x800, %r14            # imm = 0x800
jne 0x56b3
movb $-0x1, 0x52(%rsp)
cmpl $0x0, 0x1914(%rbx)
movl %ebp, (%rsp)
jle 0x5711
leaq 0x28(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rcx
callq 0x9b54
jmp 0x5719
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rbx)
sete %bpl
jmp 0x5a55
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x5747
movq 0x8897(%rip), %rcx      # 0xdfd0
cmpb $0x0, (%rcx)
je 0x5743
incl 0x8(%rax)
jmp 0x5747
lock
incl 0x8(%rax)
leaq 0x18(%rsp), %r15
cmpl $0x2, 0x1914(%rbx)
jl 0x57e7
leaq 0x4(%rbx), %r13
movl $0x1, %r14d
leaq 0x40(%rsp), %rbp
leaq 0xf(%rsp), %r12
movq $0x0, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x9b54
movq 0x10(%rsp), %rax
movaps 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x40(%rsp)
movq 0x8(%rax), %rdi
movups %xmm0, (%rax)
testq %rdi, %rdi
je 0x57ac
callq 0x3794
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x57bb
callq 0x3794
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
addq $0x8, %rsi
movq %r15, %rdi
callq 0x3804
incq %r14
movslq 0x1914(%rbx), %rax
addq $0x4, %r13
cmpq %rax, %r14
jl 0x576d
leaq 0x18f4(%rbx), %rbp
movl $0x200, 0x4(%rsp)       # imm = 0x200
movq $0x0, 0x30(%rsp)
movl $0x0, 0x8(%rsp)
movl (%rsp), %r12d
cmpl $0x34, 0x8(%rsp)
jl 0x5821
movq 0x30(%rsp), %rax
cmpl 0x3c(%rsp), %eax
jge 0x59f5
testl %r12d, %r12d
jle 0x59f5
decl %r12d
movl 0x190c(%rbx), %r14d
movl 0x1914(%rbx), %r13d
cmpl $0x33, %r14d
jg 0x5916
movl 0x1910(%rbx), %eax
testl %eax, %eax
jle 0x5916
cmpl $0x1f3, %r13d           # imm = 0x1F3
jg 0x5916
cmpl $0xa, 0x1908(%rbx)
jg 0x5916
xorl %r14d, %r14d
cmpb $0x0, 0x1874(%rbx,%r14,4)
je 0x5889
incq %r14
cmpq %r14, %rax
jne 0x5870
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %r14d, %r14d
jle 0x58e8
movq %rbp, %rdi
callq 0x9cf2
movl %eax, %r13d
movq %rbp, %rdi
callq 0x9cf2
andl $0x1, %eax
addl %r14d, %eax
movl 0x1910(%rbx), %ecx
sarl $0x3, %ecx
addl %eax, %ecx
movl %r13d, %eax
cltd
idivl %ecx
cmpl %r14d, %edx
jl 0x58d4
movq %rbp, %rdi
callq 0x9cf2
movl 0x1910(%rbx), %ecx
subl %r14d, %ecx
cltd
idivl %ecx
addl %r14d, %edx
movslq %edx, %rax
movl 0x1874(%rbx,%rax,4), %esi
movq %rbx, %rdi
callq 0x42a4
jmp 0x5909
movq %rbp, %rdi
callq 0x9cf2
cltd
idivl 0x1910(%rbx)
movslq %edx, %rax
movl 0x1874(%rbx,%rax,4), %esi
movq %rbx, %rdi
callq 0x42a4
movq %rbx, %rdi
callq 0x4456
jmp 0x582d
cmpl $0x34, %r14d
setne %al
cmpl 0x8(%rsp), %r14d
setle %cl
testb %cl, %al
jne 0x5993
movl %r12d, (%rsp)
xorl %r12d, %r12d
cmpl $0x34, %r14d
sete %r12b
movq %rbx, %rdi
callq 0x4bfc
movq 0x30(%rsp), %rcx
addl %r12d, %ecx
movq %rcx, 0x30(%rsp)
cmpl $0x34, %r14d
jl 0x5959
cmpl 0x4(%rsp), %eax
jge 0x598f
cmpl $0x34, %r14d
movl 0x4(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x4(%rsp)
testl %r13d, %r13d
jle 0x5982
leaq (,%r13,4), %rdx
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x2180
movslq %r13d, %rax
movb $-0x1, 0x52(%rsp,%rax,4)
movl %r14d, 0x8(%rsp)
movl (%rsp), %r12d
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3804
movl 0x1904(%rbx), %esi
movq %rbx, %rdi
callq 0x5aae
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x59e8
movl 0x10(%rax), %esi
movq %rbx, %rdi
callq 0x42a4
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
addq $0x8, %rsi
movq %r15, %rdi
callq 0x3804
jmp 0x59b8
movq %rbx, %rdi
callq 0x4456
jmp 0x580b
movl 0x1904(%rbx), %esi
movq %rbx, %rdi
callq 0x5aae
cmpb $-0x1, 0x52(%rsp)
leaq 0x50(%rsp), %r14
je 0x5a25
movl (%r14), %esi
movq %rbx, %rdi
callq 0x42a4
cmpb $-0x1, 0x6(%r14)
leaq 0x4(%r14), %r14
jne 0x5a0f
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rbx)
sete %bl
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5a40
callq 0x3794
movb %bl, %bpl
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x5a52
callq 0x3794
orl $-0x2, %ebp
movl %ebp, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5a75
jmp 0x5a75
jmp 0x5a75
jmp 0x5a75
jmp 0x5a75
jmp 0x5a75
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5a87
callq 0x3794
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x5a96
callq 0x3794
movq %rbx, %rdi
callq 0x22e0