// File: /workspace/github_cpp_makefile_star2_O2/davidjosepha[P]slitherlink/src/solver/solver.cpp
// Function: Solver::ruleApplies(int, int, Rule const&, Orientation) const
bool Solver::ruleApplies(int i, int j, Rule const & rule, Orientation orient) const {
    int m = rule.getHeight();
    int n = rule.getWidth();
    if (i > grid_->getHeight() - rule.getNumberHeight(orient)
            || j > grid_->getWidth() - rule.getNumberWidth(orient)) {
        return false;
    }

    std::vector<NumberPosition> const * numberPattern = rule.getNumberPattern();
    for (int k = 0; k < numberPattern->size(); k++) {
        NumberPosition pattern = (*numberPattern)[k];
        Coordinates adjusted = rotateNumber(pattern.coords.i, pattern.coords.j, m, n, orient);

        if (pattern.num != grid_->getNumber(adjusted.i + i, adjusted.j + j)) {
            return false;
        }
    }

    std::vector<EdgePosition> const * hLinePattern = rule.getHLinePattern();
    for (int k = 0; k < hLinePattern->size(); k++) {
        EdgePosition pattern = (*hLinePattern)[k];
        Coordinates adjusted = rotateHLine(pattern.coords.i, pattern.coords.j, m, n, orient);

        switch (orient) {
            case UPFLIP:
            case UP:
            case DOWNFLIP:
            case DOWN:
                if (pattern.edge != grid_->getHLine(adjusted.i + i, adjusted.j + j)) {
                    return false;
                }
                break;
            case LEFTFLIP:
            case LEFT:
            case RIGHTFLIP:
            case RIGHT:
                if (pattern.edge != grid_->getVLine(adjusted.i + i, adjusted.j + j)) {
                    return false;
                }
                break;
        }
    }

    std::vector<EdgePosition> const * vLinePattern = rule.getVLinePattern();
    for (int k = 0; k < vLinePattern->size(); k++) {
        EdgePosition pattern = (*vLinePattern)[k];
        Coordinates adjusted = rotateVLine(pattern.coords.i, pattern.coords.j, m, n, orient);

        switch (orient) {
            case UPFLIP:
            case UP:
            case DOWNFLIP:
            case DOWN:
                if (pattern.edge != grid_->getVLine(adjusted.i + i, adjusted.j + j)) {
                    return false;
                }
                break;
            case LEFTFLIP:
            case LEFT:
            case RIGHTFLIP:
            case RIGHT:
                if (pattern.edge != grid_->getHLine(adjusted.i + i, adjusted.j + j)) {
                    return false;
                }
                break;
        }
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, -0x38(%rbp)
movl %esi, %r13d
movq %rdi, %r12
movl (%rcx), %eax
movl %eax, -0x30(%rbp)
movl 0x4(%rcx), %eax
movl %eax, -0x2c(%rbp)
movq 0x8(%rdi), %rax
movl 0xc(%rax), %r15d
movq %rcx, %rdi
movl %r8d, %esi
callq 0x5140
subl %eax, %r15d
movq %r13, -0x48(%rbp)
cmpl %r13d, %r15d
jl 0x7acf
movq 0x8(%r12), %rax
movl 0x10(%rax), %r15d
movq %r14, %rdi
movl %ebx, %esi
callq 0x5156
subl %eax, %r15d
movq -0x38(%rbp), %rax
cmpl %eax, %r15d
jl 0x7acf
movq %r12, -0x40(%rbp)
pushq $-0x1
popq %r15
pushq $0x8
popq %rcx
movq 0x8(%r14), %r8
movq 0x10(%r14), %rax
subq %r8, %rax
cqto
pushq $0xc
popq %rsi
idivq %rsi
incq %r15
cmpq %r15, %rax
jbe 0x7ae4
leaq 0xc(%rcx), %r12
movl -0x8(%r8,%rcx), %edi
movl -0x4(%r8,%rcx), %esi
movl (%r8,%rcx), %r13d
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl %ebx, %r8d
callq 0x50a8
movq -0x48(%rbp), %rcx
leal (%rax,%rcx), %esi
shrq $0x20, %rax
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x38(%rbp), %rcx
leal (%rax,%rcx), %edx
callq 0x3b66
movq %r12, %rcx
cmpl %eax, %r13d
je 0x7a6d
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %r15
xorl %r12d, %r12d
movq 0x20(%r14), %rcx
movq 0x28(%r14), %rax
subq %rcx, %rax
cqto
pushq $0xc
popq %rsi
idivq %rsi
cmpq %r12, %rax
jbe 0x7b78
movl -0x8(%rcx,%r15), %edi
movl -0x4(%rcx,%r15), %esi
movl (%rcx,%r15), %r13d
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl %ebx, %r8d
callq 0x50f8
cmpl $0x7, %ebx
ja 0x7b6c
movq %rax, %rdx
shrq $0x20, %rdx
pushq $0x33
popq %rcx
btl %ebx, %ecx
jae 0x7b4c
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x48(%rbp), %rcx
leal (%rax,%rcx), %esi
addl -0x38(%rbp), %edx
callq 0x3baa
jmp 0x7b63
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x48(%rbp), %rcx
leal (%rax,%rcx), %esi
addl -0x38(%rbp), %edx
callq 0x3bee
cmpl %eax, %r13d
jne 0x7acf
incq %r12
addq $0xc, %r15
jmp 0x7aeb
pushq $0x8
popq %r12
xorl %edi, %edi
movq 0x38(%r14), %rcx
movq 0x40(%r14), %rax
subq %rcx, %rax
cqto
pushq $0xc
popq %rsi
idivq %rsi
cmpq %rdi, %rax
setbe %r15b
jbe 0x7ad2
movq %rdi, -0x50(%rbp)
movl -0x8(%rcx,%r12), %edi
movl -0x4(%rcx,%r12), %esi
movl (%rcx,%r12), %r13d
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl %ebx, %r8d
callq 0x5104
cmpl $0x7, %ebx
ja 0x7c0b
movq %rax, %rdx
shrq $0x20, %rdx
pushq $0x33
popq %rcx
btl %ebx, %ecx
jae 0x7beb
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x48(%rbp), %rcx
leal (%rax,%rcx), %esi
addl -0x38(%rbp), %edx
callq 0x3bee
jmp 0x7c02
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x48(%rbp), %rcx
leal (%rax,%rcx), %esi
addl -0x38(%rbp), %edx
callq 0x3baa
cmpl %eax, %r13d
jne 0x7ad2
movq -0x50(%rbp), %rdi
incq %rdi
addq $0xc, %r12
jmp 0x7b7e
nop