// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movl 0x40c3(%rip), %eax      # 0x9298
addl $0x1, %eax
movl %eax, 0x40ba(%rip)      # 0x9298
movl 0x40b4(%rip), %eax      # 0x9298
cmpl 0x40b2(%rip), %eax      # 0x929c
jle 0x51f8
movl 0x40a6(%rip), %eax      # 0x9298
movl %eax, 0x40a4(%rip)      # 0x929c
movq -0x10(%rbp), %rax
movsd 0x100c(%rip), %xmm0     # 0x6210
addsd 0xa8(%rax), %xmm0
movsd %xmm0, 0xa8(%rax)
movl 0x4086(%rip), %eax      # 0x92a0
addl $0x1, %eax
movl %eax, 0x407d(%rip)      # 0x92a0
movq -0x10(%rbp), %rax
cmpl $0xa, 0x78(%rax)
jle 0x5244
movq -0x10(%rbp), %rdi
callq 0x4030
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x5487
movq -0x10(%rbp), %rax
cmpl $0x0, 0x90(%rax)
je 0x531f
movq -0x10(%rbp), %rax
movl 0x80(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x90(%rcx), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x90(%rax), %ecx
subl $0x1, %ecx
movl %ecx, 0x90(%rax)
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0xb0, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
movl $0x80, %edx
callq 0x11c0
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rsi
movslq -0x1c(%rbp), %rax
imulq $0x58, %rax, %rax
addq %rax, %rsi
leaq -0x100(%rbp), %rdi
movl $0x58, %edx
callq 0x11c0
leaq -0xa8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x23f0
movq -0x28(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x55a0
movq -0x28(%rbp), %rdi
callq 0x54a0
movsd %xmm0, -0x18(%rbp)
movsd -0x18(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0xa0(%rax)
jmp 0x5419
movl $0x0, -0x104(%rbp)
movq -0x10(%rbp), %rax
movsd 0xa8(%rax), %xmm0
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rdi
movslq -0x104(%rbp), %rax
imulq $0xb0, %rax, %rax
addq %rax, %rdi
callq 0x54c0
movsd %xmm0, -0x110(%rbp)
movl $0x0, -0x114(%rbp)
movl -0x114(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x80(%rcx), %eax
jge 0x53f3
movq -0x10(%rbp), %rax
movsd 0xa8(%rax), %xmm0
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rdi
movslq -0x114(%rbp), %rax
imulq $0xb0, %rax, %rax
addq %rax, %rdi
callq 0x54c0
movsd %xmm0, -0x120(%rbp)
movsd -0x120(%rbp), %xmm0
ucomisd -0x110(%rbp), %xmm0
jbe 0x53dd
movl -0x114(%rbp), %eax
movl %eax, -0x104(%rbp)
movsd -0x120(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
jmp 0x53df
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x5368
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rdi
movslq -0x104(%rbp), %rax
imulq $0xb0, %rax, %rax
addq %rax, %rdi
callq 0x51c0
movsd %xmm0, -0x18(%rbp)
movsd -0x18(%rbp), %xmm0
movq -0x10(%rbp), %rax
divsd 0xa8(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd 0xa0(%rax), %xmm1
movq -0x10(%rbp), %rax
movsd 0xa8(%rax), %xmm2
movsd 0xdc6(%rip), %xmm3      # 0x6210
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm1
movq -0x10(%rbp), %rax
divsd 0xa8(%rax), %xmm1
addsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0xa0(%rax)
movl 0x3e24(%rip), %eax      # 0x9298
subl $0x1, %eax
movl %eax, 0x3e1b(%rip)      # 0x9298
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)