// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Write_RMSD
int Write_RMSD(float *reduce_step, float *RMSD_opt, float *RMSD_kf,
	       float STEP_MIN, float STEP_MAX, float factor, int iter,
	       FILE *file_rmsd,
	       struct bond *bonds, float *coord_all, int natoms1,
	       struct ali_atoms ali_a, float *coord_new, float *coord_old,
	       float *coord_ref_1, float *coord_ref_2)
{
  Put_coord(coord_all, bonds, natoms1);
  Write_ref_coord(coord_new, ali_a.N_ref, coord_all, ali_a.ali1);
  if(Check_nan_f(coord_new, ali_a.N_Cart, "ref_coord")){
    printf("ERROR nan!!!!\n");
    fprintf(file_rmsd, "# nan\n"); return(0);   
  }
  
  //  Compute rmsd and energy
  float RMSD_ki, RMSD_step;
  RMSD_ki=rmsd_mclachlan_f(coord_ref_1, coord_new, ali_a.mass, ali_a.N_ref);
  RMSD_step=rmsd_mclachlan_f(coord_old, coord_new, ali_a.mass, ali_a.N_ref);
  *RMSD_kf=rmsd_mclachlan_f(coord_ref_2, coord_new, ali_a.mass, ali_a.N_ref);
  
  // Check if RMSD improves
  int accept; //float RMSD_TAR=STEP_MAX*0.3333;
  if(RMSD_step<STEP_MIN){
    if((*RMSD_kf<*RMSD_opt)&&(factor==1.0)){
      accept=0; *reduce_step=2*(STEP_MIN/RMSD_step);
    }else{
      printf("Too small step\n");
      accept=0; *reduce_step=0.0;
    }
  }else if(*RMSD_kf<*RMSD_opt){
    /*if((RMSD_step<RMSD_TAR)&&(factor==1.0)){
      accept=0; *reduce_step=1.1*(RMSD_TAR/RMSD_step);
      }*/
    if((RMSD_step>STEP_MAX)&&(factor==1.0)){
      accept=0; *reduce_step=0.95*(STEP_MAX/RMSD_step);
    }else{
      accept=1; *reduce_step=1.;
    }
  }else{ // RMSD increases
    accept=0; *reduce_step=0.5;
  }
  if(accept)*RMSD_opt=*RMSD_kf;   

  printf("%2d round, rmsd= %.2f %.2f step= %.3f f= %.5f accept=%d\n",
	 iter+1, RMSD_ki, *RMSD_kf, RMSD_step, factor, accept);
  float Ene_new=Energy_clashes(coord_all, natoms1);
  if(accept==0)fprintf(file_rmsd, "#");
  fprintf(file_rmsd, "%.3f\t%.3f\t%.3f\t%.2g\t%.2g\t%d\n",
	  RMSD_ki, *RMSD_kf, RMSD_step, Ene_new, factor, accept);
  fflush(file_rmsd);
  return(accept);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x38(%rsp)
movl %ecx, 0x20(%rsp)
movss %xmm2, 0x8(%rsp)
movss %xmm1, 0x2c(%rsp)
movss %xmm0, 0x34(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, (%rsp)
movq 0xb0(%rsp), %r13
leaq 0x90(%rsp), %rbp
movl 0x88(%rsp), %edx
movq 0x80(%rsp), %r14
movq %r14, %rdi
movq %r9, %rsi
callq 0x2f358
movl 0x18(%rbp), %ebx
movq (%rbp), %rcx
movq %r13, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x2f9b8
movl 0x1c(%rbp), %r15d
xorl %r14d, %r14d
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
cmpl %r14d, %eax
je 0x31834
movss (%r13,%r14,4), %xmm0
ucomiss %xmm0, %xmm0
jp 0x317c7
incq %r14
jmp 0x317b1
leaq 0x24360(%rip), %rdi     # 0x55b2e
leaq 0x242bf(%rip), %rsi     # 0x55a94
xorl %r12d, %r12d
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x20f0
pushq $0x3
popq %rcx
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %esi
movl %r15d, %eax
cltd
idivl %ecx
leaq 0x24359(%rip), %rdi     # 0x55b57
movl %eax, %edx
xorl %eax, %eax
callq 0x20f0
cmpl %r15d, %r14d
jge 0x31834
leaq 0x2439b(%rip), %rdi     # 0x55bae
callq 0x2090
leaq 0x2427f(%rip), %rdi     # 0x55a9e
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq 0x38(%rsp), %rcx
callq 0x2280
jmp 0x319fb
movq 0x10(%rbp), %r14
movq 0xc0(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x47a64
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
movq 0xb8(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x47a64
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
movq 0xc8(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x47a64
movss 0x14(%rsp), %xmm2
cvtsd2ss %xmm0, %xmm0
movq 0x40(%rsp), %r14
movss %xmm0, (%r14)
movss 0x34(%rsp), %xmm3
ucomiss %xmm2, %xmm3
movq 0x18(%rsp), %rcx
movss (%rcx), %xmm1
jbe 0x318db
movss 0x8(%rsp), %xmm5
ucomiss 0x19750(%rip), %xmm5    # 0x4b010
jne 0x31926
jp 0x31926
ucomiss %xmm0, %xmm1
jbe 0x31926
divss %xmm2, %xmm3
addss %xmm3, %xmm3
movq (%rsp), %rax
movss %xmm3, (%rax)
jmp 0x31951
ucomiss %xmm0, %xmm1
movss 0x8(%rsp), %xmm5
jbe 0x31947
ucomiss 0x19723(%rip), %xmm5    # 0x4b010
movss 0x2c(%rsp), %xmm0
jne 0x31a0d
jp 0x31a0d
ucomiss %xmm0, %xmm2
jbe 0x31a0d
divss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x23398(%rip), %xmm0    # 0x54cb0
cvtsd2ss %xmm0, %xmm0
movq (%rsp), %rax
movss %xmm0, (%rax)
jmp 0x31951
leaq 0x24272(%rip), %rdi     # 0x55b9f
callq 0x2090
movss 0x14(%rsp), %xmm2
movss 0x8(%rsp), %xmm5
movq (%rsp), %rax
andl $0x0, (%rax)
jmp 0x31951
movq (%rsp), %rax
movl $0x3f000000, (%rax)     # imm = 0x3F000000
movb $0x1, %bl
xorl %r12d, %r12d
movl 0x20(%rsp), %esi
incl %esi
cvtss2sd 0x30(%rsp), %xmm0
cvtss2sd (%r14), %xmm1
cvtss2sd %xmm2, %xmm2
cvtss2sd %xmm5, %xmm3
leaq 0x2412f(%rip), %rdi     # 0x55aa5
movsd %xmm0, 0x20(%rsp)
movsd %xmm2, (%rsp)
movsd %xmm3, 0x8(%rsp)
movl %r12d, %edx
movb $0x4, %al
callq 0x20f0
movq 0x80(%rsp), %rdi
movl 0x88(%rsp), %esi
callq 0x2f3a6
testb %bl, %bl
movq 0x38(%rsp), %rbx
je 0x319c5
pushq $0x23
popq %rdi
movq %rbx, %rsi
movss %xmm0, 0x18(%rsp)
callq 0x2140
movss 0x18(%rsp), %xmm0
cvtss2sd (%r14), %xmm1
cvtss2sd %xmm0, %xmm3
leaq 0x23775(%rip), %rsi     # 0x5514a
movq %rbx, %rdi
movsd 0x20(%rsp), %xmm0
movsd (%rsp), %xmm2
movsd 0x8(%rsp), %xmm4
movl %r12d, %edx
movb $0x5, %al
callq 0x2190
movq %rbx, %rdi
callq 0x21e0
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movl $0x3f800000, (%rax)     # imm = 0x3F800000
movss (%r14), %xmm0
movss %xmm0, (%rcx)
pushq $0x1
popq %r12
xorl %ebx, %ebx
jmp 0x31956