// File: /workspace/github_c_makefile_star2_O1/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 0x42a2(%rip), %ecx      # 0x8288
leal 0x1(%rcx), %eax
movl %eax, 0x4299(%rip)      # 0x8288
cmpl 0x4297(%rip), %ecx      # 0x828c
jl 0x3ffd
movl %eax, 0x428f(%rip)      # 0x828c
movsd 0xa8(%rbx), %xmm0
addsd 0x1233(%rip), %xmm0     # 0x5240
movsd %xmm0, 0xa8(%rbx)
incl 0x4275(%rip)            # 0x8290
cmpl $0xb, 0x78(%rbx)
jl 0x4035
movq %rbx, %rdi
callq 0x3533
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x4244
movslq 0x90(%rbx), %rax
testq %rax, %rax
je 0x40ca
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 0x20ee
movq %r14, %rdi
movq %r15, %rsi
callq 0x42ec
movq %r14, %rdi
callq 0x351f
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xa0(%r12,%r13)
jmp 0x420a
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 0x4105
sqrtsd %xmm0, %xmm0
jmp 0x410a
callq 0x11b0
cmpl $0x0, 0x80(%rbx)
jle 0x41f5
movsd 0x1129(%rip), %xmm1     # 0x5248
testb %bpl, %bpl
jne 0x412c
movsd 0x1124(%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 0x10e1(%rip), %xmm0     # 0x5248
jne 0x4171
movsd 0x10df(%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 0x10bd(%rip), %xmm0     # 0x5260
jb 0x41ab
sqrtsd %xmm0, %xmm0
jmp 0x41b0
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 0x4149
movslq %ebp, %rax
jmp 0x41f7
xorl %eax, %eax
imulq $0xb0, %rax, %rdi
addq 0x98(%rbx), %rdi
callq 0x3fcc
movsd 0xa8(%rbx), %xmm1
movapd %xmm0, %xmm2
divsd %xmm1, %xmm2
movsd 0x1036(%rip), %xmm3     # 0x5258
addsd %xmm1, %xmm3
mulsd 0xa0(%rbx), %xmm3
divsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, 0xa0(%rbx)
decl 0x4044(%rip)            # 0x8288
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq