// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::ConvexHull() const
const R2Polygon R2Polygon::
ConvexHull(void) const
{
  // Check number of points
  if (npoints <= 3) return *this;

  // Create convex hull polygon
  R2Polygon convex_hull;
  convex_hull.bbox = R2null_box;
  convex_hull.clockwise = FALSE;
  convex_hull.points = new R2Point [ npoints ];
  convex_hull.npoints = 0;

  // Find index of leftmost point
  int leftmost_index = 0;
  for (int i = 1; i < npoints; i++) {
    if (points[i][0] < points[leftmost_index][0]) {
      leftmost_index = i;
    }
  }

  // Iteratively find next vertex for convex hull via gift-wrapping
  int index = leftmost_index;
  for (int i = 0; i < npoints; i++) {
    // Insert convex hull point
    convex_hull.points[convex_hull.npoints++] = points[index];
    convex_hull.bbox.Union(points[index]);
    R2Point last_point = points[index];
    int last_index = index;

    // Find next giftwrapping point index
    index = (last_index+1)%npoints;
    R2Line line(last_point, points[index]);
    for (int j = 0; j < npoints; j++) {
      if (j == last_index) continue;
      if (j == leftmost_index) {
        RNScalar d = R2SignedDistance(line, points[j]) + 1000 * RN_EPSILON;
        if (d > 0) {
          line = R2Line(last_point, points[j]);
          index = j;
        }
      }
      else {
        if (R2Contains(last_point, points[j])) continue;
        RNScalar d = R2SignedDistance(line, points[j]);
        if (d > 0) {
          line = R2Line(last_point, points[j]);
          index = j;
        }
      }
    }

    // Check if completed loop
    if (index == leftmost_index) break;
  }

  // Return the convex polygon
  return convex_hull;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %rbp
cmpl $0x3, 0x10(%rsi)
jg 0x63a0f
movq %rbp, %rdi
movq %r14, %rsi
callq 0x633f4
jmp 0x63ce6
movq %rbp, %rdi
callq 0x6339c
leaq 0x20(%rbp), %rdi
leaq 0x4844b6(%rip), %r15    # 0x4e7ed8
leaq 0x8(%r15), %rsi
callq 0x681de
leaq 0x30(%rbp), %rdi
addq $0x18, %r15
movq %r15, %rsi
callq 0x681de
movl $0x0, 0x40(%rbp)
movslq 0x10(%r14), %r13
movq %r13, %rbx
shlq $0x4, %rbx
testq %r13, %r13
movq $-0x1, %r15
cmovnsq %rbx, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r12
testl %r13d, %r13d
je 0x63cdb
leaq 0x18(%rbp), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
callq 0x680a4
addq $0x10, %r13
cmpq %r13, %rbx
jne 0x63a7b
movslq 0x10(%r14), %rax
xorl %r9d, %r9d
movq %r12, 0x8(%rbp)
movl $0x0, 0x10(%rbp)
cmpq $0x2, %rax
jl 0x63adc
movq 0x8(%r14), %rcx
leaq 0x10(%rcx), %rdx
xorl %edi, %edi
movl $0x1, %esi
movslq %edi, %r8
shlq $0x4, %r8
movsd (%rcx,%r8), %xmm0
ucomisd (%rdx), %xmm0
movl %esi, %r9d
ja 0x63acd
movl %edi, %r9d
incq %rsi
addq $0x10, %rdx
movl %r9d, %edi
cmpq %rsi, %rax
jne 0x63ab4
testl %eax, %eax
jle 0x63ce6
movl %r9d, %eax
xorl %ecx, %ecx
leaq 0x88(%rsp), %r8
movq %rax, 0x30(%rsp)
shlq $0x4, %rax
movq %rax, 0x18(%rsp)
movl %r9d, %edx
movq %rbp, 0x8(%rsp)
movl %r9d, 0x14(%rsp)
movq %r8, %rbx
movq %rcx, 0x28(%rsp)
movq %rdx, %r13
movslq %edx, %r12
shlq $0x4, %r12
movq 0x8(%r14), %rsi
addq %r12, %rsi
movslq 0x10(%rbp), %rdi
leal 0x1(%rdi), %eax
movl %eax, 0x10(%rbp)
shlq $0x4, %rdi
movq %rbp, %r15
addq 0x8(%rbp), %rdi
callq 0x681de
movq 0x8(%r14), %rsi
addq %r12, %rsi
movq 0x20(%rsp), %rdi
callq 0x65986
addq 0x8(%r14), %r12
movq %rbx, %rbp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x680b0
leal 0x1(%r13), %eax
cltd
idivl 0x10(%r14)
movl %edx, %r12d
movslq %edx, %rdx
shlq $0x4, %rdx
addq 0x8(%r14), %rdx
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x67c32
movl 0x10(%r14), %eax
testl %eax, %eax
movl 0x14(%rsp), %r9d
jle 0x63cbc
movl %r13d, %r15d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %rbx, %r8
cmpq %r13, %r15
je 0x63c99
movq 0x8(%r14), %rsi
cmpq %r13, 0x30(%rsp)
jne 0x63c24
addq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x6c745
leaq 0x81fcc(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
mulsd 0x45c60(%rip), %xmm1    # 0xa9838
addsd %xmm0, %xmm1
ucomisd 0x3e604(%rip), %xmm1    # 0xa21e8
movq %rbx, %r8
jbe 0x63c99
movq 0x8(%r14), %rdx
addq 0x18(%rsp), %rdx
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r8, %rsi
callq 0x67c32
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x68720
leaq 0x48(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x68720
jmp 0x63c84
addq %rbp, %rsi
movq %r8, %rdi
callq 0x56d15
testl %eax, %eax
jne 0x63c96
movq 0x8(%r14), %rsi
addq %rbp, %rsi
leaq 0x38(%rsp), %rdi
callq 0x6c745
ucomisd 0x3e59c(%rip), %xmm0    # 0xa21e8
movq %rbx, %r8
jbe 0x63c99
movq 0x8(%r14), %rdx
addq %rbp, %rdx
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r8, %rsi
callq 0x67c32
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x68720
leaq 0x48(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x68720
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movl %r13d, %r12d
movq %rbx, %r8
incq %r13
movslq 0x10(%r14), %rax
addq $0x10, %rbp
cmpq %rax, %r13
jl 0x63ba2
movl %r12d, %edx
movq 0x8(%rsp), %rbp
movl 0x14(%rsp), %r9d
jmp 0x63cc5
movq %r15, %rbp
movl %r12d, %edx
movq %rbx, %r8
cmpl %r9d, %edx
movq 0x28(%rsp), %rcx
je 0x63ce6
incl %ecx
cmpl %eax, %ecx
jl 0x63b0c
jmp 0x63ce6
movq %r12, 0x8(%rbp)
movl $0x0, 0x10(%rbp)
movq %rbp, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x63d2c
jmp 0x63d16
jmp 0x63d16
movq %rax, %r14
movq %r12, %rdi
movq %r15, %rsi
callq 0xe8c0
jmp 0x63d2c
jmp 0x63d24
movq %rax, %r14
movq %r15, %rbp
jmp 0x63d2c
jmp 0x63d24
jmp 0x63d24
jmp 0x63d24
movq %rax, %r14
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x636e8
movq %r14, %rdi
callq 0xe7e0