// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movb $0x1, %bpl
cmpl $0x8000, %edx           # imm = 0x8000
je 0x3aea
movq %rdi, %r14
movzwl %dx, %eax
movl %eax, 0x1c(%rsp)
movl %eax, %ebx
shrl $0x8, %ebx
movzbl %dl, %r9d
pushq $0x3
popq %r12
movl %esi, 0x18(%rsp)
subl %esi, %r12d
movl 0x778b(%rip), %ecx      # 0xb174
movl %ecx, %r8d
imull %r9d, %r8d
leal (%r8,%rbx), %eax
incl %eax
movslq %eax, %rdx
movq (%rdi), %rdi
cmpl %r12d, (%rdi,%rdx,4)
movq 0x20(%r14), %rsi
setne %al
leal -0x1(%r8,%rbx), %r8d
movslq %r8d, %r8
movl $0x0, 0x4(%rsp)
movl $0x0, %ebp
cmpl %r12d, (%rdi,%r8,4)
jne 0x3a26
movl (%rsi,%r8,4), %ebp
leal 0x1(%r9), %r10d
movl %ecx, %r8d
movl %r10d, 0x14(%rsp)
imull %r10d, %r8d
addl %ebx, %r8d
movslq %r8d, %r8
movl $0x0, %r15d
cmpl %r12d, (%rdi,%r8,4)
jne 0x3a4c
movl (%rsi,%r8,4), %r15d
movl (%rsi,%rdx,4), %edx
movq %r9, 0x20(%rsp)
leal -0x1(%r9), %r8d
movl %r8d, 0x10(%rsp)
imull %r8d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
cmpl %r12d, (%rdi,%rcx,4)
jne 0x3a73
movl (%rsi,%rcx,4), %ecx
movl %ecx, 0x4(%rsp)
testl %edx, %edx
sete %cl
orb %cl, %al
jne 0x3a9b
leaq 0x8(%rsp), %r13
andq $0x0, (%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x5100
movq (%r13), %rax
cmpl $0x1, 0xc(%rax)
je 0x3ae7
testl %ebp, %ebp
je 0x3ac0
leaq 0x8(%rsp), %r13
andq $0x0, (%r13)
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x5100
movq (%r13), %rax
cmpl $0x1, 0xc(%rax)
je 0x3ae7
testl %r15d, %r15d
je 0x3afb
leaq 0x8(%rsp), %r13
andq $0x0, (%r13)
movq %r14, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x5100
movq (%r13), %rax
cmpl $0x1, 0xc(%rax)
jne 0x3afb
movb $0x1, %bpl
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %edx
testl %edx, %edx
movb $0x1, %bpl
je 0x3b23
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x5100
movq (%r15), %rax
cmpl $0x1, 0xc(%rax)
je 0x3aea
movq %r14, %rdi
movl %r12d, %esi
movl 0x1c(%rsp), %r15d
movl %r15d, %edx
callq 0x3bda
testb %al, %al
je 0x3b3e
xorl %ebp, %ebp
jmp 0x3aea
leal 0x1(%rbx), %edi
movq (%r14), %rax
movl 0x762a(%rip), %ecx      # 0xb174
movq 0x20(%rsp), %r8
imull %r8d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
movl 0x18(%rsp), %esi
movl %esi, (%rax,%rcx,4)
movl 0x760f(%rip), %edx      # 0xb174
movl %edx, %ecx
imull %r8d, %ecx
addl %ecx, %edi
movslq %edi, %rdi
movb $0x1, %bpl
cmpl $0x0, (%rax,%rdi,4)
je 0x3bcb
leal -0x1(%rbx), %edi
addl %ecx, %edi
movslq %edi, %rdi
cmpl $0x0, (%rax,%rdi,4)
je 0x3bcb
movl 0x14(%rsp), %edi
imull %edx, %edi
addl %ebx, %edi
movslq %edi, %rdi
cmpl $0x0, (%rax,%rdi,4)
je 0x3bcb
imull 0x10(%rsp), %edx
addl %ebx, %edx
movslq %edx, %rdx
cmpl $0x0, (%rax,%rdx,4)
je 0x3bcb
movq %r14, %rdi
movl %r15d, %edx
movq %r8, %r15
callq 0x512a
testl %eax, %eax
sete %bpl
movq (%r14), %rax
imull 0x75ac(%rip), %r15d     # 0xb174
movl %r15d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
andl $0x0, (%rax,%rcx,4)
jmp 0x3aea
nop