// File: /workspace/github_cpp_makefile_star2_O1/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 $0x438, %rsp            # imm = 0x438
movq %rsi, %r14
movl %edi, %ebp
movw $0x8000, %r12w          # imm = 0x8000
xorl %r15d, %r15d
movq %rsi, 0x10(%rsp)
leaq 0x34(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x40f8
xorl %eax, %eax
cmpl $0x0, 0x434(%rsp)
je 0x68e5
movl $0x0, 0xc(%rsp)
movl %ebp, 0x8(%rsp)
movq %r15, 0x28(%rsp)
xorl %eax, %eax
movl $0x1, %edx
testl %eax, %eax
jne 0x6815
movzwl %r12w, %esi
movq 0x10(%rsp), %rdi
callq 0x496c
movl %eax, %r13d
movzwl %ax, %r14d
xorl %edx, %edx
cmpl $0x8000, %r14d          # imm = 0x8000
je 0x680b
movl 0x434(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x67c2
xorl %edx, %edx
movq 0x10(%rsp), %rdi
cmpw %r13w, 0x34(%rsp,%rdx,2)
je 0x67c9
incq %rdx
cmpq %rdx, %rax
jne 0x67b0
jmp 0x67cb
movq 0x10(%rsp), %rdi
jmp 0x67cb
movl %edx, %ecx
cmpl $-0x1, %ecx
je 0x67e6
decl %eax
movzwl 0x34(%rsp,%rax,2), %edx
movslq %ecx, %rcx
movw %dx, 0x34(%rsp,%rcx,2)
movl %eax, 0x434(%rsp)
movl 0x8(%rsp), %ebx
movl %ebx, %esi
movl %r14d, %edx
callq 0x3046
movl $0x3, %eax
subl %ebx, %eax
incl 0xc(%rsp)
movl $0x1, %edx
movl %r14d, %r12d
movl %eax, 0x8(%rsp)
negw %r13w
jno 0x68bf
movl %edx, 0x1c(%rsp)
movl %r12d, %r15d
movl 0x434(%rsp), %eax
decl %eax
movl $0x0, 0x20(%rsp)
movl %eax, 0x24(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x1193b(%rip), %rsi     # 0x18178
movq %rdi, %rdx
callq 0x5a8a
movslq %eax, %r13
movzwl 0x34(%rsp,%r13,2), %r12d
movzwl %r12w, %r14d
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movl 0x8(%rsp), %ebx
movl %ebx, %esi
movl %r14d, %edx
callq 0x4050
testb %al, %al
jne 0x689d
movq %rbp, %rdi
movl %ebx, %esi
movl %r14d, %edx
callq 0x3e1e
testb %al, %al
je 0x689d
movq %rbp, %rdi
movl %ebx, %esi
movl %r14d, %edx
callq 0x3046
movl $0x3, %eax
subl %ebx, %eax
incl 0xc(%rsp)
xorl %edx, %edx
movl %eax, 0x8(%rsp)
jmp 0x68a4
movl %r15d, %r12d
movl 0x1c(%rsp), %edx
movl 0x434(%rsp), %eax
decl %eax
movzwl 0x34(%rsp,%rax,2), %ecx
movw %cx, 0x34(%rsp,%r13,2)
movl %eax, 0x434(%rsp)
cmpl $0x0, 0x434(%rsp)
movl %edx, %eax
jne 0x676b
cmpl $0x0, 0xc(%rsp)
setg %al
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r15
movl 0x8(%rsp), %ebp
testb %al, %al
je 0x68fa
leal 0x1(%r15), %eax
cmpl $0x3, %r15d
movl %eax, %r15d
jb 0x6739
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq