// File: /workspace/github_cpp_makefile_star2_O2/realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyCycle::AddLineId(PolyDetector&, unsigned int)
bool PolyCycle::AddLineId(PolyDetector &pd, uint32_t id)
{
    auto l = pd.findLine(id);
    if (!l)
    {
        logoutf("findLine(%u) failed!", id);
        return false;
    }
    
    if (l->took >= 2)
    {
        if (pd.verbose > 3)
            logoutf("line %u can't be added to cycle! tooked %u times!", id, l->took);
        return false;
    }
    
    l->test0 = 0; // a cnt
    l->test1 = 0; // b cnt
    
    bool shareA = false, shareB = false;
    bool collinear = false;
    
    for (auto &id1 : idx)
    {
        auto l1 = pd.findLine(id1);
        if (!l1) continue;
        
        shareA = l->aIdx == l1->aIdx || l->aIdx == l1->bIdx;
        shareB = l->bIdx == l1->aIdx || l->bIdx == l1->bIdx;
        collinear = false;

        // share a point
        if (shareA)
            l->test0++;
        
        // share b point
        if (shareB)
            l->test1++;
        
        if (l->test0 >= 2 || l->test1 >= 2)
        {
            if (pd.verbose > 2)
                logoutf("line %u can't be added to cycle! ta:%d tb:%d ids: [%s]", id, l->test0, l->test1, idxToString().c_str());
            return false;
        }
        
        if (shareA || shareB)
        {
            collinear = pd.CollinearIdx(*l, *l1);
            if (pd.verbose > 2)
                logoutf("l(%u), l1(%u) sa:%d sb:%d col:%d", l->id, l1->id, shareA, shareB, collinear);
            if (collinear)
            {
                if (pd.verbose > 2)
                    logoutf("line %u can't be added to cycle! colliniar with %u sa:%d sb:%d", id, id1, shareA, shareB);
                return false;
            }
        }
        
        for (auto &id2 : idx)
        {
            if (id2 != id && id2 != id1
                //&& !pd.CollinearIdx(id2, id) && !pd.CollinearIdx(id2, id1)
            )
            {
                auto l2 = pd.findLine(id2);
                
                if (l2 && doIntersect(l->center, l1->center, l2->a, l2->b))
                //if (l2 && !Collinear(ls, *l2) && ls.PolyIntersects(*l2))
                {
                    if (pd.verbose > 2)
                        logoutf("line %u can't be added to cycle (coll test)! id2:%u intersects with middle(%u, %u)", id, id2, l->id, l1->id);
                    return false;
                }
            }
        }
    }
    
#if 1 // brute force
    for (auto &pid : {l->aIdx, l->bIdx})
    {
        auto &pl = pd.pointToLines[pid];
        if (pl.size() >= 2)
        {
            for (auto it1 = pl.begin(); it1 != pl.end(); ++it1)
            {
                auto &nlid1 = *it1;

                if (nlid1 != id && contains(nlid1))
                {
                    for (auto it2 = pl.begin(); it2 != pl.end(); ++it2)
                    {
                        if (*it1 == *it2) continue;
                        
                        auto nlid2 = *it2;
                        
                        if (nlid2 != id && !contains(nlid2))
                        {
                            //auto l1 = pd.findLine(nlid1, false);
                            auto l1 = pd.findLine(nlid1, false);
                            auto l2 = pd.findLine(nlid2);
                            if (l1 && l2)
                            {
                                //logoutf("checking lines %u %u %u in P%u", l->id, l1->id, l2->id, pid);
                                if (l2->betweenNeighbors(pd, *l1, *l))
                                    return false;
                            }
                        }
                    }
                }
            }
        }
    }
#endif
    
    
    idx.insert(id);
    //l->processed = id;
    lastIdx = id;
    l->processed = startIdx + 1;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
movl %edx, 0x2c(%rsp)
pushq $0x1
popq %rdx
movq %rsi, %rdi
movl %ebx, %esi
callq 0x46ec
testq %rax, %rax
je 0x5b38
movq %rax, %r14
movl 0x78(%rax), %edx
cmpl $0x2, %edx
jb 0x5b4f
cmpl $0x4, 0x60(%r12)
jb 0x5ece
leaq 0x7e00(%rip), %rdi      # 0xd928
xorl %ebp, %ebp
movl %ebx, %esi
xorl %eax, %eax
callq 0x2040
jmp 0x5ed0
leaq 0x7dd3(%rip), %rdi      # 0xd912
xorl %ebp, %ebp
movl %ebx, %esi
xorl %eax, %eax
callq 0x2040
jmp 0x5ed0
andq $0x0, 0x64(%r14)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r13
leaq 0x8(%rax), %rbp
movq %r14, %rax
addq $0x58, %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x8(%rsp)
movq %r12, 0x10(%rsp)
cmpq %rbp, %r13
je 0x5d6f
movl 0x20(%r13), %esi
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x46ec
movq %rax, (%rsp)
testq %rax, %rax
je 0x5cd0
movq 0x8(%rsp), %rdx
movl 0x80(%rdx), %ecx
movq (%rsp), %rax
movl 0x80(%rax), %eax
movb $0x1, %r15b
movb $0x1, %r12b
cmpl %eax, %ecx
je 0x5bc9
movq (%rsp), %rsi
cmpl 0x84(%rsi), %ecx
sete %r12b
movl 0x84(%rdx), %ecx
cmpl %eax, %ecx
je 0x5be1
movq (%rsp), %rax
cmpl 0x84(%rax), %ecx
sete %r15b
testb %r12b, %r12b
je 0x5be9
incl 0x64(%rdx)
testb %r15b, %r15b
je 0x5bf1
incl 0x68(%rdx)
movl 0x64(%rdx), %r14d
cmpl $0x1, %r14d
ja 0x5d1c
cmpl $0x2, 0x68(%rdx)
jae 0x5d1c
movl %r12d, %eax
orb %r15b, %al
testb $0x1, %al
je 0x5c6f
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
callq 0x5f0e
movl %eax, %ecx
cmpl $0x3, 0x60(%r14)
movzbl %r12b, %r14d
movzbl %r15b, %r15d
jb 0x5c67
movq 0x8(%rsp), %rax
movl 0x60(%rax), %esi
movq (%rsp), %rax
movl 0x60(%rax), %edx
movzbl %cl, %r9d
leaq 0x7d3f(%rip), %rdi      # 0xd993
movl %ecx, %r12d
movl %r14d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x2040
movl %r12d, %ecx
testb %cl, %cl
jne 0x5ea9
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r14
movq (%rsp), %r15
addq $0x58, %r15
movq 0x10(%rsp), %r12
cmpq %rbp, %r14
je 0x5cd0
movl 0x20(%r14), %esi
cmpl %ebx, %esi
je 0x5cc3
cmpl 0x20(%r13), %esi
je 0x5cc3
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x46ec
testq %rax, %rax
je 0x5cc3
movq %rax, %rcx
addq $0x8, %rcx
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2419
testb %al, %al
jne 0x5ce5
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
jmp 0x5c85
movq %r13, %rdi
callq 0x2160
movq %rax, %r13
movq 0x8(%rsp), %r14
jmp 0x5b77
cmpl $0x3, 0x60(%r12)
jb 0x5ece
movl 0x20(%r14), %edx
movq 0x8(%rsp), %rax
movl 0x60(%rax), %ecx
movq (%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x7ce9(%rip), %rdi      # 0xd9f5
xorl %ebp, %ebp
movl %ebx, %esi
xorl %eax, %eax
callq 0x2040
jmp 0x5ed0
movq 0x10(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x5ece
movq 0x8(%rsp), %rax
movl 0x68(%rax), %r15d
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x8970
movq (%r12), %r8
leaq 0x7c0a(%rip), %rdi      # 0xd95b
xorl %ebp, %ebp
movl %ebx, %esi
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2040
movq %r12, %rdi
callq 0x20b0
jmp 0x5ed0
movq 0x80(%r14), %rax
movq %rax, 0x38(%rsp)
leaq 0xe0(%r12), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
cmpq $0x8, %rax
je 0x5ee1
movq %rax, (%rsp)
leaq (%rsp,%rax), %rsi
addq $0x38, %rsi
movq 0x30(%rsp), %rdi
callq 0x875e
movq %rax, %r15
movq (%rax), %rbp
movq 0x8(%rax), %r12
movq %r12, %rax
subq %rbp, %rax
cmpq $0x5, %rax
jb 0x5e9c
cmpq %r12, %rbp
je 0x5e9c
movl (%rbp), %esi
cmpl %ebx, %esi
je 0x5e93
movq 0x18(%rsp), %rdi
callq 0x8a48
testb %al, %al
je 0x5e88
movq (%r15), %r12
movq %rbp, 0x20(%rsp)
cmpq 0x8(%r15), %r12
je 0x5e8e
movl (%r12), %r13d
cmpl %r13d, (%rbp)
sete %al
cmpl %ebx, %r13d
sete %cl
orb %al, %cl
jne 0x5e7f
movq 0x18(%rsp), %rdi
movl %r13d, %esi
callq 0x8a48
testb %al, %al
jne 0x5e7f
movl (%rbp), %esi
movl %ebx, %ebp
movq %r15, %rbx
movq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x46ec
movq %rax, %r14
movq %r15, %rdi
movq %rbx, %r15
movl %ebp, %ebx
movq 0x20(%rsp), %rbp
movl %r13d, %esi
pushq $0x1
popq %rdx
callq 0x46ec
testq %r14, %r14
sete %cl
testq %rax, %rax
sete %sil
orb %cl, %sil
jne 0x5e7f
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
callq 0x5f1a
testb %al, %al
jne 0x5ece
addq $0x4, %r12
jmp 0x5df2
movq 0x8(%r15), %r12
jmp 0x5e93
movq 0x8(%rsp), %r14
addq $0x4, %rbp
jmp 0x5dc4
movq (%rsp), %rax
addq $0x4, %rax
jmp 0x5d8a
movq 0x10(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x5ece
movl 0x20(%r13), %edx
leaq 0x7af6(%rip), %rdi      # 0xd9b5
xorl %ebp, %ebp
movl %ebx, %esi
movl %r14d, %ecx
movl %r15d, %r8d
jmp 0x5d10
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c(%rsp), %rbx
movq %r14, %r15
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa77a
movl (%rbx), %eax
movl %eax, 0x34(%r14)
movl 0x30(%r14), %eax
incl %eax
movl %eax, 0x7c(%r15)
movb $0x1, %bpl
jmp 0x5ed0