// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rdx, %rbx
movsd %xmm0, 0x18(%rsp)
movq %rsi, %r14
leaq 0x100(%rsp), %rax
xorl %ecx, %ecx
movq %rdi, %rdx
cmpq $0x3, %rcx
je 0x792e5
movq %rdx, %rsi
xorl %r8d, %r8d
cmpq $0x3, %r8
je 0x792d8
movsd (%rsi), %xmm0
movsd %xmm0, (%rax,%r8,8)
incq %r8
addq $0x18, %rsi
jmp 0x792bf
incq %rcx
addq $0x8, %rdx
addq $0x18, %rax
jmp 0x792b3
leaq 0x100(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x79352
imulq $0x18, %rcx, %rdx
addq %rsp, %rdx
addq $0x150, %rdx            # imm = 0x150
movq %rdi, %rsi
xorl %r8d, %r8d
cmpq $0x3, %r8
je 0x79349
leaq (%rdx,%r8,8), %r9
xorpd %xmm0, %xmm0
movq %rax, %r10
xorl %r11d, %r11d
cmpq $0x3, %r11
je 0x7933b
movsd (%r10), %xmm1
mulsd (%rsi,%r11,8), %xmm1
addsd %xmm1, %xmm0
incq %r11
addq $0x18, %r10
jmp 0x7931d
movsd %xmm0, (%r9)
incq %r8
addq $0x18, %rsi
jmp 0x79309
incq %rcx
addq $0x8, %rax
jmp 0x792ef
leaq 0x150(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x7916a
testl %eax, %eax
je 0x795b0
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movupd 0xb8(%rsp), %xmm2
movapd 0xd0(%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, 0xe0(%rsp)
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0xf0(%rsp)
leaq 0x40(%rsp), %rbp
xorl %ebx, %ebx
leaq 0x100(%rsp), %r15
cmpq $0x3, %rbx
je 0x7942e
imulq $0x18, %rbx, %rax
leaq (%rsp,%rax), %r12
addq $0xb0, %r12
movq %r15, %r13
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0x79425
movq %r12, %rdi
movq %r13, %rsi
callq 0x78ab3
movsd %xmm0, (%rbp,%r14,8)
incq %r14
addq $0x18, %r13
jmp 0x79404
incq %rbx
addq $0x18, %rbp
jmp 0x793e9
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
leaq 0xb0(%rsp), %r15
cmpq $0x48, %rbx
je 0x79456
leaq (%rsp,%rbx), %rdi
addq $0x40, %rdi
callq 0x78a6c
addq $0x18, %rbx
jmp 0x7943d
movsd 0x58(%rsp), %xmm2
movsd 0x40(%rsp), %xmm0
movupd 0x48(%rsp), %xmm3
movapd 0x60(%rsp), %xmm1
movapd %xmm3, %xmm4
shufpd $0x1, %xmm0, %xmm4      # xmm4 = xmm4[1],xmm0[0]
mulpd %xmm1, %xmm4
mulsd %xmm1, %xmm0
shufpd $0x1, %xmm2, %xmm1      # xmm1 = xmm1[1],xmm2[0]
mulpd %xmm3, %xmm1
subpd %xmm4, %xmm1
movapd %xmm1, 0x90(%rsp)
mulsd %xmm3, %xmm2
subsd %xmm2, %xmm0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
mulsd 0x78(%rsp), %xmm2
movsd %xmm0, 0xa0(%rsp)
mulsd 0x70(%rsp), %xmm1
addsd %xmm2, %xmm1
mulsd 0x80(%rsp), %xmm0
addsd %xmm1, %xmm0
xorl %eax, %eax
movq 0x10(%rsp), %r10
cmpq $0x3, %rax
je 0x794ec
movsd 0x90(%rsp,%rax,8), %xmm1
movsd %xmm1, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x794d2
leaq 0x40(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x79549
imulq $0x18, %rcx, %rdx
addq %r14, %rdx
movq %r15, %rsi
xorl %edi, %edi
cmpq $0x3, %rdi
je 0x79540
leaq (%rdx,%rdi,8), %r8
xorpd %xmm1, %xmm1
xorl %r9d, %r9d
cmpq $0x48, %r9
je 0x79532
movsd (%rax,%r9), %xmm2
mulsd (%rsi,%r9), %xmm2
addsd %xmm2, %xmm1
addq $0x18, %r9
jmp 0x79516
movsd %xmm1, (%r8)
incq %rdi
addq $0x8, %rsi
jmp 0x79505
incq %rcx
addq $0x8, %rax
jmp 0x794f3
xorpd %xmm1, %xmm1
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
movapd 0x82b42(%rip), %xmm0    # 0xfc0a0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movsd 0x30(%rsp), %xmm3
andpd %xmm0, %xmm1
sqrtsd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm4
subsd %xmm1, %xmm4
andpd %xmm0, %xmm2
sqrtsd %xmm2, %xmm1
andpd %xmm0, %xmm3
sqrtsd %xmm3, %xmm0
leaq 0x8c207(%rip), %rcx     # 0x1057a0
mulsd (%rcx,%rax,8), %xmm0
subsd %xmm1, %xmm4
addsd %xmm4, %xmm0
movsd %xmm0, (%r10)
pushq $0x1
popq %rax
jmp 0x795b2
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq