// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::InteriorAngle(int, double) const
RNAngle R2Polygon::
InteriorAngle(int k, RNLength radius) const
{
  // Just checking
  if (npoints < 3) return 0;

  // Find first point
  R2Point *pointA = &points[(k - 1 + npoints) % npoints];
  if (radius > 0) {
    R2Point *prev = pointA;
    RNLength distance = R2Distance(*pointA, points[k]);
    if (distance < radius) {
      for (int i = 2;  i < npoints/3; i++) {
        pointA = &points[(k - i + npoints) % npoints];
        distance += R2Distance(*pointA, *prev);
        if (distance >= radius) break;
        prev = pointA;
      }
    }
  }

  // Find second point
  R2Point *pointB = &points[(k + 1) % npoints];
  if (radius > 0) {
    R2Point *prev = pointB;
    RNLength distance = R2Distance(*pointB, points[k]);
    if (distance < radius) {
      for (int i = 2;  i < npoints/3; i++) {
        pointB = &points[(k + i) % npoints];
        distance += R2Distance(*pointB, *prev);
        if (distance >= radius) break;
        prev = pointB;
      }
    }
  }

  // Compute interior angle
  R2Vector va = *pointA - points[k];
  R2Vector vb = *pointB - points[k];
  RNLength lena = va.Length();
  RNLength lenb = vb.Length();
  if (RNIsZero(lena) || RNIsZero(lenb)) return RN_UNKNOWN;
  RNAngle angle = R2InteriorAngle(va, vb);
  RNScalar cross = (va[0]*vb[1] - va[1]*vb[0]);
  if (clockwise) { if (cross < 0) angle = RN_TWO_PI - angle; }
  else { if (cross > 0) angle = RN_TWO_PI - angle; }
  return angle;
}

// Assembly:
movapd %xmm0, %xmm1
movl 0x10(%rdi), %ecx
xorpd %xmm0, %xmm0
cmpl $0x3, %ecx
jl 0x5bd69
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %r15d
movq %rdi, %rbx
movq 0x8(%rdi), %rsi
leal (%r15,%rcx), %eax
decl %eax
cltd
idivl %ecx
movslq %edx, %r13
shlq $0x4, %r13
addq %rsi, %r13
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movslq %r15d, %rax
movsd %xmm1, 0x8(%rsp)
movq %rax, 0x10(%rsp)
jbe 0x5bbe4
shlq $0x4, %rax
addq %rax, %rsi
movq %r13, %rdi
callq 0x63469
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5bbe4
leal -0x2(%r15), %ebp
pushq $0x2
popq %r14
movl 0x10(%rbx), %ecx
movl %ecx, %eax
cltd
pushq $0x3
popq %rsi
idivl %esi
cmpl %eax, %r14d
jge 0x5bbe4
leal (%rcx,%rbp), %eax
cltd
idivl %ecx
movslq %edx, %r12
shlq $0x4, %r12
addq 0x8(%rbx), %r12
movq %r12, %rdi
movq %r13, %rsi
movsd %xmm0, (%rsp)
callq 0x63469
movsd 0x8(%rsp), %xmm1
movsd (%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
movsd (%rsp), %xmm0
incl %r14d
decl %ebp
ucomisd %xmm1, %xmm0
movq %r12, %r13
jb 0x5bb8a
jmp 0x5bbe7
movq %r13, %r12
movq 0x8(%rbx), %rsi
leal 0x1(%r15), %eax
cltd
idivl 0x10(%rbx)
movslq %edx, %r13
shlq $0x4, %r13
addq %rsi, %r13
ucomisd 0x375e3(%rip), %xmm1    # 0x931e8
jbe 0x5bc7a
movq 0x10(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq %r13, %rdi
callq 0x63469
movapd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x5bc7a
pushq $0x2
popq %r14
movl 0x10(%rbx), %ecx
movl %ecx, %eax
cltd
pushq $0x3
popq %rsi
idivl %esi
cmpl %eax, %r14d
jge 0x5bc7a
leal (%r15,%r14), %eax
cltd
idivl %ecx
movslq %edx, %rbp
shlq $0x4, %rbp
addq 0x8(%rbx), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movsd %xmm1, (%rsp)
callq 0x63469
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
incl %r14d
ucomisd 0x8(%rsp), %xmm1
movq %rbp, %r13
jb 0x5bc2f
jmp 0x5bc7d
movq %r13, %rbp
movq 0x10(%rsp), %r14
shlq $0x4, %r14
movq 0x8(%rbx), %rdx
addq %r14, %rdx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x5f7c9
addq 0x8(%rbx), %r14
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x5f7c9
movq %r15, %rdi
callq 0x5f958
movsd %xmm0, (%rsp)
movq %r12, %rdi
callq 0x5f958
movsd (%rsp), %xmm4
movapd %xmm0, %xmm1
leaq 0x7eecf(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm2
movapd 0x3734b(%rip), %xmm3    # 0x93030
xorpd %xmm2, %xmm3
ucomisd %xmm3, %xmm4
movsd 0x3732b(%rip), %xmm0    # 0x93020
jb 0x5bcfd
ucomisd %xmm4, %xmm2
jae 0x5bd5b
ucomisd %xmm3, %xmm1
jb 0x5bd09
ucomisd %xmm1, %xmm2
jae 0x5bd5b
leaq 0x28(%rsp), %r14
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x63ecc
movsd (%r14), %xmm1
movsd (%r15), %xmm2
mulsd 0x8(%r14), %xmm2
mulsd 0x8(%r15), %xmm1
subsd %xmm2, %xmm1
cmpl $0x0, 0x40(%rbx)
je 0x5bd6a
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm2
jbe 0x5bd5b
leaq 0x3f219(%rip), %rax     # 0x9af68
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
ja 0x5bd48
jmp 0x5bd5b