// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movl %edx, %r12d
movq %rdi, 0x20(%rsp)
movl %edx, 0x34(%rsp)
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
movl %edx, %esi
movl $0x1, %edx
callq 0x5842
testq %rax, %rax
je 0x71d8
movl 0x78(%rax), %edx
cmpl $0x2, %edx
jb 0x71f0
movq 0x8(%rsp), %rax
cmpl $0x4, 0x60(%rax)
jb 0x75f2
leaq 0x978d(%rip), %rdi      # 0x10954
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x2030
jmp 0x75f4
leaq 0x975f(%rip), %rdi      # 0x1093e
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x2030
jmp 0x75f4
movq %rax, 0x18(%rsp)
movq $0x0, 0x64(%rax)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %r13
leaq 0x8(%rax), %r14
cmpq %r14, %r13
je 0x738c
movq 0x18(%rsp), %rbx
addq $0x58, %rbx
movq %rbx, 0x28(%rsp)
movl 0x20(%r13), %esi
movq 0x8(%rsp), %rdi
movl $0x1, %edx
callq 0x5842
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x7378
movq 0x18(%rsp), %rdx
movl 0x80(%rdx), %ecx
movq 0x10(%rsp), %rax
movl 0x80(%rax), %eax
movb $0x1, %bl
movb $0x1, %r15b
cmpl %eax, %ecx
je 0x7270
movq 0x10(%rsp), %rsi
cmpl 0x84(%rsi), %ecx
sete %r15b
movl 0x84(%rdx), %ecx
cmpl %eax, %ecx
je 0x7288
movq 0x10(%rsp), %rax
cmpl 0x84(%rax), %ecx
sete %bl
testb %r15b, %r15b
je 0x7290
incl 0x64(%rdx)
testb %bl, %bl
je 0x7297
incl 0x68(%rdx)
movl 0x64(%rdx), %ebp
cmpl $0x1, %ebp
ja 0x7580
cmpl $0x2, 0x68(%rdx)
jae 0x7580
movl %r15d, %eax
orb %bl, %al
testb $0x1, %al
je 0x730d
movl 0x60(%rdx), %esi
movq 0x10(%rsp), %rax
movl 0x60(%rax), %edx
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x8ff8
movl %eax, %edi
cmpl $0x3, 0x60(%rbp)
jb 0x7304
movq 0x18(%rsp), %rax
movl 0x60(%rax), %esi
movq 0x10(%rsp), %rax
movl 0x60(%rax), %edx
movzbl %r15b, %ecx
movzbl %bl, %r8d
movzbl %dil, %r9d
movl %edi, %ebp
leaq 0x96c4(%rip), %rdi      # 0x109bf
xorl %eax, %eax
callq 0x2030
movl %ebp, %edi
testb %dil, %dil
jne 0x75d2
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rbp
cmpq %r14, %rbp
movq 0x28(%rsp), %rbx
je 0x7378
movq 0x10(%rsp), %r15
addq $0x58, %r15
movl 0x20(%rbp), %esi
cmpl %r12d, %esi
je 0x7368
cmpl 0x20(%r13), %esi
je 0x7368
movq 0x8(%rsp), %rdi
movl $0x1, %edx
callq 0x5842
testq %rax, %rax
je 0x7368
movq %rax, %rcx
addq $0x8, %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x251f
testb %al, %al
jne 0x7548
movq %rbp, %rdi
callq 0x2180
movq %rax, %rbp
cmpq %r14, %rax
jne 0x7329
movq %r13, %rdi
callq 0x2180
movq %rax, %r13
cmpq %r14, %rax
jne 0x7221
movl %r12d, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq 0x80(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x8(%rsp), %rax
addq $0xe0, %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movq %r14, 0x40(%rsp)
movq %rax, 0x28(%rsp)
leaq (%rsp,%rax), %rsi
addq $0x48, %rsi
movq 0x38(%rsp), %rdi
callq 0xb536
movq %rax, %rbp
movq (%rax), %r12
movq 0x8(%rax), %rbx
movq %rbx, %rax
subq %r12, %rax
sete %cl
cmpq $0x5, %rax
setb %al
orb %cl, %al
jne 0x7504
movl (%r12), %r15d
cmpl 0x10(%rsp), %r15d
sete %cl
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %r14, %rax
sete %dl
orb %cl, %dl
jne 0x74f7
cmpl %r15d, 0x20(%rax)
je 0x742d
movq %rax, %rdi
callq 0x2180
cmpq %r14, %rax
jne 0x7415
jmp 0x74f7
cmpq %r14, %rax
je 0x74f7
movq (%rbp), %rax
cmpq %rbx, %rax
je 0x74f7
movq %rax, %rbx
movl (%r12), %r13d
movl (%rbx), %r15d
cmpl %r15d, %r13d
sete %al
cmpl 0x10(%rsp), %r15d
sete %cl
orb %al, %cl
jne 0x74e9
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %r14, %rax
je 0x7486
cmpl %r15d, 0x20(%rax)
je 0x7481
movq %rax, %rdi
callq 0x2180
jmp 0x746c
cmpq %r14, %rax
jne 0x74e9
movq %r12, %r14
movq %rbp, %r12
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x5842
movq %rax, %r13
movq %rbp, %rdi
movq %r12, %rbp
movq %r14, %r12
movq 0x40(%rsp), %r14
movl %r15d, %esi
movl $0x1, %edx
callq 0x5842
testq %r13, %r13
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x74e9
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
callq 0x7612
testb %al, %al
jne 0x75f2
addq $0x4, %rbx
cmpq 0x8(%rbp), %rbx
jne 0x7446
addq $0x4, %r12
cmpq %rbx, %r12
jne 0x73f2
movq 0x28(%rsp), %rax
addq $0x4, %rax
cmpq $0x8, %rax
jne 0x73b9
leaq 0x34(%rsp), %rbx
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe00a
movl (%rbx), %eax
movl %eax, 0x34(%r14)
movl 0x30(%r14), %eax
incl %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x7c(%rcx)
movb $0x1, %bpl
jmp 0x75f4
movq 0x8(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x75f2
movl 0x20(%rbp), %edx
movq 0x18(%rsp), %rax
movl 0x60(%rax), %ecx
movq 0x10(%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x94af(%rip), %rdi      # 0x10a21
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x2030
jmp 0x75f4
movq 0x8(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x75f2
movl 0x68(%rdx), %r15d
movl %r12d, %ebx
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0xb5c8
movq (%r12), %r8
leaq 0x93d8(%rip), %rdi      # 0x10987
movl %ebx, %esi
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2030
movq (%r12), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x75f2
callq 0x2140
jmp 0x75f2
movq 0x8(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jb 0x75f2
movl 0x20(%r13), %edx
movzbl %r15b, %ecx
movzbl %bl, %r8d
leaq 0x93f1(%rip), %rdi      # 0x109e1
jmp 0x7572
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop