// File: 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
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rdi
callq 0xfda0
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0xfdb0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, -0x2c(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, -0x94(%rbp)
movq 0x8(%rax), %rdi
callq 0x2ad0
movl %eax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xab50
movl -0x98(%rbp), %ecx
movl %eax, %edx
movl -0x94(%rbp), %eax
subl %edx, %ecx
cmpl %ecx, %eax
jg 0xf4d0
movq -0xa0(%rbp), %rax
movl -0x18(%rbp), %ecx
movl %ecx, -0xa4(%rbp)
movq 0x8(%rax), %rdi
callq 0x2af0
movl %eax, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xabc0
movl -0xa8(%rbp), %ecx
movl %eax, %edx
movl -0xa4(%rbp), %eax
subl %edx, %ecx
cmpl %ecx, %eax
jle 0xf4d9
movb $0x0, -0x1(%rbp)
jmp 0xf816
movq -0x20(%rbp), %rdi
callq 0xfe70
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x81f0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jae 0xf594
movq -0x38(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0xfe90
movq (%rax), %rcx
movq %rcx, -0x48(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x40(%rbp)
movl -0x48(%rbp), %edi
movl -0x44(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0xa990
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq %rcx, -0x50(%rbp)
movl -0x40(%rbp), %ecx
movl %ecx, -0xb4(%rbp)
movq 0x8(%rax), %rdi
movl -0x50(%rbp), %esi
addl -0x14(%rbp), %esi
movl -0x4c(%rbp), %edx
addl -0x18(%rbp), %edx
callq 0x5b50
movl %eax, %ecx
movl -0xb4(%rbp), %eax
cmpl %ecx, %eax
je 0xf584
movb $0x0, -0x1(%rbp)
jmp 0xf816
jmp 0xf586
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xf4ed
movq -0x20(%rbp), %rdi
callq 0xfeb0
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x58(%rbp), %rdi
callq 0x79f0
movq %rax, %rcx
movq -0xc0(%rbp), %rax
cmpq %rcx, %rax
jae 0xf6ca
movq -0x58(%rbp), %rdi
movslq -0x5c(%rbp), %rsi
callq 0xfdf0
movl 0x8(%rax), %ecx
movl %ecx, -0x60(%rbp)
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movl -0x68(%rbp), %edi
movl -0x64(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0xaa60
movq %rax, -0x70(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xc4(%rbp)
subl $0x2, %eax
jb 0xf642
jmp 0xf612
movl -0xc4(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0xf67e
jmp 0xf622
movl -0xc4(%rbp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0xf642
jmp 0xf632
movl -0xc4(%rbp), %eax
addl $-0x6, %eax
subl $0x2, %eax
jb 0xf67e
jmp 0xf6ba
movq -0xa0(%rbp), %rax
movl -0x60(%rbp), %ecx
movl %ecx, -0xc8(%rbp)
movq 0x8(%rax), %rdi
movl -0x70(%rbp), %esi
addl -0x14(%rbp), %esi
movl -0x6c(%rbp), %edx
addl -0x18(%rbp), %edx
callq 0x5bf0
movl %eax, %ecx
movl -0xc8(%rbp), %eax
cmpl %ecx, %eax
je 0xf67c
movb $0x0, -0x1(%rbp)
jmp 0xf816
jmp 0xf6ba
movq -0xa0(%rbp), %rax
movl -0x60(%rbp), %ecx
movl %ecx, -0xcc(%rbp)
movq 0x8(%rax), %rdi
movl -0x70(%rbp), %esi
addl -0x14(%rbp), %esi
movl -0x6c(%rbp), %edx
addl -0x18(%rbp), %edx
callq 0x5c90
movl %eax, %ecx
movl -0xcc(%rbp), %eax
cmpl %ecx, %eax
je 0xf6b8
movb $0x0, -0x1(%rbp)
jmp 0xf816
jmp 0xf6ba
jmp 0xf6bc
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xf5a8
movq -0x20(%rbp), %rdi
callq 0xfed0
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movslq -0x7c(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x78(%rbp), %rdi
callq 0x79f0
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0xf812
movq -0x78(%rbp), %rdi
movslq -0x7c(%rbp), %rsi
callq 0xfdf0
movl 0x8(%rax), %ecx
movl %ecx, -0x80(%rbp)
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movl -0x88(%rbp), %edi
movl -0x84(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0xaaa0
movq %rax, -0x90(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xdc(%rbp)
subl $0x2, %eax
jb 0xf784
jmp 0xf754
movl -0xdc(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0xf7c3
jmp 0xf764
movl -0xdc(%rbp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0xf784
jmp 0xf774
movl -0xdc(%rbp), %eax
addl $-0x6, %eax
subl $0x2, %eax
jb 0xf7c3
jmp 0xf802
movq -0xa0(%rbp), %rax
movl -0x80(%rbp), %ecx
movl %ecx, -0xe0(%rbp)
movq 0x8(%rax), %rdi
movl -0x90(%rbp), %esi
addl -0x14(%rbp), %esi
movl -0x8c(%rbp), %edx
addl -0x18(%rbp), %edx
callq 0x5c90
movl %eax, %ecx
movl -0xe0(%rbp), %eax
cmpl %ecx, %eax
je 0xf7c1
movb $0x0, -0x1(%rbp)
jmp 0xf816
jmp 0xf802
movq -0xa0(%rbp), %rax
movl -0x80(%rbp), %ecx
movl %ecx, -0xe4(%rbp)
movq 0x8(%rax), %rdi
movl -0x90(%rbp), %esi
addl -0x14(%rbp), %esi
movl -0x8c(%rbp), %edx
addl -0x18(%rbp), %edx
callq 0x5bf0
movl %eax, %ecx
movl -0xe4(%rbp), %eax
cmpl %ecx, %eax
je 0xf800
movb $0x0, -0x1(%rbp)
jmp 0xf816
jmp 0xf802
jmp 0xf804
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xf6de
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)