// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq (%rdi), %r12
movq 0x8(%rdi), %r14
subq %r12, %r14
je 0xebe3
movsd %xmm0, (%rsp)
sarq $0x4, %r14
pushq $0x28
popq %rcx
movq %r14, %rax
mulq %rcx
pushq $-0x1
popq %rdi
cmovnoq %rax, %rdi
callq 0x3030
movq %rax, %r15
imulq $0x28, %r14, %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r15,%rcx)
addq $0x28, %rcx
cmpq %rcx, %rax
jne 0xea33
movq %r14, %rax
negq %rax
pushq $0x1
popq %rcx
xorl %edx, %edx
movq %r15, %rsi
movsd (%rsp), %xmm4
leaq (%rax,%rcx), %rdi
cmpq $0x1, %rdi
je 0xea99
movupd (%r12), %xmm0
movq %rax, %rdi
addq %rcx, %rdi
movq %rcx, %rdi
cmoveq %rdx, %rdi
movupd %xmm0, 0x8(%rsi)
imulq $0x28, %rdi, %rdi
leaq (%r15,%rdi), %r8
movq %r8, 0x18(%rsi)
movq %rsi, 0x20(%r15,%rdi)
andl $0x0, (%rsi)
incq %rcx
addq $0x10, %r12
addq $0x28, %rsi
jmp 0xea56
mulsd %xmm4, %xmm4
movq %r15, %r12
movsd %xmm4, (%rsp)
cmpl $0x0, (%r12)
jne 0xeb96
movq 0x18(%r12), %rbp
movq 0x20(%r12), %r13
cmpq %r13, %rbp
je 0xeb96
cvtsi2sdq 0x8(%r12), %xmm2
cvtsi2sdq 0x8(%r13), %xmm0
subsd %xmm0, %xmm2
cvtsi2sdq 0x10(%r12), %xmm3
cvtsi2sdq 0x10(%r13), %xmm1
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
mulsd %xmm2, %xmm2
addsd %xmm3, %xmm2
ucomisd %xmm2, %xmm4
jae 0xeb4a
cvtsi2sdq 0x8(%rbp), %xmm2
subsd %xmm2, %xmm0
cvtsi2sdq 0x10(%rbp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm0, %xmm4
jae 0xeb62
leaq 0x8(%r12), %rdi
leaq 0x8(%r13), %rsi
leaq 0x8(%rbp), %rdx
callq 0xe923
movsd (%rsp), %xmm4
ucomisd %xmm0, %xmm4
ja 0xeb4a
movl $0x1, (%r12)
movq %rbp, %r12
jmp 0xeaa5
movq %rbp, 0x18(%r13)
movq %r13, 0x20(%rbp)
andl $0x0, (%r13)
decq %r14
movq %r13, %r12
jmp 0xeaa5
movq 0x18(%rbp), %rax
movq 0x20(%rbp), %rcx
movq %rax, 0x18(%rcx)
movq %rcx, 0x20(%rax)
andl $0x0, (%rcx)
movq 0x18(%r12), %rax
movq 0x20(%r12), %r12
movq %rax, 0x18(%r12)
movq %r12, 0x20(%rax)
andl $0x0, (%r12)
addq $-0x2, %r14
jmp 0xeaa5
xorl %r13d, %r13d
cmpq $0x3, %r14
cmovbq %r13, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfb60
subq $0x1, %r14
jb 0xebcd
movq (%rbx), %rax
movupd 0x8(%r12), %xmm0
movupd %xmm0, (%rax,%r13)
movq 0x18(%r12), %r12
addq $0x10, %r13
jmp 0xebac
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0xebf0
movq %rax, 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq