// File: 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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movw $0x8000, -0x1a(%rbp)    # imm = 0x8000
xorl %eax, %eax
cmpl $0x0, -0x14(%rbp)
movb %al, -0x44b(%rbp)
jle 0xb1b1
cmpl $0x4, -0x18(%rbp)
setl %al
movb %al, -0x44b(%rbp)
movb -0x44b(%rbp), %al
testb $0x1, %al
jne 0xb1c0
jmp 0xb454
movl $0x0, -0x14(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rsi
movl -0x4(%rbp), %edx
leaq -0x420(%rbp), %rdi
callq 0x5a50
movl $0x0, -0x424(%rbp)
leaq -0x420(%rbp), %rdi
callq 0x92b0
movl %eax, -0x450(%rbp)
jmp 0xb201
movl -0x450(%rbp), %eax
cmpl $0x0, %eax
jbe 0xb443
cmpl $0x0, -0x424(%rbp)
jne 0xb314
movq -0x10(%rbp), %rdi
movzwl -0x1a(%rbp), %esi
callq 0x6a30
movw %ax, -0x452(%rbp)
jmp 0xb233
movw -0x452(%rbp), %ax
movw %ax, -0x436(%rbp)
movzwl -0x436(%rbp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0xb312
movzwl -0x436(%rbp), %esi
leaq -0x420(%rbp), %rdi
callq 0xb6d0
movl %eax, -0x458(%rbp)
jmp 0xb26e
movl -0x458(%rbp), %eax
movl %eax, -0x43c(%rbp)
cmpl $-0x1, -0x43c(%rbp)
je 0xb2bc
movl -0x43c(%rbp), %esi
leaq -0x420(%rbp), %rdi
callq 0xb740
jmp 0xb297
jmp 0xb2bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x430(%rbp)
movl %eax, -0x434(%rbp)
leaq -0x420(%rbp), %rdi
callq 0x8f30
jmp 0xb45d
movq -0x10(%rbp), %rdi
movl -0x4(%rbp), %esi
movzwl -0x436(%rbp), %edx
callq 0x3d50
jmp 0xb2d1
movw -0x436(%rbp), %ax
movw %ax, -0x1a(%rbp)
movl -0x4(%rbp), %edi
callq 0x8c10
movl %eax, -0x45c(%rbp)
jmp 0xb2ec
movl -0x45c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x424(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x424(%rbp)
jmp 0xb1ed
jmp 0xb314
leaq -0x420(%rbp), %rdi
callq 0x92b0
movl %eax, -0x460(%rbp)
jmp 0xb328
movl -0x460(%rbp), %edx
decl %edx
leaq -0x444(%rbp), %rdi
xorl %esi, %esi
callq 0x9810
jmp 0xb340
leaq 0x14e91(%rip), %rsi     # 0x201d8
leaq -0x444(%rbp), %rdi
callq 0x9840
movl %eax, -0x464(%rbp)
jmp 0xb35b
movl -0x464(%rbp), %eax
movl %eax, -0x448(%rbp)
movl -0x448(%rbp), %esi
leaq -0x420(%rbp), %rdi
callq 0xb5e0
movw %ax, -0x466(%rbp)
jmp 0xb382
movw -0x466(%rbp), %ax
movw %ax, -0x44a(%rbp)
movq -0x10(%rbp), %rdi
movl -0x4(%rbp), %esi
movzwl -0x44a(%rbp), %edx
callq 0x5890
movb %al, -0x467(%rbp)
jmp 0xb3ab
movb -0x467(%rbp), %al
testb $0x1, %al
jne 0xb42a
jmp 0xb3b7
movq -0x10(%rbp), %rdi
movl -0x4(%rbp), %esi
movzwl -0x44a(%rbp), %edx
callq 0x53f0
movb %al, -0x468(%rbp)
jmp 0xb3d2
movb -0x468(%rbp), %al
testb $0x1, %al
jne 0xb3de
jmp 0xb42a
movq -0x10(%rbp), %rdi
movl -0x4(%rbp), %esi
movzwl -0x44a(%rbp), %edx
callq 0x3d50
jmp 0xb3f3
movw -0x44a(%rbp), %ax
movw %ax, -0x1a(%rbp)
movl -0x4(%rbp), %edi
callq 0x8c10
movl %eax, -0x46c(%rbp)
jmp 0xb40e
movl -0x46c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x424(%rbp)
movl -0x448(%rbp), %esi
leaq -0x420(%rbp), %rdi
callq 0xb740
jmp 0xb43e
jmp 0xb1ed
leaq -0x420(%rbp), %rdi
callq 0x8f30
jmp 0xb196
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
movq -0x430(%rbp), %rdi
callq 0x2350
nopl (%rax)