// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/go.c
// Function: select_mcts
float select_mcts(mcts_tree *root, network *net, float *prev, float cpuct)
{
    if(root->done) return -root->result;
    int i;
    float max = -1000;
    int max_i = 0;
    for(i = 0; i < 19*19+1; ++i){
        root->prob[i] = root->mean[i] + cpuct*root->prior[i] * sqrt(root->total_count) / (1. + root->visit_count[i]);
        if(root->prob[i] > max){
            max = root->prob[i];
            max_i = i;
        }
    }
    float val;
    i = max_i;
    root->visit_count[i]++;
    root->total_count++;
    if (root->children[i]) {
        val = select_mcts(root->children[i], net, root->board, cpuct);
    } else {
        if(max_i < 19*19 && !legal_go(root->board, prev, 1, max_i/19, max_i%19)) {
            root->mean[i]  = -1;
            root->value[i] = -1;
            root->prior[i] = 0;
            --root->total_count;
            return select_mcts(root, net, prev, cpuct);
            //printf("Detected ko\n");
            //getchar();
        } else {
            float *next = copy_board(root->board);
            if (max_i < 19*19) {
                move_go(next, 1, max_i / 19, max_i % 19);
            }
            flip_board(next);
            root->children[i] = expand(next, root->board, net);
            val = -root->children[i]->result;
            if(max_i == 19*19){
                root->children[i]->pass = 1;
                if (root->pass){
                    root->children[i]->done = 1;
                }
            }
        }
    }
    root->value[i] += val;
    root->mean[i] = root->value[i]/root->visit_count[i];
    return -val;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movss %xmm0, 0xc(%rsp)
movq %rdx, %r15
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
pushq $0x1
popq %r12
movl $0xbf800000, %r13d      # imm = 0xBF800000
cmpl $0x0, 0x40(%rbx)
jne 0x3ae1f
xorl %r14d, %r14d
movss 0x16692(%rip), %xmm3    # 0x51324
xorl %ebp, %ebp
cmpq $0x16a, %r14            # imm = 0x16A
je 0x3ad3f
movq 0x10(%rbx), %rax
movq 0x28(%rbx), %rcx
cvtss2sd (%rcx,%r14,4), %xmm4
movss (%rax,%r14,4), %xmm1
mulss 0xc(%rsp), %xmm1
cvtsi2sdl 0x38(%rbx), %xmm0
ucomisd 0x15b08(%rip), %xmm0    # 0x507d0
jb 0x3acd0
sqrtsd %xmm0, %xmm0
jmp 0x3acf9
movss %xmm3, 0x14(%rsp)
movsd %xmm4, 0x20(%rsp)
movss %xmm1, 0x10(%rsp)
callq 0x3460
movss 0x10(%rsp), %xmm1
movsd 0x20(%rsp), %xmm4
movss 0x14(%rsp), %xmm3
cvtss2sd %xmm1, %xmm1
movq 0x18(%rbx), %rax
cvtsi2sdl (%rax,%r14,4), %xmm2
mulsd %xmm0, %xmm1
addsd 0x11415(%rip), %xmm2    # 0x4c128
divsd %xmm2, %xmm1
addsd %xmm4, %xmm1
cvtsd2ss %xmm1, %xmm0
ucomiss %xmm3, %xmm0
movq 0x30(%rbx), %rax
cmoval %r14d, %ebp
movss %xmm0, (%rax,%r14,4)
maxss %xmm3, %xmm0
incq %r14
movaps %xmm0, %xmm3
jmp 0x3ac94
movq 0x18(%rbx), %rax
movslq %ebp, %r14
incl (%rax,%r14,4)
incl 0x38(%rbx)
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
jne 0x3ae26
cmpl $0x168, %ebp            # imm = 0x168
jg 0x3ada6
movq (%rbx), %rdi
movl %ebp, %eax
cltd
pushq $0x13
popq %rcx
idivl %ecx
movl %edx, %r8d
movq %r15, %rsi
movl %r12d, %edx
movl %eax, %ecx
callq 0x3ae7b
testl %eax, %eax
jne 0x3ada6
movq 0x28(%rbx), %rax
movl %r13d, (%rax,%r14,4)
movq 0x20(%rbx), %rax
movl %r13d, (%rax,%r14,4)
movq 0x10(%rbx), %rax
andl $0x0, (%rax,%r14,4)
decl 0x38(%rbx)
jmp 0x3ac7d
movq (%rbx), %rdi
callq 0x3ac1c
movq %rax, %r15
cmpl $0x168, %ebp            # imm = 0x168
jg 0x3add0
movl %ebp, %eax
cltd
pushq $0x13
popq %rcx
idivl %ecx
movl %edx, %ecx
pushq $0x1
popq %rsi
movq %r15, %rdi
movl %eax, %edx
callq 0x3a4b9
movq %r15, %rdi
callq 0x3a26f
movq %r15, %rdi
movq 0x18(%rsp), %rdx
callq 0x3aac9
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %rax
movss 0x3c(%rax), %xmm0
xorps 0x1134f(%rip), %xmm0    # 0x4c150
cmpl $0x169, %ebp            # imm = 0x169
jne 0x3ae39
movl $0x1, 0x44(%rax)
cmpl $0x0, 0x44(%rbx)
je 0x3ae39
movl $0x1, 0x40(%rax)
jmp 0x3ae39
movss 0x3c(%rbx), %xmm0
jmp 0x3ae65
movq (%rbx), %rdx
movq 0x18(%rsp), %rsi
movss 0xc(%rsp), %xmm0
callq 0x3ac54
movq 0x20(%rbx), %rax
movss (%rax,%r14,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%rax,%r14,4)
movq 0x18(%rbx), %rax
cvtsi2ssl (%rax,%r14,4), %xmm2
movq 0x28(%rbx), %rax
divss %xmm2, %xmm1
movss %xmm1, (%rax,%r14,4)
xorps 0x112e4(%rip), %xmm0    # 0x4c150
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq