// File: fredericmarchand[P]TexasHoldem/./Search.h
// Function: UCTSearch(NodeState*, int)
static Move UCTSearch(NodeState *startState, int maxIter)
{
	Node *root = new Node(startState);
	for (int i = 0; i < maxIter; ++i)
	{
		Node *node = root;
		NodeState *state = root->getState();
		state->randomize();

		//Select
#if DEBUG == 1
		cout << "Select" << endl;
#endif
		while (node->getUntriedMoves()->empty() && !node->getChildren()->empty()) //node is fully expanded and non-terminal
		{
			node = node->getUCB();
			state->doMove(node->getMove());
		}

		//Expand
#if DEBUG == 1
		cout << "Expand" << endl;
#endif
		if (!node->getUntriedMoves()->empty()) //if we can expand (i.e., state/node is non-terminal)
		{
			srand(time(NULL));
        	Move move = node->getUntriedMoves()->at(rand() % (node->getUntriedMoves()->size()));
        	state->doMove(move);
        	node = node->addChild(move, state); //Add child and descend tree
		}
		
		//Rollout
#if DEBUG == 1
		cout << "Rollout" << endl;
#endif
		while (!state->getMoves()->empty()) //While state is non-terminal
		{
			srand(time(NULL));
			state->doMove(state->getMoves()->at(rand() % (state->getMoves()->size())));
		}

		//Backpropagate
#if DEBUG == 1
		cout << "Backpropagate" << endl;
#endif
		while (node != NULL)
		{
			node->update(state->getResult(node->playerJustMoved()));
			node = node->getParent();
		}
	}

	Move moveToDo;
	int highest = 0;
	for (int i = 0; i < root->getChildren()->size(); ++i)
	{
		Node *child = root->getChildren()->at(i);
		if (child->getVisits() > highest)
		{
			highest = child->getVisits();
			moveToDo = child->getMove();
		}
	}

	delete root;
	
	return moveToDo; //Return the move that was the most visited
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x50, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rsi
callq 0x6bd0
jmp 0x5fa1
movq -0x58(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x61e4
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6f60
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x97a0
movq -0x30(%rbp), %rdi
callq 0x6fb0
movq %rax, %rdi
callq 0x67a0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x59(%rbp)
jne 0x5ff9
jmp 0x600f
movq -0x30(%rbp), %rdi
callq 0x6fd0
movq %rax, %rdi
callq 0x67e0
xorb $-0x1, %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x6018
jmp 0x6062
movq -0x30(%rbp), %rdi
callq 0x6dd0
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6f40
movq -0x68(%rbp), %rdi
movl %eax, %esi
callq 0x9520
jmp 0x5fda
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x50, %esi
callq 0x21a0
jmp 0x62a6
movq -0x30(%rbp), %rdi
callq 0x6fb0
movq %rax, %rdi
callq 0x67a0
testb $0x1, %al
jne 0x60ed
xorl %eax, %eax
movl %eax, %edi
callq 0x2160
movl %eax, %edi
callq 0x2170
movq -0x30(%rbp), %rdi
callq 0x6fb0
movq %rax, -0x78(%rbp)
callq 0x2050
cltq
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6fb0
movq %rax, %rdi
callq 0x6860
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movq -0x70(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rsi
callq 0x6820
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x9520
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x38(%rbp), %rdx
callq 0x7020
movq %rax, -0x30(%rbp)
jmp 0x60ef
movq -0x38(%rbp), %rdi
callq 0x9500
movq %rax, %rdi
callq 0x67a0
xorb $-0x1, %al
testb $0x1, %al
jne 0x6108
jmp 0x617d
xorl %eax, %eax
movl %eax, %edi
callq 0x2160
movl %eax, %edi
callq 0x2170
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rdi
callq 0x9500
movq %rax, -0x90(%rbp)
callq 0x2050
cltq
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rdi
callq 0x9500
movq %rax, %rdi
callq 0x6860
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rsi
callq 0x6820
movq -0x80(%rbp), %rdi
movl (%rax), %esi
callq 0x9520
jmp 0x60ef
jmp 0x617f
cmpq $0x0, -0x30(%rbp)
je 0x61d4
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6fa0
movq -0xa0(%rbp), %rdi
movzbl %al, %esi
andl $0x1, %esi
callq 0x9620
movq -0x98(%rbp), %rdi
movl %eax, %esi
callq 0x6ff0
movq -0x30(%rbp), %rdi
callq 0x6f80
movq %rax, -0x30(%rbp)
jmp 0x617f
jmp 0x61d6
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5fb0
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movslq -0x48(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6fd0
movq %rax, %rdi
callq 0x6880
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
jae 0x626c
movq -0x18(%rbp), %rdi
callq 0x6fd0
movq %rax, %rdi
movslq -0x48(%rbp), %rsi
callq 0x68a0
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x6f20
cmpl -0x44(%rbp), %eax
jle 0x625f
movq -0x50(%rbp), %rdi
callq 0x6f20
movl %eax, -0x44(%rbp)
movq -0x50(%rbp), %rdi
callq 0x6f40
movl %eax, -0x40(%rbp)
jmp 0x6261
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x61f2
movq -0x18(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x629a
movq -0xb0(%rbp), %rdi
callq 0x6d20
movq -0xb0(%rbp), %rdi
movl $0x50, %esi
callq 0x21a0
movl -0x40(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x22a0
nop