// File: /workspace/github_cpp_makefile_star2_O3/realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyDetector::dissolve()
bool PolyDetector::dissolve()
{
    //return false; // test
    //if (dissolveCount == 1) return false;
    //if (dissolveCount == 1) return false;
    
    if (verbose > 3)
    {
        for (auto &l : lines)
        {
            if (!l.ignore)
            {
                logoutf("DIS: %s", l.toString(*this).c_str());
            }
        }
    }
    
    uint32_t nLinesBefore = uint32_t(lines.size());
    uint32_t nIgnoredBefore = 0;
    for (auto &l : lines)
        if (l.ignore)
            nIgnoredBefore++;
    
    if (_cycles.empty())
    {
        if (verbose)
            logoutf("No need to dissolve for %u cycles", uint32_t(_cycles.size()));
        return false;
    }
    
    if (verbose)
        logoutf("Dissolve for %u cycles", uint32_t(_cycles.size()));
    
    rmLines(RmLinesType::PointConsumed);
    rmLines(RmLinesType::TakenTwice); // points taken twice
    rmLines(RmLinesType::NoPointNeigh);
    rmLines(RmLinesType::Collinear);
    
#if 0
    return false; // test
#endif
    
    uint32_t nIgnored = 0;
    uint32_t nValid = 0;
    for (auto &l : lines)
    {
        if (!l.ignore)
        {
            nValid++;
        }
        else
        {
            nIgnored++;
        }
    }
    if (verbose)
        logoutf("Remaining lines:%u/%u nIgnored:%u", nValid, nLinesBefore, nIgnored);
    
    if (!nValid)
    {
        if (verbose)
            logoutf1("No valid lines. Skipping ...");
        return false;
    }
    
    if (nIgnored == nIgnoredBefore)
    {
        if (verbose)
            logoutf1("No lines ignored. Skipping ...");
        return false;
    }

#if 0
    // reassign new lines
    LineVector newLines;
    newLines.reserve(lines.size());
    for (auto it = lines.begin(); it != lines.end(); ++it)
    {
        if (!it->ignore)
        {
            newLines.push_back(std::move(*it));
        }
    }
    lines = std::move(newLines);
    
    logoutf("Remaining lines:%u/%u (after filter)", uint32_t(lines.size()), nLinesBefore);
#endif
    
    //logoutf("intersectionPoints:%u", uint32_t(intersectionPoints.size())); // should remain the same
    // also clear prev cycles
    _cycles.clear();
        
#if 0 // test
    polys.clear();
#endif
    
    dissolveCount++;
    
    for (auto &l : lines)
    {
        if (!l.ignore)
            l.lastDissolveStep = dissolveCount;
        //l.took = 0;
        if (l.took > 2)
        {
            logoutf("WARN: line %s tooked %u times!", l.toString(*this).c_str(), l.took);
            //assert(false);
            //l.took = 2;
        }
    }
    
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
cmpl $0x4, 0x60(%rdi)
jb 0x6c44
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %r13
cmpq %r13, %r14
je 0x6c44
leaq 0x18(%rsp), %rbp
leaq 0x8(%rsp), %r15
leaq 0xa1a0(%rip), %r12      # 0x10da5
cmpb $0x0, 0x74(%r14)
jne 0x6c38
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4e48
movq 0x8(%rsp), %rsi
movq %r12, %rdi
xorl %eax, %eax
callq 0x2030
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6c38
callq 0x2140
addq $0x90, %r14
cmpq %r13, %r14
jne 0x6c05
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rcx
movq %rcx, %r12
xorl %r15d, %r15d
subq %rax, %r12
je 0x6c69
movzbl 0x74(%rax), %edx
addl %edx, %r15d
addq $0x90, %rax
cmpq %rcx, %rax
jne 0x6c57
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
movl 0x60(%rbx), %ecx
cmpq %rsi, %rax
je 0x6d33
testl %ecx, %ecx
je 0x6c95
subq %rax, %rsi
shrq $0x6, %rsi
leaq 0xa143(%rip), %rdi      # 0x10dd1
xorl %eax, %eax
callq 0x2030
movq %rbx, %rdi
movl $0x3, %esi
callq 0x8d60
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8d60
movq %rbx, %rdi
movl $0x2, %esi
callq 0x8d60
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8d60
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rcx
movl $0x0, %r14d
cmpq %rcx, %rax
je 0x6cf7
xorl %r14d, %r14d
xorl %ebp, %ebp
movzbl 0x74(%rax), %edx
addl %edx, %r14d
xorl $0x1, %edx
addl %edx, %ebp
addq $0x90, %rax
cmpq %rcx, %rax
jne 0x6ce0
cmpl $0x0, 0x60(%rbx)
je 0x6d1b
shrq $0x4, %r12
imull $0x38e38e39, %r12d, %edx # imm = 0x38E38E39
leaq 0xa0da(%rip), %rdi      # 0x10de9
movl %ebp, %esi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2030
testl %ebp, %ebp
je 0x6d4b
cmpl %r15d, %r14d
jne 0x6d70
cmpl $0x0, 0x60(%rbx)
je 0x6d5d
leaq 0xa2ec(%rip), %rdi      # 0x1101d
jmp 0x6d58
testl %ecx, %ecx
je 0x6d5d
leaq 0xa070(%rip), %rdi      # 0x10dae
xorl %ebp, %ebp
xorl %esi, %esi
xorl %eax, %eax
callq 0x2030
jmp 0x6d5f
cmpl $0x0, 0x60(%rbx)
je 0x6d5d
leaq 0xa2a8(%rip), %rdi      # 0x11000
callq 0x21d0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %r15
movq 0x8(%rbx), %r12
cmpq %r15, %r12
je 0x6d94
movq %r15, %r14
movq %r14, %rdi
callq 0xc2b6
addq $0x40, %r14
cmpq %r12, %r14
jne 0x6d7f
movq %r15, 0x8(%rbx)
incl 0x148(%rbx)
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %r13
movb $0x1, %bpl
cmpq %r13, %r14
je 0x6d5f
leaq 0x8(%rsp), %r15
leaq 0xa056(%rip), %r12      # 0x10e0c
cmpb $0x0, 0x74(%r14)
jne 0x6dca
movl 0x148(%rbx), %eax
movl %eax, 0x88(%r14)
cmpl $0x3, 0x78(%r14)
jb 0x6e06
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4e48
movq 0x8(%rsp), %rsi
movl 0x78(%r14), %edx
movq %r12, %rdi
xorl %eax, %eax
callq 0x2030
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x6e06
callq 0x2140
addq $0x90, %r14
cmpq %r13, %r14
jne 0x6db6
jmp 0x6d5f
nop