// File: /workspace/github_cpp_makefile_star2_O1/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 0x3356
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 0x3356
movq $-0x1, %rdx
movq %rdx, (%rcx,%rax,8)
movq %rdx, (%r14,%r8,8)
incl 0x50(%rdi)
jmp 0x3424
movl %r11d, %r14d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %r14
jne 0x338b
movq (%r9,%rbx,8), %rdx
movq (%rdx,%r8,8), %r11
cmpl $-0x1, %r11d
jne 0x33c9
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 0x3427
movq (%r9,%rbx,8), %rdx
movq (%rdx,%r8,8), %r15
cmpl $-0x1, %r15d
jne 0x33ef
cmpl $-0x1, %r14d
je 0x3363
movslq %r11d, %rdx
movq (%r9,%rdx,8), %rdx
sarq $0x20, %r11
movl %ebx, (%rdx,%r11,8)
movl %r8d, 0x4(%rdx,%r11,8)
movq (%rcx,%rax,8), %rdx
movq (%r9,%rbx,8), %rsi
movq %rdx, (%rsi,%r8,8)
movq $-0x1, (%rcx,%rax,8)
jmp 0x3427
movslq %r11d, %rdi
movq (%r9,%rdi,8), %rdi
sarq $0x20, %r11
movl %esi, (%rdi,%r11,8)
movl %eax, 0x4(%rdi,%r11,8)
movq (%rdx,%r8,8), %rsi
movq %rsi, (%rcx,%rax,8)
movq $-0x1, (%rdx,%r8,8)
jmp 0x3427
movslq %r11d, %rsi
movq (%r9,%rsi,8), %rsi
sarq $0x20, %r11
movq %r15, (%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)
movq $-0x1, %rsi
movq %rsi, (%rcx,%rax,8)
movq %rsi, (%rdx,%r8,8)
decl 0x4c(%rdi)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq