// File: /workspace/github_c_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movq %r8, -0x78(%rbp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
movq 0x48(%rbp), %rdx
leaq 0x28ffb(%rip), %rsi     # 0x5acf6
movq $0x0, -0xb8(%rbp)
leaq -0x170(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2260
leaq 0x1ffbd(%rip), %rdi     # 0x51cdb
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
leaq 0x25511(%rip), %rsi     # 0x57240
movq %rbx, %rdi
callq 0x2210
movq %rax, %rbx
movq %rsp, -0x100(%rbp)
movl (%r13), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movl (%r13), %edx
testl %edx, %edx
movq %rbx, -0x48(%rbp)
movq %r12, -0x58(%rbp)
movq %r15, -0xf8(%rbp)
jle 0x31e01
movq %r14, -0x40(%rbp)
leaq 0x10(%rbp), %rax
movq 0x18(%rax), %rbx
movl 0x4(%rax), %r14d
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
movsd %xmm1, -0x70(%rbp)
movq 0x8(%r15), %rdi
movq 0x38(%r13), %rax
movq (%rax,%r12,8), %rsi
movq %rbx, %rdx
movl %r14d, %ecx
callq 0x10c76
movq -0x50(%rbp), %rax
movss %xmm0, (%rax,%r12,4)
movaps %xmm0, %xmm1
mulss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd -0x38(%rbp), %xmm2
addsd %xmm1, %xmm2
movsd -0x70(%rbp), %xmm1
movsd %xmm2, -0x38(%rbp)
andps 0x2553d(%rip), %xmm0    # 0x57320
cvtss2sd %xmm0, %xmm0
addsd %xmm0, %xmm1
incq %r12
movslq (%r13), %rdx
cmpq %rdx, %r12
jl 0x31d98
movq -0x48(%rbp), %rbx
movq -0x40(%rbp), %r14
jmp 0x31e0d
xorpd %xmm1, %xmm1
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, -0x70(%rbp)
leaq 0x28edf(%rip), %rsi     # 0x5ad03
movq %rbx, %rdi
movapd %xmm1, %xmm0
movb $0x1, %al
callq 0x2190
movl (%r13), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
movq %rcx, %rdi
movq %rcx, -0x90(%rbp)
leaq 0x10(%rbp), %rdx
movl (%rdx), %eax
leal (%rax,%rax,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, %rsp
movq %rcx, -0x40(%rbp)
movq -0x78(%rbp), %r15
leal (%r15,%r15,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movl 0x8(%r13), %eax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
xorps %xmm0, %xmm0
cvtsd2ss -0x70(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd %xmm1, -0x70(%rbp)
movsd 0x27e52(%rip), %xmm0    # 0x59d20
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x9c(%rbp)
subq $0x50, %rsp
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movl %r15d, 0x10(%rsp)
movq -0x58(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r14, (%rsp)
xorps %xmm0, %xmm0
movq %rbx, %rsi
movq %r12, %rdx
movq -0x50(%rbp), %r8
movsd -0x38(%rbp), %xmm1
movq %r13, %r9
callq 0x32357
addq $0x50, %rsp
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
xorps 0x249ed(%rip), %xmm0    # 0x56930
movss %xmm0, -0x80(%rbp)
leaq 0x29bf9(%rip), %rsi     # 0x5bb48
xorps %xmm0, %xmm0
movq -0x48(%rbp), %rdi
movb $0x2, %al
callq 0x2190
movss -0x30(%rbp), %xmm0
movss %xmm0, -0xa8(%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)
movl %r15d, 0x10(%rsp)
movq -0x58(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r14, (%rsp)
movq -0x90(%rbp), %rdi
movq %rbx, %rsi
movq %r12, -0x98(%rbp)
movq %r12, %rdx
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %r8
movsd -0x38(%rbp), %xmm1
movq %r13, %r9
callq 0x32357
addq $0x50, %rsp
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
xorps 0x2494e(%rip), %xmm0    # 0x56930
movaps %xmm0, -0xf0(%rbp)
movss %xmm0, -0x80(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x29b4f(%rip), %rsi     # 0x5bb48
movsd -0x70(%rbp), %xmm0
movb $0x2, %al
callq 0x2190
movaps -0xf0(%rbp), %xmm1
movss -0x30(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
movq %r13, -0xc8(%rbp)
movq %rbx, -0xc0(%rbp)
movss %xmm1, -0xf0(%rbp)
movss -0x2c(%rbp), %xmm2
movaps %xmm2, %xmm0
movss -0x9c(%rbp), %xmm1
subss %xmm1, %xmm0
xorps %xmm3, %xmm3
maxss %xmm0, %xmm3
movss %xmm3, -0xe0(%rbp)
movss %xmm3, -0xac(%rbp)
addss %xmm1, %xmm2
movss %xmm2, -0xa0(%rbp)
movss %xmm2, -0xa4(%rbp)
xorl %r12d, %r12d
movq %r14, %r13
leaq 0x10(%rbp), %r14
movq -0x90(%rbp), %rbx
movss -0xac(%rbp,%r12,4), %xmm0
movss %xmm0, -0x70(%rbp)
subq $0x50, %rsp
movups (%r14), %xmm3
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm3, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movl %r15d, 0x10(%rsp)
movq -0x58(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r13, (%rsp)
movq %rbx, %rdi
movq -0xc0(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %r8
movsd -0x38(%rbp), %xmm1
movq -0xc8(%rbp), %r9
callq 0x32357
addq $0x50, %rsp
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
xorps 0x2482d(%rip), %xmm0    # 0x56930
movss %xmm0, -0x84(%rbp,%r12,4)
xorps %xmm0, %xmm0
cvtss2sd -0x70(%rbp), %xmm0
movq -0x48(%rbp), %rdi
leaq 0x29a28(%rip), %rsi     # 0x5bb48
movb $0x2, %al
callq 0x2190
addq $0x2, %r12
cmpq $0x2, %r12
je 0x3207c
movss -0x84(%rbp), %xmm3
movss -0x80(%rbp), %xmm4
movss -0x7c(%rbp), %xmm5
xorps %xmm6, %xmm6
movss -0xe0(%rbp), %xmm0
movss -0x2c(%rbp), %xmm1
movss -0xa0(%rbp), %xmm2
movss 0x1eead(%rip), %xmm7    # 0x51014
callq 0x41150
movss %xmm0, -0x2c(%rbp)
subq $0x50, %rsp
movups (%r14), %xmm3
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm3, 0x18(%rsp)
movq 0x40(%rbp), %rax
movq %rax, 0x48(%rsp)
movl %r15d, 0x10(%rsp)
movq -0x58(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %r14
movq %r13, (%rsp)
movq %rbx, %rdi
movq -0xc0(%rbp), %rbx
movq %rbx, %rsi
movq -0x98(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %r8
movsd -0x38(%rbp), %xmm1
movq -0xc8(%rbp), %r13
movq %r13, %r9
callq 0x32357
addq $0x50, %rsp
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
xorps 0x2473f(%rip), %xmm2    # 0x56930
movaps %xmm2, -0x70(%rbp)
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0xa8(%rbp)
movss %xmm2, -0x80(%rbp)
cvtss2sd %xmm0, %xmm0
movaps %xmm1, -0xe0(%rbp)
cvtss2sd %xmm1, %xmm1
movq -0x48(%rbp), %rdi
leaq 0x29927(%rip), %rsi     # 0x5bb48
movb $0x2, %al
callq 0x2190
movaps -0x70(%rbp), %xmm3
movb $0x1, %al
movss -0xf0(%rbp), %xmm2
ucomiss %xmm2, %xmm3
ja 0x32240
movaps %xmm2, %xmm1
jmp 0x32278
movaps -0xe0(%rbp), %xmm0
addss %xmm2, %xmm0
andps 0x250ce(%rip), %xmm0    # 0x57320
cvtss2sd %xmm0, %xmm0
movsd 0x25102(%rip), %xmm1    # 0x57360
ucomisd %xmm0, %xmm1
jbe 0x3226b
xorl %eax, %eax
movaps %xmm2, %xmm1
jmp 0x3226e
movaps %xmm3, %xmm1
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x78(%rbp), %r15
testb %al, %al
je 0x3229f
ucomiss %xmm2, %xmm3
jbe 0x3229f
movq -0xb8(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl $0x62, %ecx
movq %rax, -0xb8(%rbp)
jbe 0x32024
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)
movl %r15d, 0x10(%rsp)
movq -0x58(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r14, (%rsp)
movss -0x30(%rbp), %xmm0
movq -0x90(%rbp), %rdi
movq %rbx, %rsi
movq -0x98(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %r8
movsd -0x38(%rbp), %xmm1
movq %r13, %r9
callq 0x32357
addq $0x50, %rsp
movslq 0x8(%r13), %rax
testq %rax, %rax
jle 0x32336
movq -0xf8(%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 0x32321
movq -0x48(%rbp), %rdi
callq 0x20c0
movq -0x100(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq