// File: jeffreyan11[P]go-engine/board.cpp
// Function: Board::isMoveValid(int, unsigned short)
bool Board::isMoveValid(Player p, Move m) {
    if (m == MOVE_PASS)
        return true;

    int x = getX(m);
    int y = getY(m);
    assert(pieces[index(x, y)] == EMPTY);

    // First check if the move makes a capture, since if so then
    // it cannot possibly be a suicide
    Player victim = otherPlayer(p);
    int eastID = (pieces[index(x+1, y)] == victim) * chainID[index(x+1, y)];
    int westID = (pieces[index(x-1, y)] == victim) * chainID[index(x-1, y)];
    int northID = (pieces[index(x, y+1)] == victim) * chainID[index(x, y+1)];
    int southID = (pieces[index(x, y-1)] == victim) * chainID[index(x, y-1)];

    if (eastID) {
        Chain *node = nullptr;
        searchChainsByID(node, eastID);
        if (node->liberties == 1)
            return true;
    }
    if (westID) {
        Chain *node = nullptr;
        searchChainsByID(node, westID);
        if (node->liberties == 1)
            return true;
    }
    if (northID) {
        Chain *node = nullptr;
        searchChainsByID(node, northID);
        if (node->liberties == 1)
            return true;
    }
    if (southID) {
        Chain *node = nullptr;
        searchChainsByID(node, southID);
        if (node->liberties == 1)
            return true;
    }

    if (isEye(victim, m))
        return false;

    pieces[index(x, y)] = p;

    // Suicides are illegal
    if (pieces[index(x+1, y)] && pieces[index(x-1, y)]
     && pieces[index(x, y+1)] && pieces[index(x, y-1)]) {
        if (doCaptures<false>(p, coordToMove(x, y))) {
            pieces[index(x, y)] = EMPTY;
            return false;
        }
    }

    pieces[index(x, y)] = EMPTY;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movw %dx, %ax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movw %ax, -0x16(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movzwl -0x16(%rbp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x5425
movb $0x1, -0x1(%rbp)
jmp 0x5873
movzwl -0x16(%rbp), %edi
callq 0x8b80
movl %eax, -0x1c(%rbp)
movzwl -0x16(%rbp), %edi
callq 0x8ba0
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %edi
callq 0x8c10
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, -0x24(%rbp)
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movl -0x1c(%rbp), %edi
addl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movq -0xc0(%rbp), %rcx
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
cmpl -0x24(%rbp), %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0xb4(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0xb0(%rbp)
movl -0x1c(%rbp), %edi
addl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl -0xb4(%rbp), %ecx
movq -0xb0(%rbp), %rdx
movl %eax, %esi
movq -0x60(%rbp), %rax
movslq %esi, %rsi
imull (%rdx,%rsi,4), %ecx
movl %ecx, -0x28(%rbp)
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movq -0xa8(%rbp), %rcx
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
cmpl -0x24(%rbp), %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x9c(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0x98(%rbp)
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl -0x9c(%rbp), %ecx
movq -0x98(%rbp), %rdx
movl %eax, %esi
movq -0x60(%rbp), %rax
movslq %esi, %rsi
imull (%rdx,%rsi,4), %ecx
movl %ecx, -0x2c(%rbp)
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
addl $0x1, %esi
callq 0x8bc0
movq -0x90(%rbp), %rcx
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
cmpl -0x24(%rbp), %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x84(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
addl $0x1, %esi
callq 0x8bc0
movl -0x84(%rbp), %ecx
movq -0x80(%rbp), %rdx
movl %eax, %esi
movq -0x60(%rbp), %rax
movslq %esi, %rsi
imull (%rdx,%rsi,4), %ecx
movl %ecx, -0x30(%rbp)
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
subl $0x1, %esi
callq 0x8bc0
movq -0x78(%rbp), %rcx
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
cmpl -0x24(%rbp), %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x64(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0x70(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
subl $0x1, %esi
callq 0x8bc0
movq -0x70(%rbp), %rcx
movl %eax, %edx
movl -0x64(%rbp), %eax
movslq %edx, %rdx
imull (%rcx,%rdx,4), %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x5631
movq -0x60(%rbp), %rdi
movq $0x0, -0x40(%rbp)
movl -0x28(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8c50
movq -0x40(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x562f
movb $0x1, -0x1(%rbp)
jmp 0x5873
jmp 0x5631
cmpl $0x0, -0x2c(%rbp)
je 0x5664
movq -0x60(%rbp), %rdi
movq $0x0, -0x48(%rbp)
movl -0x2c(%rbp), %edx
leaq -0x48(%rbp), %rsi
callq 0x8c50
movq -0x48(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x5662
movb $0x1, -0x1(%rbp)
jmp 0x5873
jmp 0x5664
cmpl $0x0, -0x30(%rbp)
je 0x5697
movq -0x60(%rbp), %rdi
movq $0x0, -0x50(%rbp)
movl -0x30(%rbp), %edx
leaq -0x50(%rbp), %rsi
callq 0x8c50
movq -0x50(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x5695
movb $0x1, -0x1(%rbp)
jmp 0x5873
jmp 0x5697
cmpl $0x0, -0x34(%rbp)
je 0x56ca
movq -0x60(%rbp), %rdi
movq $0x0, -0x58(%rbp)
movl -0x34(%rbp), %edx
leaq -0x58(%rbp), %rsi
callq 0x8c50
movq -0x58(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x56c8
movb $0x1, -0x1(%rbp)
jmp 0x5873
jmp 0x56ca
movq -0x60(%rbp), %rdi
movl -0x24(%rbp), %esi
movzwl -0x16(%rbp), %edx
callq 0x5890
testb $0x1, %al
jne 0x56e0
jmp 0x56e9
movb $0x0, -0x1(%rbp)
jmp 0x5873
movq -0x60(%rbp), %rax
movl -0x14(%rbp), %ecx
movl %ecx, -0xd4(%rbp)
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl -0xd4(%rbp), %esi
movq -0xd0(%rbp), %rcx
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq %edx, %rdx
movl %esi, (%rcx,%rdx,4)
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movl -0x1c(%rbp), %edi
addl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x5843
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0xe0(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x5843
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
addl $0x1, %esi
callq 0x8bc0
movl %eax, %ecx
movq -0xe8(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x5843
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
subl $0x1, %esi
callq 0x8bc0
movl %eax, %ecx
movq -0xf0(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x5843
movl -0x14(%rbp), %eax
movl %eax, -0xf4(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8c30
movq -0x60(%rbp), %rdi
movl -0xf4(%rbp), %esi
movzwl %ax, %edx
callq 0x8cf0
cmpl $0x0, %eax
je 0x5841
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x100(%rbp), %rax
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
movb $0x0, -0x1(%rbp)
jmp 0x5873
jmp 0x5843
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x108(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x108(%rbp), %rax
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)