// File: /workspace/github_cpp_makefile_star2_O1/davidjosepha[P]slitherlink/src/generator/loopgen.cpp
// Function: LoopGen::validCell(Coordinates, Coordinates) const
bool LoopGen::validCell(Coordinates coords, Coordinates cur) const {
    bool valid = true;

    /* check to make sure the cell is within the grid */
    if (!inBounds(coords)) {
        return false;
    }

    valid = valid && loop_[coords.i][coords.j] != NOEXP;
    valid = valid && loop_[coords.i][coords.j] != OUT;
    valid = valid && loop_[coords.i][coords.j] != EXP;

    Coordinates shift = { coords.i - cur.i, coords.j - cur.j };

    if ((shift.i + shift.j != 1 && shift.i + shift.j != -1) || shift.i * shift.j != 0) {
        valid = false;
    }

    if (shift.i == 0) {
        valid = valid && cellOpen(coords.i, coords.j + shift.j);

        if (coords.i == 0) {
            valid = valid && cellOpen(1, coords.j + shift.j);
        } else if (coords.i == m_ - 1) {
            valid = valid && cellOpen(m_ - 2, coords.j + shift.j);
        }
        valid = valid && cellOpen(coords.i + 1, coords.j + shift.j);
        valid = valid && cellOpen(coords.i - 1, coords.j + shift.j);
    }

    if (shift.j == 0) {
        valid = valid && cellOpen(coords.i + shift.i, coords.j);

        if (coords.j == 0) {
            valid = valid && cellOpen(coords.i + shift.i, 1);
        } else if (coords.j == n_ - 1) {
            valid = valid && cellOpen(coords.i + shift.i, n_ - 2);
        }
        valid = valid && cellOpen(coords.i + shift.i, coords.j + 1);
        valid = valid && cellOpen(coords.i + shift.i, coords.j - 1);
    }

    if (!valid && loop_[coords.i][coords.j] == UNKNOWN) {
        loop_[coords.i][coords.j] = OUT;
    }

    return valid;
}

// Assembly:
xorl %eax, %eax
movabsq $-0x7fffffff80000000, %rcx # imm = 0x8000000080000000
testq %rcx, %rsi
jne 0xe8ec
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl 0x10(%rdi), %r10d
cmpl %r15d, %r10d
jle 0xe8de
movq %r15, %rbx
shrq $0x20, %rbx
cmpl %ebx, 0x14(%r14)
jle 0xe8de
movq %rdx, %r12
shrq $0x20, %r12
movq (%r14), %rcx
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rcx, -0x48(%rbp)
movq (%rcx,%rax,8), %rax
movq %rax, -0x50(%rbp)
movl (%rax,%rbx,4), %r8d
movl %r15d, %r9d
subl %edx, %r9d
movl %ebx, %r13d
subl %r12d, %r13d
leal (%r9,%r13), %eax
cmpl $0x1, %eax
je 0xe6f4
cmpl $-0x1, %eax
jne 0xe71a
movl %r8d, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
setne %cl
cmpl $0x1, %r8d
setne %al
testl %r9d, %r9d
sete %sil
testl %r13d, %r13d
sete %dil
orb %sil, %dil
jne 0xe71e
xorl %eax, %eax
jmp 0xe720
andb %cl, %al
cmpl %edx, %r15d
jne 0xe7f6
testb %al, %al
movl %r8d, -0x2c(%rbp)
movq %r9, -0x40(%rbp)
movq %r10, -0x38(%rbp)
je 0xe756
leal (%rbx,%r13), %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0xe8ee
movq -0x38(%rbp), %r10
movq -0x40(%rbp), %r9
movl -0x2c(%rbp), %r8d
jmp 0xe758
xorl %eax, %eax
testl %r15d, %r15d
je 0xe788
leal -0x1(%r10), %ecx
cmpl %r15d, %ecx
jne 0xe7b9
testb %al, %al
je 0xe7e4
leal -0x2(%r10), %esi
leal (%rbx,%r13), %edx
movq %r14, %rdi
callq 0xe8ee
movq -0x38(%rbp), %r10
movq -0x40(%rbp), %r9
movl -0x2c(%rbp), %r8d
jmp 0xe7b9
testb %al, %al
je 0xe7e4
movl %r13d, %eax
addl %ebx, %eax
js 0xe7bd
cmpl $0x2, %r10d
jl 0xe7bd
cmpl %eax, 0x14(%r14)
jle 0xe7bd
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %eax, %eax
movl (%rcx,%rax,4), %eax
testl %eax, %eax
sete %cl
cmpl $0x3, %eax
sete %al
orb %cl, %al
testb %al, %al
je 0xe7e4
leal 0x1(%r15), %esi
addl %ebx, %r13d
movq %r14, %rdi
movl %r13d, %edx
callq 0xe8ee
testb %al, %al
je 0xe7e8
leal -0x1(%r15), %esi
movq %r14, %rdi
movl %r13d, %edx
callq 0xe8ee
jmp 0xe7ea
xorl %eax, %eax
jmp 0xe7f6
xorl %eax, %eax
movl -0x2c(%rbp), %r8d
movq -0x40(%rbp), %r9
movq -0x38(%rbp), %r10
cmpl %r12d, %ebx
jne 0xe8c8
testb %al, %al
je 0xe827
leal (%r9,%r15), %esi
movq %r14, %rdi
movl %ebx, %edx
movl %r8d, %r12d
movq %r9, %r13
movq %r10, -0x38(%rbp)
callq 0xe8ee
movq -0x38(%rbp), %r10
movq %r13, %r9
movl %r12d, %r8d
jmp 0xe829
xorl %eax, %eax
testq %rbx, %rbx
jne 0xe866
testb %al, %al
je 0xe8bf
movl %r9d, %eax
addl %r15d, %eax
js 0xe894
cmpl %eax, %r10d
jle 0xe894
cmpl $0x2, 0x14(%r14)
jl 0xe894
movl %eax, %eax
movq -0x48(%rbp), %rcx
movq (%rcx,%rax,8), %rax
movl 0x4(%rax), %eax
testl %eax, %eax
sete %cl
cmpl $0x3, %eax
sete %al
orb %cl, %al
jmp 0xe890
movl 0x14(%r14), %edx
leal -0x1(%rdx), %ecx
cmpl %ebx, %ecx
jne 0xe890
testb %al, %al
je 0xe8bf
leal (%r9,%r15), %esi
addl $-0x2, %edx
movq %r14, %rdi
movl %r8d, %r12d
movq %r9, %r13
callq 0xe8ee
movq %r13, %r9
movl %r12d, %r8d
testb %al, %al
je 0xe8bf
movl %r8d, %r12d
addl %r15d, %r9d
leal 0x1(%rbx), %edx
movq %r14, %rdi
movl %r9d, %esi
movq %r9, %r15
callq 0xe8ee
testb %al, %al
je 0xe8c3
leal -0x1(%rbx), %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0xe8ee
jmp 0xe8c5
xorl %eax, %eax
jmp 0xe8c8
xorl %eax, %eax
movl %r12d, %r8d
testb %al, %al
jne 0xe8de
testl %r8d, %r8d
jne 0xe8dc
movq -0x50(%rbp), %rax
movl $0x3, (%rax,%rbx,4)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop