// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r13
movq %rsi, %r12
shrq $0x20, %r12
movabsq $-0x7fffffff80000000, %rax # imm = 0x8000000080000000
testq %rax, %rsi
sete %al
movl 0x10(%rdi), %r11d
movl 0x14(%rdi), %r10d
cmpl %r13d, %r11d
setg %cl
cmpl %r12d, %r10d
setg %sil
andb %cl, %sil
andb %al, %sil
cmpb $0x1, %sil
jne 0xe927
movq %rdi, %r14
movq %rdx, %r15
shrq $0x20, %r15
movq (%rdi), %rcx
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rcx, -0x50(%rbp)
movq (%rcx,%rax,8), %rax
movq %rax, -0x58(%rbp)
movl (%rax,%r12,4), %r8d
movl %r13d, %esi
subl %edx, %esi
movl %r12d, %ebx
subl %r15d, %ebx
leal (%rbx,%rsi), %eax
cmpl $0x1, %eax
je 0xe731
cmpl $-0x1, %eax
jne 0xe755
movl %r8d, %eax
andl $-0x2, %eax
cmpl $0x2, %eax
setne %cl
cmpl $0x1, %r8d
setne %al
testl %esi, %esi
sete %r9b
testl %ebx, %ebx
sete %dil
orb %r9b, %dil
jne 0xe759
xorl %eax, %eax
jmp 0xe75b
andb %cl, %al
cmpl %edx, %r13d
jne 0xe83e
testb %al, %al
je 0xe7be
movq %r11, -0x40(%rbp)
movq %r10, -0x48(%rbp)
movq %rsi, -0x38(%rbp)
movl %r8d, -0x2c(%rbp)
addl %r12d, %ebx
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
callq 0xe93a
testl %r13d, %r13d
je 0xe7c2
movq -0x40(%rbp), %r11
leal -0x1(%r11), %ecx
cmpl %r13d, %ecx
movq -0x48(%rbp), %r10
jne 0xe7b8
testb %al, %al
je 0xe820
leal -0x2(%r11), %esi
movq %r14, %rdi
movl %ebx, %edx
callq 0xe93a
movq -0x40(%rbp), %r11
movq -0x48(%rbp), %r10
testb %al, %al
jne 0xe7fe
jmp 0xe820
xorl %eax, %eax
jmp 0xe83e
testb %al, %al
movq -0x48(%rbp), %r10
movq -0x40(%rbp), %r11
je 0xe820
testl %ebx, %ebx
setns %al
cmpl $0x2, %r11d
setge %cl
andb %al, %cl
cmpl %ebx, %r10d
setg %al
andb %cl, %al
cmpb $0x1, %al
jne 0xe7fe
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movl %ebx, %ecx
movl (%rax,%rcx,4), %eax
testl %eax, %eax
je 0xe7fe
cmpl $0x3, %eax
jne 0xe820
leal 0x1(%r13), %esi
movq %r14, %rdi
movl %ebx, %edx
callq 0xe93a
testb %al, %al
je 0xe82c
leal -0x1(%r13), %esi
movq %r14, %rdi
movl %ebx, %edx
callq 0xe93a
jmp 0xe82e
xorl %eax, %eax
movl -0x2c(%rbp), %r8d
movq -0x38(%rbp), %rsi
jmp 0xe83e
xorl %eax, %eax
movl -0x2c(%rbp), %r8d
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %r10
movq -0x40(%rbp), %r11
cmpl %r15d, %r12d
jne 0xe910
testb %al, %al
je 0xe916
movq %r11, %r15
movq %r10, %rbx
movl %r8d, -0x2c(%rbp)
addl %r13d, %esi
movq %r14, %rdi
movq %rsi, %r13
movl %r12d, %edx
callq 0xe93a
testq %r12, %r12
jne 0xe8af
testb %al, %al
movl -0x2c(%rbp), %r8d
je 0xe916
movq %r13, %rsi
testl %esi, %esi
setns %al
cmpl %esi, %r15d
setg %cl
andb %al, %cl
cmpl $0x2, %ebx
setge %al
andb %cl, %al
cmpb $0x1, %al
jne 0xe8e7
movl %esi, %eax
movq -0x50(%rbp), %rcx
movq (%rcx,%rax,8), %rax
movl 0x4(%rax), %eax
testl %eax, %eax
je 0xe8e7
cmpl $0x3, %eax
je 0xe8e7
jmp 0xe916
leal -0x1(%rbx), %ecx
cmpl %r12d, %ecx
jne 0xe8dc
testb %al, %al
movl -0x2c(%rbp), %r8d
je 0xe916
movq %r13, %rsi
addl $-0x2, %ebx
movq %r14, %rdi
movl %ebx, %edx
callq 0xe93a
movq %r13, %rsi
movl -0x2c(%rbp), %r8d
testb %al, %al
jne 0xe8e7
jmp 0xe916
testb %al, %al
movl -0x2c(%rbp), %r8d
movq %r13, %rsi
je 0xe916
leal 0x1(%r12), %edx
movq %r14, %rdi
callq 0xe93a
movl -0x2c(%rbp), %r8d
testb %al, %al
je 0xe916
movq %r13, %rsi
leal -0x1(%r12), %edx
movq %r14, %rdi
callq 0xe93a
movl -0x2c(%rbp), %r8d
movb $0x1, %cl
testb %al, %al
jne 0xe929
testl %r8d, %r8d
jne 0xe927
movq -0x58(%rbp), %rax
movl $0x3, (%rax,%r12,4)
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq