// File: /workspace/github_cpp_makefile_star2_O3/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, 0xc(%rsp)
movw $0x8000, %r12w          # imm = 0x8000
movq $0x0, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x18(%rsp), %rbp
leaq 0x34(%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %edx
callq 0x40dc
movl 0x434(%rsp), %ecx
testl %ecx, %ecx
je 0x68bd
movq 0x20(%rsp), %rax
incl %eax
movl %eax, 0x28(%rsp)
movl $0x0, 0x10(%rsp)
xorl %eax, %eax
movl $0x1, %edx
testl %eax, %eax
jne 0x677f
movzwl %r12w, %esi
movq 0x18(%rsp), %rdi
callq 0x4974
movzwl %ax, %r13d
xorl %edx, %edx
movl 0x434(%rsp), %ecx
cmpl $0x8000, %r13d          # imm = 0x8000
jne 0x6829
movl %edx, 0x14(%rsp)
movl %r12d, %r14d
decl %ecx
movl $0x0, 0x2c(%rsp)
movl %ecx, 0x30(%rsp)
leaq 0x2c(%rsp), %rdi
leaq 0x119d8(%rip), %rsi     # 0x18178
movq %rdi, %rdx
callq 0x5a12
movslq %eax, %r15
movzwl 0x34(%rsp,%r15,2), %r12d
movzwl %r12w, %r13d
movq %rbp, %rdi
movl 0xc(%rsp), %ebx
movl %ebx, %esi
movl %r13d, %edx
callq 0x4034
testb %al, %al
jne 0x67fb
movq %rbp, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x3e0c
testb %al, %al
je 0x67fb
movq %rbp, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x3030
movl $0x3, %eax
subl %ebx, %eax
incl 0x10(%rsp)
xorl %edx, %edx
movl %eax, 0xc(%rsp)
jmp 0x6802
movl %r14d, %r12d
movl 0x14(%rsp), %edx
movl 0x434(%rsp), %ecx
decl %ecx
movzwl 0x34(%rsp,%rcx,2), %eax
movw %ax, 0x34(%rsp,%r15,2)
movl %ecx, 0x434(%rsp)
movl %edx, %eax
testl %ecx, %ecx
jne 0x674e
jmp 0x689d
testl %ecx, %ecx
movq 0x18(%rsp), %rdi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl 0xc(%rsp), %ebx
je 0x6873
xorl %edx, %edx
xorl %esi, %esi
cmpw %ax, 0x34(%rsp,%rsi,2)
je 0x6857
incq %rsi
addq %r8, %rdx
incq %rdx
cmpq %rsi, %rcx
jne 0x6840
jmp 0x6873
cmpq %rsi, %r8
je 0x6873
decl %ecx
movzwl 0x34(%rsp,%rcx,2), %eax
sarq $0x1f, %rdx
movw %ax, 0x34(%rsp,%rdx)
movl %ecx, 0x434(%rsp)
movl %ebx, %esi
movl %r13d, %edx
callq 0x3030
movl $0x3, %eax
subl %ebx, %eax
incl 0x10(%rsp)
movl $0x1, %edx
movl 0x434(%rsp), %ecx
movl %r13d, %r12d
movl %eax, 0xc(%rsp)
jmp 0x681d
cmpl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r14
jle 0x68bd
cmpl $0x3, 0x20(%rsp)
movl 0x28(%rsp), %eax
movq %rax, 0x20(%rsp)
jb 0x6719
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq