// File: /workspace/github_c_makefile_star2_O0/ugobas[P]tnm/McLachlan.c
// Function: rmsd_mclachlan
double rmsd_mclachlan(double *x1, double *x2, double *wt1, int n)
{
    
  int i=0,j=0,m=0, ncoord=3*n;
  double cent1[3], cent2[3], rm[3][3];
  double tmpcent[3];
  double rmsd;

  double tmpatom1[ncoord], *x=x1, *t=tmpatom1;
  for(i=0; i<ncoord; i++){*t=*x; x++; t++;}

  if(DBG){
    double sum=0;
    for(i=0; i<n; i++)sum+=wt1[i];
    printf("Average mass: %.2f\n", sum/n);
  }

  // compute centre of masses for the reference
  // and the mobile proteins    
  center_of_mass(cent1,x1,n,wt1);
  center_of_mass(cent2,x2,n,wt1);
  
  if(DBG){
    printf("Centre of Mass for str1\n");
    printf("\t%.4f %.4f %.4f\n",cent1[0],cent1[1],cent1[2]);
    printf("Centre of Mass for str2\n");
    printf("\t%.4f %.4f %.4f\n",cent2[0],cent2[1],cent2[2]);
  }
  
  // centre both structures in their centres of masses
  tmpcent[0] = -cent1[0];
  tmpcent[1] = -cent1[1];
  tmpcent[2] = -cent1[2];
  
  TranslateVector(tmpatom1,tmpcent,n);
  
  tmpcent[0] = -cent2[0];
  tmpcent[1] = -cent2[1];
  tmpcent[2] = -cent2[2];
  
  TranslateVector(x2,tmpcent,n);
    
  // compute the superposition
  matfit(tmpatom1,x2,rm,n,wt1,1);
  
  // Print rotation matrix
  if(DBG){
    printf("Rotation Matrix\n");
    for (i=0;i<3;i++){
      printf("\t");
      for (j=0;j<3;j++)printf("%.4f ",rm[i][j]);
      printf("\n");
    }
  }
 
  for (i=0;i<n;i++){  
    Apply3Matrix2dot(x2+m,rm);
    m+=3;
  }
  TranslateVector(x2,cent1,n);
    
  if(DBG){
    printf("Translation vector (between Centres of masses)\n");
    printf("\t%.4f %.4f %.4f\n",cent1[0]-cent2[0],
	   cent1[1]-cent2[1],cent1[2]-cent2[2]);
  }
  
  // computes weighted rmsd
  rmsd=rmsd_w(x1,x2,n,wt1);
  
  if(DBG)printf("\t mass-weighted RMS: %.4f\n", rmsd);
  
  
  return (rmsd);
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
movq %rsp, %rax
movq %rax, -0xf0(%rbp)
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rax
subq %rdx, %rax
movq %rax, -0x110(%rbp)
movq %rax, %rsp
movq %rcx, -0xf8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x100(%rbp)
movq %rax, -0x108(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x725b2
movq -0x100(%rbp), %rax
movsd (%rax), %xmm0
movq -0x108(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x100(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x100(%rbp)
movq -0x108(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x108(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x72565
leaq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rcx
callq 0x72780
leaq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rcx
callq 0x72780
movq -0x110(%rbp), %rdi
movsd -0x50(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0x48(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0x40(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xd0(%rbp)
leaq -0xe0(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x728d0
movsd -0x70(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0x68(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0x60(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x728d0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %r8
movl $0x1, %r9d
callq 0x72970
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x7273d
movq -0x10(%rbp), %rdi
movslq -0x28(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x730b0
movl -0x28(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x28(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x72706
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x728d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rcx
callq 0x73170
movsd %xmm0, -0xe8(%rbp)
movsd -0xe8(%rbp), %xmm0
movq -0xf0(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq