// File: /workspace/github_cpp_makefile_star2_O1/ShootMe[P]Klondike-Solver/Solitaire.cpp
// Function: SolitaireWorker::Run(int)
SolveResult SolitaireWorker::Run(int numThreads) {
	solitaire->MakeAutoMoves();
	if (solitaire->MovesAvailableCount() == 0) { return solitaire->FoundationCount() == 52 ? SolvedMinimal : Impossible; }

	openCount = 1;
	maxFoundationCount = solitaire->FoundationCount();
	bestSolutionMoveCount = 512;
	bestSolution[0].Count = 255;
	startMoves = solitaire->MinimumMovesLeft() + solitaire->MovesMadeNormalizedCount();
	int powerOf2 = 1;
	while (maxClosedCount > (1 << (powerOf2 + 2))) {
		powerOf2++;
	}
	HashMap<int> * closed = new HashMap<int>(powerOf2);

	shared_ptr<MoveNode> firstNode = solitaire->MovesMadeCount() > 0 ? make_shared<MoveNode>(solitaire->GetMoveMade(solitaire->MovesMadeCount() - 1)) : NULL;
	shared_ptr<MoveNode> node = firstNode;
	for (int i = solitaire->MovesMadeCount() - 2; i >= 0; i--) {
		node->Parent = make_shared<MoveNode>(solitaire->GetMoveMade(i));
		node = node->Parent;
	}
	open[startMoves].push(firstNode);

	thread * threads = new thread[numThreads];
	for (int i = 0; i < numThreads; i++) {
		threads[i] = thread(&SolitaireWorker::RunMinimalWorker, this, (void*)closed);
		this_thread::sleep_for(chrono::milliseconds(23));
	}

	for (int i = 0; i < numThreads; i++) {
		threads[i].join();
	}
	delete[] threads;

	//Reset game to best solution found
	solitaire->ResetGame();
	for (int i = 0; bestSolution[i].Count < 255; i++) {
		solitaire->MakeMove(bestSolution[i]);
	}

	SolveResult result = closed->Size() >= maxClosedCount ? (maxFoundationCount == 52 ? SolvedMayNotBeMinimal : CouldNotComplete) : (maxFoundationCount == 52 ? SolvedMinimal : Impossible);
	delete closed;
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movq 0xa800(%rdi), %rdi
callq 0x5658
movq 0xa800(%rbx), %rdi
cmpl $0x0, 0x1910(%rdi)
je 0x5327
movl $0x1, 0xa830(%rbx)
movl 0x190c(%rdi), %eax
movl %eax, 0xa834(%rbx)
movl $0x200, 0xa838(%rbx)    # imm = 0x200
movb $-0x1, 0xa002(%rbx)
callq 0x4cb4
movl %eax, %ebp
movq 0xa800(%rbx), %rdi
callq 0x4bfc
addl %ebp, %eax
movl %eax, 0xa83c(%rbx)
movl 0xa840(%rbx), %eax
xorl %ebp, %ebp
incl %ebp
movl $0x4, %edx
movl %ebp, %ecx
shll %cl, %edx
cmpl %edx, %eax
jg 0x52c3
movl $0x20, %edi
callq 0x21c0
movq %rax, 0x48(%rsp)
movq %rax, %rdi
movl %ebp, %esi
callq 0x9248
movq 0xa800(%rbx), %rax
movslq 0x1914(%rax), %rcx
testq %rcx, %rcx
jle 0x5339
movl -0x4(%rax,%rcx,4), %eax
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x38(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x9aa8
jmp 0x5341
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rdi)
sete %bpl
jmp 0x55f9
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x536f
movq 0x8c6f(%rip), %rcx      # 0xdfd0
cmpb $0x0, (%rcx)
je 0x536b
incl 0x8(%rax)
jmp 0x536f
lock
incl 0x8(%rax)
movq 0xa800(%rbx), %rax
movl 0x1914(%rax), %r15d
cmpl $0x2, %r15d
jl 0x541e
addl $-0x2, %r15d
leaq 0x18(%rsp), %r12
incq %r15
leaq 0x10(%rsp), %r13
leaq 0x58(%rsp), %rbp
leaq 0x50(%rsp), %r14
movq 0xa800(%rbx), %rax
movl -0x4(%rax,%r15,4), %eax
movl %eax, 0x50(%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x9aa8
movq 0x20(%rsp), %rax
movaps 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x8(%rax), %rdi
movups %xmm0, (%rax)
testq %rdi, %rdi
je 0x53ef
callq 0x3794
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x53fe
callq 0x3794
movq 0x20(%rsp), %rsi
movq (%rsi), %rax
movq %rax, 0x20(%rsp)
addq $0x8, %rsi
leaq 0x28(%rsp), %rdi
callq 0x3804
decq %r15
jg 0x53a2
movslq 0xa83c(%rbx), %rax
leaq (%rax,%rax,4), %rdi
shlq $0x4, %rdi
addq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x9808
movslq 0xc(%rsp), %r13
leaq (,%r13,8), %r14
movq %r14, %rdi
addq $0x8, %rdi
movq $-0x1, %rax
cmovbq %rax, %rdi
testl %r13d, %r13d
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r15
movq %r13, (%rax)
movq %rax, %r12
addq $0x8, %r12
testl %r13d, %r13d
je 0x5484
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
movl 0xc(%rsp), %eax
movl %eax, %ebp
testl %eax, %eax
jle 0x5521
xorl %r14d, %r14d
leaq 0x10(%rsp), %r13
leaq -0x185b(%rip), %rax     # 0x3c46
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %rbx, 0x58(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
leaq 0x58(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x92ce
cmpq $0x0, (%r12,%r14,8)
jne 0x560a
movq 0x50(%rsp), %rax
movq %rax, (%r12,%r14,8)
movq $0x0, 0x10(%rsp)
movq $0x15ef3c0, 0x18(%rsp)  # imm = 0x15EF3C0
movq %r13, %rdi
movq %r13, %rsi
callq 0x2080
cmpl $-0x1, %eax
jne 0x5515
callq 0x2040
cmpl $0x4, (%rax)
je 0x54fb
incq %r14
cmpq %rbp, %r14
jne 0x549a
cmpl $0x0, 0xc(%rsp)
jle 0x5539
movq %r12, %rdi
callq 0x22b0
addq $0x8, %r12
decq %rbp
jne 0x5528
movq (%r15), %rax
leaq (,%rax,8), %rsi
testq %rax, %rax
je 0x555d
movq %rsi, %rax
cmpq $0x0, (%r15,%rax)
jne 0x560a
addq $-0x8, %rax
jne 0x554c
addq $0x8, %rsi
movq %r15, %rdi
callq 0x2310
movq 0xa800(%rbx), %rdi
movl 0x1904(%rdi), %esi
callq 0x5aae
cmpb $-0x1, 0xa002(%rbx)
je 0x55a5
leaq 0xa000(%rbx), %r14
movq 0xa800(%rbx), %rdi
movl (%r14), %esi
callq 0x42a4
cmpb $-0x1, 0x6(%r14)
leaq 0x4(%r14), %r14
jne 0x558b
movq 0x48(%rsp), %r14
movl 0x8(%r14), %eax
xorl %ecx, %ecx
cmpl $0x34, 0xa834(%rbx)
sete %cl
leal -0x2(%rcx), %ebp
cmpl 0xa840(%rbx), %eax
cmovll %ecx, %ebp
movq %r14, %rdi
callq 0x9368
movl $0x20, %esi
movq %r14, %rdi
callq 0x21d0
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x55ea
callq 0x3794
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x55f9
callq 0x3794
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070
jmp 0x562f
movq %rax, %rbx
movl $0x20, %esi
movq 0x48(%rsp), %rdi
callq 0x21d0
jmp 0x5650
jmp 0x562f
jmp 0x562f
jmp 0x562f
jmp 0x562f
jmp 0x562f
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x5641
callq 0x3794
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x5650
callq 0x3794
movq %rbx, %rdi
callq 0x22e0