// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movss %xmm2, 0x18(%rsp)
movss %xmm1, 0x38(%rsp)
movq %rdi, (%rsp)
leaq 0x90(%rsp), %rbx
movl 0x88(%rsp), %eax
movq 0x80(%rsp), %r15
testl %eax, %eax
jle 0x355a4
movl %eax, %eax
leaq 0x8(%r9), %rcx
xorl %r10d, %r10d
movq %r15, %r11
leaq (%r10,%r10,2), %rdi
shlq $0x6, %rdi
cmpl $0x0, 0x20(%r9,%rdi)
js 0x355a4
xorl %edi, %edi
xorps %xmm1, %xmm1
cvtsd2ss (%rcx,%rdi,2), %xmm1
movss %xmm1, (%r11,%rdi)
addq $0x4, %rdi
cmpq $0xc, %rdi
jne 0x3557a
incq %r10
addq $0xc0, %rcx
addq %rdi, %r11
cmpq %rax, %r10
jne 0x35568
movq %rsi, 0x30(%rsp)
movq 0xb0(%rsp), %r13
movslq 0x18(%rbx), %rbp
testq %rbp, %rbp
jle 0x355f0
movq (%rbx), %rax
xorl %ecx, %ecx
movq %r13, %r9
movl (%rax,%rcx,4), %esi
leal (%rsi,%rsi,2), %esi
movslq %esi, %rsi
leaq (%r15,%rsi,4), %rdi
xorl %esi, %esi
movss (%rdi,%rsi), %xmm1
movss %xmm1, (%r9,%rsi)
addq $0x4, %rsi
cmpl $0xc, %esi
jne 0x355d1
incq %rcx
addq %rsi, %r9
cmpq %rbp, %rcx
jne 0x355c2
movl 0x1c(%rbx), %ebx
testl %ebx, %ebx
setg %r14b
movq %r8, 0x10(%rsp)
movq %rdx, 0x40(%rsp)
movss %xmm0, 0x3c(%rsp)
jle 0x35681
movss (%r13), %xmm1
ucomiss %xmm1, %xmm1
jp 0x358d3
leal -0x1(%rbx), %eax
xorl %r12d, %r12d
cmpl %r12d, %eax
je 0x356b4
movss 0x4(%r13,%r12,4), %xmm1
incq %r12
ucomiss %xmm1, %xmm1
jnp 0x35620
cmpl %ebx, %r12d
setl %r14b
leaq 0x265d0(%rip), %rdi     # 0x5bc16
leaq 0x2652f(%rip), %rsi     # 0x5bb7c
movl %r12d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x20f0
movl %r12d, %esi
movl $0xaaaaaaab, %eax       # imm = 0xAAAAAAAB
imulq %rax, %rsi
shrq $0x21, %rsi
movl %ebx, %edx
imulq %rax, %rdx
shrq $0x21, %rdx
leaq 0x265c5(%rip), %rdi     # 0x5bc3f
xorl %eax, %eax
callq 0x20f0
testb %r14b, %r14b
je 0x356b4
leaq 0x26609(%rip), %rdi     # 0x5bc96
callq 0x2090
leaq 0x264ed(%rip), %rdi     # 0x5bb86
movl $0x6, %esi
movl $0x1, %edx
movq 0x10(%rsp), %rcx
callq 0x2280
xorl %ebp, %ebp
jmp 0x358c2
movq 0xc8(%rsp), %rbx
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %r12
leaq 0x90(%rsp), %rax
movq 0x10(%rax), %r14
movq %r13, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x4e010
movsd %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x4e010
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x4e010
movss 0x24(%rsp), %xmm4
movss 0x3c(%rsp), %xmm3
cvtsd2ss %xmm0, %xmm0
movq 0x40(%rsp), %r12
movss %xmm0, (%r12)
ucomiss %xmm4, %xmm3
movq 0x30(%rsp), %r13
movss (%r13), %xmm1
jbe 0x35764
movss 0x18(%rsp), %xmm2
ucomiss 0x1b8c3(%rip), %xmm2    # 0x51010
jne 0x357c0
jp 0x357c0
ucomiss %xmm0, %xmm1
jbe 0x357c0
divss %xmm4, %xmm3
addss %xmm3, %xmm3
xorl %ebp, %ebp
movb $0x1, %bl
jmp 0x357d9
ucomiss %xmm0, %xmm1
movl 0x8(%rsp), %esi
movq (%rsp), %rax
jbe 0x357e8
xorl %ebx, %ebx
movss 0x1b895(%rip), %xmm3    # 0x51010
movss 0x18(%rsp), %xmm0
ucomiss %xmm3, %xmm0
movl $0x1, %ebp
movq 0x10(%rsp), %r14
movsd 0x28(%rsp), %xmm1
jne 0x357ff
jp 0x357ff
movss 0x38(%rsp), %xmm0
ucomiss %xmm0, %xmm4
jbe 0x357ff
divss %xmm4, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x255fd(%rip), %xmm0    # 0x5adb0
xorps %xmm3, %xmm3
cvtsd2ss %xmm0, %xmm3
xorl %ebp, %ebp
movb $0x1, %bl
jmp 0x357ff
leaq 0x264c0(%rip), %rdi     # 0x5bc87
callq 0x2090
movss 0x24(%rsp), %xmm4
xorl %ebp, %ebp
movb $0x1, %bl
xorps %xmm3, %xmm3
movq 0x10(%rsp), %r14
movl 0x8(%rsp), %esi
movq (%rsp), %rax
jmp 0x357f9
xorl %ebp, %ebp
movss 0x2038a(%rip), %xmm3    # 0x55b7c
movb $0x1, %bl
movq 0x10(%rsp), %r14
movsd 0x28(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm3, (%rax)
testb %bl, %bl
jne 0x3581a
movss (%r12), %xmm1
movss %xmm1, (%r13)
incl %esi
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm4, %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x18(%rsp), %xmm3
leaq 0x2634d(%rip), %rdi     # 0x5bb8d
movsd %xmm0, 0x8(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm3, 0x18(%rsp)
movl %ebp, %edx
movb $0x4, %al
callq 0x20f0
movq %r15, %rdi
movl 0x88(%rsp), %esi
callq 0x32b71
testb %bl, %bl
je 0x35885
movl $0x23, %edi
movq %r14, %rsi
movss %xmm0, (%rsp)
callq 0x2140
movss (%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
leaq 0x25996(%rip), %rsi     # 0x5b232
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x30(%rsp), %xmm2
movsd 0x18(%rsp), %xmm4
movl %ebp, %edx
movb $0x5, %al
callq 0x2190
movq %r14, %rdi
callq 0x21e0
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x3563f