// File: /workspace/github_cpp_makefile_star2_O2/realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyDetector::dissolveCollinear(PolyLine&, PolyLine&)
bool PolyDetector::dissolveCollinear(PolyLine &l1, PolyLine &l2)
{
    if (verbose)
        logoutf("Dissolving collinear neighbors %s and %s", l1.toString(*this).c_str(), l2.toString(*this).c_str());
    
    assert(&l1 != &l2);
    
    uint32_t *p1 = nullptr, *p2 = nullptr;
    if (l1.aIdx == l2.aIdx || l1.aIdx == l2.bIdx)
    {
        p1 = &l1.bIdx; // other point (B)
        p2 = l1.aIdx == l2.aIdx ? &l2.bIdx : &l2.aIdx; // aidx is common point. other point
    }
    else if (l1.bIdx == l2.aIdx || l1.bIdx == l2.bIdx)
    {
        p1 = &l1.aIdx; // other point (A)
        p2 = l1.bIdx == l2.aIdx ? &l2.bIdx : &l2.aIdx; // bidx is common point. other point
    }
    if (!p1 || !p2)
    {
        assert(p1 && p2);
        return false;
    }

#if 1
    auto search = findLine(*p1, *p2, false);
    if (search)
    {
        logoutf("A line %s origLine:#%u already exists with P%u P%u!", search->toString(*this).c_str(), search->origLine, *p1, *p2);
        //assert(false);
        
        l1.setIgnore(*this, "rmCollinear.l1 (dup)");
        l2.setIgnore(*this, "rmCollinear.l2 (dup)");
        
        return false;
    }
#endif

    PolyLine nl;
    nl.id = uint32_t(lines.size());
    nl.aIdx = *p1;
    nl.bIdx = *p2;
    nl.a = intersectionPoints[nl.aIdx];
    nl.b = intersectionPoints[nl.bIdx];
    nl.calcCenter(); // calls CalculateFirstAndLastPoint
    nl.origLine = l1.origLine; // will be used to construct collinearLineMap
    nl.lastDissolveStep = l1.lastDissolveStep;
    nl.attr0 = l1.attr0;
    
    // build point to lines link
    addPointToLine(nl.aIdx, nl.id);
    addPointToLine(nl.bIdx, nl.id);
    
    // build neighbors
    for (auto &id : {l1.id, l2.id})
    {
        for (auto &n : _neighbors[id])
        {
            if (n != l1.id && n != l2.id)
            {
                auto l = findLine(n);
                if (l)
                {
                    _neighbors[nl.id].push_back(n);
                    _neighbors[n].push_back(nl.id);
                }
            }
        }
    }
    
    // set proper collinearities
    for (auto &kv : collinearLineMap)
    {
        if (kv.first == l1.id || kv.first == l2.id)
        {
            for (auto &c : kv.second)
                setCollinear(nl.id, c);
        }
    }
    
    // after neigh built!
    if (verbose)
        logoutf("ignore line %u and line %u", l1.id, l2.id);
    l1.setIgnore(*this, "rmCollinear.l1");
    l2.setIgnore(*this, "rmCollinear.l2");
    
    //return false; // test
    
    lineIdToIdx[nl.id] = uint32_t(lines.size());
    lines.push_back(nl);
    lines.back().sortNeigh(*this);
    if (verbose)
        logoutf("added new line %s by merging %u and %u", lines.back().toString(*this).c_str(), l1.id, l2.id);
    
    // it can be removed
    //dissolveCollinearLine(lines.back());
    
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r14
movq %rdi, %r15
cmpl $0x0, 0x60(%rdi)
je 0x692f
movq %rdx, %rbx
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4208
movq (%r12), %r12
leaq 0x28(%rsp), %rdi
movq %rbx, %rbp
movq %rbx, %rsi
movq %r15, %rdx
callq 0x4208
leaq 0x28(%rsp), %r13
movq (%r13), %rdx
leaq 0x7306(%rip), %rdi      # 0xdc16
movq %r12, %rsi
xorl %eax, %eax
callq 0x2040
movq %r13, %rdi
callq 0x20b0
leaq 0x48(%rsp), %rdi
callq 0x20b0
movq %rbp, %rdx
cmpq %rdx, %r14
je 0x6d21
movl $0x80, %r13d
movl 0x80(%r14), %ecx
leaq (%rdx,%r13), %r8
movl 0x80(%rdx), %eax
cmpl %eax, %ecx
movq %rdx, 0x8(%rsp)
je 0x6962
movl 0x84(%rdx), %ebp
cmpl %ebp, %ecx
jne 0x6988
movl $0x84, %edi
leaq (%r14,%rdi), %r13
addq %rdx, %rdi
xorl %esi, %esi
cmpl %eax, %ecx
sete %sil
cmoveq %rdi, %r8
movq %r8, 0x10(%rsp)
movl 0x80(%rdx,%rsi,4), %ebp
jmp 0x69bc
movl 0x84(%r14), %ecx
cmpl %eax, %ecx
setne %dil
cmpl %ebp, %ecx
setne %sil
testb %sil, %dil
jne 0x6d40
addq %r14, %r13
addq $0x84, %rdx
cmpl %eax, %ecx
cmovnel %eax, %ebp
cmoveq %rdx, %r8
movq %r8, 0x10(%rsp)
movl (%r13), %r12d
movq %r15, %rdi
movl %r12d, %esi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x5974
movq %rax, %rbx
testq %rax, %rax
je 0x6a3e
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x4208
movq (%r12), %rsi
movl 0x6c(%rbx), %edx
movl (%r13), %ecx
movq 0x10(%rsp), %rax
movl (%rax), %r8d
leaq 0x7282(%rip), %rdi      # 0xdc86
xorl %eax, %eax
callq 0x2040
movq %r12, %rdi
callq 0x20b0
leaq 0x72a1(%rip), %rdx      # 0xdcbb
movq %r14, %rdi
movq %r15, %rsi
callq 0x6d8c
leaq 0x72a4(%rip), %rdx      # 0xdcd0
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x6d8c
jmp 0x6d09
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
leaq 0x78(%rsp), %rbx
andq $0x0, -0x10(%rbx)
movups %xmm0, -0x20(%rbx)
andl $0x0, (%rbx)
andq $0x0, 0x8(%rbx)
movq %rbx, 0x10(%rbx)
movq %rbx, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
movaps 0x65e0(%rip), %xmm1     # 0xd050
movups %xmm1, 0x34(%rbx)
movb $0x0, 0x44(%rbx)
movups %xmm0, 0x48(%rbx)
andl $0x0, 0x58(%rbx)
movq 0x38(%r15), %rax
subq 0x30(%r15), %rax
movl $0x90, %ecx
cqto
idivq %rcx
movl %eax, 0x30(%rbx)
movl %r12d, 0x50(%rbx)
movl %ebp, 0x54(%rbx)
movq 0xc8(%r15), %rax
movq (%rax,%r12,8), %rcx
movq %rcx, -0x30(%rbx)
movl %ebp, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbx)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x2868
movl 0x88(%r14), %eax
movl %eax, 0x58(%rbx)
movq 0x6c(%r14), %rax
movq %rax, 0x6c(%r12)
movl 0x60(%r12), %edx
movl 0x50(%rbx), %esi
movq %r15, %rdi
callq 0x44ce
movl 0xa8(%rsp), %edx
movl 0xcc(%rsp), %esi
movq %r15, %rdi
callq 0x44ce
leaq 0x30(%r15), %rax
movq %rax, 0x18(%rsp)
movl 0x60(%r14), %eax
movl %eax, 0x28(%rsp)
movq 0x8(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x2c(%rsp)
leaq 0x68(%r15), %r13
xorl %eax, %eax
pushq $0x1
popq %r12
cmpq $0x8, %rax
je 0x6bba
movq %rax, 0x10(%rsp)
leaq (%rsp,%rax), %rsi
addq $0x28, %rsi
movq %r13, %rdi
callq 0x875e
movq (%rax), %rbp
movq 0x8(%rax), %rbx
cmpq %rbx, %rbp
je 0x6bac
movl (%rbp), %esi
cmpl 0x60(%r14), %esi
je 0x6ba6
movq 0x8(%rsp), %rax
cmpl 0x60(%rax), %esi
je 0x6ba6
movq %r15, %rdi
movl %r12d, %edx
callq 0x46ec
testq %rax, %rax
je 0x6ba6
movq %r13, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x875e
movq %rax, %rdi
movq %rbp, %rsi
callq 0x87ac
movq %r13, %rdi
movq %rbp, %rsi
callq 0x875e
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x87ac
addq $0x4, %rbp
jmp 0x6b48
movq 0x10(%rsp), %rax
addq $0x4, %rax
jmp 0x6b22
movq 0xb0(%r15), %r12
leaq 0xa0(%r15), %rbx
cmpq %rbx, %r12
je 0x6c17
movl 0x20(%r12), %eax
cmpl 0x60(%r14), %eax
je 0x6bef
movq 0x8(%rsp), %rcx
cmpl 0x60(%rcx), %eax
je 0x6bef
movq %r12, %rdi
callq 0x2100
movq %rax, %r12
jmp 0x6bc8
movq 0x28(%r12), %r13
movq 0x30(%r12), %rbp
cmpq %rbp, %r13
je 0x6be2
movl 0xa8(%rsp), %esi
movl (%r13), %edx
movq %r15, %rdi
callq 0x6526
addq $0x4, %r13
jmp 0x6bf9
cmpl $0x0, 0x60(%r15)
je 0x6c38
movl 0x60(%r14), %esi
movq 0x8(%rsp), %rax
movl 0x60(%rax), %edx
leaq 0x70b4(%rip), %rdi      # 0xdce5
xorl %eax, %eax
callq 0x2040
leaq 0x70c2(%rip), %rdx      # 0xdd01
movq %r14, %rdi
movq %r15, %rsi
callq 0x6d8c
leaq 0x70bf(%rip), %rdx      # 0xdd10
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x6d8c
movq 0x30(%r15), %rbx
movq 0x38(%r15), %r12
leaq 0x110(%r15), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xb2e4
movq %rax, %rcx
subq %rbx, %r12
movl $0x90, %esi
movq %r12, %rax
cqto
idivq %rsi
movl %eax, (%rcx)
leaq 0x48(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x8940
movq $-0x90, %rdi
addq 0x38(%r15), %rdi
movq %r15, %rsi
callq 0x6480
cmpl $0x0, 0x60(%r15)
movq 0x20(%rsp), %rbx
je 0x6cff
movq $-0x90, %rsi
addq 0x38(%r15), %rsi
leaq 0x28(%rsp), %rdi
movq %r15, %rdx
callq 0x4208
leaq 0x28(%rsp), %r15
movq (%r15), %rsi
movl 0x60(%r14), %edx
movq 0x8(%rsp), %rax
movl 0x60(%rax), %ecx
leaq 0x702f(%rip), %rdi      # 0xdd1f
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
callq 0x20b0
leaq 0x48(%rsp), %rdi
callq 0x8736
testq %rbx, %rbx
sete %al
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6884(%rip), %rdi      # 0xd5ac
leaq 0x63e5(%rip), %rsi      # 0xd114
leaq 0x6f0a(%rip), %rcx      # 0xdc40
movl $0x956, %edx            # imm = 0x956
callq 0x20f0
leaq 0x6f36(%rip), %rdi      # 0xdc7d
leaq 0x63c6(%rip), %rsi      # 0xd114
leaq 0x6eeb(%rip), %rcx      # 0xdc40
movl $0x965, %edx            # imm = 0x965
callq 0x20f0
jmp 0x6d76
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20b0
jmp 0x6d83
jmp 0x6d76
jmp 0x6d76
jmp 0x6d76
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x8736
movq %rbx, %rdi
callq 0x2200
nop