// File: 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
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x11641e
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x116411
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x2c(%rbp), %rcx
leaq -0x80(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1163d0
jmp 0x116413
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1163c3
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x1164ee
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x1164de
movslq -0x2c(%rbp), %rcx
leaq -0xd0(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x1164ce
movslq -0x34(%rbp), %rcx
leaq -0x80(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movslq -0x2c(%rbp), %rcx
leaq -0xd0(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x116465
jmp 0x1164d0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x116436
jmp 0x1164e0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x116425
leaq -0xd0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
leaq -0x190(%rbp), %rdx
callq 0x116180
cmpl $0x0, %eax
jne 0x116519
movl $0x0, -0x4(%rbp)
jmp 0x116816
leaq -0x170(%rbp), %rdi
addq $0x30, %rdi
leaq -0x170(%rbp), %rsi
leaq -0x170(%rbp), %rdx
addq $0x18, %rdx
callq 0x116830
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x1165ae
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x1165a1
movslq -0x2c(%rbp), %rax
leaq -0x170(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movslq -0x30(%rbp), %rax
leaq -0x80(%rbp), %rsi
imulq $0x18, %rax, %rax
addq %rax, %rsi
callq 0x115380
movslq -0x2c(%rbp), %rcx
leaq -0x120(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x11654f
jmp 0x1165a3
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x116542
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x1165dd
movslq -0x2c(%rbp), %rax
leaq -0x120(%rbp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x1152d0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1165b5
leaq -0x1b0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x120(%rbp), %rdx
addq $0x18, %rdx
callq 0x116830
leaq -0x1b0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
addq $0x30, %rsi
callq 0x115380
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x116630
movsd 0xdc152(%rip), %xmm0    # 0x1f2778
movsd %xmm0, -0x1b8(%rbp)
jmp 0x116640
movsd 0xdba18(%rip), %xmm0    # 0x1f2050
movsd %xmm0, -0x1b8(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x116672
movslq -0x2c(%rbp), %rax
movsd -0x1b0(%rbp,%rax,8), %xmm0
movslq -0x2c(%rbp), %rax
movsd %xmm0, -0xf0(%rbp,%rax,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x116647
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x116742
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x116732
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0x34(%rbp)
cmpl $0x3, -0x34(%rbp)
jge 0x116722
movslq -0x34(%rbp), %rcx
leaq -0x120(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x34(%rbp), %rcx
leaq -0x170(%rbp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1166b6
jmp 0x116724
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x11668a
jmp 0x116734
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x116679
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x1d8(%rbp)
movsd -0x190(%rbp), %xmm0
movaps 0xdb922(%rip), %xmm1    # 0x1f2080
pand %xmm1, %xmm0
callq 0x53f0
movaps %xmm0, %xmm1
movsd -0x1d8(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x1d0(%rbp)
movsd -0x188(%rbp), %xmm0
movaps 0xdb8f3(%rip), %xmm1    # 0x1f2080
pand %xmm1, %xmm0
callq 0x53f0
movaps %xmm0, %xmm1
movsd -0x1d0(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x1c8(%rbp)
movsd -0x1b8(%rbp), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movsd -0x180(%rbp), %xmm0
movaps 0xdb8b4(%rip), %xmm1    # 0x1f2080
pand %xmm1, %xmm0
callq 0x53f0
movsd -0x1c8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x1c0(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopw %cs:(%rax,%rax)