// File: 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:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x158(%rsp)
movq %rsi, 0x150(%rsp)
movl %edx, 0x14c(%rsp)
movq 0x158(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x150(%rsp), %rdi
movl 0x14c(%rsp), %esi
movl $0x1, %edx
callq 0x7240
movq %rax, 0x140(%rsp)
cmpq $0x0, 0x140(%rsp)
jne 0xa149
movl 0x14c(%rsp), %esi
leaq 0x1c813(%rip), %rdi     # 0x26948
movb $0x0, %al
callq 0x2050
movb $0x0, 0x167(%rsp)
jmp 0xa97a
movq 0x140(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jb 0xa192
movq 0x150(%rsp), %rax
cmpl $0x3, 0x60(%rax)
jbe 0xa185
movl 0x14c(%rsp), %esi
movq 0x140(%rsp), %rax
movl 0x78(%rax), %edx
leaq 0x1c7e0(%rip), %rdi     # 0x2695e
movb $0x0, %al
callq 0x2050
movb $0x0, 0x167(%rsp)
jmp 0xa97a
movq 0x20(%rsp), %rax
movq 0x140(%rsp), %rcx
movl $0x0, 0x64(%rcx)
movq 0x140(%rsp), %rcx
movl $0x0, 0x68(%rcx)
movb $0x0, 0x13f(%rsp)
movb $0x0, 0x13e(%rsp)
movb $0x0, 0x13d(%rsp)
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rdi
callq 0x13140
movq %rax, 0x128(%rsp)
movq 0x130(%rsp), %rdi
callq 0x13190
movq %rax, 0x120(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x13170
testb $0x1, %al
jne 0xa21d
jmp 0xa696
leaq 0x128(%rsp), %rdi
callq 0x131c0
movq %rax, 0x118(%rsp)
movq 0x150(%rsp), %rdi
movq 0x118(%rsp), %rax
movl (%rax), %esi
movl $0x1, %edx
callq 0x7240
movq %rax, 0x110(%rsp)
cmpq $0x0, 0x110(%rsp)
jne 0xa266
jmp 0xa684
movq 0x140(%rsp), %rax
movl 0x80(%rax), %ecx
movq 0x110(%rsp), %rdx
movb $0x1, %al
cmpl 0x80(%rdx), %ecx
movb %al, 0x1f(%rsp)
je 0xa2ad
movq 0x140(%rsp), %rax
movl 0x80(%rax), %eax
movq 0x110(%rsp), %rcx
cmpl 0x84(%rcx), %eax
sete %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
andb $0x1, %al
movb %al, 0x13f(%rsp)
movq 0x140(%rsp), %rax
movl 0x84(%rax), %ecx
movq 0x110(%rsp), %rdx
movb $0x1, %al
cmpl 0x80(%rdx), %ecx
movb %al, 0x1e(%rsp)
je 0xa301
movq 0x140(%rsp), %rax
movl 0x84(%rax), %eax
movq 0x110(%rsp), %rcx
cmpl 0x84(%rcx), %eax
sete %al
movb %al, 0x1e(%rsp)
movb 0x1e(%rsp), %al
andb $0x1, %al
movb %al, 0x13e(%rsp)
movb $0x0, 0x13d(%rsp)
testb $0x1, 0x13f(%rsp)
je 0xa331
movq 0x140(%rsp), %rax
movl 0x64(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x64(%rax)
testb $0x1, 0x13e(%rsp)
je 0xa34c
movq 0x140(%rsp), %rax
movl 0x68(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x68(%rax)
movq 0x140(%rsp), %rax
cmpl $0x2, 0x64(%rax)
jae 0xa36c
movq 0x140(%rsp), %rax
cmpl $0x2, 0x68(%rax)
jb 0xa421
movq 0x150(%rsp), %rax
cmpl $0x2, 0x60(%rax)
jbe 0xa414
movq 0x20(%rsp), %rsi
movl 0x14c(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x140(%rsp), %rax
movl 0x64(%rax), %ecx
movl %ecx, 0x14(%rsp)
movl 0x68(%rax), %eax
movl %eax, 0x18(%rsp)
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x142f0
movq 0x8(%rsp), %rdi
callq 0x20b0
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movl 0x18(%rsp), %ecx
movq %rax, %r8
leaq 0x1c5bb(%rip), %rdi     # 0x26991
xorl %eax, %eax
callq 0x2050
jmp 0xa3df
leaq 0xf0(%rsp), %rdi
callq 0x2100
jmp 0xa414
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x2100
jmp 0xa98b
movb $0x0, 0x167(%rsp)
jmp 0xa97a
testb $0x1, 0x13f(%rsp)
jne 0xa439
testb $0x1, 0x13e(%rsp)
je 0xa516
movq 0x150(%rsp), %rdi
movq 0x140(%rsp), %rsi
movq 0x110(%rsp), %rdx
callq 0xa9a0
andb $0x1, %al
movb %al, 0x13d(%rsp)
movq 0x150(%rsp), %rax
cmpl $0x2, 0x60(%rax)
jbe 0xa4b7
movq 0x140(%rsp), %rax
movl 0x60(%rax), %esi
movq 0x110(%rsp), %rax
movl 0x60(%rax), %edx
movb 0x13f(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
movb 0x13e(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x13d(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
leaq 0x1c519(%rip), %rdi     # 0x269c9
movb $0x0, %al
callq 0x2050
testb $0x1, 0x13d(%rsp)
je 0xa514
movq 0x150(%rsp), %rax
cmpl $0x2, 0x60(%rax)
jbe 0xa507
movl 0x14c(%rsp), %esi
movq 0x118(%rsp), %rax
movl (%rax), %edx
movb 0x13f(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
movb 0x13e(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
leaq 0x1c4eb(%rip), %rdi     # 0x269eb
movb $0x0, %al
callq 0x2050
movb $0x0, 0x167(%rsp)
jmp 0xa97a
jmp 0xa516
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x13140
movq %rax, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x13190
movq %rax, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x13170
testb $0x1, %al
jne 0xa56b
jmp 0xa682
leaq 0xd0(%rsp), %rdi
callq 0x131c0
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movl (%rax), %eax
cmpl 0x14c(%rsp), %eax
je 0xa66e
movq 0xc0(%rsp), %rax
movl (%rax), %eax
movq 0x118(%rsp), %rcx
cmpl (%rcx), %eax
je 0xa66e
movq 0x150(%rsp), %rdi
movq 0xc0(%rsp), %rax
movl (%rax), %esi
movl $0x1, %edx
callq 0x7240
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
je 0xa66c
movq 0x140(%rsp), %rdi
addq $0x58, %rdi
movq 0x110(%rsp), %rsi
addq $0x58, %rsi
movq 0xb8(%rsp), %rdx
movq 0xb8(%rsp), %rcx
addq $0x8, %rcx
callq 0x2680
testb $0x1, %al
jne 0xa61b
jmp 0xa66c
movq 0x150(%rsp), %rax
cmpl $0x2, 0x60(%rax)
jbe 0xa65f
movl 0x14c(%rsp), %esi
movq 0xc0(%rsp), %rax
movl (%rax), %edx
movq 0x140(%rsp), %rax
movl 0x60(%rax), %ecx
movq 0x110(%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x1c3d3(%rip), %rdi     # 0x26a2b
movb $0x0, %al
callq 0x2050
movb $0x0, 0x167(%rsp)
jmp 0xa97a
jmp 0xa66e
jmp 0xa670
leaq 0xd0(%rsp), %rdi
callq 0x13240
jmp 0xa54d
jmp 0xa684
leaq 0x128(%rsp), %rdi
callq 0x13240
jmp 0xa1ff
movq 0x140(%rsp), %rax
movl 0x80(%rax), %eax
movl %eax, 0x98(%rsp)
movq 0x140(%rsp), %rax
movl 0x84(%rax), %eax
movl %eax, 0x9c(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq $0x2, 0xa8(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x13b00
movq %rax, 0x90(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x13b10
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rax
cmpq 0x88(%rsp), %rax
je 0xa934
movq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x150(%rsp), %rdi
addq $0xe0, %rdi
movq 0x80(%rsp), %rsi
callq 0x12dd0
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
callq 0x12b80
cmpq $0x2, %rax
jb 0xa919
movq 0x78(%rsp), %rdi
callq 0x128b0
movq %rax, 0x70(%rsp)
movq 0x78(%rsp), %rdi
callq 0x128e0
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x134e0
testb $0x1, %al
jne 0xa7a7
jmp 0xa917
leaq 0x70(%rsp), %rdi
callq 0x13520
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movl (%rax), %eax
cmpl 0x14c(%rsp), %eax
je 0xa906
movq 0x20(%rsp), %rdi
movq 0x60(%rsp), %rax
movl (%rax), %esi
callq 0x14490
testb $0x1, %al
jne 0xa7e4
jmp 0xa906
movq 0x78(%rsp), %rdi
callq 0x128b0
movq %rax, 0x58(%rsp)
movq 0x78(%rsp), %rdi
callq 0x128e0
movq %rax, 0x50(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x134e0
testb $0x1, %al
jne 0xa81a
jmp 0xa904
leaq 0x70(%rsp), %rdi
callq 0x13520
movl (%rax), %eax
movl %eax, 0x4(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x13520
movq %rax, %rcx
movl 0x4(%rsp), %eax
cmpl (%rcx), %eax
jne 0xa844
jmp 0xa8f5
leaq 0x58(%rsp), %rdi
callq 0x13520
movl (%rax), %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x14c(%rsp), %eax
je 0xa8f3
movq 0x20(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x14490
testb $0x1, %al
jne 0xa8f3
movq 0x150(%rsp), %rdi
movq 0x60(%rsp), %rax
movl (%rax), %esi
xorl %edx, %edx
callq 0x7240
movq %rax, 0x40(%rsp)
movq 0x150(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl $0x1, %edx
callq 0x7240
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0xa8f1
cmpq $0x0, 0x38(%rsp)
je 0xa8f1
movq 0x38(%rsp), %rdi
movq 0x150(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x140(%rsp), %rcx
callq 0xa9e0
testb $0x1, %al
jne 0xa8e2
jmp 0xa8ef
movb $0x0, 0x167(%rsp)
jmp 0xa97a
jmp 0xa8f1
jmp 0xa8f3
jmp 0xa8f5
leaq 0x58(%rsp), %rdi
callq 0x13530
jmp 0xa7f3
jmp 0xa906
jmp 0xa908
leaq 0x70(%rsp), %rdi
callq 0x13530
jmp 0xa780
jmp 0xa919
jmp 0xa91b
movq 0x90(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x90(%rsp)
jmp 0xa716
movq 0x20(%rsp), %rdi
leaq 0x14c(%rsp), %rsi
callq 0x130d0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x28(%rsp)
movb %dl, 0x30(%rsp)
movl 0x14c(%rsp), %ecx
movl %ecx, 0x34(%rax)
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movq 0x140(%rsp), %rax
movl %ecx, 0x7c(%rax)
movb $0x1, 0x167(%rsp)
movb 0x167(%rsp), %al
andb $0x1, %al
addq $0x168, %rsp            # imm = 0x168
retq
movq 0xe8(%rsp), %rdi
callq 0x2280
nopl (%rax,%rax)