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

  // 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;
      }
    }
  }

  // Return normal
  R2Vector vector = *pointB - *pointA;
  R2Vector n(vector[1], -vector[0]);
  n.Normalize();

  // Flip if polygon is clockwise
  if (clockwise) n = -n;

  // Return normal
  return n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl 0x10(%rsi), %ecx
cmpl $0x2, %ecx
jg 0x63d69
leaq 0x45cd4(%rip), %rsi     # 0xa9a30
movq %rbx, %rdi
callq 0x683dc
jmp 0x63f43
movl %edx, %r12d
movq %rsi, %r14
movq 0x8(%rsi), %rbp
leal (%r12,%rcx), %eax
decl %eax
cltd
idivl %ecx
movslq %edx, %r15
shlq $0x4, %r15
addq %rbp, %r15
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x63e39
movslq %r12d, %r13
shlq $0x4, %r13
addq %r13, %rbp
movq %r15, %rdi
movq %rbp, %rsi
movsd %xmm0, (%rsp)
callq 0x6c52b
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x63e51
movq %r13, 0x10(%rsp)
movl 0x10(%r14), %ecx
cmpl $0x9, %ecx
jl 0x63e2a
leal -0x2(%r12), %ebp
movl $0x2, %r13d
movq %r15, %rsi
movsd %xmm0, 0x8(%rsp)
leal (%rcx,%rbp), %eax
cltd
idivl %ecx
movslq %edx, %r15
shlq $0x4, %r15
addq 0x8(%r14), %r15
movq %r15, %rdi
callq 0x6c52b
addsd 0x8(%rsp), %xmm0
ucomisd (%rsp), %xmm0
movl 0x10(%r14), %ecx
jae 0x63e2a
incl %r13d
movslq %ecx, %rax
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
decl %ebp
cmpl %eax, %r13d
jl 0x63dd6
leal 0x1(%r12), %eax
cltd
idivl %ecx
movq 0x10(%rsp), %r13
jmp 0x63e5b
incl %r12d
movl %r12d, %eax
cltd
idivl %ecx
movslq %edx, %rax
shlq $0x4, %rax
addq %rax, %rbp
jmp 0x63eea
leal 0x1(%r12), %eax
cltd
idivl 0x10(%r14)
movq 0x8(%r14), %rax
movslq %edx, %rbp
shlq $0x4, %rbp
addq %rax, %rbp
addq %rax, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6c52b
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
seta %al
movl 0x10(%r14), %ecx
cmpl $0x9, %ecx
setge %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x63eea
movl $0x2, %r13d
movq %rbp, %rsi
movsd %xmm0, 0x8(%rsp)
leal (%r12,%r13), %eax
cltd
idivl %ecx
movslq %edx, %rbp
shlq $0x4, %rbp
addq 0x8(%r14), %rbp
movq %rbp, %rdi
callq 0x6c52b
addsd 0x8(%rsp), %xmm0
ucomisd (%rsp), %xmm0
jae 0x63eea
incl %r13d
movslq 0x10(%r14), %rcx
imulq $0x55555556, %rcx, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
cmpl %eax, %r13d
jl 0x63e9a
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x682e3
movsd 0x8(%r12), %xmm0
movsd (%r12), %xmm1
xorps 0x3e10f(%rip), %xmm1    # 0xa2020
movq %rbx, %rdi
callq 0x683d2
movq %rbx, %rdi
callq 0x685c0
cmpl $0x0, 0x40(%r14)
je 0x63f43
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6887f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68720
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop