// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movl %r9d, %r14d
movq %r8, %r12
movl %ecx, -0x44(%rbp)
movq %rdx, -0xa0(%rbp)
movq %rdi, %rbx
movq %rsi, -0x68(%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
movsd 0x2026d(%rip), %xmm0    # 0x5adb0
movsd %xmm0, -0xb8(%rbp)
movl $0x3f333333, -0xb0(%rbp) # imm = 0x3F333333
leaq 0x212a8(%rip), %rdi     # 0x5be04
callq 0x2090
xorps %xmm1, %xmm1
movss 0x1c344(%rip), %xmm0    # 0x56eb0
movss %xmm0, -0x2c(%rbp)
xorl %r13d, %r13d
movl %r14d, -0x3c(%rbp)
movq %r12, -0x98(%rbp)
movaps %xmm1, -0x90(%rbp)
movss -0xb8(%rbp,%r13,4), %xmm0
movss %xmm0, -0x60(%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 -0xa0(%rbp), %rdi
movl -0x44(%rbp), %esi
movq %r12, %rdx
movq %r15, %rcx
movl %r14d, %r8d
movq 0x40(%rbp), %r9
callq 0x3affb
addq $0x30, %rsp
movaps %xmm0, -0x80(%rbp)
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x60(%rbp), %xmm1
leaq 0x20f91(%rip), %rdi     # 0x5bb7e
movb $0x2, %al
callq 0x20f0
movss -0x60(%rbp), %xmm3
movaps -0x80(%rbp), %xmm2
movaps %xmm3, %xmm1
cmpeqss 0x16513(%rip), %xmm1    # 0x5111c
movss -0x2c(%rbp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x3ac9e
movq -0x68(%rbp), %rax
movss %xmm3, (%rax)
movq $0x0, 0x28(%rbx)
movss %xmm2, -0x2c(%rbp)
testl %r14d, %r14d
jle 0x3ac9e
movaps %xmm1, -0x60(%rbp)
xorl %r12d, %r12d
xorl %r14d, %r14d
leaq (%r15,%r12), %rsi
leaq (%rbx,%r12), %rdi
movl $0xc0, %edx
callq 0x21b0
testl %r14d, %r14d
je 0x3ac76
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, -0x3c(%rbp)
jne 0x3ac3b
movaps -0x80(%rbp), %xmm2
movss %xmm2, -0x2c(%rbp)
movl -0x3c(%rbp), %r14d
movq -0x98(%rbp), %r12
movaps -0x60(%rbp), %xmm1
movaps %xmm2, %xmm0
andps %xmm1, %xmm0
andnps -0x90(%rbp), %xmm1
orps %xmm0, %xmm1
movaps %xmm1, -0x60(%rbp)
xorps 0x16377(%rip), %xmm2    # 0x51030
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
callq 0x22b0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xac(%rbp,%r13,4)
incq %r13
movaps -0x60(%rbp), %xmm1
cmpq $0x3, %r13
jne 0x3ab7f
movss -0xac(%rbp), %xmm0
movss %xmm0, -0x38(%rbp)
movss -0xa8(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movss 0x2009e(%rip), %xmm2    # 0x5ada4
movss 0x2009a(%rip), %xmm0    # 0x5ada8
movss %xmm0, -0x90(%rbp)
xorl %r13d, %r13d
movss -0xa4(%rbp), %xmm0
movss %xmm0, -0x34(%rbp)
movss 0x163ee(%rip), %xmm1    # 0x5111c
movss -0x90(%rbp), %xmm0
movss %xmm1, -0x40(%rbp)
movss %xmm2, -0x6c(%rbp)
movss -0x38(%rbp), %xmm3
movss -0x30(%rbp), %xmm4
movss -0x34(%rbp), %xmm5
movss 0x1c559(%rip), %xmm6    # 0x572b0
movss 0x1c145(%rip), %xmm7    # 0x56ea4
callq 0x4110c
movss %xmm0, -0x80(%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 -0xa0(%rbp), %rdi
movl -0x44(%rbp), %esi
movq %r12, %rdx
movq %r15, %rcx
movl %r14d, %r8d
movq 0x40(%rbp), %r9
callq 0x3affb
addq $0x30, %rsp
movaps %xmm0, -0xd0(%rbp)
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x80(%rbp), %xmm1
leaq 0x20dba(%rip), %rdi     # 0x5bb7e
movb $0x2, %al
callq 0x20f0
movss -0x2c(%rbp), %xmm0
movaps -0xd0(%rbp), %xmm1
ucomiss %xmm1, %xmm0
movaps %xmm1, %xmm0
jbe 0x3ae6c
movq -0x68(%rbp), %rax
movss -0x80(%rbp), %xmm1
movss %xmm1, (%rax)
movq $0x0, 0x28(%rbx)
movss %xmm0, -0x2c(%rbp)
testl %r14d, %r14d
jle 0x3ae76
xorl %r12d, %r12d
xorl %r14d, %r14d
leaq (%r15,%r12), %rsi
leaq (%rbx,%r12), %rdi
movl $0xc0, %edx
callq 0x21b0
testl %r14d, %r14d
je 0x3ae43
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, -0x3c(%rbp)
jne 0x3ae08
movaps -0xd0(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
movl -0x3c(%rbp), %r14d
movq -0x98(%rbp), %r12
jmp 0x3ae76
cmpl $0xa, %r13d
ja 0x3afa8
xorps 0x161b3(%rip), %xmm0    # 0x51030
cvtss2sd %xmm0, %xmm0
callq 0x22b0
cvtsd2ss %xmm0, %xmm0
movss -0x90(%rbp), %xmm6
movss -0x80(%rbp), %xmm8
ucomiss %xmm8, %xmm6
jbe 0x3aecb
movaps %xmm6, %xmm1
subss %xmm8, %xmm1
movss 0x1fefe(%rip), %xmm2    # 0x5adac
ucomiss %xmm1, %xmm2
movss -0x30(%rbp), %xmm2
movss -0x38(%rbp), %xmm1
movss -0x40(%rbp), %xmm4
jbe 0x3af7d
jmp 0x3afa8
movss -0x40(%rbp), %xmm3
ucomiss %xmm8, %xmm3
movss 0x1fed0(%rip), %xmm5    # 0x5adac
jbe 0x3af1c
movaps %xmm3, %xmm1
subss %xmm8, %xmm1
ucomiss %xmm1, %xmm5
ja 0x3afa8
movaps %xmm6, %xmm7
movaps %xmm8, %xmm6
movaps %xmm8, %xmm1
subss %xmm7, %xmm1
ucomiss %xmm1, %xmm5
movss -0x30(%rbp), %xmm2
movaps %xmm0, %xmm1
movss -0x38(%rbp), %xmm0
movaps %xmm3, %xmm4
movaps %xmm7, %xmm8
jbe 0x3af7d
jmp 0x3afa8
movss -0x6c(%rbp), %xmm6
ucomiss %xmm8, %xmm6
jbe 0x3af5b
movaps %xmm6, %xmm1
subss %xmm8, %xmm1
ucomiss %xmm1, %xmm5
ja 0x3afa8
movaps %xmm6, %xmm4
movaps %xmm8, %xmm6
movaps %xmm8, %xmm2
subss %xmm3, %xmm2
ucomiss %xmm2, %xmm5
movss -0x34(%rbp), %xmm2
movaps %xmm0, %xmm1
movss -0x30(%rbp), %xmm0
movaps %xmm3, %xmm8
jbe 0x3af7d
jmp 0x3afa8
movaps %xmm8, %xmm1
subss %xmm6, %xmm1
ucomiss %xmm1, %xmm5
movaps %xmm8, %xmm4
movaps %xmm0, %xmm2
movss -0x30(%rbp), %xmm0
movaps %xmm3, %xmm8
movss -0x34(%rbp), %xmm1
ja 0x3afa8
movss %xmm1, -0x30(%rbp)
incl %r13d
movss %xmm2, -0x34(%rbp)
movss %xmm8, -0x90(%rbp)
movaps %xmm6, %xmm1
movaps %xmm4, %xmm2
movss %xmm0, -0x38(%rbp)
cmpl $0x14, %r13d
jne 0x3ad2e
xorps %xmm0, %xmm0
cvtss2sd -0x60(%rbp), %xmm0
leaq 0x20bd9(%rip), %rdi     # 0x5bb90
movsd 0x16189(%rip), %xmm1    # 0x51148
movb $0x2, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd -0x2c(%rbp), %xmm0
movq -0x68(%rbp), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
leaq 0x20bcd(%rip), %rdi     # 0x5bbad
movb $0x2, %al
callq 0x20f0
movss -0x2c(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq