// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movq %r9, -0x68(%rbp)
movq %r8, -0x78(%rbp)
movq %rcx, -0x48(%rbp)
movq %rdx, -0x38(%rbp)
movq %rsi, %r13
movq %rdi, -0x40(%rbp)
movq 0xf8(%rbp), %rbx
movq 0xc0(%rbp), %r15
movq 0x50(%rbp), %r12
movaps 0x90(%rbp), %xmm3
movaps 0xa0(%rbp), %xmm4
movq 0x40(%rbp), %r14
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), %r10
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 %rbx, 0x60(%rsp)
movq %r10, 0x28(%rsp)
movl %eax, 0x20(%rsp)
movups %xmm4, 0x10(%rsp)
movups %xmm3, (%rsp)
movq %r14, %rdi
callq 0x39ca0
addq $0x70, %rsp
movq 0x38(%rbp), %rcx
movl %eax, (%rcx)
subq $0xe0, %rsp
leaq 0x8(%rsp), %rdi
pushq $0x1a
popq %rcx
leaq 0x100(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x1d0(%rbp), %rax
movq %rax, 0xd8(%rsp)
movq %r14, (%rsp)
movq %r12, %rdi
movl 0x58(%rbp), %esi
movq 0x60(%rbp), %rdx
movl 0x68(%rbp), %ecx
movq -0x40(%rbp), %r8
movq 0x48(%rbp), %r9
callq 0x3a11c
addq $0xe0, %rsp
xorl %eax, %eax
cmpq $0x3, %rax
je 0x397bd
andl $0x0, -0x54(%rbp,%rax,4)
incq %rax
jmp 0x397ad
xorl %eax, %eax
movl 0x30(%rbp), %ecx
testl %ecx, %ecx
movl $0x0, %r14d
cmovgl %ecx, %r14d
movq 0x18(%rbp), %rcx
movq -0x38(%rbp), %r10
movq 0x1d8(%rbp), %r11
cmpq %r14, %rax
je 0x3981d
movslq (%r15,%rax,4), %rdx
imull $0x3, (%rbx,%rdx,4), %edx
movslq %edx, %rdx
leaq (%r12,%rdx,4), %rsi
xorl %edx, %edx
cmpq $0xc, %rdx
je 0x39815
movss (%rsi,%rdx), %xmm0
movss %xmm0, (%rcx,%rdx)
addss -0x54(%rbp,%rdx), %xmm0
movss %xmm0, -0x54(%rbp,%rdx)
addq $0x4, %rdx
jmp 0x397f3
incq %rax
addq %rdx, %rcx
jmp 0x397dd
movl 0x30(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x40(%rbp)
xorl %eax, %eax
movq 0x20(%rbp), %rsi
movq 0x18(%rbp), %r15
cmpq $0x3, %rax
je 0x3984f
movss -0x54(%rbp,%rax,4), %xmm0
divss -0x40(%rbp), %xmm0
movss %xmm0, -0x54(%rbp,%rax,4)
incq %rax
jmp 0x39833
andl $0x0, (%r13)
xorl %eax, %eax
movq %r15, %rcx
cmpq %r14, %rax
je 0x398b0
xorps %xmm0, %xmm0
xorl %edx, %edx
cmpq $0xc, %rdx
je 0x3988b
movss (%rcx,%rdx), %xmm1
subss -0x54(%rbp,%rdx), %xmm1
movss %xmm1, (%rcx,%rdx)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
addq $0x4, %rdx
jmp 0x39863
cvtss2sd (%r13), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm0
movss %xmm1, (%r13)
movss %xmm0, (%rsi,%rax,4)
incq %rax
addq %rdx, %rcx
jmp 0x39859
movss (%r13), %xmm0
divss -0x40(%rbp), %xmm0
movss %xmm0, (%r13)
andl $0x0, (%r10)
cmpq $0x0, 0x1e8(%rbp)
sete %al
cmpq $0x0, 0x1f0(%rbp)
sete %cl
orb %al, %cl
xorps %xmm0, %xmm0
jne 0x39c91
movq %rsp, -0x70(%rbp)
movl 0x30(%rbp), %eax
movl %eax, %r12d
movq %rsp, %rbx
leaq 0xf(,%r12,4), %r13
andq $-0x10, %r13
subq %r13, %rbx
movq %rbx, %rsp
xorl %eax, %eax
cmpq %rax, %r14
je 0x39915
andl $0x0, (%rbx,%rax,4)
incq %rax
jmp 0x39907
movl 0x2a64d(%rip), %eax     # 0x63f68
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rax, %rcx
je 0x39985
movq (%r11,%rcx,8), %rdi
xorl %edx, %edx
movq %r15, %rsi
cmpq %r14, %rdx
je 0x39980
leaq 0xc(%rdi), %r8
xorps %xmm0, %xmm0
xorl %r9d, %r9d
cmpl $0x3, %r9d
je 0x3995e
movss (%rsi,%r9,4), %xmm1
mulss (%rdi,%r9,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r9
jmp 0x3993f
cvtss2sd (%rbx,%rdx,4), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
addq $0xc, %rsi
movss %xmm0, (%rbx,%rdx,4)
incq %rdx
movq %r8, %rdi
jmp 0x39930
incq %rcx
jmp 0x39922
andl $0x0, (%r10)
xorps %xmm0, %xmm0
xorl %r15d, %r15d
xorps %xmm1, %xmm1
movsd 0x1749e(%rip), %xmm2    # 0x50e38
cmpq %r15, %r14
je 0x399e2
cvtss2sd (%rbx,%r15,4), %xmm0
ucomisd %xmm1, %xmm0
jb 0x399b1
sqrtsd %xmm0, %xmm0
jmp 0x399c5
callq 0x2290
movsd 0x1747a(%rip), %xmm2    # 0x50e38
xorps %xmm1, %xmm1
movq -0x38(%rbp), %r10
mulsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rbx,%r15,4)
addss (%r10), %xmm0
movss %xmm0, (%r10)
incq %r15
jmp 0x3999a
divss -0x40(%rbp), %xmm0
movss %xmm0, (%r10)
movq %rsp, %rax
addq $-0x10, %rax
movq %rax, -0x60(%rbp)
movq %rax, %rsp
movq %rsp, %rax
subq %r13, %rax
movq %rax, -0x38(%rbp)
movq %rax, %rsp
movq %rsp, %r13
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
xorl %r15d, %r15d
pushq $0xc
popq %r12
cmpq %r15, %r14
je 0x39a65
movq %r12, %rdi
callq 0x21d0
movq %rax, (%r13,%r15,8)
movq 0x20(%rbp), %rcx
movss (%rcx,%r15,4), %xmm0
movss %xmm0, (%rax)
movq 0x1e0(%rbp), %rcx
movss (%rcx,%r15,4), %xmm0
movss %xmm0, 0x4(%rax)
movss (%rbx,%r15,4), %xmm0
movss %xmm0, 0x8(%rax)
incq %r15
jmp 0x39a23
pushq $0xc
popq %rdi
callq 0x21d0
movq %rax, -0xc0(%rbp)
movb $0x43, %al
cmpl $0x0, 0x2a4e7(%rip)     # 0x63f64
jne 0x39a91
movss 0x3ab09(%rip), %xmm0    # 0x74590
movss %xmm0, -0xb4(%rbp)
movb $0x4c, %al
subq $0x8, %rsp
movzbl %al, %ebx
leaq 0x1cac9(%rip), %rcx     # 0x56568
leaq -0xc0(%rbp), %r12
movq %r12, %rdi
movq -0x38(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %r13, %r8
movq 0x1f0(%rbp), %r9
pushq %rbx
pushq $0x3
movl 0x30(%rbp), %eax
pushq %rax
callq 0x27fe8
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq (%r12), %rax
cvtss2sd (%rax), %xmm0
cvtss2sd 0x4(%rax), %xmm1
cvtss2sd 0x8(%rax), %xmm2
leaq 0x1ca90(%rip), %rdi     # 0x56579
movb $0x3, %al
callq 0x20f0
movq (%r12), %rax
xorps %xmm0, %xmm0
ucomiss (%rax), %xmm0
movq -0x78(%rbp), %r15
jae 0x39b06
ucomiss 0x4(%rax), %xmm0
jb 0x39b63
andl $0x0, 0x8(%rax)
subq $0x8, %rsp
leaq 0x1ca53(%rip), %rcx     # 0x56568
movq %r12, %rdi
movq -0x38(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %r13, %r8
movq 0x1f0(%rbp), %r9
pushq %rbx
pushq $0x2
movl 0x30(%rbp), %eax
pushq %rax
callq 0x27fe8
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq (%r12), %rax
cvtss2sd (%rax), %xmm0
cvtss2sd 0x4(%rax), %xmm1
cvtss2sd 0x8(%rax), %xmm2
leaq 0x1ca50(%rip), %rdi     # 0x565a8
movb $0x3, %al
callq 0x20f0
movq (%r12), %rax
movq -0x68(%rbp), %r12
movss -0xb4(%rbp), %xmm0
movss %xmm0, 0x3aa19(%rip)    # 0x74590
movss (%rax), %xmm0
movq -0x48(%rbp), %rcx
movss %xmm0, (%rcx)
movss 0x4(%rax), %xmm0
movss %xmm0, (%r15)
movss 0x8(%rax), %xmm0
movss %xmm0, (%r12)
movq 0x10(%rbp), %rax
andl $0x0, (%rax)
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x39be3
movq 0x1f0(%rbp), %rax
movss (%rax,%rbx,4), %xmm0
movq -0x38(%rbp), %rax
movss (%rax,%rbx,4), %xmm1
subss %xmm1, %xmm0
movq 0x10(%rbp), %rax
addss (%rax), %xmm0
movss %xmm0, (%rax)
movq 0x28(%rbp), %rax
movss %xmm1, (%rax,%rbx,4)
movq (%r13,%rbx,8), %rdi
callq 0x2050
incq %rbx
jmp 0x39ba1
movq 0x10(%rbp), %r14
movss (%r14), %xmm0
divss -0x40(%rbp), %xmm0
movss %xmm0, (%r14)
movq -0xc0(%rbp), %rdi
callq 0x2050
andl $0x0, 0x2a35b(%rip)     # 0x63f64
xorps %xmm0, %xmm0
ucomiss (%r15), %xmm0
jae 0x39c41
movq -0x48(%rbp), %rbx
ucomiss (%rbx), %xmm0
jb 0x39c88
movq 0x1f0(%rbp), %rdi
movq 0x1e0(%rbp), %rsi
movl 0x30(%rbp), %edx
movq %r15, %rcx
movq %r14, %r8
callq 0x3bbea
movss %xmm0, -0x2c(%rbp)
movq %rbx, %rax
jmp 0x39c66
movq 0x1f0(%rbp), %rdi
movq 0x20(%rbp), %rsi
movl 0x30(%rbp), %edx
movq -0x48(%rbp), %rbx
movq %rbx, %rcx
movq %r14, %r8
callq 0x3bbea
movss %xmm0, -0x2c(%rbp)
movq %r15, %rax
andl $0x0, (%rax)
andl $0x0, (%r12)
cvtss2sd (%rbx), %xmm0
cvtss2sd (%r15), %xmm1
leaq 0x1c959(%rip), %rdi     # 0x565d7
xorps %xmm2, %xmm2
movb $0x3, %al
callq 0x20f0
movq -0x70(%rbp), %rsp
movss -0x2c(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq