// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movss %xmm0, 0x4(%rsp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl $0xbf800000, %r12d      # imm = 0xBF800000
cmpl $0x0, 0x40(%rbx)
jne 0x4493f
xorl %r13d, %r13d
xorl %ebp, %ebp
movss 0x13bd2(%rip), %xmm2    # 0x582ec
movq 0x10(%rbx), %rax
movq 0x28(%rbx), %rcx
xorps %xmm3, %xmm3
cvtss2sd (%rcx,%r13,4), %xmm3
movss (%rax,%r13,4), %xmm1
mulss 0x4(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0x38(%rbx), %xmm0
ucomisd 0x10d29(%rip), %xmm0    # 0x55470
jb 0x4474f
sqrtsd %xmm0, %xmm0
jmp 0x44778
movss %xmm2, 0xc(%rsp)
movsd %xmm3, 0x10(%rsp)
movss %xmm1, 0x8(%rsp)
callq 0x3470
movss 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
cvtss2sd %xmm1, %xmm1
mulsd %xmm0, %xmm1
movq 0x18(%rbx), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r13,4), %xmm0
movq 0x30(%rbx), %rax
addsd 0xe98f(%rip), %xmm0     # 0x53128
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
ucomiss %xmm2, %xmm0
movss %xmm0, (%rax,%r13,4)
cmoval %r13d, %ebp
maxss %xmm2, %xmm0
incq %r13
movaps %xmm0, %xmm2
cmpq $0x16a, %r13            # imm = 0x16A
jne 0x4471a
movq 0x18(%rbx), %rax
movslq %ebp, %r13
incl (%rax,%r13,4)
incl 0x38(%rbx)
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
jne 0x44946
cmpl $0x168, %ebp            # imm = 0x168
jg 0x4484b
movq (%rbx), %rdi
imulq $0x6bca1af3, %r13, %rcx # imm = 0x6BCA1AF3
movq %rcx, %rax
shrq $0x3f, %rax
sarq $0x23, %rcx
addl %eax, %ecx
leal (%rcx,%rcx,8), %eax
leal (%rcx,%rax,2), %eax
movl %ebp, %r8d
subl %eax, %r8d
movq %r15, %rsi
movl $0x1, %edx
callq 0x4499c
testl %eax, %eax
jne 0x4484b
movq 0x28(%rbx), %rax
movl %r12d, (%rax,%r13,4)
movq 0x20(%rbx), %rax
movl %r12d, (%rax,%r13,4)
movq 0x10(%rbx), %rax
movl $0x0, (%rax,%r13,4)
decl 0x38(%rbx)
jmp 0x44703
movq (%rbx), %r12
movl $0x43b, %edi            # imm = 0x43B
movl $0x4, %esi
callq 0x3260
movq %rax, %r15
movl $0x43b, %edi            # imm = 0x43B
movq %r12, %rsi
movl $0x1, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0x2879c
cmpl $0x168, %ebp            # imm = 0x168
jg 0x448ae
imulq $0x6bca1af3, %r13, %rdx # imm = 0x6BCA1AF3
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x23, %rdx
addl %eax, %edx
leal (%rdx,%rdx,8), %eax
leal (%rdx,%rax,2), %eax
movl %ebp, %ecx
subl %eax, %ecx
movq %r15, %rdi
movl $0x1, %esi
callq 0x43e98
xorl %eax, %eax
movss 0xe750(%rip), %xmm0     # 0x53008
movss (%r15,%rax,4), %xmm1
movss 0x5a4(%r15,%rax,4), %xmm2
movss %xmm2, (%r15,%rax,4)
movss %xmm1, 0x5a4(%r15,%rax,4)
movaps %xmm0, %xmm1
subss 0xb48(%r15,%rax,4), %xmm1
movss %xmm1, 0xb48(%r15,%rax,4)
incq %rax
cmpq $0x169, %rax            # imm = 0x169
jne 0x448b8
movq %r15, %rdi
movq %r14, %rdx
callq 0x44539
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rax
movss 0x3c(%rax), %xmm0
xorps 0xe82f(%rip), %xmm0     # 0x53150
cmpl $0x169, %ebp            # imm = 0x169
jne 0x44957
movl $0x1, 0x44(%rax)
cmpl $0x0, 0x44(%rbx)
je 0x44957
movl $0x1, 0x40(%rax)
jmp 0x44957
movss 0x3c(%rbx), %xmm0
jmp 0x44986
movq (%rbx), %rdx
movq %r14, %rsi
movss 0x4(%rsp), %xmm0
callq 0x446e0
movq 0x20(%rbx), %rax
movss (%rax,%r13,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%rax,%r13,4)
movq 0x18(%rbx), %rax
movq 0x28(%rbx), %rcx
xorps %xmm2, %xmm2
cvtsi2ssl (%rax,%r13,4), %xmm2
divss %xmm2, %xmm1
movss %xmm1, (%rcx,%r13,4)
xorps 0xe7c3(%rip), %xmm0     # 0x53150
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq