// File: davidjosepha[P]slitherlink/src/shared/grid.cpp
// Function: Grid::updateContourMatrix(int, int, bool)
void Grid::updateContourMatrix(int i, int j, bool hline) {

    int i2 = i;
    int j2 = j;

    // The second endpoint of the new line is determined by whether the line is
    // horizontal or vertical
    if (hline) {
        j2 = j + 1;
    } else {
        i2 = i + 1;
    }

    /* Both ends of the new line are already endpoints to a single contour.
       So get rid of both open endpoints and add one count of a closed loop. */
    if (getContourMatrix(i,j).first == i2 && getContourMatrix(i,j).second == j2 && getContourMatrix(i2,j2).first == i && getContourMatrix(i2,j2).second == j) {
        setContourMatrix(i,j,std::make_pair(-1,-1));
        setContourMatrix(i2,j2,std::make_pair(-1,-1));
        numClosedLoops_++;
        numOpenLoops_--;
    }
    /* Both ends of the new line are already endpoints to two different
     * conoturs. Get rid of the open endpoints, update the new ends of the
     * merged contour, and count one less open contour */
    else if (getContourMatrix(i,j).first != -1 && getContourMatrix(i2,j2).first != -1) {
        setContourMatrix(getContourMatrix(i,j).first,getContourMatrix(i,j).second,getContourMatrix(i2,j2));
        setContourMatrix(getContourMatrix(i2,j2).first,getContourMatrix(i2,j2).second,getContourMatrix(i,j));
        setContourMatrix(i,j,std::make_pair(-1,-1));
        setContourMatrix(i2,j2,std::make_pair(-1,-1));
        numOpenLoops_--;
    }
    /* First end of the new line is already an endpoint to a contour. Extend
     * the contour and update new endpoints. */
    else if (getContourMatrix(i,j).first != -1) {
        setContourMatrix(getContourMatrix(i,j).first,getContourMatrix(i,j).second,std::make_pair(i2,j2));
        setContourMatrix(i2,j2,getContourMatrix(i,j));
        setContourMatrix(i,j,std::make_pair(-1,-1));
    }
    /* Second end of the new line is already an endpoint to a contour. Extend
     * the contour and update new endpoints. */
    else if (getContourMatrix(i2,j2).first != -1) {
        setContourMatrix(getContourMatrix(i2,j2).first,getContourMatrix(i2,j2).second,std::make_pair(i,j));
        setContourMatrix(i,j,getContourMatrix(i2,j2));
        setContourMatrix(i2,j2,std::make_pair(-1,-1));
    }
    /* Neither end of new line is shared by a contour, so create a new contour
     * with endpoints (i,j) and (i,j+1) */
    else {
        setContourMatrix(i,j,std::make_pair(i2,j2));
        setContourMatrix(i2,j2,std::make_pair(i,j));
        numOpenLoops_++;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movb %cl, %al
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x148(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x1c(%rbp)
testb $0x1, -0x11(%rbp)
je 0x3ba4
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x3bad
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq %rax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
xorl %eax, %eax
cmpl -0x18(%rbp), %ecx
movb %al, -0x149(%rbp)
jne 0x3c44
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq %rax, -0x2c(%rbp)
movl -0x28(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x149(%rbp)
jne 0x3c44
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq %rax, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl -0xc(%rbp), %ecx
movb %al, -0x149(%rbp)
jne 0x3c44
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq %rax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x10(%rbp), %eax
sete %al
movb %al, -0x149(%rbp)
movb -0x149(%rbp), %al
testb $0x1, %al
jne 0x3c53
jmp 0x3d0b
movl -0xc(%rbp), %eax
movl %eax, -0x15c(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x158(%rbp)
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
leaq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x49b0
movl -0x15c(%rbp), %esi
movl -0x158(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x44(%rbp)
movq -0x44(%rbp), %rcx
callq 0x4970
movl -0x18(%rbp), %eax
movl %eax, -0x154(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x150(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
leaq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x49b0
movl -0x154(%rbp), %esi
movl -0x150(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x54(%rbp)
movq -0x54(%rbp), %rcx
callq 0x4970
movq -0x148(%rbp), %rax
movl 0x50(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x50(%rax)
movl 0x4c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4c(%rax)
jmp 0x426e
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq %rax, -0x64(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0x64(%rbp)
movb %al, -0x15d(%rbp)
je 0x3d52
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq %rax, -0x6c(%rbp)
cmpl $-0x1, -0x6c(%rbp)
setne %al
movb %al, -0x15d(%rbp)
movb -0x15d(%rbp), %al
testb $0x1, %al
jne 0x3d61
jmp 0x3f2c
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq -0x148(%rbp), %rdi
movq %rax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x180(%rbp)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq -0x148(%rbp), %rdi
movq %rax, -0x7c(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x17c(%rbp)
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movl -0x180(%rbp), %esi
movl -0x17c(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x84(%rbp)
movq -0x84(%rbp), %rcx
callq 0x4970
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq -0x148(%rbp), %rdi
movq %rax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x178(%rbp)
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq -0x148(%rbp), %rdi
movq %rax, -0x94(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x174(%rbp)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movl -0x178(%rbp), %esi
movl -0x174(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x9c(%rbp)
movq -0x9c(%rbp), %rcx
callq 0x4970
movl -0xc(%rbp), %eax
movl %eax, -0x170(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x16c(%rbp)
movl $0xffffffff, -0xa8(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xac(%rbp) # imm = 0xFFFFFFFF
leaq -0xa8(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0x49b0
movl -0x170(%rbp), %esi
movl -0x16c(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0xa4(%rbp)
movq -0xa4(%rbp), %rcx
callq 0x4970
movl -0x18(%rbp), %eax
movl %eax, -0x168(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x164(%rbp)
movl $0xffffffff, -0xb8(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
leaq -0xb8(%rbp), %rdi
leaq -0xbc(%rbp), %rsi
callq 0x49b0
movl -0x168(%rbp), %esi
movl -0x164(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0xb4(%rbp)
movq -0xb4(%rbp), %rcx
callq 0x4970
movq -0x148(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4c(%rax)
jmp 0x426c
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq %rax, -0xc4(%rbp)
cmpl $-0x1, -0xc4(%rbp)
je 0x407d
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq -0x148(%rbp), %rdi
movq %rax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, -0x198(%rbp)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movq %rax, -0xd4(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0x194(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x4ae0
movl -0x198(%rbp), %esi
movl -0x194(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0xdc(%rbp)
movq -0xdc(%rbp), %rcx
callq 0x4970
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, -0x190(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x18c(%rbp)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x4ab0
movl -0x190(%rbp), %esi
movl -0x18c(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0xe4(%rbp)
movq -0xe4(%rbp), %rcx
callq 0x4970
movl -0xc(%rbp), %eax
movl %eax, -0x188(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x184(%rbp)
movl $0xffffffff, -0xf0(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xf4(%rbp) # imm = 0xFFFFFFFF
leaq -0xf0(%rbp), %rdi
leaq -0xf4(%rbp), %rsi
callq 0x49b0
movq -0x148(%rbp), %rdi
movl -0x188(%rbp), %esi
movl -0x184(%rbp), %edx
movq %rax, -0xec(%rbp)
movq -0xec(%rbp), %rcx
callq 0x4970
jmp 0x426a
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq %rax, -0xfc(%rbp)
cmpl $-0x1, -0xfc(%rbp)
je 0x41ce
movq -0x148(%rbp), %rdi
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq -0x148(%rbp), %rdi
movq %rax, -0x104(%rbp)
movl -0x104(%rbp), %eax
movl %eax, -0x1b0(%rbp)
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movq %rax, -0x10c(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0x1ac(%rbp)
leaq -0xc(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x4ae0
movl -0x1b0(%rbp), %esi
movl -0x1ac(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x114(%rbp)
movq -0x114(%rbp), %rcx
callq 0x4970
movq -0x148(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x1a8(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x1a4(%rbp)
movl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x4ab0
movl -0x1a8(%rbp), %esi
movl -0x1a4(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x11c(%rbp)
movq -0x11c(%rbp), %rcx
callq 0x4970
movl -0x18(%rbp), %eax
movl %eax, -0x1a0(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x19c(%rbp)
movl $0xffffffff, -0x128(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x12c(%rbp) # imm = 0xFFFFFFFF
leaq -0x128(%rbp), %rdi
leaq -0x12c(%rbp), %rsi
callq 0x49b0
movq -0x148(%rbp), %rdi
movl -0x1a0(%rbp), %esi
movl -0x19c(%rbp), %edx
movq %rax, -0x124(%rbp)
movq -0x124(%rbp), %rcx
callq 0x4970
jmp 0x4268
movl -0xc(%rbp), %eax
movl %eax, -0x1c0(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x1bc(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x4ae0
movl -0x1c0(%rbp), %esi
movl -0x1bc(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x134(%rbp)
movq -0x134(%rbp), %rcx
callq 0x4970
movl -0x18(%rbp), %eax
movl %eax, -0x1b8(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x1b4(%rbp)
leaq -0xc(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x4ae0
movl -0x1b8(%rbp), %esi
movl -0x1b4(%rbp), %edx
movq -0x148(%rbp), %rdi
movq %rax, -0x13c(%rbp)
movq -0x13c(%rbp), %rcx
callq 0x4970
movq -0x148(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
jmp 0x426a
jmp 0x426c
jmp 0x426e
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw (%rax,%rax)