// File: /workspace/github_c_makefile_star2_O3/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
movl %ecx, 0x14(%rsp)
movss %xmm2, 0x20(%rsp)
movss %xmm1, 0x34(%rsp)
movss %xmm0, 0x28(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x90(%rsp), %r12
movl 0x88(%rsp), %eax
movq 0x80(%rsp), %r15
testl %eax, %eax
jle 0x35302
movl %eax, %eax
leaq 0x8(%r9), %rcx
xorl %edx, %edx
movq %r15, %rsi
leaq (%rdx,%rdx,2), %rdi
shlq $0x6, %rdi
cmpl $0x0, 0x20(%r9,%rdi)
js 0x35302
xorl %edi, %edi
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rdi,2), %xmm0
movss %xmm0, (%rsi,%rdi)
addq $0x4, %rdi
cmpq $0xc, %rdi
jne 0x352d9
incq %rdx
addq $0xc0, %rcx
addq %rdi, %rsi
cmpq %rax, %rdx
jne 0x352c7
movq %r8, 0x8(%rsp)
movq 0xb0(%rsp), %rbp
movslq 0x18(%r12), %r13
testq %r13, %r13
jle 0x3534f
movq (%r12), %rax
xorl %ecx, %ecx
movq %rbp, %rdx
movl (%rax,%rcx,4), %esi
leal (%rsi,%rsi,2), %esi
movslq %esi, %rsi
leaq (%r15,%rsi,4), %rdi
xorl %esi, %esi
movss (%rdi,%rsi), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpl $0xc, %esi
jne 0x35331
incq %rcx
addq %rsi, %rdx
cmpq %r13, %rcx
jne 0x35322
movl 0x1c(%r12), %ebx
testl %ebx, %ebx
jle 0x353fd
movss (%rbp), %xmm0
ucomiss %xmm0, %xmm0
jp 0x35656
leal -0x1(%rbx), %eax
xorl %r14d, %r14d
cmpl %r14d, %eax
je 0x353fd
movss 0x4(%rbp,%r14,4), %xmm0
incq %r14
ucomiss %xmm0, %xmm0
jnp 0x35370
leaq 0x2683f(%rip), %rdi     # 0x5bbce
leaq 0x2679e(%rip), %rsi     # 0x5bb34
movl %r14d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x20f0
movl %r14d, %esi
movl $0xaaaaaaab, %eax       # imm = 0xAAAAAAAB
imulq %rax, %rsi
shrq $0x21, %rsi
movl %ebx, %edx
imulq %rax, %rdx
shrq $0x21, %rdx
leaq 0x26834(%rip), %rdi     # 0x5bbf7
xorl %eax, %eax
callq 0x20f0
cmpl %ebx, %r14d
jge 0x353fd
leaq 0x26878(%rip), %rdi     # 0x5bc4e
callq 0x2090
leaq 0x2675c(%rip), %rdi     # 0x5bb3e
movl $0x6, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rcx
callq 0x2290
xorl %ebp, %ebp
jmp 0x35624
movq 0xc8(%rsp), %rbx
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %r14
movq 0x10(%r12), %r12
movq %rbp, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x4dfa0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x4dfa0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x4dfa0
movss 0x4(%rsp), %xmm3
cvtsd2ss %xmm0, %xmm0
movq 0x40(%rsp), %r12
movss %xmm0, (%r12)
movss 0x28(%rsp), %xmm1
ucomiss %xmm3, %xmm1
movq 0x38(%rsp), %rcx
movss (%rcx), %xmm1
jbe 0x354cc
movss 0x20(%rsp), %xmm2
ucomiss 0x1bc89(%rip), %xmm2    # 0x5111c
movl 0x88(%rsp), %r13d
jne 0x35536
jp 0x35536
ucomiss %xmm0, %xmm1
jbe 0x35536
movss 0x28(%rsp), %xmm0
divss %xmm3, %xmm0
addss %xmm0, %xmm0
movq 0x18(%rsp), %rax
movss %xmm0, (%rax)
jmp 0x35553
ucomiss %xmm0, %xmm1
movl 0x88(%rsp), %r13d
movl 0x14(%rsp), %esi
jbe 0x35562
movss 0x20(%rsp), %xmm0
ucomiss 0x1bc2e(%rip), %xmm0    # 0x5111c
movq 0x8(%rsp), %r14
movss 0x34(%rsp), %xmm0
movss (%rsp), %xmm1
jne 0x35635
jp 0x35635
ucomiss %xmm0, %xmm3
jbe 0x35635
divss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x2582d(%rip), %xmm0    # 0x5ad50
cvtsd2ss %xmm0, %xmm0
movq 0x18(%rsp), %rax
movss %xmm0, (%rax)
movb $0x1, %bl
xorl %ebp, %ebp
jmp 0x3557b
leaq 0x26702(%rip), %rdi     # 0x5bc3f
callq 0x2090
movss 0x4(%rsp), %xmm3
movq 0x18(%rsp), %rax
movl $0x0, (%rax)
movb $0x1, %bl
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movl 0x14(%rsp), %esi
jmp 0x35576
movq 0x18(%rsp), %rax
movl $0x3f000000, (%rax)     # imm = 0x3F000000
movb $0x1, %bl
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movss (%rsp), %xmm1
incl %esi
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x20(%rsp), %xmm3
leaq 0x265a1(%rip), %rdi     # 0x5bb45
movsd %xmm0, 0x28(%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm3, 0x20(%rsp)
movl %ebp, %edx
movb $0x4, %al
callq 0x20f0
movq %r15, %rdi
movl %r13d, %esi
callq 0x3273e
testb %bl, %bl
je 0x355e7
movl $0x23, %edi
movq %r14, %rsi
movss %xmm0, 0x4(%rsp)
callq 0x2140
movss 0x4(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
leaq 0x25bec(%rip), %rsi     # 0x5b1ea
movq %r14, %rdi
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm2
movsd 0x20(%rsp), %xmm4
movl %ebp, %edx
movb $0x5, %al
callq 0x21a0
movq %r14, %rdi
callq 0x21f0
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
movl $0x3f800000, (%rax)     # imm = 0x3F800000
movss (%r12), %xmm0
movss %xmm0, (%rcx)
movl $0x1, %ebp
xorl %ebx, %ebx
jmp 0x3557b
leaq 0x26571(%rip), %rdi     # 0x5bbce
leaq 0x264d0(%rip), %rsi     # 0x5bb34
xorl %edx, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x20f0
movl %ebx, %eax
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
imulq %rax, %rdx
shrq $0x21, %rdx
leaq 0x26572(%rip), %rdi     # 0x5bbf7
xorl %esi, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x353cf