// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/buildup.c
// Function: Internal_coordinates
void Internal_coordinates(double *int_coord, int N_axes,
			  struct bond *bonds, int N_atoms)
{
  struct bond *bond; int i, k=0;
  for(i=0; i<N_axes; i++)int_coord[i]=0;
  for(i=1; i<N_atoms; i++){
    bond=bonds+i;
    if(bond->i_atom<0)break;
    if(bond->len >= 0){
      if(bond->len>=N_axes)goto error;
      int_coord[bond->len]=bond->l; k++;
      if(isnan(int_coord[bond->len]))goto error;
    }
    if(bond->angle >= 0){
      if(bond->angle>=N_axes)goto error;
      double c=bond->r_cos_theta/bond->l;
      if(c>1){c=1;}else if(c<-1){c=-1;}
      int_coord[bond->angle]=acos(c); k++;
      if(isnan(int_coord[bond->angle]))goto error;
    }
    if((bond->torsion >= 0)&&(bond->atom->main)){
      if(bond->torsion>=N_axes)goto error;
      double c=bond->phi;
      while(c > TWOPI){c-=TWOPI;}
      while(c <-TWOPI){c+=TWOPI;}
      int_coord[bond->torsion]=c; k++;
      if(isnan(int_coord[bond->torsion]))goto error;
    }
  }
  if(0)printf("%d internal coordinates over %d stored\n", k, N_axes);
  return;
 error:
  printf("ERROR in internal_coordinates, bond %d atom %s %c%d chain %d\n",
	 i, bond->atom->name, bond->atom->aa,
	 bond->atom->res, bond->atom->chain);
  printf("len: %d angle: %d torsion: %d Total_dofs= %d\n",
	 bond->len, bond->angle, bond->torsion, N_axes);
  if(bond->len>=0)printf("Len: %.2f\n", int_coord[bond->len]);
  if(bond->angle>=0)printf("Angle: %.2f\n", int_coord[bond->angle]);
  if(bond->torsion>=0)printf("Torsion: %.2f\n", int_coord[bond->torsion]);
  printf("r= %.2f r_cos_theta %.4f phi= %.4f\n",
	  bond->l, bond->r_cos_theta, bond->phi);
  exit(8);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %r15d
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
testl %esi, %esi
jle 0x16830
movl %ebp, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2110
cmpl $0x2, %r15d
jl 0x16946
movl %r15d, %r13d
movl $0x1, %r15d
leaq (%r15,%r15,2), %r12
shlq $0x6, %r12
cmpl $0x0, 0x20(%r14,%r12)
js 0x16946
addq %r14, %r12
movslq 0x34(%r12), %rax
testq %rax, %rax
js 0x16882
cmpl %ebp, %eax
jge 0x16955
movsd 0x58(%r12), %xmm0
movsd %xmm0, (%rbx,%rax,8)
ucomisd %xmm0, %xmm0
jp 0x16955
movl 0x38(%r12), %eax
testl %eax, %eax
js 0x168de
cmpl %ebp, %eax
jge 0x16955
movsd 0x60(%r12), %xmm1
divsd 0x58(%r12), %xmm1
movsd 0x3a89f(%rip), %xmm0    # 0x51148
ucomisd %xmm0, %xmm1
ja 0x168c9
movsd 0x3f841(%rip), %xmm0    # 0x560f8
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm0
jbe 0x168c9
movsd 0x3f82f(%rip), %xmm0    # 0x560f8
callq 0x2150
movslq 0x38(%r12), %rax
movsd %xmm0, (%rbx,%rax,8)
ucomisd %xmm0, %xmm0
jp 0x16955
movslq 0x3c(%r12), %rax
testq %rax, %rax
js 0x1693a
movq (%r12), %rcx
cmpl $0x0, 0x48(%rcx)
je 0x1693a
cmpl %ebp, %eax
jge 0x16955
movsd 0x68(%r12), %xmm0
movss 0x51f2b(%rip), %xmm2    # 0x68830
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
jmp 0x16912
subsd %xmm1, %xmm0
ucomisd %xmm1, %xmm0
ja 0x1690e
xorps 0x3a711(%rip), %xmm2    # 0x51030
cvtss2sd %xmm2, %xmm2
jmp 0x16929
addsd %xmm1, %xmm0
ucomisd %xmm0, %xmm2
ja 0x16925
movsd %xmm0, (%rbx,%rax,8)
ucomisd %xmm0, %xmm0
jp 0x16955
incq %r15
cmpq %r13, %r15
jne 0x16843
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rax
leaq 0x18(%rax), %rdx
movsbl 0x68(%rax), %ecx
movl 0x64(%rax), %r8d
movl 0x6c(%rax), %r9d
leaq 0x4026e(%rip), %rdi     # 0x56bde
movl %r15d, %esi
xorl %eax, %eax
callq 0x20f0
movl 0x34(%r12), %esi
movl 0x38(%r12), %edx
movl 0x3c(%r12), %ecx
leaq 0x4028c(%rip), %rdi     # 0x56c1c
movl %ebp, %r8d
xorl %eax, %eax
callq 0x20f0
movslq 0x34(%r12), %rax
testq %rax, %rax
js 0x169b7
movsd (%rbx,%rax,8), %xmm0
leaq 0x4029a(%rip), %rdi     # 0x56c4a
movb $0x1, %al
callq 0x20f0
movslq 0x38(%r12), %rax
testq %rax, %rax
js 0x169d4
movsd (%rbx,%rax,8), %xmm0
leaq 0x40288(%rip), %rdi     # 0x56c55
movb $0x1, %al
callq 0x20f0
movslq 0x3c(%r12), %rax
testq %rax, %rax
js 0x169f1
movsd (%rbx,%rax,8), %xmm0
leaq 0x40278(%rip), %rdi     # 0x56c62
movb $0x1, %al
callq 0x20f0
movsd 0x58(%r12), %xmm0
movsd 0x60(%r12), %xmm1
movsd 0x68(%r12), %xmm2
leaq 0x40264(%rip), %rdi     # 0x56c71
movb $0x3, %al
callq 0x20f0
movl $0x8, %edi
callq 0x2280
nop