// File: jeffreyan11[P]go-engine/board.cpp
// Function: Board::isSurrounded(int, int, int, int, int*, GoArrayList<unsigned short>&)
bool Board::isSurrounded(Player victim, Player open, int x, int y,
    Stone *visited, MoveList &captured) {
    visited[index(x, y)] = 1;

    Stone east = pieces[index(x+1, y)];
    // If we are next to a non-blocker and non-victim, then we are not surrounded
    if (east == open)
        return false;
    // If we next to victim, we need to recursively see if the entire group
    // is surrounded
    else if (east == victim && visited[index(x+1, y)] == 0)
        if (!isSurrounded(victim, open, x+1, y, visited, captured))
            return false;
    // Else the piece is surrounded by a blocker or edge

    Stone west = pieces[index(x-1, y)];
    if (west == open)
        return false;
    else if (west == victim && visited[index(x-1, y)] == 0)
        if (!isSurrounded(victim, open, x-1, y, visited, captured))
            return false;

    Stone north = pieces[index(x, y+1)];
    if (north == open)
        return false;
    else if (north == victim && visited[index(x, y+1)] == 0)
        if (!isSurrounded(victim, open, x, y+1, visited, captured))
            return false;

    Stone south = pieces[index(x, y-1)];
    if (south == open)
        return false;
    else if (south == victim && visited[index(x, y-1)] == 0)
        if (!isSurrounded(victim, open, x, y-1, visited, captured))
            return false;

    // If we got here, we are surrounded on all four sides
    captured.add(coordToMove(x, y));
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movq -0x50(%rbp), %rcx
movl %eax, %edx
movq -0x48(%rbp), %rax
movslq %edx, %rdx
movl $0x1, (%rcx,%rdx,4)
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %edi
addl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x5ea8
movb $0x0, -0x1(%rbp)
jmp 0x610d
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x5f09
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x1c(%rbp), %edi
addl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x5f09
movq -0x48(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movl -0x20(%rbp), %r8d
movq -0x28(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x5e20
testb $0x1, %al
jne 0x5f07
movb $0x0, -0x1(%rbp)
jmp 0x610d
jmp 0x5f09
jmp 0x5f0b
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x60(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x5f44
movb $0x0, -0x1(%rbp)
jmp 0x610d
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x5fa5
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movl -0x20(%rbp), %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x5fa5
movq -0x48(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movl -0x20(%rbp), %r8d
movq -0x28(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x5e20
testb $0x1, %al
jne 0x5fa3
movb $0x0, -0x1(%rbp)
jmp 0x610d
jmp 0x5fa5
jmp 0x5fa7
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
addl $0x1, %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x70(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x5fe0
movb $0x0, -0x1(%rbp)
jmp 0x610d
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x6042
movq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
addl $0x1, %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x78(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x6042
movq -0x48(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
addl $0x1, %r8d
movq -0x28(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x5e20
testb $0x1, %al
jne 0x6040
movb $0x0, -0x1(%rbp)
jmp 0x610d
jmp 0x6042
jmp 0x6044
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
subl $0x1, %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x80(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x607d
movb $0x0, -0x1(%rbp)
jmp 0x610d
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x60e2
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
subl $0x1, %esi
callq 0x8bc0
movl %eax, %ecx
movq -0x88(%rbp), %rax
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x60e2
movq -0x48(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
subl $0x1, %r8d
movq -0x28(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x5e20
testb $0x1, %al
jne 0x60e0
movb $0x0, -0x1(%rbp)
jmp 0x610d
jmp 0x60e2
jmp 0x60e4
movq 0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x8c30
movq -0x90(%rbp), %rdi
movzwl %ax, %esi
callq 0x8ef0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)