// File: /workspace/github_cpp_makefile_star2_O3/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 $0x868, %rsp            # imm = 0x868
movl %edx, 0x2c(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
callq 0x54ec
cmpl $0x0, 0x1910(%rbx)
je 0x55cc
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x60, %rdi
callq 0x3704
addq $0x4, %r14
cmpq $0x800, %r14            # imm = 0x800
jne 0x554b
movb $-0x1, 0x62(%rsp)
cmpl $0x0, 0x1914(%rbx)
leaq 0x48(%rsp), %r14
movl %ebp, (%rsp)
movq %r14, 0x58(%rsp)
jle 0x55de
leaq 0x40(%rsp), %r15
movq $0x0, (%r15)
leaq 0x10(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x9a6a
movq (%r15), %rcx
movq 0x8(%r15), %rax
movq %rcx, (%r12)
leaq 0x18(%rsp), %r12
movq %rax, (%r12)
testq %rax, %rax
je 0x55f7
movq 0x8a0e(%rip), %rcx      # 0xdfd0
cmpb $0x0, (%rcx)
je 0x55f3
incl 0x8(%rax)
jmp 0x55f7
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rbx)
sete %bpl
jmp 0x58fe
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0x18(%rsp), %r12
movaps %xmm0, -0x8(%r12)
jmp 0x55f7
lock
incl 0x8(%rax)
cmpl $0x2, 0x1914(%rbx)
jl 0x5692
leaq 0x4(%rbx), %rbp
movl $0x1, %r14d
leaq 0x30(%rsp), %r15
leaq 0xf(%rsp), %r13
movq $0x0, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x9a6a
movq 0x10(%rsp), %rax
movaps 0x30(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x30(%rsp)
movq 0x8(%rax), %rdi
movups %xmm0, (%rax)
testq %rdi, %rdi
je 0x5657
callq 0x3748
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x5666
callq 0x3748
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
addq $0x8, %rsi
movq %r12, %rdi
callq 0x37b8
incq %r14
movslq 0x1914(%rbx), %rax
addq $0x4, %rbp
cmpq %rax, %r14
jl 0x5618
leaq 0x18f4(%rbx), %rbp
movl $0x200, 0x4(%rsp)       # imm = 0x200
movq $0x0, 0x20(%rsp)
movl $0x0, 0x8(%rsp)
movl (%rsp), %r13d
cmpl $0x34, 0x8(%rsp)
jl 0x56cc
movq 0x20(%rsp), %rax
cmpl 0x2c(%rsp), %eax
jge 0x589e
testl %r13d, %r13d
jle 0x589e
decl %r13d
movl 0x190c(%rbx), %r14d
movl 0x1914(%rbx), %r15d
cmpl $0x33, %r14d
jg 0x57bf
movl 0x1910(%rbx), %eax
testl %eax, %eax
jle 0x57bf
cmpl $0x1f3, %r15d           # imm = 0x1F3
jg 0x57bf
cmpl $0xa, 0x1908(%rbx)
jg 0x57bf
xorl %r14d, %r14d
cmpb $0x0, 0x1874(%rbx,%r14,4)
je 0x5730
incq %r14
cmpq %r14, %rax
jne 0x571b
jmp 0x5791
testq %r14, %r14
je 0x5791
movq %rbp, %rdi
callq 0x9bee
movl %eax, %r15d
movq %rbp, %rdi
callq 0x9bee
movl %eax, %ecx
andl $0x1, %ecx
movl 0x1910(%rbx), %eax
sarl $0x3, %eax
addl %r14d, %ecx
addl %eax, %ecx
movl %r15d, %eax
cltd
idivl %ecx
cmpl %r14d, %edx
jl 0x577d
movq %rbp, %rdi
callq 0x9bee
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 0x4232
jmp 0x57b2
movq %rbp, %rdi
callq 0x9bee
cltd
idivl 0x1910(%rbx)
movslq %edx, %rax
movl 0x1874(%rbx,%rax,4), %esi
movq %rbx, %rdi
callq 0x4232
movq %rbx, %rdi
callq 0x43e4
jmp 0x56d8
cmpl $0x34, %r14d
setne %al
cmpl 0x8(%rsp), %r14d
setle %cl
testb %cl, %al
jne 0x583c
movl %r13d, (%rsp)
xorl %r13d, %r13d
cmpl $0x34, %r14d
sete %r13b
movq %rbx, %rdi
callq 0x4a7e
movq 0x20(%rsp), %rcx
addl %r13d, %ecx
movq %rcx, 0x20(%rsp)
cmpl $0x34, %r14d
jl 0x5802
cmpl 0x4(%rsp), %eax
jge 0x5838
cmpl $0x34, %r14d
movl 0x4(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x4(%rsp)
testl %r15d, %r15d
jle 0x582b
leaq (,%r15,4), %rdx
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x2180
movslq %r15d, %rax
movb $-0x1, 0x62(%rsp,%rax,4)
movl %r14d, 0x8(%rsp)
movl (%rsp), %r13d
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq 0x58(%rsp), %rsi
callq 0x37b8
movl 0x1904(%rbx), %esi
movq %rbx, %rdi
callq 0x5958
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x5891
movl 0x10(%rax), %esi
movq %rbx, %rdi
callq 0x4232
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq %rax, 0x10(%rsp)
addq $0x8, %rsi
movq %r12, %rdi
callq 0x37b8
jmp 0x5861
movq %rbx, %rdi
callq 0x43e4
jmp 0x56b6
movl 0x1904(%rbx), %esi
movq %rbx, %rdi
callq 0x5958
cmpb $-0x1, 0x62(%rsp)
leaq 0x60(%rsp), %r14
je 0x58ce
movl (%r14), %esi
movq %rbx, %rdi
callq 0x4232
cmpb $-0x1, 0x6(%r14)
leaq 0x4(%r14), %r14
jne 0x58b8
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rbx)
sete %bl
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x58e9
callq 0x3748
movb %bl, %bpl
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x58fb
callq 0x3748
orl $-0x2, %ebp
movl %ebp, %eax
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x591e
jmp 0x591e
jmp 0x591e
jmp 0x591e
jmp 0x591e
jmp 0x591e
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5930
callq 0x3748
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x593f
callq 0x3748
movq %rbx, %rdi
callq 0x22e0
nop