// File: /workspace/github_cpp_makefile_star2_O2/realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyLine::canBeRemoved(PolyDetector&, RmLinesType) const
uint32_t PolyLine::canBeRemoved(PolyDetector &pd, RmLinesType type) const
{
    assert(!ignore);
    
    //if (type == 1 && verbose)
    //    logoutf("canBeRemoved(%s) type:%d", toString(pd).c_str(), type);
    
    if (type == RmLinesType::NoPointNeigh)
    {
        // Remove lines having points with no neighbors
        for (auto &abId : {aIdx, bIdx})
        {
            auto &neigh = pd.pointToLines[abId];
            if (neigh.size() <= 1) // including self
            {
                //if (pd.verbose)
                //    logoutf("canBeRemoved(%s) point with no neighbors! CAN be removed", toString(pd).c_str());
                return true;
            }
            
            // check also ignore flag
            uint32_t nNeigh = 0;
            for (auto &n : neigh)
            {
                auto l = pd.findLine(n);
                if (l && l->id != id)
                    nNeigh++;
            }
            if (nNeigh == 0)
            {
                if (pd.verbose)
                    logoutf("canBeRemoved(%s) point P%u with no neighbors (%u) (check ignore flag)! CAN be removed", toString(pd).c_str(), abId, nNeigh);
                return true;
            }
        }
    }
    if (type == RmLinesType::TakenTwice)
    {
        if (took >= 2)
        {
            for (auto &lid : pd._neighbors[id])
            {
                auto l = pd.findLine(lid);
                if (l && l->id != id)
                {
                    //if (pd.verbose)
                    //    logoutf("nlid %s", l->toString(pd).c_str());
                    
                    //if (!l->took)
                    if (l->took < 0)
                    {
                        return false;
                    }
                }
            }
            //logoutf("canBeRemoved(%s) line tooked 2 times! CAN be removed", toString(pd).c_str());
            return true;
        }
    }
    
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpb $0x1, 0x74(%rdi)
je 0x4938
movq %rsi, %r14
movq %rdi, %r15
xorl %ebx, %ebx
testl %edx, %edx
je 0x48ec
cmpl $0x2, %edx
jne 0x4927
movq 0x80(%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0xe0(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
pushq $0x1
popq %rbx
cmpq $0x8, %rax
je 0x4925
movq %rax, 0x18(%rsp)
leaq (%rsp,%rax), %rbp
addq $0x20, %rbp
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x875e
movq (%rax), %r13
movq 0x8(%rax), %r12
movq %r12, %rax
subq %r13, %rax
cmpq $0x5, %rax
jb 0x4927
movq %rbp, 0x8(%rsp)
xorl %ebp, %ebp
cmpq %r12, %r13
je 0x489e
movl (%r13), %esi
movq %r14, %rdi
movl %ebx, %edx
callq 0x46ec
testq %rax, %rax
je 0x4898
movl 0x60(%rax), %eax
xorl %ecx, %ecx
cmpl 0x60(%r15), %eax
setne %cl
addl %ecx, %ebp
addq $0x4, %r13
jmp 0x4872
movq 0x18(%rsp), %rax
addq $0x4, %rax
testl %ebp, %ebp
jne 0x4830
cmpl $0x0, 0x60(%r14)
movq 0x8(%rsp), %r12
je 0x4927
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4208
movq (%r13), %rsi
movl (%r12), %edx
leaq 0x8ec3(%rip), %rdi      # 0xd79c
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2040
movq %r13, %rdi
callq 0x20b0
jmp 0x4927
cmpl $0x2, 0x78(%r15)
jb 0x4927
leaq 0x68(%r14), %rdi
addq $0x60, %r15
movq %r15, %rsi
callq 0x875e
movq (%rax), %r15
movq 0x8(%rax), %r12
pushq $0x1
popq %rbx
cmpq %r12, %r15
je 0x4927
movl (%r15), %esi
movq %r14, %rdi
movl %ebx, %edx
callq 0x46ec
addq $0x4, %r15
jmp 0x490d
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8e12(%rip), %rdi      # 0xd751
leaq 0x87ce(%rip), %rsi      # 0xd114
leaq 0x8e0c(%rip), %rcx      # 0xd759
movl $0x46a, %edx            # imm = 0x46A
callq 0x20f0
nop