// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq (%rdi), %r12
movq 0x8(%rdi), %r13
movq %r13, %r15
subq %r12, %r15
je 0x11268
movsd %xmm0, 0x8(%rsp)
sarq $0x4, %r15
movl $0x28, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x3030
movq %rax, %r14
leaq (,%r15,8), %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r14,%rcx)
addq $0x28, %rcx
cmpq %rcx, %rax
jne 0x111e8
cmpq %r12, %r13
je 0x11254
xorl %eax, %eax
cmpq $0x1, %r15
sbbq %r15, %rax
movl $0x1, %ecx
movq %r14, %rdx
movupd (%r12), %xmm0
movupd %xmm0, 0x8(%rdx)
cmpq %rcx, %r15
movl $0x0, %esi
cmovneq %rcx, %rsi
leaq (%rsi,%rsi,4), %rsi
leaq (%r14,%rsi,8), %rdi
movq %rdi, 0x18(%rdx)
movq %rdx, 0x20(%r14,%rsi,8)
movl $0x0, (%rdx)
leaq (%rax,%rcx), %rsi
incq %rsi
incq %rcx
addq $0x28, %rdx
addq $0x10, %r12
cmpq $0x1, %rsi
jne 0x1120e
cmpl $0x0, (%r14)
movsd 0x8(%rsp), %xmm6
je 0x11283
movq %r14, %r13
jmp 0x113e4
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x11275
movq %rax, 0x8(%rbx)
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
mulsd %xmm6, %xmm6
xorl %eax, %eax
movq %r14, %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rcx), %r13
cmpq %r13, %rdx
je 0x113e1
movq 0x8(%r13), %rsi
xorps %xmm0, %xmm0
cvtsi2sdq 0x8(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
movapd %xmm0, %xmm3
subsd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtsi2sdq 0x10(%rcx), %xmm4
xorps %xmm2, %xmm2
cvtsi2sdq 0x10(%r13), %xmm2
subsd %xmm2, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
ucomisd %xmm3, %xmm6
jae 0x1137b
movq 0x8(%rdx), %rdi
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
movapd %xmm1, %xmm4
subsd %xmm3, %xmm4
xorps %xmm3, %xmm3
cvtsi2sdq 0x10(%rdx), %xmm3
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm4, %xmm4
addsd %xmm2, %xmm4
ucomisd %xmm4, %xmm6
jae 0x11394
movq 0x10(%r13), %r8
xorps %xmm2, %xmm2
cvtsi2sd %r8, %xmm2
subq 0x10(%rdx), %r8
xorps %xmm3, %xmm3
cvtsi2sd %r8, %xmm3
subq %rsi, %rdi
xorps %xmm4, %xmm4
cvtsi2sd %rdi, %xmm4
mulsd %xmm4, %xmm2
mulsd %xmm3, %xmm1
xorps %xmm5, %xmm5
cvtsi2sdq 0x10(%rcx), %xmm5
addsd %xmm2, %xmm1
mulsd %xmm4, %xmm5
mulsd %xmm3, %xmm0
addsd %xmm5, %xmm0
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm3
addsd %xmm4, %xmm3
divsd %xmm3, %xmm0
ucomisd %xmm0, %xmm6
jbe 0x113c8
movq %rdx, 0x18(%r13)
movq 0x18(%rcx), %rcx
movq %r13, 0x20(%rcx)
movl $0x0, (%r13)
decq %r15
jmp 0x113d1
movq 0x18(%rdx), %rsi
movq 0x20(%rdx), %rdi
movq %rsi, 0x18(%rdi)
movq 0x18(%rdx), %rdx
movq %rdi, 0x20(%rdx)
movl %eax, (%rdi)
movq 0x18(%rcx), %rdx
movq 0x20(%rcx), %r13
movq %rdx, 0x18(%r13)
movq 0x18(%rcx), %rcx
movq %r13, 0x20(%rcx)
movl %eax, (%r13)
addq $-0x2, %r15
jmp 0x113d1
movl $0x1, (%rcx)
movq %rdx, %r13
cmpl $0x0, (%r13)
movq %r13, %rcx
je 0x1128c
jmp 0x113e4
movq %rcx, %r13
xorl %r12d, %r12d
cmpq $0x3, %r15
cmovaeq %r15, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1262c
cmpq $0x3, %r15
jb 0x11425
cmpq $0x1, %r12
adcq $0x0, %r12
xorl %eax, %eax
movq (%rbx), %rcx
movupd 0x8(%r13), %xmm0
movupd %xmm0, (%rcx,%rax)
movq 0x18(%r13), %r13
addq $0x10, %rax
decq %r12
jne 0x1140a
movq %r14, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x32a0