// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Torsional_confchange_1angle
int Torsional_confchange_1angle(double *diff_tors, int naxes, float angle,
				struct bond *bonds, int natoms1,
				struct ali_atoms ali_a,
				float *coord_ref_1, float *coord_ref_2,
				float STEP_MIN, float RMSD_min, atom *atoms1,
				float *coord_all)
{
  printf("Changing %d internal coordinates one by one\n", naxes);

  // Initialize random numbers
  if(INIRAN==0){
     INIRAN=1;
     unsigned long iran=randomgenerator();
     InitRandom( (RANDOMTYPE)iran);
  }

  int i, i_min=-1, dir_min=0;
  for(i=0; i<naxes; i++)diff_tors[i]=0;
  int tested[naxes]; for(i=0; i<naxes; i++)tested[i]=0;
  int N_to_test=naxes;

  struct bond bonds2[natoms1];
  int N_ref=ali_a.N_ref;
  float coord_new[ali_a.N_Cart];  
  float *mass_ref=ali_a.mass;
  float RMSD_kf, RMSD_step;
  for(int k=0; k<naxes; k++){
    // Change internal coordinate i in positive and negative direction
    int ran=RandomFloating()*N_to_test, iran=0;
    if(ran>=N_to_test)ran=N_to_test-1;
    for(i=0; i<naxes; i++){
      if(tested[i])continue;
      if(iran==ran)break;
      iran++;
    }
    if(i==naxes)break;

    // Test i
    tested[i]=1; N_to_test--;
    for(int dir=-1; dir<=1; dir+=2){
      diff_tors[i]=dir*angle;
      Copy_bonds(bonds2, bonds, natoms1);
      Build_up(bonds2, natoms1, diff_tors, naxes);
      Put_coord(coord_all, bonds2, natoms1);
      Write_ref_coord(coord_new, N_ref, coord_all, ali_a.ali1);
      RMSD_kf=rmsd_mclachlan_f(coord_ref_2, coord_new, mass_ref, N_ref);
      if(RMSD_kf<RMSD_min){
	RMSD_step=rmsd_mclachlan_f(coord_ref_1, coord_new, mass_ref, N_ref);
	if(RMSD_step > STEP_MIN){
	  i_min=i; dir_min=dir; RMSD_min=RMSD_kf; break;
	}
      }
    }
    diff_tors[i]=0.0;
  }

  if(i_min<0){
    printf("\nERROR, internal coordinate %d does not exist\n", i_min);
    return(-1);
  }
  diff_tors[i_min]=dir_min*angle;
  return(i_min);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movss %xmm2, -0x34(%rbp)
movss %xmm1, -0x5c(%rbp)
movq %r9, -0xa8(%rbp)
movq %r8, -0x88(%rbp)
movl %ecx, -0x3c(%rbp)
movq %rdx, -0xa0(%rbp)
movss %xmm0, -0x38(%rbp)
movl %esi, %r12d
movq %rdi, %rbx
leaq 0x22922(%rip), %rdi     # 0x55445
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x419d4(%rip)     # 0x74508
movl %r12d, %r14d
jne 0x32b50
movl $0x1, 0x419c5(%rip)     # 0x74508
callq 0x2f672
movq %rax, %rdi
callq 0x49ebc
leaq 0x10(%rbp), %rax
testl %r12d, %r12d
movl $0x0, %edx
cmovgl %r12d, %edx
cmpq %r15, %rdx
je 0x32b6f
andq $0x0, (%rbx,%r15,8)
incq %r15
jmp 0x32b60
movq %rbx, -0x50(%rbp)
movq %rsp, %rbx
leaq 0xf(,%r14,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rbx
movq %rbx, %rsp
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x32b98
andl $0x0, (%rbx,%rcx,4)
incq %rcx
jmp 0x32b8a
movl -0x3c(%rbp), %ecx
movq %rsp, %rsi
imulq $0xc0, %rcx, %rcx
subq %rcx, %rsi
movq %rsi, -0x98(%rbp)
movq %rsi, %rsp
movl 0x18(%rax), %ecx
movl %ecx, -0x40(%rbp)
movl 0x1c(%rax), %ecx
movq %rsp, %rsi
leaq 0xf(,%rcx,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rsi
movq %rsi, -0x70(%rbp)
movq %rsi, %rsp
movq (%rax), %rcx
movq %rcx, -0x90(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
pushq $-0x1
popq %r15
movl $0x0, -0x30(%rbp)
xorl %eax, %eax
movq %rdx, -0x80(%rbp)
movl %eax, -0x58(%rbp)
cmpl %edx, %eax
je 0x32d6c
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x49ffd
cvtsi2sd %r12d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
leal -0x1(%r12), %ecx
cmpl %eax, %r12d
movl %ecx, -0x54(%rbp)
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %r13, %r14
je 0x32d6c
cmpl $0x0, (%rbx,%r13,4)
jne 0x32c3f
cmpl %eax, %ecx
je 0x32c44
incl %ecx
incq %r13
jmp 0x32c29
cmpl %r13d, %r14d
je 0x32d6c
movl %r15d, -0x2c(%rbp)
movq %r14, -0x48(%rbp)
movq %rbx, -0x78(%rbp)
movl $0x1, (%rbx,%r13,4)
pushq $-0x1
popq %r15
cmpl $0x1, %r15d
jg 0x32d43
cvtsi2ss %r15d, %xmm0
mulss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x50(%rbp), %r14
movsd %xmm0, (%r14,%r13,8)
movq -0x98(%rbp), %r12
movq %r12, %rdi
movq -0xa0(%rbp), %rsi
movl -0x3c(%rbp), %ebx
movl %ebx, %edx
callq 0x301ca
movq %r12, %rdi
movl %ebx, %esi
movq %r14, %rdx
movq -0x48(%rbp), %rcx
callq 0x13d70
movq 0x38(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x2f358
movq -0x70(%rbp), %r12
movq %r12, %rdi
movl -0x40(%rbp), %ebx
movl %ebx, %esi
movq %r14, %rdx
movq -0x90(%rbp), %rcx
callq 0x2f9b8
movq -0xa8(%rbp), %rdi
movq %r12, %rsi
movq -0x68(%rbp), %rdx
movl %ebx, %ecx
callq 0x47a64
cvtsd2ss %xmm0, %xmm1
movss -0x34(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x32d2d
movq -0x88(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl -0x40(%rbp), %ecx
movss %xmm1, -0x60(%rbp)
callq 0x47a64
movss -0x60(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
ucomiss -0x5c(%rbp), %xmm0
ja 0x32d36
addl $0x2, %r15d
jmp 0x32c65
movl %r13d, -0x2c(%rbp)
movl %r15d, -0x30(%rbp)
movss %xmm1, -0x34(%rbp)
movq -0x48(%rbp), %r14
movq -0x50(%rbp), %rax
andq $0x0, (%rax,%r13,8)
movl -0x58(%rbp), %eax
incl %eax
movl -0x54(%rbp), %ecx
movl %ecx, %r12d
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rbx
movl -0x2c(%rbp), %r15d
jmp 0x32bf7
testl %r15d, %r15d
js 0x32d8d
cvtsi2ssl -0x30(%rbp), %xmm0
mulss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movl %r15d, %eax
movq -0x50(%rbp), %rcx
movsd %xmm0, (%rcx,%rax,8)
jmp 0x32da2
leaq 0x22680(%rip), %rdi     # 0x55414
movl %r15d, %esi
xorl %eax, %eax
callq 0x20f0
pushq $-0x1
popq %r15
movl %r15d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq