// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Align.cpp
// Function: gaps::R2NormalizationTransformation(gaps::RNArray<gaps::R2Point*> const&, int, int, int)
R2Affine 
R2NormalizationTransformation(const RNArray<R2Point *>& points,  RNBoolean translate, RNBoolean rotate, int scale) 
{
  // Initialize transformation
  R2Affine affine(R2identity_affine);

  // Compute center of mass
  R2Point centroid = R2Centroid(points);

  // Translate center of mass back to original (if not translating)
  if (!translate) {
    affine.Translate(centroid.Vector());
  }

  // Scale by inverse of radius
  if ((scale != 0) && (scale != 2)) {
    RNScalar radius = R2AverageDistance(centroid, points);
    if (RNIsPositive(radius)) affine.Scale(1.0 / radius);
  }

  // Rotate to align principal axes with XYZ
  if (rotate || (scale == 2)) {
    RNScalar variances[3] = { 0 };
    R2Diad diad = R2PrincipleAxes(centroid, points, variances);
    if (!rotate) affine.Transform(R2Affine(diad.InverseMatrix()));
    if (scale == 2) {
      if (variances[0] > 0) affine.XScale(1.0 / variances[0]);
      if (variances[1] > 0) affine.YScale(1.0 / variances[1]);
    }
    affine.Transform(R2Affine(diad.InverseMatrix()));
  }

  // Translate center of mass to origin
  affine.Translate(-(centroid.Vector()));
  
  // Return normalization transformation
  return affine;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %r8d, 0xc(%rsp)
movl %ecx, %ebp
movl %edx, %r13d
movq %rsi, %r12
movq %rdi, %r15
leaq 0x767b5(%rip), %rax     # 0xe44f0
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x8, %rdi
leaq 0x47a20b(%rip), %r14    # 0x4e7f58
leaq 0x8(%r14), %rsi
movq %rdi, %rbx
callq 0x66172
movl 0x50(%r14), %eax
movq %r15, 0x20(%rsp)
movl %eax, 0x50(%r15)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x6d8dc
movq %r12, %r15
testl %r13d, %r13d
movq %rbx, %r14
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %r12d
jne 0x6ddad
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
leaq 0x28(%rsp), %rdi
callq 0x683d2
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x6687e
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x6ddea
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6dc5b
leaq 0x77dcc(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm0
jbe 0x6ddea
movsd 0x3425e(%rip), %xmm1    # 0xa2038
divsd %xmm0, %xmm1
movq %r14, %rdi
movapd %xmm1, %xmm0
callq 0x66944
testl %ebp, %ebp
sete %al
cmpl $0x2, %r12d
setne %cl
testb %cl, %al
jne 0x6df06
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %rcx
movapd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0x6d984
testl %ebp, %ebp
jne 0x6de72
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x66072
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x65c5e
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x65d68
leaq 0x28(%rsp), %rdi
callq 0x65f5c
cmpl $0x2, %r12d
jne 0x6dec6
movsd 0x80(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x6dea3
movsd 0x341a5(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x668ca
xorpd %xmm0, %xmm0
movsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x6dec6
movsd 0x3417e(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x668e2
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x66072
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x65c5e
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x65d68
leaq 0x28(%rsp), %rdi
callq 0x65f5c
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
leaq 0xc0(%rsp), %rdi
callq 0x683d2
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x6887f
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x6687e
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6df57
jmp 0x6df78
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x65f5c
jmp 0x6df7b
jmp 0x6df78
jmp 0x6df78
jmp 0x6df78
jmp 0x6df78
jmp 0x6df78
movq %rax, %r14
movq %r15, %rdi
jmp 0x6df80
movq %rax, %r14
movq 0x20(%rsp), %rdi
callq 0x65f5c
movq %r14, %rdi
callq 0xe7e0