// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %ecx, %r8d
andl $0x1, %r8d
addl %edx, %r8d
notb %cl
movzbl %cl, %r10d
andl $0x1, %r10d
addl %esi, %r10d
movq 0x40(%rdi), %r9
movslq %esi, %rax
movq (%r9,%rax,8), %rcx
movslq %edx, %rax
movq (%rcx,%rax,8), %r11
movl %r11d, %ebx
xorl %r10d, %ebx
movq %r11, %r14
shrq $0x20, %r14
xorl %r8d, %r14d
orl %ebx, %r14d
movslq %r10d, %rbx
movslq %r8d, %r8
jne 0x2f27
movq (%r9,%rbx,8), %r14
movq (%r14,%r8,8), %r15
movl %r15d, %r12d
xorl %esi, %r12d
shrq $0x20, %r15
xorl %edx, %r15d
orl %r12d, %r15d
jne 0x2f27
orq $-0x1, (%rcx,%rax,8)
orq $-0x1, (%r14,%r8,8)
incl 0x50(%rdi)
jmp 0x2fcc
movq (%r9,%rbx,8), %rdx
movq (%rdx,%r8,8), %rbx
movl %ebx, %r14d
cmpl $-0x1, %r11d
jne 0x2f55
cmpl $-0x1, %r14d
jne 0x2f7e
movl %r10d, (%rcx,%rax,8)
movl %r8d, 0x4(%rcx,%rax,8)
movl %esi, (%rdx,%r8,8)
movl %eax, 0x4(%rdx,%r8,8)
incl 0x4c(%rdi)
jmp 0x2fcf
movslq %r11d, %rsi
cmpl $-0x1, %r14d
jne 0x2f9f
movq (%r9,%rsi,8), %rsi
sarq $0x20, %r11
movl %r10d, (%rsi,%r11,8)
movl %r8d, 0x4(%rsi,%r11,8)
movq (%rcx,%rax,8), %rsi
movq %rsi, (%rdx,%r8,8)
orq $-0x1, (%rcx,%rax,8)
jmp 0x2fcf
movslq %ebx, %rdi
movq (%r9,%rdi,8), %rdi
sarq $0x20, %rbx
movl %esi, (%rdi,%rbx,8)
movl %eax, 0x4(%rdi,%rbx,8)
movq (%rdx,%r8,8), %rsi
movq %rsi, (%rcx,%rax,8)
orq $-0x1, (%rdx,%r8,8)
jmp 0x2fcf
movq (%r9,%rsi,8), %rsi
sarq $0x20, %r11
movq %rbx, (%rsi,%r11,8)
movq (%rdx,%r8,8), %rsi
movq (%rcx,%rax,8), %r10
movslq %esi, %r11
movq (%r9,%r11,8), %r9
sarq $0x20, %rsi
movq %r10, (%r9,%rsi,8)
orq $-0x1, (%rcx,%rax,8)
orq $-0x1, (%rdx,%r8,8)
decl 0x4c(%rdi)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq