// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/confchange.c
// Function: Lasso_confchange_fit
int Lasso_confchange_fit(struct Tors *Diff, struct Normal_Mode *NM,
			 struct bond *bonds, atom *atoms, int natoms,
			 struct Reference Ref, float *coord_1, float *coord_2,
			 char *nameout)
{
  char out[100]; sprintf(out, "%s_Lasso.dat", nameout);
  printf("Writing %s\n", out);
  FILE *file_out=fopen(out, "w");
  
  float c[NM->N]; double c2_sum=0, sum_c1=0; int i;
  for (i=0; i<NM->N; i++){
    c[i]=Scalar_product_weighted(Diff->Cart, NM->Cart[i],
				 Ref.mass_coord, Ref.N_Cart);
    c2_sum+=c[i]*c[i]; sum_c1+=fabs(c[i]);
  }
  sum_c1/=NM->N; // average projection
  fprintf(file_out, "# %d modes 
=%.4g\n#lambda rmsd(conf,str2)\n",
	  NM->N, sum_c1);

  float cnew[NM->N], coord_new[3*Ref.N_ref], coord_all[3*natoms];
  double d_phi[NM->N_axes];

  float x[3], y[3], lambda=sum_c1, lambda_step=lambda*0.2;
  y[1]=-RMSD_Lasso_confchange(0, cnew, d_phi, coord_all, coord_new,
			      c,c2_sum,NM,bonds,atoms,natoms,Ref,coord_2);
  fprintf(file_out, "%.3f %.3f\n", 0.0, -y[1]);
  x[1]=lambda;
  y[1]=-RMSD_Lasso_confchange(lambda, cnew, d_phi, coord_all, coord_new,
			      c,c2_sum,NM,bonds,atoms,natoms,Ref,coord_2);
  fprintf(file_out, "%.3f %.3f\n", x[1], -y[1]);
  int n_down=0; float score_opt=y[1], lambda_opt=lambda;
  
  for(int iter=0; iter<100; iter++){
    if(n_down==1)break;
    x[0]=x[1]-lambda_step; if(x[0]<0)x[0]=0;
    x[2]=x[1]+lambda_step;
    for(i=0; i<=2; i+=2){
      y[i]=-RMSD_Lasso_confchange(x[i], cnew, d_phi, coord_all, coord_new,
				  c,c2_sum,NM,bonds,atoms,natoms,Ref,coord_2);
      fprintf(file_out, "%.3f %.3f\n", x[i], -y[i]);
    }
    lambda=Find_max_quad(x[0],x[1],x[2],y[0],y[1],y[2],0,100);
    float yy=-RMSD_Lasso_confchange(lambda, cnew, d_phi, coord_all, coord_new,
				    c,c2_sum,NM,bonds,atoms,natoms,Ref,coord_2);
    x[1]=lambda; y[1]=yy;
    fprintf(file_out, "%.3f %.3f\n", x[1], -y[1]);
    if(yy>score_opt){
      lambda_opt=lambda;
      if(fabs(score_opt-yy)<0.01)break;
      score_opt=yy; n_down=0;
    }else{
      n_down++;
    }
  }

  RMSD_Lasso_confchange(lambda_opt, cnew, d_phi, coord_all, coord_new,
			c,c2_sum,NM,bonds,atoms,natoms,Ref,coord_2);
  for(i=0; i<NM->N_axes; i++)Diff->Tors[i]=d_phi[i];

  fclose(file_out);
  return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r8, -0x48(%rbp)
movq %rcx, -0x78(%rbp)
movq %rdx, -0x50(%rbp)
movq %rsi, %r13
movq %rdi, -0xb8(%rbp)
leaq 0x10(%rbp), %r14
movq 0x48(%rbp), %rdx
leaq 0x26533(%rip), %rsi     # 0x54bfa
xorl %r12d, %r12d
leaq -0x160(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2260
leaq 0x1d5c5(%rip), %rdi     # 0x4bca7
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
leaq 0x22a5d(%rip), %rsi     # 0x51150
movq %rbx, %rdi
callq 0x2210
movq %rax, -0x38(%rbp)
movq %rsp, -0xe0(%rbp)
movl (%r13), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq 0x18(%r14), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movl 0x4(%r14), %r14d
movslq (%r13), %rdx
cmpq %rdx, %r12
jge 0x2e78a
movq -0xb8(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x38(%r13), %rax
movq (%rax,%r12,8), %rsi
movq %rbx, %rdx
movl %r14d, %ecx
callq 0xfd1e
movq -0x40(%rbp), %rax
movss %xmm0, (%rax,%r12,4)
movaps %xmm0, %xmm1
mulss %xmm0, %xmm1
andps 0x22abf(%rip), %xmm0    # 0x51230
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
cvtps2pd %xmm0, %xmm0
movapd -0x70(%rbp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, -0x70(%rbp)
incq %r12
jmp 0x2e732
cvtsi2sd %edx, %xmm1
movapd -0x70(%rbp), %xmm0
divsd %xmm1, %xmm0
movapd %xmm0, -0x90(%rbp)
leaq 0x26461(%rip), %rsi     # 0x54c07
movq -0x38(%rbp), %rdi
movb $0x1, %al
callq 0x2190
movl (%r13), %eax
movq %rsp, %r14
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
leaq 0x10(%rbp), %rcx
imull $0x3, (%rcx), %eax
movq %rsp, %r12
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq -0x48(%rbp), %rdx
leal (%rdx,%rdx,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movl 0x8(%r13), %eax
movq %r13, %r9
movq %r13, -0xd8(%rbp)
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
cvtsd2ss -0x90(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
cvtss2sd %xmm0, %xmm1
movsd %xmm1, -0x90(%rbp)
movsd 0x253e2(%rip), %xmm0    # 0x53c28
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x94(%rbp)
movaps -0x70(%rbp), %xmm1
movhlps %xmm1, %xmm1            # xmm1 = xmm1[1,1]
movaps %xmm1, -0x70(%rbp)
subq $0x50, %rsp
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm3
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm3, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movl %edx, 0x10(%rsp)
movq -0x78(%rbp), %r13
movq %r13, 0x8(%rsp)
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
xorps %xmm0, %xmm0
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq -0x40(%rbp), %r8
callq 0x2ecb2
addq $0x50, %rsp
cvtss2sd %xmm0, %xmm1
leaq 0x2719c(%rip), %rsi     # 0x55a60
xorps %xmm0, %xmm0
movq -0x38(%rbp), %rdi
movb $0x2, %al
callq 0x2190
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0xac(%rbp)
subq $0x50, %rsp
leaq 0x10(%rbp), %rax
movups (%rax), %xmm3
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm3, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movq -0x48(%rbp), %rax
movl %eax, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
movq %r14, %r13
movq %r14, %rdi
movq %rbx, -0x80(%rbp)
movq %rbx, %rsi
movq %r15, %r14
movq %r15, %rdx
movq %r12, %r15
movq %r12, %rcx
movq -0x40(%rbp), %r8
movaps -0x70(%rbp), %xmm1
movq -0xd8(%rbp), %r9
movq %r9, %rbx
callq 0x2ecb2
addq $0x50, %rsp
cvtss2sd %xmm0, %xmm1
xorps 0x21f02(%rip), %xmm0    # 0x50860
movaps %xmm0, -0xf0(%rbp)
movss %xmm0, -0xa0(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x270e8(%rip), %rsi     # 0x55a60
movsd -0x90(%rbp), %xmm0
movb $0x2, %al
callq 0x2190
xorl %eax, %eax
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
xorl %ecx, %ecx
cmpl $0x1, %eax
je 0x2ebf9
cmpl $0x63, %ecx
ja 0x2ebf9
movl %ecx, -0x54(%rbp)
movss -0x2c(%rbp), %xmm2
movaps %xmm2, %xmm0
movss -0x94(%rbp), %xmm1
subss %xmm1, %xmm0
xorps %xmm3, %xmm3
maxss %xmm0, %xmm3
movss %xmm3, -0xd0(%rbp)
movss %xmm3, -0xb0(%rbp)
addss %xmm1, %xmm2
movss %xmm2, -0x98(%rbp)
movss %xmm2, -0xa8(%rbp)
xorl %r12d, %r12d
cmpq $0x2, %r12
ja 0x2eaa5
movss -0xb0(%rbp,%r12,4), %xmm0
movss %xmm0, -0x90(%rbp)
subq $0x50, %rsp
leaq 0x10(%rbp), %rax
movups (%rax), %xmm3
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm3, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movq -0x48(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x78(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq -0x80(%rbp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq -0x40(%rbp), %r8
movaps -0x70(%rbp), %xmm1
movq %rbx, %r9
callq 0x2ecb2
addq $0x50, %rsp
cvtss2sd %xmm0, %xmm1
xorps 0x21de8(%rip), %xmm0    # 0x50860
movss %xmm0, -0xa4(%rbp,%r12,4)
cvtss2sd -0x90(%rbp), %xmm0
movq -0x38(%rbp), %rdi
leaq 0x26fcb(%rip), %rsi     # 0x55a60
movb $0x2, %al
callq 0x2190
addq $0x2, %r12
jmp 0x2e9ec
movss -0xa4(%rbp), %xmm3
movss -0xa0(%rbp), %xmm4
movss -0x9c(%rbp), %xmm5
xorps %xmm6, %xmm6
movss -0xd0(%rbp), %xmm0
movss -0x2c(%rbp), %xmm1
movss -0x98(%rbp), %xmm2
movss 0x1c537(%rip), %xmm7    # 0x4b014
callq 0x3bda0
movss %xmm0, -0x2c(%rbp)
subq $0x50, %rsp
leaq 0x10(%rbp), %rax
movups (%rax), %xmm3
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm3, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movq -0x48(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x78(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq -0x80(%rbp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq -0x40(%rbp), %r8
movaps -0x70(%rbp), %xmm1
movq %rbx, %r9
callq 0x2ecb2
addq $0x50, %rsp
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
xorps 0x21d07(%rip), %xmm2    # 0x50860
movaps %xmm2, -0x90(%rbp)
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0xac(%rbp)
movss %xmm2, -0xa0(%rbp)
cvtss2sd %xmm0, %xmm0
movaps %xmm1, -0xd0(%rbp)
cvtss2sd %xmm1, %xmm1
movq -0x38(%rbp), %rdi
leaq 0x26ed1(%rip), %rsi     # 0x55a60
movb $0x2, %al
callq 0x2190
movaps -0x90(%rbp), %xmm2
movaps -0xf0(%rbp), %xmm0
ucomiss %xmm0, %xmm2
jbe 0x2ebe2
addss -0xd0(%rbp), %xmm0
andps 0x22678(%rip), %xmm0    # 0x51230
cvtss2sd %xmm0, %xmm0
movsd 0x226cc(%rip), %xmm1    # 0x51290
ucomisd %xmm0, %xmm1
movl -0x54(%rbp), %ecx
ja 0x2ebef
xorl %eax, %eax
movaps %xmm2, -0xf0(%rbp)
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
jmp 0x2ebe8
pushq $0x1
popq %rax
movl -0x54(%rbp), %ecx
incl %ecx
jmp 0x2e995
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
subq $0x50, %rsp
leaq 0x10(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movq -0x48(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x78(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
movss -0x30(%rbp), %xmm0
movq %r13, %rdi
movq -0x80(%rbp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movq -0x40(%rbp), %r8
movapd -0x70(%rbp), %xmm1
movq %rbx, %r9
callq 0x2ecb2
addq $0x50, %rsp
movl 0x8(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rcx, %rax
je 0x2ec91
movq -0x80(%rbp), %rdx
cvtsd2ss (%rdx,%rcx,8), %xmm0
movq -0xb8(%rbp), %rdx
movq 0x18(%rdx), %rdx
movss %xmm0, (%rdx,%rcx,4)
incq %rcx
jmp 0x2ec6e
movq -0x38(%rbp), %rdi
callq 0x20c0
movq -0xe0(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq