// File: /workspace/github_c_makefile_star2_O3/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 %rsi, 0x18(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x40(%rdi)
je 0x469de
movss 0x3c(%rbx), %xmm0
jmp 0x46c7e
movq %rdx, %r15
movss 0x13a3b(%rip), %xmm2    # 0x5a424
xorl %r13d, %r13d
movl $0xbf800000, %r12d      # imm = 0xBF800000
xorl %ebp, %ebp
movq 0x10(%rbx), %rax
movq 0x28(%rbx), %rcx
xorps %xmm3, %xmm3
cvtss2sd (%rcx,%r13,4), %xmm3
movss (%rax,%r13,4), %xmm1
mulss 0xc(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0x38(%rbx), %xmm0
ucomisd 0x12e5f(%rip), %xmm0    # 0x59880
jb 0x46a29
sqrtsd %xmm0, %xmm0
jmp 0x46a52
movss %xmm2, 0x14(%rsp)
movsd %xmm3, 0x20(%rsp)
movss %xmm1, 0x10(%rsp)
callq 0x3460
movss 0x10(%rsp), %xmm1
movsd 0x20(%rsp), %xmm3
movss 0x14(%rsp), %xmm2
cvtss2sd %xmm1, %xmm1
mulsd %xmm0, %xmm1
movq 0x18(%rbx), %rax
movq 0x30(%rbx), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r13,4), %xmm0
addsd 0xe6dd(%rip), %xmm0     # 0x55150
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%rcx,%r13,4)
ucomiss %xmm2, %xmm0
movl %r13d, %r14d
ja 0x46a93
movl %ebp, %r14d
maxss %xmm2, %xmm0
incq %r13
movl %r14d, %ebp
movaps %xmm0, %xmm2
cmpq $0x16a, %r13            # imm = 0x16A
jne 0x469f4
movslq %r14d, %r13
incl (%rax,%r13,4)
incl 0x38(%rbx)
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
jne 0x46c3c
cmpl $0x168, %r14d           # imm = 0x168
jg 0x46b43
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 %r14d, %r8d
subl %eax, %r8d
movq %r15, %rsi
movl $0x1, %edx
callq 0x46c94
testl %eax, %eax
jne 0x46b43
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)
xorl %r13d, %r13d
cmpl $0x0, 0x40(%rbx)
movl $0x0, %ebp
movss 0x138ec(%rip), %xmm2    # 0x5a424
je 0x469f4
jmp 0x469d4
movq (%rbx), %r12
movl $0x43b, %edi            # imm = 0x43B
movl $0x4, %esi
callq 0x3250
movq %rax, %r15
movl $0x43b, %edi            # imm = 0x43B
movq %r12, %rsi
movl $0x1, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0x29fe0
cmpl $0x168, %r14d           # imm = 0x168
jg 0x46ba8
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 %r14d, %ecx
subl %eax, %ecx
movq %r15, %rdi
movl $0x1, %esi
callq 0x46164
xorl %eax, %eax
movss 0xe456(%rip), %xmm0     # 0x55008
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 0x46bb2
movq %r15, %rdi
movq 0x18(%rsp), %rdx
callq 0x46806
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rax
movss 0x3c(%rax), %xmm0
xorps 0xe473(%rip), %xmm0     # 0x55090
cmpl $0x169, %r14d           # imm = 0x169
jne 0x46c4f
movl $0x1, 0x44(%rax)
cmpl $0x0, 0x44(%rbx)
je 0x46c4f
movl $0x1, 0x40(%rax)
jmp 0x46c4f
movq (%rbx), %rdx
movq 0x18(%rsp), %rsi
movss 0xc(%rsp), %xmm0
callq 0x469b2
movq 0x20(%rbx), %rax
movss (%rax,%r13,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%rax,%r13,4)
movq 0x18(%rbx), %rax
xorps %xmm2, %xmm2
cvtsi2ssl (%rax,%r13,4), %xmm2
movq 0x28(%rbx), %rax
divss %xmm2, %xmm1
movss %xmm1, (%rax,%r13,4)
xorps 0xe40b(%rip), %xmm0     # 0x55090
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq