// File: /workspace/github_c_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movq %r8, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, %r14
movq %rdi, -0xa8(%rbp)
leaq 0x10(%rbp), %r15
movq 0x48(%rbp), %rdx
leaq 0x29429(%rip), %rsi     # 0x5ac9a
movq $0x0, -0xb8(%rbp)
leaq -0x180(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2270
leaq 0x204bb(%rip), %rdi     # 0x51d4f
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
leaq 0x2595b(%rip), %rsi     # 0x57200
movq %rbx, %rdi
callq 0x2220
movq %rsp, -0xf8(%rbp)
movl (%r14), %edx
movq %rsp, %rcx
leaq 0xf(,%rdx,4), %rdx
andq $-0x10, %rdx
subq %rdx, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movl (%r14), %edx
testl %edx, %edx
movq %rax, -0x58(%rbp)
jle 0x3194a
movq 0x18(%r15), %rbx
movl 0x4(%r15), %r15d
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movq -0xa8(%rbp), %r12
movapd %xmm1, -0x70(%rbp)
movq 0x8(%r12), %rdi
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %rsi
movq %rbx, %rdx
movl %r15d, %ecx
callq 0x107c4
movq -0x40(%rbp), %rax
movss %xmm0, (%rax,%r13,4)
movaps %xmm0, %xmm1
mulss %xmm0, %xmm1
andps 0x259ba(%rip), %xmm0    # 0x572e0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd -0x70(%rbp), %xmm1
cvtps2pd %xmm0, %xmm0
addpd %xmm0, %xmm1
incq %r13
movslq (%r14), %rdx
cmpq %rdx, %r13
jl 0x318f1
movq -0x58(%rbp), %rax
leaq 0x10(%rbp), %r15
jmp 0x3194e
xorpd %xmm1, %xmm1
movapd %xmm1, -0x70(%rbp)
cvtsi2sd %edx, %xmm2
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
movapd %xmm0, -0x50(%rbp)
leaq 0x2933c(%rip), %rsi     # 0x5aca7
movq %rax, %rdi
movb $0x1, %al
callq 0x21a0
movl (%r14), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
movq %rcx, %rdi
movq %rcx, -0x80(%rbp)
movl (%r15), %eax
leal (%rax,%rax,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq -0x30(%rbp), %rcx
leal (%rcx,%rcx,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movl 0x8(%r14), %eax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
xorps %xmm0, %xmm0
cvtsd2ss -0x50(%rbp), %xmm0
movss %xmm0, -0x50(%rbp)
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd %xmm1, -0x88(%rbp)
movsd 0x282c0(%rip), %xmm0    # 0x59cc8
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x9c(%rbp)
movaps -0x70(%rbp), %xmm1
movhlps %xmm1, %xmm1            # xmm1 = xmm1[1,1]
movaps %xmm1, -0x70(%rbp)
subq $0x50, %rsp
movups (%r15), %xmm0
movups 0x10(%r15), %xmm3
movups 0x20(%r15), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm3, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq %r14, -0x60(%rbp)
movq 0x40(%rbp), %r14
movq %r14, 0x48(%rsp)
movl %ecx, 0x10(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x78(%rbp), %rax
movq %rax, (%rsp)
xorps %xmm0, %xmm0
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq -0x40(%rbp), %r8
movq -0x60(%rbp), %r9
callq 0x31ef9
addq $0x50, %rsp
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
leaq 0x2a06f(%rip), %rsi     # 0x5bb00
xorps %xmm0, %xmm0
movq -0x58(%rbp), %r14
movq %r14, %rdi
movb $0x2, %al
callq 0x21a0
subq $0x50, %rsp
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movq -0x30(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x78(%rbp), %rax
movq %rax, (%rsp)
movss -0x50(%rbp), %xmm0
movq -0x80(%rbp), %rdi
movq %rbx, -0x98(%rbp)
movq %rbx, %rsi
movq %r13, -0xb0(%rbp)
movq %r13, %rdx
movq %r12, -0x90(%rbp)
movq %r12, %rcx
movq -0x40(%rbp), %r8
movaps -0x70(%rbp), %xmm1
movq -0x60(%rbp), %r9
callq 0x31ef9
addq $0x50, %rsp
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
xorps 0x1f501(%rip), %xmm0    # 0x51030
movaps %xmm0, -0xe0(%rbp)
movq %r14, %rdi
leaq 0x29fc0(%rip), %rsi     # 0x5bb00
movsd -0x88(%rbp), %xmm0
movb $0x2, %al
callq 0x21a0
movss -0x50(%rbp), %xmm3
movaps -0xe0(%rbp), %xmm1
movq -0xb0(%rbp), %r12
movaps %xmm1, -0xe0(%rbp)
movss %xmm3, -0x88(%rbp)
movaps %xmm3, %xmm0
movss -0x9c(%rbp), %xmm1
subss %xmm1, %xmm0
xorps %xmm2, %xmm2
maxss %xmm0, %xmm2
movaps %xmm2, %xmm0
movss %xmm2, -0xd0(%rbp)
addss %xmm1, %xmm3
movss %xmm3, -0x50(%rbp)
subq $0x50, %rsp
leaq 0x10(%rbp), %r15
movups (%r15), %xmm3
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm3, 0x18(%rsp)
movq 0x40(%rbp), %r14
movq %r14, 0x48(%rsp)
movq -0x30(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x78(%rbp), %r15
movq %r15, (%rsp)
movq -0x80(%rbp), %rbx
movq %rbx, %rdi
movq -0x98(%rbp), %r13
movq %r13, %rsi
movq %r12, %rdx
movq -0x90(%rbp), %rcx
movq -0x40(%rbp), %r14
movq %r14, %r8
movaps -0x70(%rbp), %xmm1
movq -0x60(%rbp), %r9
callq 0x31ef9
addq $0x50, %rsp
movaps %xmm0, %xmm3
xorps %xmm0, %xmm0
cvtss2sd -0xd0(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm3, %xmm1
movaps 0x1f3ff(%rip), %xmm2    # 0x51030
xorps %xmm2, %xmm3
movaps %xmm3, -0xf0(%rbp)
movq -0x58(%rbp), %rdi
leaq 0x29eba(%rip), %rsi     # 0x5bb00
movb $0x2, %al
callq 0x21a0
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 -0x30(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r15, (%rsp)
movss -0x50(%rbp), %xmm0
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq -0x90(%rbp), %r13
movq %r13, %rcx
movq %r14, %r8
movaps -0x70(%rbp), %xmm1
movq -0x60(%rbp), %r9
callq 0x31ef9
addq $0x50, %rsp
movaps %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtss2sd -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
xorps 0x1f35e(%rip), %xmm2    # 0x51030
movaps %xmm2, -0x110(%rbp)
movq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq 0x29e19(%rip), %rsi     # 0x5bb00
movb $0x2, %al
callq 0x21a0
xorps %xmm6, %xmm6
movss -0xd0(%rbp), %xmm0
movss -0x88(%rbp), %xmm1
movss -0x50(%rbp), %xmm2
movaps -0xf0(%rbp), %xmm3
movaps -0xe0(%rbp), %xmm4
movaps -0x110(%rbp), %xmm5
movss 0x1f3fd(%rip), %xmm7    # 0x51120
callq 0x4110c
movss %xmm0, -0x50(%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 -0x60(%rbp), %r14
movq -0x30(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r15, (%rsp)
movq -0x80(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq %r12, %rdx
movq %r13, %rcx
movq -0x40(%rbp), %r8
movaps -0x70(%rbp), %xmm1
movq %r14, %r9
callq 0x31ef9
addq $0x50, %rsp
movaps %xmm0, %xmm1
xorps 0x1f290(%rip), %xmm0    # 0x51030
movaps %xmm0, -0xd0(%rbp)
xorps %xmm0, %xmm0
cvtss2sd -0x50(%rbp), %xmm0
movaps %xmm1, -0xf0(%rbp)
cvtss2sd %xmm1, %xmm1
movq %rbx, %rdi
leaq 0x29d3c(%rip), %rsi     # 0x5bb00
movb $0x2, %al
callq 0x21a0
movaps -0xd0(%rbp), %xmm4
movss -0x88(%rbp), %xmm0
movss -0x50(%rbp), %xmm3
movaps -0xe0(%rbp), %xmm1
ucomiss %xmm1, %xmm4
jbe 0x31e39
movaps -0xf0(%rbp), %xmm0
addss %xmm1, %xmm0
andps 0x254e3(%rip), %xmm0    # 0x572e0
cvtss2sd %xmm0, %xmm0
movsd 0x25527(%rip), %xmm2    # 0x57330
ucomisd %xmm0, %xmm2
ja 0x31e36
ucomiss %xmm1, %xmm4
jbe 0x31e36
movq -0xb8(%rbp), %rcx
leal 0x1(%rcx), %eax
movaps %xmm4, %xmm1
movaps %xmm3, %xmm0
cmpl $0x63, %ecx
movq %rax, -0xb8(%rbp)
jb 0x31b62
jmp 0x31e39
movaps %xmm3, %xmm0
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 -0x30(%rbp), %rax
movl %eax, 0x10(%rsp)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x78(%rbp), %rax
movq %rax, (%rsp)
movq -0x80(%rbp), %rdi
movq -0x98(%rbp), %rbx
movq %rbx, %rsi
movq -0xb0(%rbp), %rdx
movq -0x90(%rbp), %rcx
movq -0x40(%rbp), %r8
movaps -0x70(%rbp), %xmm1
movq %r14, %r9
callq 0x31ef9
addq $0x50, %rsp
movslq 0x8(%r14), %rax
testq %rax, %rax
jle 0x31ed8
movq -0xa8(%rbp), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsd2ss (%rbx,%rdx,8), %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x31ec3
movq -0x58(%rbp), %rdi
callq 0x20c0
movq -0xf8(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq