// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::Clip(gaps::R2Line const&)
void R2Polygon::
Clip(const R2Line& line) 
{
  // Check number of points
  if (npoints == 0) {
    return;
  }
  else if (npoints == 1) {
    if (R2SignedDistance(line, points[0]) < 0) {
      bbox = R2null_box;
      delete [] points;
      points = NULL;
      npoints = 0;
    }
  }
  else {
    // Check bounding box
    R2Halfspace halfspace(line, 0);

    // Check if bounding box is entirely on positive side of line
    if (R2Contains(halfspace, bbox)) {
      return;
    }

    // Check if bounding box is entirely on negative side of line
    if (R2Contains(-halfspace, bbox)) {
      bbox = R2null_box;
      delete [] points;
      points = NULL;
      npoints = 0;
      return;
    }

    // Create new array for points
    int nbuffer = 0;
    R2Point *buffer = new R2Point [ 4 * npoints ];
    if (!buffer) RNAbort("Unable to allocate points during clip");

    // Build buffer with clipped points
    const R2Point *p1 = &points[npoints-1];
    RNScalar d1 = R2SignedDistance(line, *p1);
    for (int i = 0; i < npoints; i++) {
      const R2Point *p2 = &points[i];
      RNScalar d2 = R2SignedDistance(line, *p2);
      if (d2 >= 0) {
        // Insert crossing from negative to positive
        if (d1 < 0) {
          RNScalar t = d2 / (d2 - d1);
          buffer[nbuffer++] = *p2 + t * (*p1 - *p2);
        }

        // Insert point on positive side
        buffer[nbuffer++] = *p2;
      }
      else {
        // Insert crossing from positive to negative
        if (d1 >= 0) {
          RNScalar t = d1 / (d1 - d2);
          buffer[nbuffer++] = *p1 + t * (*p2 - *p1);
        }
      }

      // Remember previous point
      p1 = p2;
      d1 = d2;
    }

    // Copy points
    bbox = R2null_box;
    npoints = nbuffer;
    delete [] points;
    points = new R2Point [ npoints ];
    for (int i = 0; i < npoints; i++) {
      points[i] = buffer[i];
      bbox.Union(points[i]);
    }

    // Delete the buffer of points
    delete [] buffer;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl 0x10(%rdi), %eax
testl %eax, %eax
je 0x64ace
movq %rsi, %r12
movq %rdi, %rbx
cmpl $0x1, %eax
jne 0x64a16
movq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x6c745
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x64a8d
jmp 0x64ace
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x66f46
leaq 0x18(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x57627
testl %eax, %eax
jne 0x64ace
movapd 0x3d5d6(%rip), %xmm3    # 0xa2020
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
xorpd %xmm3, %xmm0
xorpd %xmm3, %xmm1
xorpd %xmm3, %xmm2
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x66ef0
movq %r15, %rdi
movq %r14, %rsi
callq 0x57627
testl %eax, %eax
je 0x64ae0
leaq 0x20(%rbx), %rdi
leaq 0x483440(%rip), %r14    # 0x4e7ed8
leaq 0x8(%r14), %rsi
callq 0x681de
leaq 0x30(%rbx), %rdi
addq $0x18, %r14
movq %r14, %rsi
callq 0x681de
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x64abf
callq 0xe5b0
movq $0x0, 0x8(%rbx)
movl $0x0, 0x10(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movl 0x10(%rbx), %r12d
leal (,%r12,4), %eax
movslq %eax, %r15
shlq $0x4, %r15
testl %r12d, %r12d
movq $-0x1, %r13
cmovnsq %r15, %r13
movq %r13, %rdi
callq 0xe030
movq %rax, %rbp
movq $0x0, 0x8(%rsp)
testl %r12d, %r12d
je 0x64b3f
xorl %r12d, %r12d
leaq (%r12,%rbp), %rdi
callq 0x680a4
addq $0x10, %r12
cmpq %r12, %r15
jne 0x64b27
movslq 0x10(%rbx), %rax
jmp 0x64b41
xorl %eax, %eax
movq 0x8(%rbx), %rcx
shlq $0x4, %rax
leaq (%rcx,%rax), %r13
addq $-0x10, %r13
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x6c745
cmpl $0x0, 0x10(%rbx)
jle 0x64cf3
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq %rbx, 0x30(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rbp, %r14
movq %r13, %rbp
movq 0x8(%rbx), %r13
addq %r15, %r13
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x6c745
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jae 0x64bbe
movsd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jae 0x64c3f
movq %r14, %rbp
jmp 0x64cdf
movsd 0x10(%rsp), %xmm2
ucomisd %xmm2, %xmm1
movsd %xmm0, 0x18(%rsp)
jbe 0x64cb3
movapd %xmm0, %xmm1
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x682e3
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x6870c
leaq 0x58(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x682cf
movq 0x8(%rsp), %rbx
movslq %ebx, %rdi
incl %ebx
shlq $0x4, %rdi
movq %r14, %rbp
addq %r14, %rdi
leaq 0x58(%rsp), %rsi
callq 0x681de
jmp 0x64cbb
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movsd %xmm0, 0x18(%rsp)
callq 0x682e3
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
callq 0x6870c
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
leaq 0x48(%rsp), %rdx
callq 0x682cf
movq 0x8(%rsp), %rax
movslq %eax, %rdi
incl %eax
movq %rax, 0x8(%rsp)
shlq $0x4, %rdi
movq %r14, %rbp
addq %r14, %rdi
leaq 0x58(%rsp), %rsi
callq 0x681de
jmp 0x64cd9
movq %r14, %rbp
movq 0x8(%rsp), %rbx
movslq %ebx, %rdi
incl %ebx
movq %rbx, 0x8(%rsp)
shlq $0x4, %rdi
addq %rbp, %rdi
movq %r13, %rsi
callq 0x681de
movq 0x30(%rsp), %rbx
movsd 0x18(%rsp), %xmm0
incq %r12
movslq 0x10(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x64b7c
leaq 0x20(%rbx), %rdi
leaq 0x4831da(%rip), %r12    # 0x4e7ed8
leaq 0x8(%r12), %rsi
callq 0x681de
leaq 0x30(%rbx), %rdi
addq $0x18, %r12
movq %r12, %rsi
callq 0x681de
movq 0x8(%rsp), %rax
movl %eax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x64d31
callq 0xe5b0
movl 0x10(%rbx), %eax
movslq %eax, %r15
movq %r15, %r14
shlq $0x4, %r14
testl %r15d, %r15d
movq $-0x1, %r12
cmovnsq %r14, %r12
movq %r12, %rdi
callq 0xe030
movq %rax, %r13
testl %r15d, %r15d
je 0x64db4
xorl %r15d, %r15d
leaq (%r15,%r13), %rdi
callq 0x680a4
addq $0x10, %r15
cmpq %r15, %r14
jne 0x64d5c
cmpl $0x0, 0x10(%rbx)
movq %r13, 0x8(%rbx)
movq 0x28(%rsp), %r12
jle 0x64db8
xorl %r14d, %r14d
xorl %r15d, %r15d
leaq (%r14,%rbp), %rsi
movq 0x8(%rbx), %rdi
addq %r14, %rdi
callq 0x681de
movq 0x8(%rbx), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x65986
incq %r15
movslq 0x10(%rbx), %rax
addq $0x10, %r14
cmpq %rax, %r15
jl 0x64d83
jmp 0x64db8
movq %r13, 0x8(%rbx)
movq %rbp, %rdi
callq 0xe5b0
jmp 0x64ace
movq %rax, %rbx
movq %r13, %rdi
movq %r12, %rsi
jmp 0x64dd9
movq %rax, %rbx
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0