// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/mutation.c
// Function: Pred_mut_str
float Pred_mut_str(float C_size, float C_stab, float C_dist,
		   float *DE, float *MSD_mut, float *MSD_cross,
		   float *A_mut, float *A_nomut, float *A_cross, float *offset,
		   float *pred_mut_3, float *pred_mut_2, float *pred_mut_tot_2,
		   int Na, int *n_cont_mut, float *Force, float *Force_coeff,
		   float *pred_mut_Cart, int N_Cart, 
		   float *pred_mut_Tors, int N_axes,
		   char *AAwt, int *Posmut, char *AAmut, int Nmut,
		   int **clist, int **cnum, struct interaction *Int_list,
		   struct residue *seq, int Nres, atom *atoms, int *resatom,
		   struct Reference Ref_kin,
		   int *kin_atom, struct Normal_Mode NM,
		   float *sigma2, float **B3, float *B_pred, 
		   float *str_diff_3, float *str_diff_2)
{
  // Compute force
  *n_cont_mut=
    Mutation_force(Force, N_Cart, C_size, C_stab, C_dist, 
		   AAwt, Posmut, AAmut, Nmut, clist, cnum, Int_list,
		   seq, Nres, atoms, Ref_kin, kin_atom);

  // Compute deformation mut_Cart and mut_Tors
  Compute_mut_def(pred_mut_Cart, N_Cart, pred_mut_Tors, N_axes,
		  DE, Force_coeff, Force, NM, sigma2, Ref_kin.mass_sum);
      
  // Deformation of resatom mut_3, mut_abs2 and MSD_mut
  float pred_mut_ave[3], *str=pred_mut_3; int i, j;
  for(j=0; j<3; j++)pred_mut_ave[j]=0;
  for(i=0; i<Na; i++){
    float *x=pred_mut_Cart+3*kin_atom[resatom[i]];
    for(j=0; j<3; j++){*str=*x; pred_mut_ave[j]+=*x; x++; str++;}
  }
  // Subtract center of mass
  for(j=0; j<3; j++)pred_mut_ave[j]/=Na;
  *MSD_mut=0; str=pred_mut_3;
  for(i=0; i<Na; i++){
    double d2=0;
    for(j=0; j<3; j++){*str-=pred_mut_ave[j]; d2+=(*str)*(*str); str++;}
    *MSD_mut+=d2;
    pred_mut_2[i]=d2;
  }
  *MSD_mut/=Na;

  //for(i=0; i<20; i++){printf("%.3g ",pred_mut_2[i]);} printf("\n");
  //printf("Predicted MSD: %.4g\n", *MSD_mut);

  *MSD_cross=0;
  // No mutant structure is present for computing observed differences
  if((str_diff_3==NULL)||(str_diff_2==NULL))return(0);


  // pred_mut_cross[i]= 2 pred_mut[i] X B3[i]
  // B3_0i = sum_a s_a va_i (B_EVEC==0)
  // Does it make sense? va_i and -va_i are equivalent,
  // the dot product should be zero... 
  float pred_mut_cross[Na];
  for(i=0; i<Na; i++){pred_mut_cross[i]=0;}
  for(int a=0; a<amax; a++){ // amax=1 (B_EVEC==0) or 3 (B_EVEC==1)
    float *bb=B3[a]; str=pred_mut_3;
    // Scalar product pred_mut_3*B3
    for(i=0; i<Na; i++){
      double d_cross=0;
      for(j=0; j<3; j++){d_cross+=(*str)*(*bb); str++; bb++;}
      pred_mut_cross[i]+=d_cross*d_cross;
    }
  }
  (*MSD_cross)=0;
  for(i=0; i<Na; i++){
    pred_mut_cross[i]=4*sqrt(pred_mut_cross[i]); // Note the factor 4!
    (*MSD_cross)+=pred_mut_cross[i];
  }
  *MSD_cross/=Na;

  // Fit observed deformation profile squared str_diff_2[i] 
  float score;
  int Npar=3;
  float D_out[Npar], Y_pred[Na], *X[Na];
  for(i=0; i<Na; i++){
    X[i]=malloc(Npar*sizeof(float));
    X[i][0]=pred_mut_2[i];
    X[i][1]=B_pred[i];
    X[i][2]=pred_mut_cross[i];
  }

  struct ridge_fit fit; fit.A=malloc(Npar*sizeof(float)); char type;
  if(ini_fit){type='C';} // Specific-heat fit
  else{type='L'; fit.Lambda=Lambda_RRR;}
  score=Ridge_regression(&fit, Y_pred, D_out, "ridge_regression",
			 X, str_diff_2, Na, Npar, type);
  printf("RRR3: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
	 fit.A[0], fit.A[1], fit.A[2]);

  if(fit.A[0]<=0 || fit.A[1]<=0){ //*A_mut<=0 *A_nomut<0
    // Fit without A_cross
    fit.A[2]=0; Npar=2;
    score=Ridge_regression(&fit, Y_pred, D_out, "ridge_regression",
			   X, str_diff_2, Na, Npar, type);
    printf("RRR2: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
	   fit.A[0], fit.A[1], fit.A[2]);
  }

  Lambda_RRR=fit.Lambda;
  *A_mut=fit.A[0];
  *A_nomut=fit.A[1];
  *A_cross=fit.A[2];
  *offset=0;
  for(i=0; i<Na; i++){
    (*offset)+=str_diff_2[i]-Y_pred[i];
    pred_mut_tot_2[i]=Y_pred[i];
    free(X[i]);
  }
  (*offset)/=Na;
  free(fit.A);

  ini_fit=0;

  if(*A_nomut<=0){
    // Fit only with the predicted effect of mutation
    score=Fit_1(str_diff_2, pred_mut_2, Na, A_mut,offset);
    *A_nomut=0; *A_cross=0;
    printf("Fit1: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
	   *A_mut, *A_nomut, *A_cross);
  }else if(*A_mut<=0){
    // Fit only with thermal fluctuations
    score=Fit_1(str_diff_2, B_pred, Na, A_nomut,offset);
    *A_mut=0; *A_cross=0;
    printf("Fit1: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
	   *A_mut, *A_nomut, *A_cross);
  }

  return(score);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, -0x70(%rbp)
movq %r8, -0x78(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, -0x30(%rbp)
movq 0xf8(%rbp), %r12
movq 0x50(%rbp), %rbx
movapd 0x90(%rbp), %xmm3
movaps 0xa0(%rbp), %xmm4
movq 0x40(%rbp), %r13
movl 0x58(%rbp), %esi
movq 0x70(%rbp), %rdx
movq 0x78(%rbp), %rcx
movq 0x80(%rbp), %r8
movl 0x88(%rbp), %r9d
movl 0xb0(%rbp), %eax
movq 0xb8(%rbp), %rdi
subq $0x70, %rsp
movups 0xc8(%rbp), %xmm5
movups 0xd8(%rbp), %xmm6
movups 0xe8(%rbp), %xmm7
movups %xmm7, 0x50(%rsp)
movups %xmm6, 0x40(%rsp)
movups %xmm5, 0x30(%rsp)
movq %r12, 0x60(%rsp)
movq %rdi, 0x28(%rsp)
movl %eax, 0x20(%rsp)
movups %xmm4, 0x10(%rsp)
movupd %xmm3, (%rsp)
movq %r13, %rdi
callq 0x3ee4a
addq $0x70, %rsp
movq 0x38(%rbp), %rcx
movl %eax, (%rcx)
subq $0xe0, %rsp
leaq 0x8(%rsp), %rdi
leaq 0x100(%rbp), %rsi
movl $0x1a, %ecx
rep  movsq (%rsi), %es:(%rdi)
movq 0x1d0(%rbp), %rax
movq %rax, 0xd8(%rsp)
movq %r13, (%rsp)
movq %rbx, %rdi
movl 0x58(%rbp), %esi
movq 0x60(%rbp), %rdx
movl 0x68(%rbp), %ecx
movq -0x30(%rbp), %r8
movq 0x48(%rbp), %r9
callq 0x3f30d
addq $0xe0, %rsp
movl $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl 0x30(%rbp), %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
testl %eax, %eax
jle 0x3e92b
movq 0xc0(%rbp), %rax
xorl %ecx, %ecx
movq 0x18(%rbp), %rdx
movslq (%rax,%rcx,4), %rsi
movl (%r12,%rsi,4), %esi
leal (%rsi,%rsi,2), %esi
movslq %esi, %rsi
leaq (%rbx,%rsi,4), %rdi
xorl %esi, %esi
movss (%rdi,%rsi), %xmm0
movss %xmm0, (%rdx,%rsi)
movss (%rdi,%rsi), %xmm0
addss -0x50(%rbp,%rsi), %xmm0
movss %xmm0, -0x50(%rbp,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x3e8fa
incq %rcx
addq %rsi, %rdx
cmpq -0x30(%rbp), %rcx
jne 0x3e8e6
movq 0x1e8(%rbp), %rax
movq 0x1e0(%rbp), %r10
movq 0x1d8(%rbp), %r12
movq 0x20(%rbp), %r11
movl 0x30(%rbp), %ebx
cvtsi2ss %ebx, %xmm2
xorl %ecx, %ecx
movss -0x50(%rbp,%rcx,4), %xmm0
divss %xmm2, %xmm0
movss %xmm0, -0x50(%rbp,%rcx,4)
incq %rcx
cmpq $0x3, %rcx
jne 0x3e94d
movl $0x0, (%r14)
testl %ebx, %ebx
jle 0x3e9cd
xorl %ecx, %ecx
movq 0x18(%rbp), %rdx
xorps %xmm0, %xmm0
xorl %esi, %esi
movss (%rdx,%rsi), %xmm1
subss -0x50(%rbp,%rsi), %xmm1
movss %xmm1, (%rdx,%rsi)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x3e97c
xorps %xmm1, %xmm1
cvtss2sd (%r14), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r14)
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r11,%rcx,4)
incq %rcx
addq %rsi, %rdx
cmpq -0x30(%rbp), %rcx
jne 0x3e977
movss (%r14), %xmm0
divss %xmm2, %xmm0
movss %xmm0, (%r14)
movl $0x0, (%r15)
testq %rax, %rax
sete %al
cmpq $0x0, 0x1f0(%rbp)
sete %cl
orb %al, %cl
xorps %xmm0, %xmm0
jne 0x3ee3b
movq %rsp, -0x60(%rbp)
movq %rsp, %r14
movq -0x30(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0x40(%rbp)
subq %rax, %r14
movq %r14, %rsp
testl %ebx, %ebx
movss %xmm2, -0x34(%rbp)
jle 0x3ea4e
movq -0x30(%rbp), %rax
leaq (,%rax,4), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2110
movss -0x34(%rbp), %xmm2
movq 0x20(%rbp), %r11
movq 0x1e0(%rbp), %r10
movslq 0x2a513(%rip), %rax     # 0x68f68
testq %rax, %rax
jle 0x3eac4
xorl %ecx, %ecx
testl %ebx, %ebx
jle 0x3eabc
movq (%r12,%rcx,8), %rdx
xorl %esi, %esi
movq 0x18(%rbp), %r8
leaq 0xc(%r8), %rdi
xorps %xmm0, %xmm0
xorl %r9d, %r9d
movss (%r8,%r9,4), %xmm1
mulss (%rdx,%r9,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r9
cmpl $0x3, %r9d
jne 0x3ea74
addq $0xc, %rdx
xorps %xmm1, %xmm1
cvtss2sd (%r14,%rsi,4), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rsi,4)
incq %rsi
movq %rdi, %r8
cmpq -0x30(%rbp), %rsi
jne 0x3ea6a
incq %rcx
cmpq %rax, %rcx
jne 0x3ea5c
movl $0x0, (%r15)
testl %ebx, %ebx
jle 0x3eb33
xorl %r12d, %r12d
xorps %xmm1, %xmm1
movsd 0x1844b(%rip), %xmm3    # 0x56f28
xorps %xmm0, %xmm0
cvtss2sd (%r14,%r12,4), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3eaf2
sqrtsd %xmm0, %xmm0
jmp 0x3eb12
callq 0x2290
movsd 0x18429(%rip), %xmm3    # 0x56f28
xorps %xmm1, %xmm1
movss -0x34(%rbp), %xmm2
movq 0x20(%rbp), %r11
movq 0x1e0(%rbp), %r10
mulsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%r12,4)
addss (%r15), %xmm0
movss %xmm0, (%r15)
incq %r12
cmpq %r12, -0x30(%rbp)
jne 0x3eadd
movss (%r15), %xmm0
divss %xmm2, %xmm0
movss %xmm0, (%r15)
movq %rsp, %rax
addq $-0x10, %rax
movq %rax, -0x68(%rbp)
movq %rax, %rsp
movq %rsp, %rax
subq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq %rax, %rsp
movq %rsp, %r13
movq -0x30(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testl %ebx, %ebx
jle 0x3ebbd
movq %r10, %r12
movq %r11, %r15
xorl %ebx, %ebx
movl $0xc, %edi
callq 0x21d0
movq %rax, (%r13,%rbx,8)
movss (%r15,%rbx,4), %xmm0
movss %xmm0, (%rax)
movss (%r12,%rbx,4), %xmm0
movss %xmm0, 0x4(%rax)
movss (%r14,%rbx,4), %xmm0
movss %xmm0, 0x8(%rax)
incq %rbx
cmpq %rbx, -0x30(%rbp)
jne 0x3eb85
movl $0xc, %edi
callq 0x21d0
movq %rax, -0xd0(%rbp)
movb $0x43, %al
cmpl $0x0, 0x2a38d(%rip)     # 0x68f64
jne 0x3ebeb
movss 0x3a9af(%rip), %xmm0    # 0x79590
movss %xmm0, -0xc4(%rbp)
movb $0x4c, %al
subq $0x8, %rsp
movzbl %al, %eax
leaq 0x1da47(%rip), %rcx     # 0x5c640
leaq -0xd0(%rbp), %r14
movq %r14, %rdi
movq -0x88(%rbp), %r12
movq %r12, %rsi
movq -0x68(%rbp), %rdx
movq %r13, %r8
movq 0x1f0(%rbp), %r9
movq %rax, -0x80(%rbp)
pushq %rax
pushq $0x3
movl 0x30(%rbp), %eax
pushq %rax
callq 0x2aff0
addq $0x20, %rsp
movss %xmm0, -0x40(%rbp)
movq (%r14), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
leaq 0x1d9fc(%rip), %rdi     # 0x5c651
movb $0x3, %al
callq 0x20f0
movq (%r14), %rax
xorps %xmm0, %xmm0
ucomiss (%rax), %xmm0
movq -0x58(%rbp), %rcx
movq 0x28(%rbp), %rdx
jae 0x3ec75
ucomiss 0x4(%rax), %xmm0
jb 0x3ece4
movl $0x0, 0x8(%rax)
subq $0x8, %rsp
movq %rcx, %r15
leaq 0x1d9b6(%rip), %rcx     # 0x5c640
movq %r14, %rdi
movq %r12, %rsi
movq -0x68(%rbp), %rdx
movq %r13, %r8
movq 0x1f0(%rbp), %r9
pushq -0x80(%rbp)
pushq $0x2
movl 0x30(%rbp), %eax
pushq %rax
callq 0x2aff0
addq $0x20, %rsp
movss %xmm0, -0x40(%rbp)
movq (%r14), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
leaq 0x1d9aa(%rip), %rdi     # 0x5c680
movb $0x3, %al
callq 0x20f0
movq %r15, %rcx
movq 0x28(%rbp), %rdx
movss -0xc4(%rbp), %xmm0
movss %xmm0, 0x3a89c(%rip)    # 0x79590
movq -0xd0(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, (%rcx)
movss 0x4(%rax), %xmm0
movq -0x78(%rbp), %rcx
movss %xmm0, (%rcx)
movss 0x8(%rax), %xmm0
movq -0x70(%rbp), %rax
movss %xmm0, (%rax)
movq 0x10(%rbp), %rax
movl $0x0, (%rax)
movl 0x30(%rbp), %ecx
testl %ecx, %ecx
movq 0x1f0(%rbp), %r15
jle 0x3ed77
xorl %ebx, %ebx
movss (%r15,%rbx,4), %xmm0
movss (%r12,%rbx,4), %xmm1
subss %xmm1, %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
movss %xmm1, (%rdx,%rbx,4)
movq (%r13,%rbx,8), %rdi
movq %r12, %r14
movq %rdx, %r12
callq 0x2050
movq %r12, %rdx
movq %r14, %r12
movq 0x10(%rbp), %rax
incq %rbx
cmpq %rbx, -0x30(%rbp)
jne 0x3ed37
movss (%rax), %xmm0
divss -0x34(%rbp), %xmm0
movss %xmm0, (%rax)
movq -0xd0(%rbp), %rdi
movq %rax, %r15
callq 0x2050
movl $0x0, 0x2a1c7(%rip)     # 0x68f64
xorps %xmm0, %xmm0
movq -0x78(%rbp), %r12
ucomiss (%r12), %xmm0
jae 0x3eddf
movq -0x58(%rbp), %rbx
ucomiss (%rbx), %xmm0
movq -0x60(%rbp), %r14
jb 0x3ee33
movq 0x1f0(%rbp), %rdi
movq 0x1e0(%rbp), %rsi
movl 0x30(%rbp), %edx
movq %r12, %rcx
movq 0x10(%rbp), %r8
callq 0x40f6d
movss %xmm0, -0x40(%rbp)
movq %rbx, %rax
jmp 0x3ee08
movq 0x1f0(%rbp), %rdi
movq 0x20(%rbp), %rsi
movl 0x30(%rbp), %edx
movq -0x58(%rbp), %rbx
movq %rbx, %rcx
movq %r15, %r8
callq 0x40f6d
movss %xmm0, -0x40(%rbp)
movq %r12, %rax
movq -0x60(%rbp), %r14
xorl %ecx, %ecx
movl %ecx, (%rax)
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
xorps %xmm0, %xmm0
cvtss2sd (%rbx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
leaq 0x1d886(%rip), %rdi     # 0x5c6af
xorps %xmm2, %xmm2
movb $0x3, %al
callq 0x20f0
movq %r14, %rsp
movss -0x40(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq