// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
subq $0x170, %rsp            # imm = 0x170
movq %rdx, %rbx
movsd %xmm0, 0x8(%rsp)
movq %rsi, %r14
leaq 0xd0(%rsp), %rax
xorl %ecx, %ecx
movq %rdi, %rdx
movq %rdx, %rsi
xorl %r8d, %r8d
movsd (%rsi), %xmm0
movsd %xmm0, (%rax,%r8,8)
incq %r8
addq $0x18, %rsi
cmpq $0x3, %r8
jne 0xa1ea0
incq %rcx
addq $0x8, %rdx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0xa1e9a
leaq 0xd0(%rsp), %rax
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
leaq (%rsp,%rdx,8), %rdx
addq $0x120, %rdx            # imm = 0x120
movq %rdi, %rsi
xorl %r8d, %r8d
leaq (%rdx,%r8,8), %r9
xorpd %xmm0, %xmm0
movq %rax, %r10
xorl %r11d, %r11d
movsd (%r10), %xmm1
mulsd (%rsi,%r11,8), %xmm1
addsd %xmm1, %xmm0
incq %r11
addq $0x18, %r10
cmpq $0x3, %r11
jne 0xa1ef5
movsd %xmm0, (%r9)
incq %r8
addq $0x18, %rsi
cmpq $0x3, %r8
jne 0xa1ee7
incq %rcx
addq $0x8, %rax
cmpq $0x3, %rcx
jne 0xa1ed2
leaq 0x120(%rsp), %rdi
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0xa1d4c
testl %eax, %eax
je 0xa219e
movsd 0x48(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movupd 0x38(%rsp), %xmm2
movapd 0x50(%rsp), %xmm3
movapd %xmm2, %xmm4
shufpd $0x1, %xmm1, %xmm4      # xmm4 = xmm4[1],xmm1[0]
mulpd %xmm3, %xmm4
mulsd %xmm3, %xmm1
shufpd $0x1, %xmm0, %xmm3      # xmm3 = xmm3[1],xmm0[0]
mulpd %xmm2, %xmm3
subpd %xmm4, %xmm3
movapd %xmm3, 0x60(%rsp)
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rsp)
leaq 0x80(%rsp), %rax
leaq 0xe0(%rsp), %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
movsd 0x30(%rsp,%rsi,8), %xmm0
movsd 0x38(%rsp,%rsi,8), %xmm1
movsd 0x40(%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 0xa1fc9
incq %rdx
addq $0x18, %rax
cmpq $0x3, %rdx
jne 0xa1fae
movl $0x10, %eax
movsd 0x80(%rsp,%rax), %xmm0
movupd 0x70(%rsp,%rax), %xmm1
movapd %xmm1, %xmm2
mulpd %xmm1, %xmm2
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
sqrtsd %xmm2, %xmm2
divsd %xmm2, %xmm0
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm1
movupd %xmm1, 0x70(%rsp,%rax)
movsd %xmm0, 0x80(%rsp,%rax)
addq $0x18, %rax
cmpq $0x58, %rax
jne 0xa200f
leaq 0x80(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd (%rax), %xmm1
movupd 0x8(%rax), %xmm3
movapd 0x20(%rax), %xmm2
movapd %xmm3, %xmm4
shufpd $0x1, %xmm1, %xmm4      # xmm4 = xmm4[1],xmm1[0]
mulpd %xmm2, %xmm4
mulsd %xmm2, %xmm1
shufpd $0x1, %xmm0, %xmm2      # xmm2 = xmm2[1],xmm0[0]
mulpd %xmm3, %xmm2
subpd %xmm4, %xmm2
mulsd %xmm3, %xmm0
subsd %xmm0, %xmm1
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
mulsd 0x38(%rax), %xmm0
movsd 0x30(%rax), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm0, %xmm3
movsd 0x40(%rax), %xmm0
mulsd %xmm1, %xmm0
addsd %xmm3, %xmm0
movapd %xmm2, 0x30(%rax)
movsd %xmm1, 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
xorpd %xmm1, %xmm1
xorl %r9d, %r9d
movsd (%rax,%r9), %xmm2
mulsd (%rsi,%r9), %xmm2
addsd %xmm2, %xmm1
addq $0x18, %r9
cmpq $0x48, %r9
jne 0xa20f7
movsd %xmm1, (%r8)
incq %rdi
addq $0x8, %rsi
cmpq $0x3, %rdi
jne 0xa20ec
incq %rcx
addq $0x8, %rax
cmpq $0x3, %rcx
jne 0xa20df
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
movapd 0xaaf5b(%rip), %xmm0    # 0x14d0a0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
andpd %xmm0, %xmm1
sqrtsd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm4
subsd %xmm1, %xmm4
andpd %xmm0, %xmm2
xorps %xmm1, %xmm1
sqrtsd %xmm2, %xmm1
andpd %xmm0, %xmm3
xorps %xmm0, %xmm0
sqrtsd %xmm3, %xmm0
leaq 0xb473a(%rip), %rcx     # 0x1568c0
mulsd (%rcx,%rax,8), %xmm0
subsd %xmm1, %xmm4
addsd %xmm4, %xmm0
movsd %xmm0, (%rbx)
movl $0x1, %eax
jmp 0xa21a0
xorl %eax, %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r14
popq %r15
retq