// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/rmsd.cc
// Function: setup_rotation(double (*) [3], double (*) [3], int, double*, double*, double (*) [3], double*)
void setup_rotation(double ref_xlist[][3],
                    double mov_xlist[][3], 
                    int n_list,
                    double mov_com[3],
                    double mov_to_ref[3],
                    double R[3][3],
                    double* E0)
{
  int i, j, n;
  double ref_com[3];

  /* calculate the centre of mass */
  for (i=0; i<3; i++)
  { 
    mov_com[i] = 0.0;
    ref_com[i] = 0.0;
  }
  
  for (n=0; n<n_list; n++) 
    for (i=0; i<3; i++)
    { 
      mov_com[i] += mov_xlist[n][i];
      ref_com[i] += ref_xlist[n][i];
    }
    
  for (i=0; i<3; i++)
  {
    mov_com[i] /= n_list;
    ref_com[i] /= n_list;
    mov_to_ref[i] = ref_com[i] - mov_com[i];
  }

  /* shift mov_xlist and ref_xlist to centre of mass */
  for (n=0; n<n_list; n++) 
    for (i=0; i<3; i++)
    { 
      mov_xlist[n][i] -= mov_com[i];
      ref_xlist[n][i] -= ref_com[i];
    }

  /* initialize */
  for (i=0; i<3; i++)
    for (j=0; j<3; j++) 
      R[i][j] = 0.0;
  *E0 = 0.0;

  for (n=0; n<n_list; n++) 
  {
    /* 
     * E0 = 1/2 * sum(over n): y(n)*y(n) + x(n)*x(n) 
     */
    for (i=0; i<3; i++)
      *E0 +=  mov_xlist[n][i] * mov_xlist[n][i]  
            + ref_xlist[n][i] * ref_xlist[n][i];
    
    /*
     * correlation matrix R:   
     *   R[i,j) = sum(over n): y(n,i) * x(n,j)  
     *   where x(n) and y(n) are two vector sets   
     */
    for (i=0; i<3; i++)
    {
      for (j=0; j<3; j++)
        R[i][j] += mov_xlist[n][i] * ref_xlist[n][j];
    }
  }
  *E0 *= 0.5;
  }

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq 0x20(%rsp), %rax
xorl %r10d, %r10d
cmpq $0x3, %r10
je 0x78afb
andq $0x0, (%rcx,%r10,8)
andq $0x0, -0x20(%rsp,%r10,8)
incq %r10
jmp 0x78ae5
xorl %r11d, %r11d
testl %edx, %edx
movl $0x0, %r10d
cmovgl %edx, %r10d
movq %rsi, %rbx
movq %rdi, %r14
cmpq %r10, %r11
je 0x78b56
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x78b49
movsd (%rbx,%r15,8), %xmm0
addsd (%rcx,%r15,8), %xmm0
movsd %xmm0, (%rcx,%r15,8)
movsd (%r14,%r15,8), %xmm0
addsd -0x20(%rsp,%r15,8), %xmm0
movsd %xmm0, -0x20(%rsp,%r15,8)
incq %r15
jmp 0x78b18
incq %r11
addq $0x18, %r14
addq $0x18, %rbx
jmp 0x78b10
cvtsi2sd %edx, %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x78b97
movsd -0x20(%rsp,%rdx,8), %xmm1
movhpd (%rcx,%rdx,8), %xmm1    # xmm1 = xmm1[0],mem[0]
divpd %xmm0, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
movhpd %xmm1, (%rcx,%rdx,8)
movlpd %xmm1, -0x20(%rsp,%rdx,8)
subsd %xmm2, %xmm1
movsd %xmm1, (%r8,%rdx,8)
incq %rdx
jmp 0x78b60
xorl %edx, %edx
movq %rsi, %r8
movq %rdi, %r11
cmpq %r10, %rdx
je 0x78be1
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x78bd4
movsd (%r8,%rbx,8), %xmm0
subsd (%rcx,%rbx,8), %xmm0
movsd %xmm0, (%r8,%rbx,8)
movsd (%r11,%rbx,8), %xmm0
subsd -0x20(%rsp,%rbx,8), %xmm0
movsd %xmm0, (%r11,%rbx,8)
incq %rbx
jmp 0x78ba6
incq %rdx
addq $0x18, %r11
addq $0x18, %r8
jmp 0x78b9f
xorl %ecx, %ecx
movq %r9, %rdx
cmpq $0x3, %rcx
je 0x78c08
xorl %r8d, %r8d
cmpq $0x3, %r8
je 0x78bff
andq $0x0, (%rdx,%r8,8)
incq %r8
jmp 0x78bef
incq %rcx
addq $0x18, %rdx
jmp 0x78be6
andq $0x0, (%rax)
xorl %ecx, %ecx
movq %rsi, %rdx
movsd (%rax), %xmm0
cmpq %r10, %rcx
je 0x78ca0
xorl %r8d, %r8d
cmpq $0x3, %r8
je 0x78c4c
movsd (%rdx,%r8,8), %xmm1
movsd (%rdi,%r8,8), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %r8
jmp 0x78c21
imulq $0x18, %rcx, %r8
addq %rsi, %r8
movq %r9, %r11
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x78c90
leaq (%r8,%rbx,8), %r14
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x78c87
movsd (%r14), %xmm0
mulsd (%rdi,%r15,8), %xmm0
addsd (%r11,%r15,8), %xmm0
movsd %xmm0, (%r11,%r15,8)
incq %r15
jmp 0x78c65
incq %rbx
addq $0x18, %r11
jmp 0x78c58
incq %rcx
addq $0x18, %rdi
addq $0x18, %rdx
jmp 0x78c11
mulsd 0x83390(%rip), %xmm0    # 0xfc038
movsd %xmm0, (%rax)
popq %rbx
popq %r14
popq %r15
retq