// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movss %xmm0, -0x24(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x40(%rax)
je 0x5a423
movq -0x10(%rbp), %rax
movss 0x3c(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x4(%rbp)
jmp 0x5a78d
movss 0x1b7a5(%rip), %xmm0    # 0x75bd0
movss %xmm0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x16a, -0x28(%rbp)     # imm = 0x16A
jge 0x5a520
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x28(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movss -0x24(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x10(%rbp), %rax
cvtsi2sdl 0x38(%rax), %xmm0
callq 0x3430
movsd -0x50(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x48(%rbp), %xmm0
mulsd %xmm2, %xmm1
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x28(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm3
movsd 0x16ba4(%rip), %xmm2    # 0x71060
addsd %xmm3, %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x28(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x28(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
ucomiss -0x2c(%rbp), %xmm0
jbe 0x5a510
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x28(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x2c(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x5a512
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5a43e
movl -0x30(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x5a58a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movss -0x24(%rbp), %xmm0
callq 0x5a3e0
movss %xmm0, -0x34(%rbp)
jmp 0x5a724
cmpl $0x169, -0x30(%rbp)     # imm = 0x169
jge 0x5a641
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x30(%rbp), %eax
movl $0x13, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x30(%rbp), %eax
movl $0x13, %r8d
cltd
idivl %r8d
movl %edx, %r8d
movl $0x1, %edx
callq 0x5a7a0
cmpl $0x0, %eax
jne 0x5a641
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x28(%rbp), %rcx
movss 0x19afa(%rip), %xmm0    # 0x740dc
movss %xmm0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movss 0x19ae1(%rip), %xmm0    # 0x740dc
movss %xmm0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movss -0x24(%rbp), %xmm0
callq 0x5a3e0
movss %xmm0, -0x4(%rbp)
jmp 0x5a78d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x5a390
movq %rax, -0x40(%rbp)
cmpl $0x169, -0x30(%rbp)     # imm = 0x169
jge 0x5a686
movq -0x40(%rbp), %rdi
movl -0x30(%rbp), %eax
movl $0x13, %ecx
cltd
idivl %ecx
movl %eax, -0x54(%rbp)
movl -0x30(%rbp), %eax
movl $0x13, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl -0x54(%rbp), %edx
movl $0x1, %esi
callq 0x598a0
movq -0x40(%rbp), %rdi
callq 0x59560
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rdx
callq 0x5a180
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movss 0x3c(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x34(%rbp)
cmpl $0x169, -0x30(%rbp)     # imm = 0x169
jne 0x5a720
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x1, 0x44(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x44(%rax)
je 0x5a71e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x1, 0x40(%rax)
jmp 0x5a720
jmp 0x5a722
jmp 0x5a724
movss -0x34(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x28(%rbp), %rcx
cvtsi2ssl (%rax,%rcx,4), %xmm1
divss %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x28(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movss -0x34(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x4(%rbp)
movss -0x4(%rbp), %xmm0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)