// File: /workspace/github_c_makefile_star2_O3/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, -0x80(%rbp)
movq %r8, -0x78(%rbp)
movq %rcx, -0x70(%rbp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, -0x40(%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 0x3eecb
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 -0x40(%rbp), %r8
movq 0x48(%rbp), %r9
callq 0x3f366
movq 0x18(%rbp), %r10
addq $0xe0, %rsp
movl $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl 0x30(%rbp), %eax
movl %eax, %r13d
testl %eax, %eax
jle 0x3e966
movq 0xc0(%rbp), %rax
xorl %ecx, %ecx
movq %r10, %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)
addss -0x50(%rbp,%rsi), %xmm0
movss %xmm0, -0x50(%rbp,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x3e93b
incq %rcx
addq %rsi, %rdx
cmpq %r13, %rcx
jne 0x3e927
movq 0x1e8(%rbp), %rax
movq 0x1d8(%rbp), %r11
movq 0x20(%rbp), %rdi
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 0x3e981
movl $0x0, (%r14)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
testl %ebx, %ebx
jle 0x3ea0d
xorl %ecx, %ecx
movq %r10, %rdx
xorpd %xmm3, %xmm3
xorl %esi, %esi
movss (%rdx,%rsi), %xmm1
subss -0x50(%rbp,%rsi), %xmm1
movss %xmm1, (%rdx,%rsi)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm3
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x3e9b6
xorps %xmm1, %xmm1
cvtss2sd (%r14), %xmm1
addsd %xmm3, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r14)
xorps %xmm1, %xmm1
cvtsd2ss %xmm3, %xmm1
movss %xmm1, (%rdi,%rcx,4)
incq %rcx
addq %rsi, %rdx
cmpq %r13, %rcx
jne 0x3e9b0
movss (%r14), %xmm1
divss %xmm2, %xmm1
movss %xmm1, (%r14)
movl $0x0, (%r15)
testq %rax, %rax
sete %al
cmpq $0x0, 0x1f0(%rbp)
sete %cl
orb %al, %cl
jne 0x3eebc
movq %rsp, -0x68(%rbp)
movq %rsp, %r12
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
movq %rax, -0x38(%rbp)
subq %rax, %r12
movq %r12, %rsp
testl %ebx, %ebx
movss %xmm2, -0x40(%rbp)
jle 0x3ea80
leaq (,%r13,4), %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %r10, %r14
callq 0x2110
movq 0x1d8(%rbp), %r11
movq %r14, %r10
movss -0x40(%rbp), %xmm2
movslq 0x2a4e1(%rip), %rax     # 0x68f68
testq %rax, %rax
jle 0x3eaf4
xorl %ecx, %ecx
testl %ebx, %ebx
jle 0x3eaec
movq (%r11,%rcx,8), %rdx
xorl %esi, %esi
movq %r10, %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 0x3eaa5
addq $0xc, %rdx
xorps %xmm1, %xmm1
cvtss2sd (%r12,%rsi,4), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r12,%rsi,4)
incq %rsi
movq %rdi, %r8
cmpq %r13, %rsi
jne 0x3ea9b
incq %rcx
cmpq %rax, %rcx
jne 0x3ea8e
movl $0x0, (%r15)
testl %ebx, %ebx
jle 0x3ebf6
xorl %r14d, %r14d
xorps %xmm1, %xmm1
movsd 0x183d7(%rip), %xmm3    # 0x56ee8
xorps %xmm0, %xmm0
cvtss2sd (%r12,%r14,4), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3eb26
sqrtsd %xmm0, %xmm0
jmp 0x3eb3b
callq 0x22a0
movsd 0x183b5(%rip), %xmm3    # 0x56ee8
xorps %xmm1, %xmm1
movss -0x40(%rbp), %xmm2
mulsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r12,%r14,4)
addss (%r15), %xmm0
movss %xmm0, (%r15)
incq %r14
cmpq %r14, %r13
jne 0x3eb11
divss %xmm2, %xmm0
movss %xmm0, (%r15)
movq %rsp, %rax
addq $-0x10, %rax
movq %rax, -0x60(%rbp)
movq %rax, %rsp
movq %rsp, %r14
subq -0x38(%rbp), %r14
movq %r14, %rsp
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
testl %ebx, %ebx
jle 0x3ec33
movq %r14, -0x58(%rbp)
xorl %ebx, %ebx
movq 0x1e0(%rbp), %rcx
movq -0x38(%rbp), %r14
movl $0xc, %edi
movq %rcx, %r15
callq 0x21e0
movq %r15, %rcx
movq %rax, (%r14,%rbx,8)
movq 0x20(%rbp), %rdx
movss (%rdx,%rbx,4), %xmm0
movss %xmm0, (%rax)
movss (%r15,%rbx,4), %xmm0
movss %xmm0, 0x4(%rax)
movss (%r12,%rbx,4), %xmm0
movss %xmm0, 0x8(%rax)
incq %rbx
cmpq %rbx, %r13
jne 0x3ebae
movl 0x30(%rbp), %ebx
movq -0x58(%rbp), %r14
jmp 0x3ec3a
xorps %xmm0, %xmm0
divss %xmm2, %xmm0
movss %xmm0, (%r15)
movq %rsp, %rax
addq $-0x10, %rax
movq %rax, -0x60(%rbp)
movq %rax, %rsp
movq %rsp, %r14
subq -0x38(%rbp), %r14
movq %r14, %rsp
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq 0x1e0(%rbp), %r15
movl $0xc, %edi
callq 0x21e0
movq %rax, -0xd0(%rbp)
movb $0x43, %al
cmpl $0x0, 0x2a310(%rip)     # 0x68f64
jne 0x3ec68
movss 0x3a922(%rip), %xmm0    # 0x79580
movss %xmm0, -0xc4(%rbp)
movb $0x4c, %al
subq $0x8, %rsp
movzbl %al, %eax
leaq 0x1d98a(%rip), %rcx     # 0x5c600
leaq -0xd0(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq -0x60(%rbp), %rdx
movq -0x38(%rbp), %r8
movq 0x1f0(%rbp), %r9
movq %rax, -0x88(%rbp)
pushq %rax
pushq $0x3
pushq %rbx
callq 0x2ac4c
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq (%r12), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
leaq 0x1d944(%rip), %rdi     # 0x5c611
movb $0x3, %al
callq 0x20f0
movq (%r12), %rdi
xorps %xmm0, %xmm0
ucomiss (%rdi), %xmm0
movq %r14, -0x58(%rbp)
jae 0x3ecea
ucomiss 0x4(%rdi), %xmm0
jb 0x3ed55
movl $0x0, 0x8(%rdi)
subq $0x8, %rsp
leaq 0x1d904(%rip), %rcx     # 0x5c600
movq %r12, %rdi
movq %r14, %rsi
movq -0x60(%rbp), %rdx
movq -0x38(%rbp), %r8
movq 0x1f0(%rbp), %r9
pushq -0x88(%rbp)
pushq $0x2
pushq %rbx
callq 0x2ac4c
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq (%r12), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
leaq 0x1d8f6(%rip), %rdi     # 0x5c640
movb $0x3, %al
callq 0x20f0
movq (%r12), %rdi
movq %rbx, %rcx
movss -0x40(%rbp), %xmm1
movss -0xc4(%rbp), %xmm0
movss %xmm0, 0x3a813(%rip)    # 0x79580
movss (%rdi), %xmm0
movq -0x70(%rbp), %rax
movq %rax, %r14
movss %xmm0, (%rax)
movss 0x4(%rdi), %xmm0
movq -0x78(%rbp), %r12
movss %xmm0, (%r12)
movss 0x8(%rdi), %xmm0
movq -0x80(%rbp), %rax
movss %xmm0, (%rax)
movq 0x10(%rbp), %rbx
movl $0x0, (%rbx)
xorps %xmm0, %xmm0
testl %ecx, %ecx
jle 0x3ee15
xorl %r12d, %r12d
movq -0x38(%rbp), %rbx
movq -0x58(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x1f0(%rbp), %rax
movss (%rax,%r12,4), %xmm0
movss (%r15,%r12,4), %xmm1
subss %xmm1, %xmm0
movq 0x10(%rbp), %rax
addss (%rax), %xmm0
movss %xmm0, (%rax)
movss %xmm1, (%r14,%r12,4)
movq (%rbx,%r12,8), %rdi
callq 0x2050
incq %r12
cmpq %r12, %r13
jne 0x3edb8
movq 0x10(%rbp), %rbx
movss (%rbx), %xmm0
movq -0xd0(%rbp), %rdi
movq -0x78(%rbp), %r12
movq -0x70(%rbp), %r14
movq 0x1e0(%rbp), %r15
movss -0x40(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, (%rbx)
callq 0x2050
movl $0x0, 0x2a138(%rip)     # 0x68f64
xorps %xmm0, %xmm0
ucomiss (%r12), %xmm0
jae 0x3ee63
ucomiss (%r14), %xmm0
movq -0x68(%rbp), %rbx
jb 0x3eeb4
movq 0x1f0(%rbp), %rdi
movq %r15, %rsi
movl 0x30(%rbp), %edx
movq %r12, %rcx
movq 0x10(%rbp), %r8
callq 0x40f14
movss %xmm0, -0x2c(%rbp)
movq %r14, %rax
jmp 0x3ee88
movq 0x1f0(%rbp), %rdi
movq 0x20(%rbp), %rsi
movl 0x30(%rbp), %edx
movq %r14, %rcx
movq %rbx, %r8
callq 0x40f14
movss %xmm0, -0x2c(%rbp)
movq %r12, %rax
movq -0x68(%rbp), %rbx
xorl %ecx, %ecx
movl %ecx, (%rax)
movq -0x80(%rbp), %rax
movl %ecx, (%rax)
xorps %xmm0, %xmm0
cvtss2sd (%r14), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
leaq 0x1d7c5(%rip), %rdi     # 0x5c66f
xorps %xmm2, %xmm2
movb $0x3, %al
callq 0x20f0
movq %rbx, %rsp
movss -0x2c(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq