// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xf8(%rbp)
movq %rdi, -0xf0(%rbp)
movq %rsi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x11e0
movslq -0xc(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, 0x42d4(%rip)      # 0x9290
jne 0x4fdf
movl $0xb0, %edi
callq 0x10b0
movq %rax, 0x42c1(%rip)      # 0x9290
movq 0x42ba(%rip), %rdi      # 0x9290
movq -0x8(%rbp), %rsi
callq 0x55a0
movl $0x0, 0x42af(%rip)      # 0x9298
movl $0x0, 0x42a9(%rip)      # 0x929c
movl $0x1, 0x42a3(%rip)      # 0x92a0
leaq -0xe0(%rbp), %rdi
callq 0x1060
cmpl $0x0, %eax
jne 0x502f
jmp 0x5010
xorl %eax, %eax
movl %eax, %edi
callq 0x11e0
cmpq -0x18(%rbp), %rax
jge 0x502d
movq 0x426a(%rip), %rdi      # 0x9290
callq 0x51c0
jmp 0x5010
jmp 0x502f
movl 0x426b(%rip), %esi      # 0x92a0
movl 0x4261(%rip), %edx      # 0x929c
leaq 0x11ee(%rip), %rdi      # 0x6230
movb $0x0, %al
callq 0x1030
movq -0xf8(%rbp), %rdi
movq 0x4239(%rip), %rax      # 0x9290
movq 0x88(%rax), %rsi
movl $0x58, %edx
callq 0x11c0
movq 0x4221(%rip), %rax      # 0x9290
movq 0x98(%rax), %rax
cvttsd2si 0xa8(%rax), %eax
movl %eax, -0xe4(%rbp)
movl $0x1, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movq 0x41f5(%rip), %rcx      # 0x9290
cmpl 0x80(%rcx), %eax
jge 0x514c
movq 0x41e2(%rip), %rax      # 0x9290
movq 0x98(%rax), %rax
movslq -0xe8(%rbp), %rcx
imulq $0xb0, %rcx, %rcx
addq %rcx, %rax
movsd 0xa8(%rax), %xmm0
cvtsi2sdl -0xe4(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5136
movq -0xf8(%rbp), %rdi
movq 0x41a6(%rip), %rax      # 0x9290
movq 0x88(%rax), %rsi
movslq -0xe8(%rbp), %rax
imulq $0x58, %rax, %rax
addq %rax, %rsi
movl $0x58, %edx
callq 0x11c0
movq 0x4180(%rip), %rax      # 0x9290
movq 0x98(%rax), %rax
movslq -0xe8(%rbp), %rcx
imulq $0xb0, %rcx, %rcx
addq %rcx, %rax
cvttsd2si 0xa8(%rax), %eax
movl %eax, -0xe4(%rbp)
jmp 0x5138
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x508e
leaq 0x1101(%rip), %rdi      # 0x6254
movb $0x0, %al
callq 0x1030
movq -0xf8(%rbp), %rdi
callq 0x4830
leaq 0x10ee(%rip), %rdi      # 0x625b
movb $0x0, %al
callq 0x1030
movq 0x3e7d(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
callq 0x11f0
movq 0x4106(%rip), %rdi      # 0x9290
callq 0x5660
leaq 0x10d3(%rip), %rdi      # 0x6269
movb $0x0, %al
callq 0x1030
movq 0x3e54(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
callq 0x11f0
movq -0xf0(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)