// File: jeffreyan11[P]go-engine/mctree.cpp
// Function: MCTree::findLeaf(int&, Board&, int&)
MCNode *MCTree::findLeaf(Player &p, Board &b, int &depth) {
    MCNode *node = root;

    // Keep going until we either decide to split another child, or find a leaf
    while (node->size > 0) {
        std::uniform_int_distribution<int> distribution(0, b.getLegalMoves(p).size() / 2);
        // Create a new child for this node with some probability
        if (node != root && distribution(mc_rng) > node->size)
            break;

        // Otherwise, choose a child to follow
        double bestScore = 0.0;
        int bestIndex = 0;
        double logD = std::log(node->denominator);
        for (int i = 0; i < node->size; i++) {
            double score = (double) node->children[i]->numerator / (double) node->children[i]->denominator
                + std::sqrt(logD / (double) node->children[i]->denominator);
            if (score > bestScore) {
                bestScore = score;
                bestIndex = i;
            }
        }

        node = node->children[bestIndex];
        b.doMove(p, node->m);
        p = otherPlayer(p);
        depth++;
    }

    return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x478(%rbp)
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jle 0x971d
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq -0x434(%rbp), %rdi
movq %rdi, -0x488(%rbp)
callq 0x5a50
movq -0x488(%rbp), %rdi
callq 0x92b0
movl %eax, -0x47c(%rbp)
jmp 0x954f
movl -0x47c(%rbp), %edx
shrl %edx
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x9810
jmp 0x9564
leaq -0x434(%rbp), %rdi
callq 0x8f30
movq -0x478(%rbp), %rcx
movq -0x28(%rbp), %rax
cmpq (%rcx), %rax
je 0x95c4
leaq -0x30(%rbp), %rdi
leaq 0xd3b5(%rip), %rsi      # 0x16940
callq 0x9840
movq -0x28(%rbp), %rcx
movswl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jle 0x95c4
jmp 0x971d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x440(%rbp)
movl %eax, -0x444(%rbp)
leaq -0x434(%rbp), %rdi
callq 0x8f30
jmp 0x972a
xorps %xmm0, %xmm0
movsd %xmm0, -0x450(%rbp)
movl $0x0, -0x454(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x9870
movsd %xmm0, -0x460(%rbp)
movl $0x0, -0x464(%rbp)
movl -0x464(%rbp), %eax
movq -0x28(%rbp), %rcx
movswl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jge 0x96cc
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x464(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cvtsi2sdl (%rax), %xmm0
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x464(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x490(%rbp)
movsd -0x460(%rbp), %xmm0
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x464(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x2180
movaps %xmm0, %xmm1
movsd -0x490(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x470(%rbp)
movsd -0x470(%rbp), %xmm0
ucomisd -0x450(%rbp), %xmm0
jbe 0x96b6
movsd -0x470(%rbp), %xmm0
movsd %xmm0, -0x450(%rbp)
movl -0x464(%rbp), %eax
movl %eax, -0x454(%rbp)
jmp 0x96b8
movl -0x464(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x464(%rbp)
jmp 0x95f7
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x454(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
movq -0x28(%rbp), %rax
movzwl 0x10(%rax), %edx
callq 0x3d50
movq -0x10(%rbp), %rax
movl (%rax), %edi
callq 0x8c10
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x950d
movq -0x28(%rbp), %rax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
movq -0x440(%rbp), %rdi
callq 0x2350
nopw %cs:(%rax,%rax)