// File: /workspace/github_cpp_makefile_star2_O1/Luthaf[P]zeopp/rmsd.cc
// Function: calculate_rotation_matrix(double (*) [3], double (*) [3], double, double*)
int calculate_rotation_matrix(double R[3][3],
                              double U[3][3], 
                              double E0,
                              double* residual)
{
  int i, j, k;
  double Rt[3][3], RtR[3][3];
  double left_eigenvec[3][3], right_eigenvec[3][3], eigenval[3];
  double v[3];
  double sigma;

  /* build Rt, transpose of R  */
  for (i=0; i<3; i++)
    for (j=0; j<3; j++)
      Rt[i][j] = R[j][i];

  /* make symmetric RtR = Rt X R */
  for (i=0; i<3; i++) 
    for (j=0; j<3; j++)
    {
      RtR[i][j] = 0.0;
      for (k = 0; k<3; k++)
        RtR[i][j] += Rt[k][i] * R[j][k];
    }

  if (!diagonalize_symmetric(RtR, right_eigenvec, eigenval))
    return(0);

  /* right_eigenvec's should be an orthogonal system but could be left
   * or right-handed. Let's force into right-handed system.
   */
  cross(&right_eigenvec[2][0], &right_eigenvec[0][0], &right_eigenvec[1][0]);

  /* From the Kabsch algorithm, the eigenvec's of RtR
   * are identical to the right_eigenvec's of R.
   * This means that left_eigenvec = R x right_eigenvec 
   */
  for (i=0; i<3; i++) 
    for (j=0; j<3; j++) 
      left_eigenvec[i][j] = dot(&right_eigenvec[i][0], &Rt[j][0]);

  for (i=0; i<3; i++) 
    normalize(&left_eigenvec[i][0]);

  /* 
   * Force left_eigenvec[2] to be orthogonal to the other vectors.
   * First check if the rotational matrices generated from the 
   * orthogonal eigenvectors are in a right-handed or left-handed
   * co-ordinate system - given by sigma. Sigma is needed to
   * resolve this ambiguity in calculating the RMSD.
   */
  cross(v, &left_eigenvec[0][0], &left_eigenvec[1][0]);
  if (dot(v, &left_eigenvec[2][0]) < 0.0)
    sigma = -1.0;
  else 
    sigma = 1.0;
  for (i=0; i<3; i++)
    left_eigenvec[2][i] = v[i]; 

  /* calc optimal rotation matrix U that minimises residual */
  for (i=0;i<3; i++)
    for (j=0; j<3; j++) 
    {
      U[i][j] = 0.0;
      for (k=0; k<3; k++)
        U[i][j] += left_eigenvec[k][i] * right_eigenvec[k][j];
    }
    
  *residual = E0 - (double) sqrt(fabs(eigenval[0])) 
                 - (double) sqrt(fabs(eigenval[1]))
                 - sigma * (double) sqrt(fabs(eigenval[2]));

  return (1);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdx, %rbx
movq %rsi, %r14
leaq 0xe0(%rsp), %rax
xorl %ecx, %ecx
movq %rdi, %rdx
movq %rdx, %rsi
xorl %r8d, %r8d
movsd (%rsi), %xmm1
movsd %xmm1, (%rax,%r8,8)
incq %r8
addq $0x18, %rsi
cmpq $0x3, %r8
jne 0xa3b8c
incq %rcx
addq $0x8, %rdx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0xa3b86
leaq 0xe0(%rsp), %rax
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
leaq (%rsp,%rdx,8), %rdx
addq $0x130, %rdx            # imm = 0x130
movq %rdi, %rsi
xorl %r8d, %r8d
leaq (%rdx,%r8,8), %r9
movq $0x0, (%r9)
xorpd %xmm2, %xmm2
movq %rax, %r10
xorl %r11d, %r11d
movsd (%r10), %xmm1
mulsd (%rsi,%r11,8), %xmm1
addsd %xmm1, %xmm2
incq %r11
addq $0x18, %r10
cmpq $0x3, %r11
jne 0xa3be8
movsd %xmm2, (%r9)
incq %r8
addq $0x18, %rsi
cmpq $0x3, %r8
jne 0xa3bd3
incq %rcx
addq $0x8, %rax
cmpq $0x3, %rcx
jne 0xa3bbe
movsd %xmm0, (%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x20(%rsp), %r15
leaq 0x70(%rsp), %rdx
movq %r15, %rsi
callq 0xa3a3e
testl %eax, %eax
je 0xa3ecc
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
movsd 0x30(%rsp), %xmm3
movsd 0x40(%rsp), %xmm4
movapd %xmm1, %xmm5
mulsd %xmm2, %xmm5
mulsd %xmm0, %xmm2
mulsd %xmm4, %xmm0
mulsd %xmm3, %xmm4
subsd %xmm4, %xmm5
movsd %xmm5, 0x50(%rsp)
movsd 0x38(%rsp), %xmm4
mulsd %xmm4, %xmm3
subsd %xmm2, %xmm3
movsd %xmm3, 0x58(%rsp)
mulsd %xmm1, %xmm4
subsd %xmm4, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x90(%rsp), %rax
leaq 0xf0(%rsp), %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
movsd 0x20(%rsp,%rsi,8), %xmm0
movsd 0x28(%rsp,%rsi,8), %xmm1
movsd 0x30(%rsp,%rsi,8), %xmm2
movq %rcx, %rsi
xorl %edi, %edi
movsd -0x8(%rsi), %xmm3
mulsd %xmm1, %xmm3
movsd -0x10(%rsi), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movsd (%rsi), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
movsd %xmm3, (%rax,%rdi,8)
incq %rdi
addq $0x18, %rsi
cmpq $0x3, %rdi
jne 0xa3cd5
incq %rdx
addq $0x18, %rax
cmpq $0x3, %rdx
jne 0xa3cba
movl $0x10, %r12d
xorpd %xmm2, %xmm2
movsd 0x80(%rsp,%r12), %xmm4
movsd 0x88(%rsp,%r12), %xmm3
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movapd %xmm4, %xmm1
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
movsd 0x90(%rsp,%r12), %xmm5
movapd %xmm5, %xmm0
mulsd %xmm5, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0xa3d6a
sqrtsd %xmm0, %xmm0
jmp 0xa3d97
movsd %xmm3, 0x8(%rsp)
movsd %xmm4, 0x18(%rsp)
movsd %xmm5, 0x10(%rsp)
callq 0x4350
movsd 0x10(%rsp), %xmm5
movsd 0x18(%rsp), %xmm4
movsd 0x8(%rsp), %xmm3
xorpd %xmm2, %xmm2
divsd %xmm0, %xmm4
movsd %xmm4, 0x80(%rsp,%r12)
divsd %xmm0, %xmm3
movsd %xmm3, 0x88(%rsp,%r12)
divsd %xmm0, %xmm5
movsd %xmm5, 0x90(%rsp,%r12)
addq $0x18, %r12
cmpq $0x58, %r12
jne 0xa3d20
leaq 0x90(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm3
movsd 0x28(%rax), %xmm4
movsd 0x10(%rax), %xmm1
movsd 0x20(%rax), %xmm5
movapd %xmm3, %xmm2
mulsd %xmm4, %xmm2
mulsd %xmm0, %xmm4
mulsd %xmm5, %xmm0
mulsd %xmm1, %xmm5
subsd %xmm5, %xmm2
movsd 0x18(%rax), %xmm5
mulsd %xmm5, %xmm1
subsd %xmm4, %xmm1
mulsd %xmm3, %xmm5
subsd %xmm5, %xmm0
movsd 0x38(%rax), %xmm3
mulsd %xmm1, %xmm3
movsd 0x30(%rax), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
movsd 0x40(%rax), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm4, %xmm3
movsd %xmm3, 0x8(%rsp)
movsd %xmm2, 0x30(%rax)
movsd %xmm1, 0x38(%rax)
movsd %xmm0, 0x40(%rax)
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
leaq (%r14,%rdx,8), %rdx
movq %r15, %rsi
xorl %edi, %edi
leaq (%rdx,%rdi,8), %r8
movq $0x0, (%r8)
xorpd %xmm0, %xmm0
xorl %r9d, %r9d
movsd (%rax,%r9), %xmm1
mulsd (%rsi,%r9), %xmm1
addsd %xmm1, %xmm0
addq $0x18, %r9
cmpq $0x48, %r9
jne 0xa3e75
movsd %xmm0, (%r8)
incq %rdi
addq $0x8, %rsi
cmpq $0x3, %rdi
jne 0xa3e63
incq %rcx
addq $0x8, %rax
cmpq $0x3, %rcx
jne 0xa3e56
movsd 0x70(%rsp), %xmm0
andpd 0xab1d4(%rip), %xmm0    # 0x14f090
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xa3ed3
sqrtsd %xmm0, %xmm0
jmp 0xa3edc
xorl %eax, %eax
jmp 0xa3f71
callq 0x4350
xorpd %xmm1, %xmm1
movsd (%rsp), %xmm2
subsd %xmm0, %xmm2
movsd 0x78(%rsp), %xmm0
andpd 0xab19d(%rip), %xmm0    # 0x14f090
ucomisd %xmm1, %xmm0
jb 0xa3eff
sqrtsd %xmm0, %xmm0
jmp 0xa3f0e
movsd %xmm2, (%rsp)
callq 0x4350
movsd (%rsp), %xmm2
movsd 0x80(%rsp), %xmm1
andpd 0xab171(%rip), %xmm1    # 0x14f090
subsd %xmm0, %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm1
jb 0xa3f36
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0xa3f4d
movapd %xmm1, %xmm0
movsd %xmm2, (%rsp)
callq 0x4350
xorpd %xmm3, %xmm3
movsd (%rsp), %xmm2
xorl %eax, %eax
ucomisd 0x8(%rsp), %xmm3
seta %al
leaq 0xb47a1(%rip), %rcx     # 0x158700
mulsd (%rcx,%rax,8), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%rbx)
movl $0x1, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r14
popq %r15
retq