// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, 0x18(%rsp)
movl %edx, 0x4(%rsp)
movq %rsi, 0x10(%rsp)
movq %rsi, %rdi
movl %edx, %esi
movl $0x1, %edx
callq 0x5706
testq %rax, %rax
je 0x7125
movq %rax, %r13
movl 0x78(%rax), %edx
cmpl $0x2, %edx
jb 0x713e
movq 0x10(%rsp), %rax
cmpl $0x4, 0x60(%rax)
jb 0x7523
movl 0x4(%rsp), %esi
leaq 0x982d(%rip), %rdi      # 0x10944
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2030
jmp 0x7525
movl 0x4(%rsp), %esi
leaq 0x97fe(%rip), %rdi      # 0x1092e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2030
jmp 0x7525
movq $0x0, 0x64(%r13)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r12
leaq 0x8(%rax), %r14
cmpq %r14, %r12
movq %r13, 0x8(%rsp)
je 0x73af
movq %r13, %rax
addq $0x58, %rax
movq %rax, 0x20(%rsp)
movl 0x20(%r12), %esi
movq 0x10(%rsp), %rdi
movl $0x1, %edx
callq 0x5706
testq %rax, %rax
je 0x724f
movq %rax, %rbp
movl 0x80(%r13), %ecx
movl 0x80(%rax), %eax
movb $0x1, %r15b
movb $0x1, %bl
cmpl %eax, %ecx
je 0x71ac
cmpl 0x84(%rbp), %ecx
sete %bl
movl 0x84(%r13), %ecx
cmpl %eax, %ecx
je 0x71c1
cmpl 0x84(%rbp), %ecx
sete %r15b
testb %bl, %bl
je 0x71c9
incl 0x64(%r13)
testb %r15b, %r15b
je 0x71d2
incl 0x68(%r13)
movl 0x64(%r13), %r13d
cmpl $0x1, %r13d
ja 0x71e7
movq 0x8(%rsp), %rax
cmpl $0x2, 0x68(%rax)
jb 0x7259
movl $0x1, %ebx
movq 0x10(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x7352
movl 0x4(%rsp), %ebp
movq 0x8(%rsp), %rax
movl 0x68(%rax), %r15d
leaq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xb458
movq 0x38(%rsp), %r8
leaq 0x9754(%rip), %rdi      # 0x10977
movl %ebp, %esi
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2030
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x7352
callq 0x2140
jmp 0x7352
movl $0x3, %ebx
jmp 0x7357
movl %ebx, %eax
orb %r15b, %al
testb $0x1, %al
je 0x72eb
movq 0x8(%rsp), %rax
movl 0x60(%rax), %esi
movl 0x60(%rbp), %edx
movq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x8f3a
movl %eax, %edi
cmpl $0x3, 0x60(%r13)
jb 0x72b1
movq 0x8(%rsp), %rax
movl 0x60(%rax), %esi
movl 0x60(%rbp), %edx
movzbl %bl, %ecx
movzbl %r15b, %r8d
movzbl %dil, %r9d
movl %edi, %r13d
leaq 0x9708(%rip), %rdi      # 0x109af
xorl %eax, %eax
callq 0x2030
movl %r13d, %edi
testb %dil, %dil
je 0x72eb
movq 0x10(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x72df
movl 0x4(%rsp), %esi
movl 0x20(%r12), %edx
movzbl %bl, %ecx
movzbl %r15b, %r8d
leaq 0x96f9(%rip), %rdi      # 0x109d1
xorl %eax, %eax
callq 0x2030
movq 0x8(%rsp), %r13
movl $0x1, %ebx
jmp 0x7357
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r13
xorl %ebx, %ebx
cmpq %r14, %r13
je 0x7352
movq %rbp, %r15
addq $0x58, %r15
movl 0x20(%r13), %esi
cmpl 0x4(%rsp), %esi
je 0x7342
cmpl 0x20(%r12), %esi
je 0x7342
movq 0x10(%rsp), %rdi
movl $0x1, %edx
callq 0x5706
testq %rax, %rax
je 0x7342
movq %rax, %rcx
addq $0x8, %rcx
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2501
testb %al, %al
jne 0x737a
movq %r13, %rdi
callq 0x2180
movq %rax, %r13
cmpq %r14, %rax
jne 0x7302
movq 0x8(%rsp), %r13
cmpl $0x3, %ebx
je 0x7364
testl %ebx, %ebx
jne 0x7523
movq %r12, %rdi
callq 0x2180
movq %rax, %r12
cmpq %r14, %rax
jne 0x716d
jmp 0x73af
movl $0x1, %ebx
movq 0x10(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x7352
movl 0x4(%rsp), %esi
movl 0x20(%r13), %edx
movq 0x8(%rsp), %r13
movl 0x60(%r13), %ecx
movl 0x60(%rbp), %r8d
leaq 0x966b(%rip), %rdi      # 0x10a11
xorl %eax, %eax
callq 0x2030
jmp 0x7357
movq 0x80(%r13), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %rax
addq $0xe0, %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
leaq (%rsp,%rax), %rsi
addq $0x38, %rsi
movq 0x28(%rsp), %rdi
callq 0xb3c8
movq %rax, %r13
movq (%rax), %rbx
movq 0x8(%rax), %rax
subq %rbx, %rax
sete %cl
cmpq $0x5, %rax
setb %al
orb %cl, %al
jne 0x74e4
movq %r13, 0x20(%rsp)
movl (%rbx), %ebp
cmpl 0x4(%rsp), %ebp
je 0x74d6
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %r14, %rax
je 0x7431
cmpl %ebp, 0x20(%rax)
je 0x7431
movq %rax, %rdi
callq 0x2180
jmp 0x741d
cmpq %r14, %rax
je 0x74d6
movq (%r13), %r15
cmpq 0x8(%r13), %r15
je 0x74d6
movl (%rbx), %r12d
movl (%r15), %ebp
cmpl %ebp, %r12d
je 0x74cd
cmpl 0x4(%rsp), %ebp
je 0x74cd
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %r14, %rax
je 0x7476
cmpl %ebp, 0x20(%rax)
je 0x7476
movq %rax, %rdi
callq 0x2180
jmp 0x7462
cmpq %r14, %rax
jne 0x74cd
movq 0x10(%rsp), %r13
movq %r13, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x5706
movq %rax, %r12
movq %r13, %rdi
movq 0x20(%rsp), %r13
movl %ebp, %esi
movl $0x1, %edx
callq 0x5706
testq %r12, %r12
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x74cd
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0x7542
testb %al, %al
jne 0x7523
addq $0x4, %r15
jmp 0x743e
addq $0x4, %rbx
cmpq 0x8(%r13), %rbx
jne 0x7408
movq 0x30(%rsp), %rax
addq $0x4, %rax
cmpq $0x8, %rax
jne 0x73cd
leaq 0x4(%rsp), %r14
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xdd88
movl (%r14), %eax
movl %eax, 0x34(%rbx)
movl 0x30(%rbx), %eax
incl %eax
movq 0x8(%rsp), %rcx
movl %eax, 0x7c(%rcx)
movb $0x1, %bl
jmp 0x7525
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq