// File: /workspace/github_cpp_makefile_star2_O2/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, 0xc(%rsp)
movq %rdi, %rbx
callq 0x5036
cmpl $0x0, 0x1910(%rbx)
je 0x50ca
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x50, %rdi
callq 0x34b4
addq $0x4, %r14
cmpq $0x800, %r14            # imm = 0x800
jne 0x5093
movb $-0x1, 0x52(%rsp)
cmpl $0x0, 0x1914(%rbx)
jle 0x50dc
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x8267
jmp 0x50e4
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rbx)
sete %bpl
jmp 0x5377
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x28(%rsp), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x841e
leaq 0x48(%rsp), %r12
leaq 0x4(%rbx), %r13
pushq $0x1
popq %r14
leaq 0x40(%rsp), %rbp
movslq 0x1914(%rbx), %rax
cmpq %rax, %r14
jge 0x514a
movq %rbp, %rdi
movq %r13, %rsi
callq 0x8267
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x35ee
movq %r12, %rdi
callq 0x350c
movq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x3588
incq %r14
addq $0x4, %r13
jmp 0x5108
leaq 0x18f4(%rbx), %r15
movl $0x200, (%rsp)          # imm = 0x200
leaq 0x28(%rsp), %r12
leaq 0x10(%rsp), %r13
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x34, 0x4(%rsp)
jl 0x5187
movl 0x8(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0x5328
cmpl $0x0, 0xc(%rsp)
jle 0x5328
decl 0xc(%rsp)
movl 0x190c(%rbx), %ebp
movl 0x1914(%rbx), %r14d
cmpl $0x33, %ebp
jg 0x5269
movl 0x1910(%rbx), %eax
testl %eax, %eax
jle 0x5269
cmpl $0x1f3, %r14d           # imm = 0x1F3
jg 0x5269
cmpl $0xa, 0x1908(%rbx)
jg 0x5269
xorl %r14d, %r14d
cmpq %r14, %rax
je 0x5243
cmpb $0x0, 0x1874(%rbx,%r14,4)
je 0x51ec
incq %r14
jmp 0x51d7
testq %r14, %r14
je 0x5243
movq %r15, %rdi
callq 0x8db0
movl %eax, %ebp
movq %r15, %rdi
callq 0x8db0
movl %eax, %ecx
andl $0x1, %ecx
movl 0x1910(%rbx), %eax
sarl $0x3, %eax
addl %r14d, %ecx
addl %eax, %ecx
movl %ebp, %eax
cltd
idivl %ecx
cmpl %r14d, %edx
jl 0x5237
movq %r15, %rdi
callq 0x8db0
movl 0x1910(%rbx), %ecx
subl %r14d, %ecx
cltd
idivl %ecx
addl %r14d, %edx
movq %rbx, %rdi
movl %edx, %esi
callq 0x53b6
jmp 0x525c
movq %r15, %rdi
callq 0x8db0
cltd
idivl 0x1910(%rbx)
movq %rbx, %rdi
movl %edx, %esi
callq 0x53b6
movq %rbx, %rdi
callq 0x4030
jmp 0x5196
cmpl $0x34, %ebp
setne %al
cmpl 0x4(%rsp), %ebp
setle %cl
testb %cl, %al
jne 0x52de
xorl %r13d, %r13d
cmpl $0x34, %ebp
sete %r13b
movq %rbx, %rdi
callq 0x46cc
addl %r13d, 0x8(%rsp)
cmpl $0x34, %ebp
jl 0x52a2
cmpl (%rsp), %eax
jl 0x52a2
leaq 0x10(%rsp), %r13
jmp 0x52de
movslq %r14d, %rcx
testl %r14d, %r14d
movl $0x0, %edx
cmovlel %edx, %r14d
xorl %edx, %edx
leaq 0x10(%rsp), %r13
cmpq %rdx, %r14
je 0x52c9
movl (%rbx,%rdx,4), %esi
movl %esi, 0x50(%rsp,%rdx,4)
incq %rdx
jmp 0x52b8
cmpl $0x34, %ebp
movl (%rsp), %edx
cmovel %eax, %edx
movl %edx, (%rsp)
movb $-0x1, 0x52(%rsp,%rcx,4)
movl %ebp, 0x4(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3588
movl 0x1904(%rbx), %esi
movq %rbx, %rdi
callq 0x53c6
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x531b
movl 0x10(%rax), %esi
movq %rbx, %rdi
callq 0x3e7e
movq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x3588
jmp 0x52f7
movq %rbx, %rdi
callq 0x4030
jmp 0x5172
movl 0x1904(%rbx), %esi
movq %rbx, %rdi
callq 0x53c6
leaq 0x52(%rsp), %r14
cmpb $-0x1, (%r14)
je 0x5353
movl -0x2(%r14), %esi
movq %rbx, %rdi
callq 0x3e7e
addq $0x4, %r14
jmp 0x533b
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rbx)
sete %bpl
orl $-0x2, %ebp
leaq 0x30(%rsp), %rdi
callq 0x350c
leaq 0x18(%rsp), %rdi
callq 0x350c
movl %ebp, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5397
jmp 0x5397
jmp 0x5397
jmp 0x5397
jmp 0x5397
jmp 0x5397
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x350c
leaq 0x18(%rsp), %rdi
callq 0x350c
movq %rbx, %rdi
callq 0x2300