// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/simulation.c
// Function: Optimize_amplitude
float Optimize_amplitude(struct bond *bonds_min, float *f_min,
			 float *Diff_Tors, int naxes,
			 struct bond *bonds,
			 int natoms1, struct Reference Ref1,
			 float *coord_ref_2)
{
  float EPS=0.005, RMSD_min=10000, RMSD_RRR=0; *f_min=-1;
  struct bond bonds_tmp[natoms1];
  float f[3], y[3], fmin=0.01, fmax=20; int i;
  f[0]=1.3; f[1]=1; f[2]=0.7;
  printf("Optimizing amplitude f of torsion angle change:\n");
  for(i=0; i<3; i++){
    y[i]=Compute_RMSD(f[i],Diff_Tors,naxes,
		      bonds,bonds_tmp,natoms1,Ref1,coord_ref_2);
    printf("RMSD=%.5f f=%.5f\n", y[i], f[i]);
    if(f[i]==1)RMSD_RRR=y[i];
    if(y[i]<RMSD_min){
      RMSD_min=y[i]; *f_min=f[i]; Copy_bonds(bonds_min, bonds_tmp, natoms1);
    }
    y[i]=exp(-y[i]);
  }

  for(int iter=0; iter<20; iter++){
    float ff=Find_max_quad(f[0],f[1],f[2],y[0],y[1],y[2],fmin,fmax);
    float yy=Compute_RMSD(ff, Diff_Tors, naxes, bonds, bonds_tmp, natoms1,
			  Ref1, coord_ref_2);
    printf("RMSD=%.5f f=%.5f\n", yy, ff);
    if(yy<RMSD_min){
      RMSD_min=yy; *f_min=ff; Copy_bonds(bonds_min, bonds_tmp, natoms1);
    }else{
      if(iter>10)break;
    }
    yy=exp(-yy);
    if(ff<f[0]){
      if((f[0]-ff)<EPS)break;
      f[2]=f[1]; y[2]=y[1]; f[1]=f[0]; y[1]=y[0]; f[0]=ff; y[0]=yy;
    }else if(ff < f[1]){
      if(((f[1]-ff)<EPS)||((ff-f[0])<EPS))break;
      f[2]=f[1]; y[2]=y[1]; f[1]=ff; y[1]=yy;
    }else if(ff < f[2]){
      if(((f[2]-ff)<EPS)||((ff-f[1])<EPS))break;
      f[0]=f[1]; y[0]=y[1]; f[1]=ff; y[1]=yy;
    }else{
      if((ff-f[2])<EPS)break;
      f[0]=f[1]; y[0]=y[1]; f[1]=f[2]; y[1]=y[2]; f[2]=ff; y[2]=yy;
    }
  }
  printf("RMSD RRR= %.4f factor= %.4g\n", RMSD_RRR, 1.0);
  printf("Minimal RMSD= %.4f factor= %.4g\n", RMSD_min, *f_min);
  return(RMSD_min);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, %r14d
movq %r8, %r12
movl %ecx, -0x4c(%rbp)
movq %rdx, -0x90(%rbp)
movq %rdi, %rbx
movq %rsi, -0x70(%rbp)
movl $0xbf800000, (%rsi)     # imm = 0xBF800000
movl %r9d, %eax
movq %rsp, %r15
shlq $0x6, %rax
leaq (%rax,%rax,2), %rax
subq %rax, %r15
movq %r15, %rsp
movabsq $0x3f8000003fa66666, %rax # imm = 0x3F8000003FA66666
movq %rax, -0x58(%rbp)
movl $0x3f333333, -0x50(%rbp) # imm = 0x3F333333
leaq 0x212b1(%rip), %rdi     # 0x5be4c
callq 0x2090
xorps %xmm1, %xmm1
movss 0x1c345(%rip), %xmm0    # 0x56ef0
movss %xmm0, -0x34(%rbp)
xorl %r13d, %r13d
movl %r14d, -0x40(%rbp)
movq %r12, -0x88(%rbp)
movaps %xmm1, -0xa0(%rbp)
movss -0x58(%rbp,%r13,4), %xmm0
movss %xmm0, -0x3c(%rbp)
subq $0x30, %rsp
leaq 0x10(%rbp), %rax
movups (%rax), %xmm3
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm3, (%rsp)
movq -0x90(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq %r12, %rdx
movq %r15, %rcx
movl %r14d, %r8d
movq 0x40(%rbp), %r9
callq 0x3b099
addq $0x30, %rsp
movaps %xmm0, -0x80(%rbp)
movss %xmm0, -0x64(%rbp,%r13,4)
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x3c(%rbp), %xmm1
leaq 0x20f96(%rip), %rdi     # 0x5bbc6
movb $0x2, %al
callq 0x20f0
movss -0x3c(%rbp), %xmm3
movaps -0x80(%rbp), %xmm2
movaps %xmm3, %xmm1
cmpeqss 0x163c4(%rip), %xmm1    # 0x51010
movss -0x34(%rbp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x3ace7
movq -0x70(%rbp), %rax
movss %xmm3, (%rax)
movq $0x0, 0x28(%rbx)
movss %xmm2, -0x34(%rbp)
testl %r14d, %r14d
jle 0x3ace7
movaps %xmm1, -0xb0(%rbp)
xorl %r12d, %r12d
xorl %r14d, %r14d
leaq (%r15,%r12), %rsi
leaq (%rbx,%r12), %rdi
movl $0xc0, %edx
callq 0x21a0
testl %r14d, %r14d
je 0x3acbc
movq 0x28(%r15,%r12), %rax
subq %r15, %rax
shrq $0x6, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
cltq
leaq (%rax,%rax,2), %rax
shlq $0x6, %rax
addq %rbx, %rax
movq %rax, 0x28(%rbx,%r12)
incl %r14d
addq $0xc0, %r12
cmpl %r14d, -0x40(%rbp)
jne 0x3ac81
movaps -0x80(%rbp), %xmm2
movss %xmm2, -0x34(%rbp)
movl -0x40(%rbp), %r14d
movq -0x88(%rbp), %r12
movaps -0xb0(%rbp), %xmm1
movaps %xmm2, %xmm0
andps %xmm1, %xmm0
andnps -0xa0(%rbp), %xmm1
orps %xmm0, %xmm1
movaps %xmm1, -0xb0(%rbp)
xorps 0x1bc2b(%rip), %xmm2    # 0x56930
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
callq 0x22a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x64(%rbp,%r13,4)
incq %r13
movaps -0xb0(%rbp), %xmm1
cmpq $0x3, %r13
jne 0x3abbe
movss -0x58(%rbp), %xmm8
movss -0x54(%rbp), %xmm1
movss -0x50(%rbp), %xmm2
movss -0x64(%rbp), %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x60(%rbp), %xmm4
xorl %r13d, %r13d
movss -0x5c(%rbp), %xmm5
movss %xmm8, -0xa0(%rbp)
movaps %xmm8, %xmm0
movss %xmm1, -0x48(%rbp)
movss %xmm2, -0x44(%rbp)
movss -0x38(%rbp), %xmm3
movss %xmm4, -0x2c(%rbp)
movss %xmm5, -0x30(%rbp)
movss 0x1c56b(%rip), %xmm6    # 0x572f0
movss 0x1c157(%rip), %xmm7    # 0x56ee4
callq 0x41150
movss %xmm0, -0x3c(%rbp)
subq $0x30, %rsp
leaq 0x10(%rbp), %rax
movups (%rax), %xmm3
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm3, (%rsp)
movq -0x90(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq %r12, %rdx
movq %r15, %rcx
movl %r14d, %r8d
movq 0x40(%rbp), %r9
callq 0x3b099
addq $0x30, %rsp
movaps %xmm0, -0x80(%rbp)
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x3c(%rbp), %xmm1
leaq 0x20dd7(%rip), %rdi     # 0x5bbc6
movb $0x2, %al
callq 0x20f0
movss -0x34(%rbp), %xmm0
movaps -0x80(%rbp), %xmm1
ucomiss %xmm1, %xmm0
movaps %xmm1, %xmm0
jbe 0x3ae95
movq -0x70(%rbp), %rax
movss -0x3c(%rbp), %xmm1
movss %xmm1, (%rax)
movq $0x0, 0x28(%rbx)
movss %xmm0, -0x34(%rbp)
testl %r14d, %r14d
jle 0x3aec0
xorl %r12d, %r12d
xorl %r14d, %r14d
leaq (%r15,%r12), %rsi
leaq (%rbx,%r12), %rdi
movl $0xc0, %edx
callq 0x21a0
testl %r14d, %r14d
je 0x3ae6f
movq 0x28(%r15,%r12), %rax
subq %r15, %rax
shrq $0x6, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
cltq
leaq (%rax,%rax,2), %rax
shlq $0x6, %rax
addq %rbx, %rax
movq %rax, 0x28(%rbx,%r12)
incl %r14d
addq $0xc0, %r12
cmpl %r14d, -0x40(%rbp)
jne 0x3ae34
movaps -0x80(%rbp), %xmm0
movss %xmm0, -0x34(%rbp)
movl -0x40(%rbp), %r14d
movq -0x88(%rbp), %r12
jmp 0x3aec0
movb $0x1, %al
cmpl $0xa, %r13d
jbe 0x3aec0
movss -0x30(%rbp), %xmm0
movss -0x2c(%rbp), %xmm9
movss -0x44(%rbp), %xmm7
movss -0x48(%rbp), %xmm3
movss -0xa0(%rbp), %xmm8
jmp 0x3b000
xorps 0x1ba69(%rip), %xmm0    # 0x56930
cvtss2sd %xmm0, %xmm0
callq 0x22a0
xorps %xmm2, %xmm2
cvtsd2ss %xmm0, %xmm2
movss -0xa0(%rbp), %xmm8
movss -0x3c(%rbp), %xmm4
ucomiss %xmm4, %xmm8
jbe 0x3af1c
movaps %xmm8, %xmm0
subss %xmm4, %xmm0
movb $0x1, %al
movss 0x1ff07(%rip), %xmm1    # 0x5ae04
ucomiss %xmm0, %xmm1
movss -0x48(%rbp), %xmm3
movss -0x44(%rbp), %xmm7
jbe 0x3af68
movss -0x30(%rbp), %xmm0
movss -0x2c(%rbp), %xmm9
jmp 0x3b000
movss -0x48(%rbp), %xmm6
ucomiss %xmm4, %xmm6
movss 0x1fed8(%rip), %xmm1    # 0x5ae04
movss -0x44(%rbp), %xmm3
jbe 0x3af87
movaps %xmm6, %xmm0
subss %xmm4, %xmm0
ucomiss %xmm0, %xmm1
movb $0x1, %al
ja 0x3afd1
movaps %xmm4, %xmm0
subss %xmm8, %xmm0
ucomiss %xmm0, %xmm1
ja 0x3afd1
xorl %eax, %eax
movss -0x2c(%rbp), %xmm0
movaps %xmm2, %xmm9
movaps %xmm6, %xmm7
movaps %xmm4, %xmm3
jmp 0x3b000
xorl %eax, %eax
movss -0x2c(%rbp), %xmm0
movss -0x38(%rbp), %xmm9
movss %xmm2, -0x38(%rbp)
movaps %xmm3, %xmm7
movaps %xmm8, %xmm3
movaps %xmm4, %xmm8
jmp 0x3b000
ucomiss %xmm4, %xmm3
jbe 0x3afc3
movaps %xmm3, %xmm0
subss %xmm4, %xmm0
ucomiss %xmm0, %xmm1
movb $0x1, %al
ja 0x3afd1
movaps %xmm4, %xmm0
subss %xmm6, %xmm0
ucomiss %xmm0, %xmm1
ja 0x3afd1
xorl %eax, %eax
movss -0x30(%rbp), %xmm0
movaps %xmm2, %xmm9
movss -0x2c(%rbp), %xmm1
movss %xmm1, -0x38(%rbp)
movaps %xmm3, %xmm7
movaps %xmm4, %xmm3
jmp 0x3affc
movaps %xmm4, %xmm0
subss %xmm3, %xmm0
movb $0x1, %al
ucomiss %xmm0, %xmm1
jbe 0x3afe4
movss -0x30(%rbp), %xmm0
movss -0x2c(%rbp), %xmm9
movaps %xmm3, %xmm7
movaps %xmm6, %xmm3
jmp 0x3b000
xorl %eax, %eax
movaps %xmm2, %xmm0
movss -0x30(%rbp), %xmm9
movss -0x2c(%rbp), %xmm1
movss %xmm1, -0x38(%rbp)
movaps %xmm4, %xmm7
movaps %xmm6, %xmm8
testb %al, %al
jne 0x3b01e
incl %r13d
movaps %xmm0, %xmm5
movaps %xmm9, %xmm4
movaps %xmm3, %xmm1
movaps %xmm7, %xmm2
cmpl $0x14, %r13d
jne 0x3ad57
movss %xmm9, -0x60(%rbp)
movss %xmm0, -0x5c(%rbp)
movss %xmm8, -0x58(%rbp)
movss %xmm3, -0x54(%rbp)
movss %xmm7, -0x50(%rbp)
movss -0x38(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
xorps %xmm0, %xmm0
cvtss2sd -0xb0(%rbp), %xmm0
leaq 0x20b83(%rip), %rdi     # 0x5bbd8
movsd 0x15fe3(%rip), %xmm1    # 0x51040
movb $0x2, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd -0x34(%rbp), %xmm0
movq -0x70(%rbp), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
leaq 0x20b77(%rip), %rdi     # 0x5bbf5
movb $0x2, %al
callq 0x20f0
movss -0x34(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq