// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0xa800(%rdi), %rdi
callq 0x5036
movq 0xa800(%rbx), %rdi
cmpl $0x0, 0x1910(%rdi)
je 0x4da4
movl $0x1, 0xa830(%rbx)
movl 0x190c(%rdi), %eax
movl %eax, 0xa834(%rbx)
movl $0x200, 0xa838(%rbx)    # imm = 0x200
movb $-0x1, 0xa002(%rbx)
callq 0x477e
movl %eax, %r14d
movq 0xa800(%rbx), %rdi
callq 0x46cc
addl %r14d, %eax
movl %eax, 0xa83c(%rbx)
movl 0xa840(%rbx), %eax
xorl %r14d, %r14d
pushq $0x4
popq %rdx
incl %r14d
movl %edx, %esi
movl %r14d, %ecx
shll %cl, %esi
cmpl %esi, %eax
jg 0x4d51
pushq $0x20
popq %rdi
callq 0x21d0
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movl %r14d, %esi
callq 0x806c
movq 0xa800(%rbx), %rax
movl 0x1914(%rax), %ecx
testl %ecx, %ecx
movl %ebp, 0x1c(%rsp)
jle 0x4db6
decl %ecx
movl (%rax,%rcx,4), %eax
leaq 0x50(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x813f
jmp 0x4dbe
xorl %ebp, %ebp
cmpl $0x34, 0x190c(%rdi)
sete %bpl
jmp 0x4fe8
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x50(%rsp), %r15
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x841e
movq %rbx, 0x8(%rsp)
movq 0xa800(%rbx), %rax
movl 0x1914(%rax), %ebp
addl $-0x2, %ebp
leaq 0x48(%rsp), %r14
leaq 0x40(%rsp), %r12
leaq 0x38(%rsp), %r13
testl %ebp, %ebp
js 0x4e3e
movq 0x8(%rsp), %rax
movq 0xa800(%rax), %rax
movl %ebp, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x38(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x813f
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x35ee
movq %r14, %rdi
callq 0x350c
movq 0x50(%rsp), %rsi
movq %r15, %rdi
callq 0x3588
decl %ebp
jmp 0x4df4
movq 0x8(%rsp), %rcx
movslq 0xa83c(%rcx), %rax
imulq $0x50, %rax, %rdi
addq %rcx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x891e
movl 0x1c(%rsp), %ebp
movslq %ebp, %rbx
leaq (,%rbx,8), %r15
movq %r15, %rdi
addq $0x8, %rdi
pushq $-0x1
popq %rax
cmovbq %rax, %rdi
testl %ebx, %ebx
cmovsq %rax, %rdi
callq 0x2030
movq %rbx, (%rax)
movq %rax, 0x60(%rsp)
leaq 0x8(%rax), %r14
testl %ebx, %ebx
je 0x4ea0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20f0
xorl %ebx, %ebx
testl %ebp, %ebp
cmovgl %ebp, %ebx
leaq 0x38(%rsp), %r13
leaq 0x40(%rsp), %rbp
movq %r14, %r12
movq %rbx, %r15
subq $0x1, %r15
jb 0x4f22
leaq -0x14de(%rip), %rax     # 0x39e6
movq %rax, 0x40(%rsp)
andq $0x0, 0x48(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x68(%rsp), %rcx
callq 0x815e
movq %r12, %rdi
movq %r13, %rsi
callq 0x81e4
movq %r13, %rdi
callq 0x81fe
movq $0x17, 0x40(%rsp)
movq %rbp, %rdi
callq 0x7ee5
addq $0x8, %r12
jmp 0x4eb7
movq 0x8(%rsp), %r15
movq 0x60(%rsp), %r12
subq $0x1, %rbx
jb 0x4f40
movq %r14, %rdi
callq 0x22d0
addq $0x8, %r14
jmp 0x4f2c
movq (%r12), %rax
leaq (,%rax,8), %r14
testq %rax, %rax
je 0x4f63
movq %r14, %rbx
leaq (%r12,%rbx), %rdi
callq 0x81fe
addq $-0x8, %rbx
jne 0x4f54
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x2330
movq 0xa800(%r15), %rdi
callq 0x3e72
leaq 0xa002(%r15), %rbx
cmpb $-0x1, (%rbx)
je 0x4f9f
movq 0xa800(%r15), %rdi
movl -0x2(%rbx), %esi
callq 0x3e7e
addq $0x4, %rbx
jmp 0x4f85
movq 0x10(%rsp), %rbx
movl 0x8(%rbx), %eax
xorl %ecx, %ecx
cmpl $0x34, 0xa834(%r15)
sete %cl
leal -0x2(%rcx), %ebp
cmpl 0xa840(%r15), %eax
cmovll %ecx, %ebp
movq %rbx, %rdi
callq 0x820c
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x21e0
leaq 0x58(%rsp), %rdi
callq 0x350c
leaq 0x28(%rsp), %rdi
callq 0x350c
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5017
movq %rax, %rbx
pushq $0x20
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x21e0
jmp 0x502e
jmp 0x5017
jmp 0x5017
jmp 0x5017
jmp 0x5017
jmp 0x5017
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x350c
leaq 0x28(%rsp), %rdi
callq 0x350c
movq %rbx, %rdi
callq 0x2300