// File: /workspace/github_cpp_makefile_star2_O2/jeffreyan11[P]go-engine/search.cpp
// Function: playRandomGame(int, Board&)
void playRandomGame(Player p, Board &b) {
    int movesPlayed = 1;
    int i = 0;
    Move last = MOVE_PASS;

    // Regenerate the movelist up to 4 times
    while (movesPlayed > 0 && i < 4) {
        movesPlayed = 0;
        i++;
        MoveList legalMoves = b.getLegalMoves(p);
        int koCount = 0;

        // While we still have legal moves remaining
        while (legalMoves.size() > 0) {
            // Check if the last move put its own chain into atari
            // Do not do this twice in a row to prevent infinite ko recapture
            if (koCount == 0) {
                Move cap = b.getPotentialCapture(last);
                if (cap != MOVE_PASS) {
                    int ci = legalMoves.find(cap);
                    if (ci != -1)
                        legalMoves.removeFast(ci);
                    
                    b.doMove(p, cap);
                    last = cap;
                    p = otherPlayer(p);
                    movesPlayed++;
                    koCount++;
                    continue;
                }
            }

            // Otherwise, pick a move at random
            std::uniform_int_distribution<int> distribution(0, legalMoves.size()-1);
            int index = distribution(rng);
            Move m = legalMoves.get(index);

            // Only play moves that are not into own eyes and not suicides
            if (!b.isEye(p, m) && b.isMoveValid(p, m)) {
                b.doMove(p, m);
                last = m;
                p = otherPlayer(p);
                movesPlayed++;
                koCount = 0;
            }

            legalMoves.removeFast(index);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %edi, %edx
pushq $0x1
popq %rax
xorl %ecx, %ecx
movw $0x8000, %bp            # imm = 0x8000
movq %rsi, 0x10(%rsp)
testl %eax, %eax
jle 0x6200
cmpl $0x3, %ecx
ja 0x6200
movl %ecx, 0x18(%rsp)
leaq 0x24(%rsp), %rdi
movl %edx, 0x4(%rsp)
callq 0x3c82
xorl %r12d, %r12d
movl $0x0, 0x8(%rsp)
movl 0x424(%rsp), %eax
testl %eax, %eax
je 0x61e8
testl %r12d, %r12d
pushq $0x1
popq %r12
jne 0x60e8
movzwl %bp, %esi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4488
cmpw $0x8000, %ax            # imm = 0x8000
jne 0x6192
xorl %r12d, %r12d
movl 0x424(%rsp), %eax
movl %r12d, 0xc(%rsp)
movl %ebp, %r12d
decl %eax
andl $0x0, 0x1c(%rsp)
movl %eax, 0x20(%rsp)
leaq 0x1c(%rsp), %rdi
leaq 0x11051(%rip), %rsi     # 0x17158
callq 0x5544
movslq %eax, %r15
movzwl 0x24(%rsp,%r15,2), %ebx
movzwl %bx, %r14d
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movl 0x4(%rsp), %r13d
movl %r13d, %esi
movl %r14d, %edx
callq 0x3bda
testb %al, %al
jne 0x6168
movq %rbp, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0x39a8
testb %al, %al
je 0x6168
movq %rbp, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0x2cc2
pushq $0x3
popq %rax
subl %r13d, %eax
incl 0x8(%rsp)
xorl %r12d, %r12d
movl %eax, 0x4(%rsp)
jmp 0x6170
movl %r12d, %ebx
movl 0xc(%rsp), %r12d
movl 0x424(%rsp), %eax
decl %eax
movzwl 0x24(%rsp,%rax,2), %ecx
movw %cx, 0x24(%rsp,%r15,2)
movl %eax, 0x424(%rsp)
movl %ebx, %ebp
jmp 0x60ac
movzwl %ax, %ebp
leaq 0x24(%rsp), %rdi
movl %ebp, %esi
callq 0x6348
cmpl $-0x1, %eax
je 0x61c2
movl 0x424(%rsp), %ecx
decl %ecx
movzwl 0x24(%rsp,%rcx,2), %edx
cltq
movw %dx, 0x24(%rsp,%rax,2)
movl %ecx, 0x424(%rsp)
movq %rbx, %rdi
movl 0x4(%rsp), %ebx
movl %ebx, %esi
movl %ebp, %edx
callq 0x2cc2
pushq $0x3
popq %rax
subl %ebx, %eax
incl 0x8(%rsp)
pushq $0x1
popq %r12
movl %eax, 0x4(%rsp)
jmp 0x60ac
movl 0x18(%rsp), %ecx
incl %ecx
movq 0x10(%rsp), %rsi
movl 0x4(%rsp), %edx
movl 0x8(%rsp), %eax
jmp 0x607e
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq