// File: 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:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movl %esi, 0xe4(%rsp)
movsd %xmm0, 0xd8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpl $0x3, 0x10(%rax)
jge 0xbb583
xorps %xmm0, %xmm0
movsd %xmm0, 0xf0(%rsp)
jmp 0xbba4f
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x48(%rsp)
movl 0xe4(%rsp), %eax
subl $0x1, %eax
addl 0x10(%rcx), %eax
cltd
idivl 0x10(%rcx)
movq 0x48(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xd0(%rsp)
movsd 0xd8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xbb6fe
movq 0x50(%rsp), %rax
movq 0xd0(%rsp), %rcx
movq %rcx, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x8(%rax), %rsi
movslq 0xe4(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xcb540
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xbb6fc
movl $0x2, 0xbc(%rsp)
movq 0x50(%rsp), %rax
movl 0xbc(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movl 0x10(%rax), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0xbb6fa
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x38(%rsp)
movl 0xe4(%rsp), %eax
subl 0xbc(%rsp), %eax
addl 0x10(%rcx), %eax
cltd
idivl 0x10(%rcx)
movq 0x38(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0xcb540
addsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
ucomisd 0xd8(%rsp), %xmm0
jb 0xbb6d4
jmp 0xbb6fa
movq 0xd0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0xbb634
jmp 0xbb6fc
jmp 0xbb6fe
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x30(%rsp)
movl 0xe4(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x10(%rcx)
movq 0x30(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xb0(%rsp)
movsd 0xd8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xbb873
movq 0x50(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq %rcx, 0xa8(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x8(%rax), %rsi
movslq 0xe4(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xcb540
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xbb871
movl $0x2, 0x9c(%rsp)
movq 0x50(%rsp), %rax
movl 0x9c(%rsp), %ecx
movl %ecx, 0x2c(%rsp)
movl 0x10(%rax), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0xbb86f
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x20(%rsp)
movl 0xe4(%rsp), %eax
addl 0x9c(%rsp), %eax
cltd
idivl 0x10(%rcx)
movq 0x20(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0xcb540
addsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
ucomisd 0xd8(%rsp), %xmm0
jb 0xbb849
jmp 0xbb86f
movq 0xb0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0xbb7ac
jmp 0xbb871
jmp 0xbb873
movq 0x50(%rsp), %rax
movq 0xd0(%rsp), %rsi
movq 0x8(%rax), %rdx
movslq 0xe4(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdx
leaq 0x88(%rsp), %rdi
callq 0xc3830
movq 0x50(%rsp), %rax
movq 0xb0(%rsp), %rsi
movq 0x8(%rax), %rdx
movslq 0xe4(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdx
leaq 0x78(%rsp), %rdi
callq 0xc3830
leaq 0x88(%rsp), %rdi
callq 0xc3d60
movsd %xmm0, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xc3d60
movsd %xmm0, 0x68(%rsp)
movsd 0x70(%rsp), %xmm0
leaq 0xbcbe6(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
jne 0xbb923
movsd 0x68(%rsp), %xmm0
leaq 0xbcbcb(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0xbb939
movsd 0x776ed(%rip), %xmm0    # 0x133018
movsd %xmm0, 0xf0(%rsp)
jmp 0xbba4f
leaq 0x88(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xcd240
movsd %xmm0, 0x60(%rsp)
leaq 0x88(%rsp), %rdi
xorl %esi, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x78(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0x88(%rsp), %rdi
movl $0x1, %esi
callq 0xa5c10
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
callq 0xa5c10
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm2
movq %rax, %rcx
movq 0x50(%rsp), %rax
mulsd (%rcx), %xmm1
movq %xmm1, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
cmpl $0x0, 0x40(%rax)
je 0xbba18
xorps %xmm0, %xmm0
ucomisd 0x58(%rsp), %xmm0
jbe 0xbba16
leaq 0x7aef2(%rip), %rax     # 0x1368f8
movsd (%rax), %xmm0
subsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0xbba40
movsd 0x58(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xbba3e
leaq 0x7aeca(%rip), %rax     # 0x1368f8
movsd (%rax), %xmm0
subsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0xbba40
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm0
addq $0xf8, %rsp
retq