// File: /workspace/github_c_makefile_star2_O1/icarroll[P]clunk/mcts.c
// Function: montecarlo
struct move montecarlo(struct thudboard * board, int searchtime) {
    time_t stoptime = time(NULL) + searchtime;

    if (! root) {
        root = malloc(sizeof(struct mctreenode));
        initmctree(root, board);
    }

    deepest = curdepth = 0;
    visited = 1;

    jmp_buf stopsearch;
    if (setjmp(stopsearch) == 0) while (time(NULL) < stoptime)
    {
        mcts_step(root);
    }

    printf("visited %d nodes with max depth %d\n", visited, deepest);

    // choose most visited child as move to make
    struct move bestmove = root->allmoves.moves[0];
    int bestvisited = root->children[0].visits;
    for (int ix=1 ; ix<root->allmoves.used ; ix+=1) {
        if (root->children[ix].visits > bestvisited) {
            bestmove = root->allmoves.moves[ix];
            bestvisited = root->children[ix].visits;
        }
    }
    printf("chose "); showmove(& bestmove);

    printf("freeing tree\n"); fflush(stdout);
    closemctree(root); //TODO reuse relevant subtree
    printf("freed tree\n"); fflush(stdout);

    return bestmove;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xd0, %rsp
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
xorl %r12d, %r12d
movl %r12d, %edi
callq 0x11d0
movq %rax, %r14
cmpq $0x0, 0x444b(%rip)      # 0x8280
jne 0x3e53
movl $0xb0, %edi
callq 0x1090
movq %rax, 0x4438(%rip)      # 0x8280
movq %rax, %rdi
movq %r15, %rsi
callq 0x42ec
movl %r12d, 0x442e(%rip)     # 0x8288
movl %r12d, 0x442b(%rip)     # 0x828c
movl $0x1, 0x4425(%rip)      # 0x8290
movq %rsp, %rdi
callq 0x1050
testl %eax, %eax
jne 0x3eac
movslq %ebp, %rax
movq %r14, %r15
addq %rax, %r15
xorl %eax, %eax
movl %eax, %edi
callq 0x11d0
cmpq %r15, %rax
jge 0x3eac
xorl %eax, %eax
movl %eax, %r14d
movq 0x43e6(%rip), %rdi      # 0x8280
callq 0x3fcc
movq %r14, %rdi
callq 0x11d0
cmpq %r15, %rax
jl 0x3e93
movq %rbx, %r14
movl 0x43db(%rip), %esi      # 0x8290
movl 0x43d1(%rip), %edx      # 0x828c
leaq 0x13a6(%rip), %rdi      # 0x5268
xorl %eax, %eax
callq 0x1030
movq 0x43b0(%rip), %rdx      # 0x8280
movq 0x88(%rdx), %rsi
movl $0x16, %ecx
movq %rbx, %rdi
rep  movsl (%rsi), %es:(%rdi)
cmpl $0x2, 0x80(%rdx)
jl 0x3f68
movq 0x98(%rdx), %rax
cvttsd2si 0xa8(%rax), %ecx
movl $0x1, %r9d
xorl %eax, %eax
movl %eax, %r8d
movl $0x16, %eax
movq 0x98(%rdx), %rsi
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x158(%rsi,%r8,2), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3f4f
movq 0x88(%rdx), %rcx
leaq (%rcx,%r8), %rsi
addq $0x58, %rsi
movq %rax, %rcx
movq %rbx, %rdi
rep  movsl (%rsi), %es:(%rdi)
movq 0x98(%rdx), %rcx
cvttsd2si 0x158(%rcx,%r8,2), %ecx
movq %r9, %rsi
incq %rsi
movslq 0x80(%rdx), %rdi
addq $0x58, %r8
movq %rsi, %r9
cmpq %rdi, %rsi
jl 0x3f09
leaq 0x131d(%rip), %rdi      # 0x528c
xorl %eax, %eax
callq 0x1030
movq %rbx, %rdi
callq 0x3af1
leaq 0x130e(%rip), %rdi      # 0x5293
callq 0x1040
movq 0x4067(%rip), %rbx      # 0x7ff8
movq (%rbx), %rdi
callq 0x11e0
movq 0x42e0(%rip), %rdi      # 0x8280
callq 0x434a
leaq 0x12f4(%rip), %rdi      # 0x52a0
callq 0x1040
movq (%rbx), %rdi
callq 0x11e0
movq %r14, %rax
addq $0xd0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq