// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movss %xmm2, -0x40(%rbp)
movss %xmm1, -0x58(%rbp)
movq %r9, -0xb8(%rbp)
movq %r8, -0xa0(%rbp)
movl %ecx, -0x2c(%rbp)
movq %rdx, %r14
movss %xmm0, -0x44(%rbp)
movl %esi, %r15d
movq %rdi, %r13
leaq 0x249ff(%rip), %rdi     # 0x5b4e5
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x42a0c(%rip)     # 0x79500
jne 0x36b0d
movl $0x1, 0x42a00(%rip)     # 0x79500
callq 0x32a14
movq %rax, %rdi
callq 0x503ac
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testl %r15d, %r15d
jle 0x36e68
movl %r15d, %ebx
leaq (,%rbx,8), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x2110
movq %rsp, %rdi
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
movq %rbx, -0x68(%rbp)
leaq (,%rbx,4), %rdx
movq %rdi, -0x90(%rbp)
xorl %esi, %esi
callq 0x2110
movl -0x2c(%rbp), %esi
movq %rsp, %rax
movq %rsi, %rcx
shlq $0x6, %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rax, %r12
subq %rcx, %r12
movq %r12, %rsp
leaq 0x10(%rbp), %r8
movl 0x18(%r8), %edi
movl 0x1c(%r8), %edx
movq %r8, %r9
movq %rsp, %r8
leaq 0xf(,%rdx,4), %rdx
andq $-0x10, %rdx
subq %rdx, %r8
movq %r8, -0x50(%rbp)
movq %r8, %rsp
movq 0x10(%r9), %rdx
movq %rdx, -0x78(%rbp)
negq %rcx
addq %rcx, %rax
addq $0x8, %rax
movq %rax, -0xa8(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ecx, %ecx
movl %r15d, -0x3c(%rbp)
movq %rsi, -0xb0(%rbp)
movq %rdi, -0x80(%rbp)
movl %ecx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl %r15d, %ebx
xorl %eax, %eax
callq 0x504e7
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
leal -0x1(%rbx), %edi
cmpl %eax, %ebx
movl -0x2c(%rbp), %ebx
cmovlel %edi, %eax
xorl %r15d, %r15d
xorl %ecx, %ecx
movq -0x68(%rbp), %rdx
movq -0x90(%rbp), %rsi
cmpl $0x0, (%rsi,%r15,4)
jne 0x36c21
cmpl %eax, %ecx
je 0x36c2e
incl %ecx
incq %r15
cmpq %r15, %rdx
jne 0x36c14
jmp 0x36e3a
cmpl %r15d, %edx
je 0x36e3a
movl %edi, -0x54(%rbp)
movl $0x1, (%rsi,%r15,4)
leaq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r13, -0x98(%rbp)
movq %rax, -0x88(%rbp)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss -0x44(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%r13,%r15,8)
movq $0x0, 0x28(%r12)
testl %ebx, %ebx
jle 0x36d49
xorl %ebx, %ebx
xorl %r13d, %r13d
leaq (%r14,%rbx), %rsi
leaq (%r12,%rbx), %rdi
movl $0xc0, %edx
callq 0x21b0
testl %r13d, %r13d
je 0x36cc8
movq 0x28(%r14,%rbx), %rax
subq %r14, %rax
shrq $0x6, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
cltq
leaq (%rax,%rax,2), %rax
shlq $0x6, %rax
addq %r12, %rax
movq %rax, 0x28(%r12,%rbx)
incl %r13d
addq $0xc0, %rbx
cmpl %r13d, -0x2c(%rbp)
jne 0x36c8d
movq %r12, %rdi
movl -0x2c(%rbp), %ebx
movl %ebx, %esi
movq -0x98(%rbp), %r13
movq %r13, %rdx
movl -0x3c(%rbp), %ecx
callq 0x15083
movq -0xa8(%rbp), %rax
xorl %ecx, %ecx
movq 0x38(%rbp), %rdi
movq %rdi, %rdx
movq -0xb0(%rbp), %r8
movq -0x70(%rbp), %r9
leaq (%rcx,%rcx,2), %rsi
shlq $0x6, %rsi
cmpl $0x0, 0x20(%r12,%rsi)
js 0x36d61
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x36d1f
incq %rcx
addq $0xc0, %rax
addq %rsi, %rdx
cmpq %r8, %rcx
jne 0x36d0d
jmp 0x36d61
movq %r12, %rdi
movl %ebx, %esi
movq %r13, %rdx
movl -0x3c(%rbp), %ecx
callq 0x15083
movq -0x70(%rbp), %r9
movq 0x38(%rbp), %rdi
movq -0x80(%rbp), %rcx
testl %ecx, %ecx
jle 0x36d9e
xorl %eax, %eax
movq -0x50(%rbp), %r8
movl (%r9,%rax,4), %edx
leal (%rdx,%rdx,2), %edx
movslq %edx, %rdx
leaq (%rdi,%rdx,4), %rsi
xorl %edx, %edx
movss (%rsi,%rdx), %xmm0
movss %xmm0, (%r8,%rdx)
addq $0x4, %rdx
cmpl $0xc, %edx
jne 0x36d7f
incq %rax
addq %rdx, %r8
cmpq %rcx, %rax
jne 0x36d6f
movq -0xb8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x4dfa0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss -0x40(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x36def
movq -0xa0(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x80(%rbp), %rcx
movss %xmm1, -0x5c(%rbp)
callq 0x4dfa0
movss -0x5c(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
ucomiss -0x58(%rbp), %xmm0
ja 0x36e0b
movq -0x88(%rbp), %rcx
leal 0x2(%rcx), %eax
testl %ecx, %ecx
js 0x36c59
movl -0x30(%rbp), %eax
movl %eax, %edx
movl -0x34(%rbp), %eax
jmp 0x36e1e
movl %r15d, -0x38(%rbp)
movss %xmm1, -0x40(%rbp)
movl -0x34(%rbp), %eax
movq -0x88(%rbp), %rdx
movq $0x0, (%r13,%r15,8)
incl %eax
movl %edx, %ecx
cmpl -0x68(%rbp), %eax
movl -0x54(%rbp), %r15d
jne 0x36bd9
jmp 0x36e3f
movl -0x30(%rbp), %eax
movl %eax, %edx
movl -0x38(%rbp), %esi
testl %esi, %esi
js 0x36e63
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
mulss -0x44(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movl %esi, %eax
movsd %xmm0, (%r13,%rax,8)
movl %esi, %ebx
jmp 0x36e76
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x24645(%rip), %rdi     # 0x5b4b4
xorl %eax, %eax
callq 0x20f0
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq