// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
callq 0xd900
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1ef1a
movq -0x10(%rbp), %rdi
callq 0x21c30
jmp 0x1f2e3
movq -0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl $0x28, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0xb0(%rbp)
callq 0x3030
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x1efbd
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x20750
jmp 0x1ef9f
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rax
addq $0x28, %rax
cmpq %rcx, %rax
movq %rax, -0xb8(%rbp)
jne 0x1ef8a
movq -0xa0(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x1f099
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x20460
movq %rax, %rcx
movq -0x28(%rbp), %rax
imulq $0x28, -0x40(%rbp), %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
addq $0x1, %rax
xorl %edx, %edx
divq -0x20(%rbp)
imulq $0x28, %rdx, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
imulq $0x28, -0x40(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rcx
imulq $0x28, -0x40(%rbp), %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
imulq $0x28, -0x40(%rbp), %rdx
addq %rdx, %rax
movq 0x18(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movl $0x0, (%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1efd0
movq -0xb0(%rbp), %rsi
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x3490
jmp 0x1f2ec
movsd -0x18(%rbp), %xmm0
mulsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, (%rcx)
movb %al, -0xc9(%rbp)
jne 0x1f0da
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x50(%rbp), %rcx
cmpq 0x20(%rcx), %rax
setne %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x1f0e9
jmp 0x1f241
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x70(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movsd -0x48(%rbp), %xmm0
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x1ee20
testb $0x1, %al
jne 0x1f135
jmp 0x1f153
movq -0x50(%rbp), %rdi
callq 0x1ee90
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1f23c
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x80(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x90(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
movsd -0x48(%rbp), %xmm0
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x1ee20
testb $0x1, %al
jne 0x1f1af
jmp 0x1f1d7
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1ee90
movq -0x50(%rbp), %rdi
callq 0x1ee90
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
subq $0x2, %rax
movq %rax, -0x20(%rbp)
jmp 0x1f23a
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x8, %rdi
movq -0x50(%rbp), %rsi
addq $0x8, %rsi
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rdx
addq $0x8, %rdx
movsd -0x48(%rbp), %xmm0
callq 0x1edd0
testb $0x1, %al
jne 0x1f207
jmp 0x1f222
movq -0x50(%rbp), %rdi
callq 0x1ee90
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1f238
movq -0x50(%rbp), %rax
movl $0x1, (%rax)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x1f23a
jmp 0x1f23c
jmp 0x1f0b0
cmpq $0x3, -0x20(%rbp)
jae 0x1f250
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x21e30
movq $0x0, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x1f2c6
movq -0x50(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x21aa0
movq -0xd8(%rbp), %rcx
movq 0x8(%rcx), %rdx
movq %rdx, (%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x1f268
movq -0x28(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x1f2e3
movq -0xe0(%rbp), %rdi
callq 0x3360
addq $0xe0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x3440
nopw %cs:(%rax,%rax)