// File: /workspace/github_c_makefile_star2_O3/icarroll[P]clunk/mcts.c
// Function: mcts_step
double mcts_step(struct mctreenode * node) {
    curdepth += 1; if (curdepth > deepest) deepest = curdepth;

    node->visits += 1;
    visited += 1;   // count all nodes visited

    if (node->boardstate.captless > 10) {
        // terminal node, game over
        return (double) score_game(& node->boardstate);
    }

    double guess;
    if (node->unexplored_moves) {
        // some children remain unvisited, visit an unvisited child
        int ix = node->allmoves.used - node->unexplored_moves;
        node->unexplored_moves -= 1;

        struct mctreenode * child = & node->children[ix];

        struct thudboard board = node->boardstate;
        struct move move = node->allmoves.moves[ix];
        domove(& board, & move);

        initmctree(child, & board);

        guess = mcts_simulate(child);
        child->scoreguess = guess;
    }
    else {
        // all children visited at least once, pick most promising via UCB1
        int choice = 0;
        double value = ucb1(node->visits, & node->children[choice]);
        for (int ix=0 ; ix<node->allmoves.used ; ix+=1) {
            double tryvalue = ucb1(node->visits, & node->children[ix]);
            if (tryvalue > value) {
                choice = ix;
                value = tryvalue;
            }
        }

        guess = mcts_step(& node->children[choice]);
    }

    node->scoreguess = guess / node->visits
                       + node->scoreguess * (node->visits-1) / node->visits;

    curdepth -= 1;

    return guess;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
movl 0x4353(%rip), %ecx      # 0x8288
leal 0x1(%rcx), %eax
movl %eax, 0x434a(%rip)      # 0x8288
cmpl 0x4348(%rip), %ecx      # 0x828c
jl 0x3f4c
movl %eax, 0x4340(%rip)      # 0x828c
movsd 0xa8(%rbx), %xmm0
addsd 0x12e4(%rip), %xmm0     # 0x5240
movsd %xmm0, 0xa8(%rbx)
incl 0x4326(%rip)            # 0x8290
cmpl $0xb, 0x78(%rbx)
jl 0x3f84
movq %rbx, %rdi
callq 0x346b
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x4193
movslq 0x90(%rbx), %rax
testq %rax, %rax
je 0x4019
movslq 0x80(%rbx), %rdx
subq %rax, %rdx
decl %eax
movl %eax, 0x90(%rbx)
movq 0x98(%rbx), %r12
imulq $0xb0, %rdx, %r13
leaq (%r12,%r13), %r14
leaq 0x78(%rsp), %r15
movl $0x10, %ecx
movq %r15, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
imulq $0x58, %rdx, %rsi
addq 0x88(%rbx), %rsi
leaq 0x20(%rsp), %rax
movl $0x16, %ecx
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %rax, %rsi
callq 0x20ec
movq %r14, %rdi
movq %r15, %rsi
callq 0x423c
movq %r14, %rdi
callq 0x3457
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xa0(%r12,%r13)
jmp 0x4159
movq 0x98(%rbx), %r14
movsd 0xa0(%r14), %xmm1
movsd %xmm1, 0x8(%rsp)
movb (%r14), %bpl
callq 0x10d0
addsd %xmm0, %xmm0
divsd 0xa8(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x4054
sqrtsd %xmm0, %xmm0
jmp 0x4059
callq 0x11b0
cmpl $0x0, 0x80(%rbx)
jle 0x4144
movsd 0x11da(%rip), %xmm1     # 0x5248
testb %bpl, %bpl
jne 0x407b
movsd 0x11d5(%rip), %xmm1     # 0x5250
movsd 0x8(%rsp), %xmm2
mulsd %xmm1, %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
xorl %r14d, %r14d
movl $0xa8, %r15d
xorl %ebp, %ebp
movsd %xmm1, 0x8(%rsp)
movq 0x98(%rbx), %r12
cmpb $0x0, -0xa8(%r12,%r15)
movsd 0x1192(%rip), %xmm0     # 0x5248
jne 0x40c0
movsd 0x1190(%rip), %xmm0     # 0x5250
movsd %xmm0, 0x10(%rsp)
movsd 0xa8(%rbx), %xmm0
movsd -0x8(%r12,%r15), %xmm1
movsd %xmm1, 0x18(%rsp)
callq 0x10d0
addsd %xmm0, %xmm0
divsd (%r12,%r15), %xmm0
ucomisd 0x116e(%rip), %xmm0     # 0x5260
jb 0x40fa
sqrtsd %xmm0, %xmm0
jmp 0x40ff
callq 0x11b0
movsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
mulsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
cmoval %r14d, %ebp
maxsd %xmm1, %xmm2
incq %r14
movslq 0x80(%rbx), %rax
addq $0xb0, %r15
movapd %xmm2, %xmm1
cmpq %rax, %r14
jl 0x4098
movslq %ebp, %rax
jmp 0x4146
xorl %eax, %eax
imulq $0xb0, %rax, %rdi
addq 0x98(%rbx), %rdi
callq 0x3f1b
movsd 0xa8(%rbx), %xmm1
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
movsd 0x10e7(%rip), %xmm3     # 0x5258
addsd %xmm1, %xmm3
mulsd 0xa0(%rbx), %xmm3
divsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, 0xa0(%rbx)
decl 0x40f5(%rip)            # 0x8288
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq