// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/go.c
// Function: self_go
void self_go(char *filename, char *weightfile, char *f2, char *w2, int multi)
{
    mcts_tree *tree1 = 0;
    mcts_tree *tree2 = 0;
    network *net = load_network(filename, weightfile, 0);
    //set_batch_network(net, 1);

    network *net2;
    if (f2) {
        net2 = parse_network_cfg(f2);
        if(w2){
            load_weights(net2, w2);
        }
    } else {
        net2 = calloc(1, sizeof(network));
        *net2 = *net;
    }
    srand(time(0));
    char boards[600][93];
    int count = 0;
    //set_batch_network(net, 1);
    //set_batch_network(net2, 1);
    float *board = calloc(19*19*3, sizeof(float));
    flip_board(board);
    float *one = calloc(19*19*3, sizeof(float));
    float *two = calloc(19*19*3, sizeof(float));
    int done = 0;
    int player = 1;
    int p1 = 0;
    int p2 = 0;
    int total = 0;
    float temp = .1;
    int mcts_iters = 500;
    float cpuct = 5;
    while(1){
        if (done){
            tree1 = move_mcts(tree1, -1);
            tree2 = move_mcts(tree2, -1);
            float score = score_game(board);
            if((score > 0) == (total%2==0)) ++p1;
            else ++p2;
            ++total;
            fprintf(stderr, "Total: %d, Player 1: %f, Player 2: %f\n", total, (float)p1/total, (float)p2/total);
            sleep(1);
            /*
               int i = (score > 0)? 0 : 1;
               int j;
               for(; i < count; i += 2){
               for(j = 0; j < 93; ++j){
               printf("%c", boards[i][j]);
               }
               printf("\n");
               }
             */
            memset(board, 0, 3*19*19*sizeof(float));
            flip_board(board);
            player = 1;
            done = 0;
            count = 0;
            fflush(stdout);
            fflush(stderr);
        }
        //print_board(stderr, board, 1, 0);
        //sleep(1);

        if ((total%2==0) == (player==1)){
            //mcts_iters = 4500;   
            cpuct = 5;
        } else {
            //mcts_iters = 500;
            cpuct = 1;
        }
        network *use = ((total%2==0) == (player==1)) ? net : net2;
        mcts_tree *t = ((total%2==0) == (player==1)) ? tree1 : tree2;
        t = run_mcts(t, use, board, two, player, mcts_iters, cpuct, 0);
        move m = pick_move(t, temp, player);
        if(((total%2==0) == (player==1))) tree1 = t;
        else tree2 = t;

        tree1 = move_mcts(tree1, m.row*19 + m.col);
        tree2 = move_mcts(tree2, m.row*19 + m.col);

        if(m.row == 19){
            done = 1;
            continue;
        }
        int row = m.row;
        int col = m.col;

        float *swap = two;
        two = one;
        one = swap;

        if(player < 0) flip_board(board);
        boards[count][0] = row;
        boards[count][1] = col;
        board_to_string(boards[count] + 2, board);
        if(player < 0) flip_board(board);
        ++count;

        move_go(board, player, row, col);
        copy_cpu(19*19*3, board, 1, one, 1);

        player = -player;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xda48, %rsp           # imm = 0xDA48
movq %rcx, %rbx
movq %rdx, %r14
xorl %edx, %edx
callq 0x22658
testq %r14, %r14
movq %rax, 0x28(%rsp)
je 0x3cd46
movq %r14, %rdi
callq 0x27071
testq %rbx, %rbx
movq %rax, 0x30(%rsp)
je 0x3cd6b
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2958f
jmp 0x3cd6b
movq %rax, %r15
pushq $0x1
popq %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x3250
movl $0x110, %edx            # imm = 0x110
movq %rax, 0x30(%rsp)
movq %rax, %rdi
movq %r15, %rsi
callq 0x32b0
movq $0x0, 0x20(%rsp)
xorl %edi, %edi
callq 0x32c0
movl %eax, %edi
callq 0x3220
pushq $0x4
popq %rbx
movl $0x43b, %edi            # imm = 0x43B
movq %rbx, %rsi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
callq 0x3a26f
movl $0x43b, %edi            # imm = 0x43B
movq %rbx, %rsi
callq 0x3250
movq %rax, %r12
movl $0x43b, %edi            # imm = 0x43B
movq %rbx, %rsi
callq 0x3250
pushq $0x1
popq %rbp
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
movq %r12, 0x48(%rsp)
movq %rax, 0x38(%rsp)
movb $0x1, %al
testb $0x1, %al
je 0x3cea2
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %r12
cmpl $0x1, %ebp
setne %r15b
xorb 0x8(%rsp), %r15b
movq %r14, %rdi
movss 0xf1f3(%rip), %xmm0     # 0x4c008
testb $0x1, %r15b
cmovneq %rbx, %rdi
cmovneq 0x30(%rsp), %rsi
jne 0x3ce2d
movss 0xf413(%rip), %xmm0     # 0x4c240
xorps %xmm1, %xmm1
movq %r12, %rdx
movq 0x38(%rsp), %rcx
movl %ebp, %r8d
movl $0x1f4, %r9d            # imm = 0x1F4
callq 0x3af25
testb $0x1, %r15b
cmovneq %rax, %rbx
cmoveq %rax, %r14
movq %rax, %rdi
movss 0xf40b(%rip), %xmm0     # 0x4c268
movl %ebp, %esi
callq 0x3b089
movq %rax, %r12
imull $0x13, %r12d, %r15d
movq %rax, %r13
shrq $0x20, %r13
addl %r13d, %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x3b04e
movq %rax, %r14
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3b04e
movq %rax, %rbx
xorl %eax, %eax
cmpl $0x13, %r12d
je 0x3cdec
jmp 0x3cf7f
movq %r14, %rdi
callq 0x3a82e
movq %rbx, %rdi
callq 0x3a82e
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x3cbcb
ucomiss 0xf1b2(%rip), %xmm0     # 0x4c078
setbe %al
movl 0x8(%rsp), %edx
xorb %dl, %al
movzbl %al, %eax
movl %eax, %ecx
andl $0x1, %ecx
movq 0x20(%rsp), %rsi
addl %ecx, %esi
notb %al
movzbl %al, %eax
andl $0x1, %eax
movl 0xc(%rsp), %ecx
addl %eax, %ecx
movl %ecx, 0xc(%rsp)
cvtsi2ss %ecx, %xmm0
incl %edx
cvtsi2ss %edx, %xmm1
movq 0x270f7(%rip), %rbx     # 0x63ff8
movq (%rbx), %rdi
movq %rsi, 0x20(%rsp)
cvtsi2ss %esi, %xmm2
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
divss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm1
leaq 0x14981(%rip), %rsi     # 0x518a5
movl %edx, 0x8(%rsp)
movb $0x2, %al
callq 0x3270
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x3490
movq $0x0, 0x10(%rsp)
movl $0x10ec, %edx           # imm = 0x10EC
movq %r12, %rdi
xorl %esi, %esi
callq 0x31c0
movq %r12, %rdi
callq 0x3a26f
movq 0x27070(%rip), %rax     # 0x63fd0
movq (%rax), %rdi
callq 0x3310
movq (%rbx), %rdi
callq 0x3310
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x28(%rsp), %rsi
jmp 0x3cdfe
movslq 0x10(%rsp), %rax
testl %ebp, %ebp
js 0x3cfab
imulq $0x5d, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0x52, %rdi
movb %r12b, -0x2(%rdi)
movb %r13b, -0x1(%rdi)
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x3997a
jmp 0x3cfe3
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rax, 0x40(%rsp)
callq 0x3a26f
imulq $0x5d, 0x40(%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0x52, %rdi
movb %r12b, -0x2(%rdi)
movb %r13b, -0x1(%rdi)
movq %r15, %rsi
callq 0x3997a
movq %r15, %rdi
callq 0x3a26f
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movl %ebp, %esi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x3a4b9
movl $0x43b, %edi            # imm = 0x43B
movq %r15, %rsi
pushq $0x1
popq %rdx
movq 0x38(%rsp), %r12
movq %r12, %rcx
movl %edx, %r8d
callq 0x1b5a3
negl %ebp
movq 0x48(%rsp), %rax
jmp 0x3cde0