// File: /workspace/github_cpp_makefile_star2_O3/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:
movb $0x1, %al
cmpl $0x8000, %edx           # imm = 0x8000
je 0x4033
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ecx
movq %rdi, %rbx
movzwl %dx, %esi
movl %esi, 0xc(%rsp)
movl %esi, %r15d
shrl $0x8, %r15d
movzbl %dl, %r13d
movl $0x3, %esi
movl %ecx, 0x14(%rsp)
subl %ecx, %esi
movq (%rdi), %r12
movl 0x8338(%rip), %r9d      # 0xc18c
movl %r9d, %r14d
imull %r13d, %r14d
leal (%r14,%r15), %ecx
incl %ecx
movslq %ecx, %r10
cmpl %esi, (%r12,%r10,4)
movq 0x20(%rdi), %r11
setne %r8b
leal (%r14,%r15), %ecx
decl %ecx
movslq %ecx, %rdx
xorl %ecx, %ecx
movl $0x0, %edi
cmpl %esi, (%r12,%rdx,4)
jne 0x3e8a
movl (%r11,%rdx,4), %edi
leal 0x1(%r13), %ebp
movl %r9d, %edx
movl %ebp, 0x10(%rsp)
imull %ebp, %edx
addl %r15d, %edx
movslq %edx, %rbp
movl $0x0, %edx
cmpl %esi, (%r12,%rbp,4)
jne 0x3ead
movl (%r11,%rbp,4), %edx
movl (%r11,%r10,4), %r10d
leal -0x1(%r13), %ebp
imull %ebp, %r9d
addl %r15d, %r9d
movslq %r9d, %r9
cmpl %esi, (%r12,%r9,4)
jne 0x3ec9
movl (%r11,%r9,4), %ecx
testl %r10d, %r10d
sete %r9b
orb %r9b, %r8b
jne 0x3eff
movq 0x28(%rbx), %r8
cmpl %r10d, (%r8)
je 0x3ef4
movl $0x1, %r9d
movslq %r9d, %r9
movq 0x28(%rbx,%r9,8), %r8
incl %r9d
cmpl %r10d, (%r8)
jne 0x3ee4
cmpl $0x1, 0xc(%r8)
je 0x4025
testl %edi, %edi
je 0x3f2d
movq 0x28(%rbx), %r8
cmpl %edi, (%r8)
je 0x3f22
movl $0x1, %r9d
movslq %r9d, %r9
movq 0x28(%rbx,%r9,8), %r8
incl %r9d
cmpl %edi, (%r8)
jne 0x3f12
cmpl $0x1, 0xc(%r8)
je 0x4025
testl %edx, %edx
je 0x3f58
movq 0x28(%rbx), %rdi
cmpl %edx, (%rdi)
je 0x3f4e
movl $0x1, %r8d
movslq %r8d, %r8
movq 0x28(%rbx,%r8,8), %rdi
incl %r8d
cmpl %edx, (%rdi)
jne 0x3f3f
cmpl $0x1, 0xc(%rdi)
je 0x4025
testl %ecx, %ecx
je 0x3f81
movq 0x28(%rbx), %rdx
cmpl %ecx, (%rdx)
je 0x3f77
movl $0x1, %edi
movslq %edi, %rdi
movq 0x28(%rbx,%rdi,8), %rdx
incl %edi
cmpl %ecx, (%rdx)
jne 0x3f69
cmpl $0x1, 0xc(%rdx)
je 0x4025
movq %rbx, %rdi
movl 0xc(%rsp), %edx
callq 0x4034
testb %al, %al
je 0x3f98
xorl %eax, %eax
jmp 0x4025
leal 0x1(%r15), %eax
addl %r15d, %r14d
movslq %r14d, %rcx
movl 0x14(%rsp), %esi
movl %esi, (%r12,%rcx,4)
movl 0x81dc(%rip), %edx      # 0xc18c
movl %edx, %ecx
imull %r13d, %ecx
addl %ecx, %eax
movslq %eax, %rdi
movb $0x1, %al
cmpl $0x0, (%r12,%rdi,4)
je 0x4017
leal -0x1(%r15), %edi
addl %ecx, %edi
movslq %edi, %rdi
cmpl $0x0, (%r12,%rdi,4)
je 0x4017
movl 0x10(%rsp), %edi
imull %edx, %edi
addl %r15d, %edi
movslq %edi, %rdi
cmpl $0x0, (%r12,%rdi,4)
je 0x4017
imull %ebp, %edx
addl %r15d, %edx
movslq %edx, %rdx
cmpl $0x0, (%r12,%rdx,4)
je 0x4017
movq %rbx, %rdi
movl 0xc(%rsp), %edx
callq 0x55ba
testl %eax, %eax
sete %al
movq (%rbx), %r12
imull 0x8178(%rip), %r13d     # 0xc18c
movl %r13d, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
movl $0x0, (%r12,%rcx,4)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq