// File: /workspace/github_c_makefile_star2_O2/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
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
cmpq %rax, %rdx
je 0x154e6
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x154d7
movslq %ecx, %r13
pushq $0x1
popq %r15
cmpq %r13, %r15
jge 0x155f1
imulq $0xc0, %r15, %r12
cmpl $0x0, 0x20(%r14,%r12)
js 0x155f1
addq %r14, %r12
movslq 0x34(%r12), %rax
testq %rax, %rax
js 0x15534
cmpl %ebp, %eax
jge 0x15600
movsd 0x58(%r12), %xmm0
movsd %xmm0, (%rbx,%rax,8)
ucomisd %xmm0, %xmm0
jp 0x15600
movl 0x38(%r12), %eax
testl %eax, %eax
js 0x15590
cmpl %ebp, %eax
jge 0x15600
movsd 0x60(%r12), %xmm1
divsd 0x58(%r12), %xmm1
movsd 0x35aed(%rip), %xmm0    # 0x4b048
ucomisd %xmm0, %xmm1
ja 0x1557b
movsd 0x3aad7(%rip), %xmm0    # 0x50040
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm0
jbe 0x1557b
movsd 0x3aac5(%rip), %xmm0    # 0x50040
callq 0x2150
movslq 0x38(%r12), %rax
movsd %xmm0, (%rbx,%rax,8)
ucomisd %xmm0, %xmm0
jp 0x15600
movslq 0x3c(%r12), %rax
testq %rax, %rax
js 0x155e9
movq (%r12), %rcx
cmpl $0x0, 0x48(%rcx)
je 0x155e9
cmpl %ebp, %eax
jge 0x15600
movsd 0x68(%r12), %xmm0
movss 0x4e279(%rip), %xmm2    # 0x63830
cvtss2sd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x155c7
subsd %xmm1, %xmm0
jmp 0x155bb
xorps 0x3b292(%rip), %xmm2    # 0x50860
cvtss2sd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x155de
addsd %xmm1, %xmm0
jmp 0x155d2
movsd %xmm0, (%rbx,%rax,8)
ucomisd %xmm0, %xmm0
jp 0x15600
incq %r15
jmp 0x154ed
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 0x3b513(%rip), %rdi     # 0x50b2e
movl %r15d, %esi
xorl %eax, %eax
callq 0x20f0
movl 0x34(%r12), %esi
movl 0x38(%r12), %edx
movl 0x3c(%r12), %ecx
leaq 0x3b531(%rip), %rdi     # 0x50b6c
movl %ebp, %r8d
xorl %eax, %eax
callq 0x20f0
movslq 0x34(%r12), %rax
testq %rax, %rax
js 0x15662
movsd (%rbx,%rax,8), %xmm0
leaq 0x3b53f(%rip), %rdi     # 0x50b9a
movb $0x1, %al
callq 0x20f0
movslq 0x38(%r12), %rax
testq %rax, %rax
js 0x1567f
movsd (%rbx,%rax,8), %xmm0
leaq 0x3b52d(%rip), %rdi     # 0x50ba5
movb $0x1, %al
callq 0x20f0
movslq 0x3c(%r12), %rax
testq %rax, %rax
js 0x1569c
movsd (%rbx,%rax,8), %xmm0
leaq 0x3b51d(%rip), %rdi     # 0x50bb2
movb $0x1, %al
callq 0x20f0
movsd 0x58(%r12), %xmm0
movsd 0x60(%r12), %xmm1
movsd 0x68(%r12), %xmm2
leaq 0x3b509(%rip), %rdi     # 0x50bc1
movb $0x3, %al
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270
nop