// File: /workspace/github_cpp_makefile_star2_O3/hzeller[P]gcode-multi-shell-extrude/third_party/clipper.cpp
// Function: ClipperLib::CleanPolygon(std::vector<ClipperLib::IntPoint, std::allocator<ClipperLib::IntPoint>> const&, std::vector<ClipperLib::IntPoint, std::allocator<ClipperLib::IntPoint>>&, double)
void CleanPolygon(const Path& in_poly, Path& out_poly, double distance)
{
  //distance = proximity in units/pixels below which vertices
  //will be stripped. Default ~= sqrt(2).
  
  size_t size = in_poly.size();
  
  if (size == 0) 
  {
    out_poly.clear();
    return;
  }

  OutPt* outPts = new OutPt[size];
  for (size_t i = 0; i < size; ++i)
  {
    outPts[i].Pt = in_poly[i];
    outPts[i].Next = &outPts[(i + 1) % size];
    outPts[i].Next->Prev = &outPts[i];
    outPts[i].Idx = 0;
  }

  double distSqrd = distance * distance;
  OutPt* op = &outPts[0];
  while (op->Idx == 0 && op->Next != op->Prev) 
  {
    if (PointsAreClose(op->Pt, op->Prev->Pt, distSqrd))
    {
      op = ExcludeOp(op);
      size--;
    } 
    else if (PointsAreClose(op->Prev->Pt, op->Next->Pt, distSqrd))
    {
      ExcludeOp(op->Next);
      op = ExcludeOp(op);
      size -= 2;
    }
    else if (SlopesNearCollinear(op->Prev->Pt, op->Pt, op->Next->Pt, distSqrd))
    {
      op = ExcludeOp(op);
      size--;
    }
    else
    {
      op->Idx = 1;
      op = op->Next;
    }
  }

  if (size < 3) size = 0;
  out_poly.resize(size);
  for (size_t i = 0; i < size; ++i)
  {
    out_poly[i] = op->Pt;
    op = op->Next;
  }
  delete [] outPts;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq (%rdi), %r12
movq 0x8(%rdi), %r14
subq %r12, %r14
je 0x10cd4
movsd %xmm0, (%rsp)
sarq $0x4, %r14
movl $0x28, %ecx
movq %r14, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x3030
movq %rax, %r15
leaq (,%r14,8), %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15,%rcx)
addq $0x28, %rcx
cmpq %rcx, %rax
jne 0x10c5a
xorl %eax, %eax
cmpq $0x1, %r14
sbbq %r14, %rax
movl $0x1, %ecx
movq %r15, %rdx
movupd (%r12), %xmm0
movupd %xmm0, 0x8(%rdx)
cmpq %rcx, %r14
movl $0x0, %esi
cmovneq %rcx, %rsi
leaq (%rsi,%rsi,4), %rsi
leaq (%r15,%rsi,8), %rdi
movq %rdi, 0x18(%rdx)
movq %rdx, 0x20(%r15,%rsi,8)
movl $0x0, (%rdx)
leaq (%rax,%rcx), %rsi
incq %rsi
incq %rcx
addq $0x28, %rdx
addq $0x10, %r12
cmpq $0x1, %rsi
jne 0x10c7b
cmpl $0x0, (%r15)
movsd (%rsp), %xmm7
je 0x10ced
movq %r15, %r12
jmp 0x10e3f
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x10ce1
movq %rax, 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
mulsd %xmm7, %xmm7
movq %r15, %rax
movq %rax, %r12
movq 0x18(%r12), %rcx
movq 0x20(%r12), %rax
cmpq %rax, %rcx
je 0x10e3f
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdi
xorps %xmm0, %xmm0
cvtsi2sdq 0x8(%r12), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
movapd %xmm0, %xmm4
subsd %xmm1, %xmm4
xorps %xmm2, %xmm2
cvtsi2sdq 0x10(%r12), %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
movapd %xmm2, %xmm5
subsd %xmm3, %xmm5
mulsd %xmm5, %xmm5
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
movq $-0x1, %rdx
ucomisd %xmm4, %xmm7
jae 0x10e26
movq 0x8(%rcx), %r8
xorps %xmm4, %xmm4
cvtsi2sd %r8, %xmm4
movq 0x10(%rcx), %r9
movapd %xmm1, %xmm5
xorps %xmm6, %xmm6
cvtsi2sd %r9, %xmm6
subsd %xmm4, %xmm5
movapd %xmm3, %xmm4
subsd %xmm6, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm5, %xmm5
addsd %xmm4, %xmm5
ucomisd %xmm5, %xmm7
jae 0x10dff
subq %r9, %rdi
xorps %xmm4, %xmm4
cvtsi2sd %rdi, %xmm4
subq %rsi, %r8
xorps %xmm5, %xmm5
cvtsi2sd %r8, %xmm5
mulsd %xmm5, %xmm3
mulsd %xmm4, %xmm1
addsd %xmm3, %xmm1
mulsd %xmm5, %xmm2
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
mulsd %xmm5, %xmm5
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
divsd %xmm4, %xmm0
ucomisd %xmm0, %xmm7
ja 0x10e26
movl $0x1, (%r12)
cmpl $0x0, (%rcx)
movq %rcx, %r12
je 0x10cf7
jmp 0x10e3c
movq 0x18(%rcx), %rax
movq 0x20(%rcx), %rcx
movq %rax, 0x18(%rcx)
movq %rcx, 0x20(%rax)
movl $0x0, (%rcx)
movq 0x18(%r12), %rcx
movq 0x20(%r12), %rax
movq $-0x2, %rdx
movq %rcx, 0x18(%rax)
movq %rax, 0x20(%rcx)
movl $0x0, (%rax)
addq %rdx, %r14
jmp 0x10cf4
movq %rcx, %r12
xorl %esi, %esi
cmpq $0x3, %r14
cmovaeq %r14, %rsi
movq %rbx, %rdi
callq 0x12088
cmpq $0x3, %r14
jb 0x10e76
xorl %eax, %eax
movq (%rbx), %rcx
movupd 0x8(%r12), %xmm0
movupd %xmm0, (%rcx,%rax)
movq 0x18(%r12), %r12
addq $0x10, %rax
decq %r14
jne 0x10e59
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x32a0