// File: /workspace/github_cpp_makefile_star2_O1/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 0x759e6(%rip), %rax     # 0xe44f0
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x8, %rdi
leaq 0x47943c(%rip), %r14    # 0x4e7f58
leaq 0x8(%r14), %rsi
movq %rdi, %rbx
callq 0x66efa
movl 0x50(%r14), %eax
movq %r15, 0x20(%rsp)
movl %eax, 0x50(%r15)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x6e6b8
movq %r12, %r15
testl %r13d, %r13d
movq %rbx, %r14
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %r12d
jne 0x6eb7c
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
leaq 0x28(%rsp), %rdi
callq 0x6915a
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x67606
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x6ebb9
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6ea2a
leaq 0x76ffd(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm0
jbe 0x6ebb9
movsd 0x3348f(%rip), %xmm1    # 0xa2038
divsd %xmm0, %xmm1
movq %r14, %rdi
movapd %xmm1, %xmm0
callq 0x676cc
testl %ebp, %ebp
sete %al
cmpl $0x2, %r12d
setne %cl
testb %cl, %al
jne 0x6ecd5
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 0x6e75f
testl %ebp, %ebp
jne 0x6ec41
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x66dfa
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x669e6
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x66af0
leaq 0x28(%rsp), %rdi
callq 0x66ce4
cmpl $0x2, %r12d
jne 0x6ec95
movsd 0x80(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x6ec72
movsd 0x333d6(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x67652
xorpd %xmm0, %xmm0
movsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x6ec95
movsd 0x333af(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x6766a
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x66dfa
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x669e6
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x66af0
leaq 0x28(%rsp), %rdi
callq 0x66ce4
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
leaq 0xc0(%rsp), %rdi
callq 0x6915a
leaq 0x28(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x69669
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x67606
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6ed26
jmp 0x6ed47
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x66ce4
jmp 0x6ed4a
jmp 0x6ed47
jmp 0x6ed47
jmp 0x6ed47
jmp 0x6ed47
jmp 0x6ed47
movq %rax, %r14
movq %r15, %rdi
jmp 0x6ed4f
movq %rax, %r14
movq 0x20(%rsp), %rdi
callq 0x66ce4
movq %r14, %rdi
callq 0xe7e0