// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/buildup.c
// Function: Set_bonds_measure
int Set_bonds_measure(struct bond *bonds, int N_atoms, atom *atoms)
{
  int i, j; atom *atom=atoms;
  struct bond *bond=bonds, *previous;
  //~ struct bond *bond2;

  if(DEBUG)printf("Setting bond lengths and angles\n");

  bonds[0].previous=NULL;
  for(i=0; i<N_atoms; i++){
    bond=bonds+i;
    if(bond->i_atom<0)break;
    atom=atoms+bond->i_atom;
    for(j=0; j<3; j++)bond->r[j]=atom->r[j];
    previous=bond->previous;

    if(previous==NULL){ // First atom
      if(i){
	printf("ERROR, atom %d %s res %d has no previous atom\n",
	       i, atom->name, atom->res); exit(8);
      }
      double d=0;
      for(j=0; j<3; j++){
	bond->dx[j]=0; bond->dy[j]=0;
	float dz=(atom+1)->r[j]-atom->r[j];
	bond->dz[j]=dz; d+=dz*dz;
      }
      d=sqrt(d); for(j=0; j<3; j++)bond->dz[j]/=d;
      continue;
    }

    // Set r
    double dr_dr=0;
    for(j=0; j<3; j++){
      double dr=bond->r[j]-previous->r[j];
      bond->dr[j]=dr; dr_dr+=dr*dr;
    }
    bond->l=sqrt(dr_dr);
    for(j=0; j<3; j++){
      bond->dz[j]=bond->dr[j]/bond->l;
    }
    // Initialize dx
    for(j=0; j<3; j++)bond->dx[j]=0;

    if(previous->previous==NULL){
      bond->r_cos_theta=-bond->l; bond->phi=0;
      double dx[3];
      for(j=0; j<3; j++)dx[j]=-(atom+1)->r[j]+atom->r[j];
      if(Make_frame(bond, NULL, dx)<0){
	printf("ERROR in Make_frame\n"); exit(8);
      }
      continue;
    }

    // i>0,1 set angle and torsion

    // Make orthogonal frame for next atom
    if(bond->terminal==0){
      if(Make_frame(bond, previous, previous->dz)<0){
	printf("ERROR in Make_frame\n"); exit(8);
      }
    }

    // Set theta
    bond->r_cos_theta=-Scalar_product_3_d(bond->dr, previous->dz);
    if(bond->r_cos_theta > bond->l){bond->r_cos_theta = bond->l;}
    else if(bond->r_cos_theta < -bond->l){bond->r_cos_theta = -bond->l;}
    double r_sin_theta= sqrt(dr_dr-bond->r_cos_theta*bond->r_cos_theta);

    // Set phi
    double ax=Scalar_product_3_d(bond->dr, previous->dx);
    double cos_phi=ax/r_sin_theta;
    if(cos_phi>1){cos_phi=1;}
    else if(cos_phi<-1){cos_phi=-1;}
    double ay=Scalar_product_3_d(bond->dr, previous->dy);
    if(ay >= 0){
      bond->phi=acos(cos_phi);
    }else{
      bond->phi=-acos(cos_phi);
    }

    if(DEBUG){
      double dr[3], dx[3];
      double az=-bond->r_cos_theta;
      for(j=0; j<3; j++){
	dr[j]=
	  az*previous->dz[j]+
	  ax*previous->dx[j]+
	  ay*previous->dy[j];
      }
      dx[0]=Scalar_product_3_d(bond->dr, previous->dx)-
	Scalar_product_3_d(dr, previous->dx);
      dx[1]=Scalar_product_3_d(bond->dr, previous->dy)-
	Scalar_product_3_d(dr, previous->dy);
      dx[2]=Scalar_product_3_d(bond->dr, previous->dz)-
	Scalar_product_3_d(dr, previous->dz);
      printf("%3d %4s %4s %8.2g %8.2g %8.2g\n", bond->i_atom,
	     atom->name, atoms[bond->previous->i_atom].name,
	     dx[0], dx[1], dx[2]);
    }
  }
  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x20(%rsp)
movl %esi, %eax
andq $0x0, 0x28(%rdi)
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
movq %rax, 0x28(%rsp)
leaq 0x8(%rdi), %r12
leaq 0xa8(%rdi), %r14
leaq 0x90(%rdi), %rbx
movq %rdi, 0x8(%rsp)
cmpq 0x28(%rsp), %rsi
je 0x13d30
imulq $0xc0, %rsi, %rbp
movslq 0x20(%rdi,%rbp), %rax
testq %rax, %rax
js 0x13d30
addq %rdi, %rbp
imulq $0x70, %rax, %r15
addq 0x20(%rsp), %r15
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13a78
movsd (%r15,%rax,8), %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
jmp 0x13a61
movq 0x28(%rbp), %r13
testq %r13, %r13
je 0x13ad1
leaq 0x40(%rbp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13ab7
movsd (%r12,%rax,8), %xmm1
subsd 0x8(%r13,%rax,8), %xmm1
movsd %xmm1, 0x38(%r12,%rax,8)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x13a90
ucomisd 0x37591(%rip), %xmm0    # 0x4b050
movq %rsi, 0x18(%rsp)
movsd %xmm0, (%rsp)
jb 0x13b35
sqrtsd %xmm0, %xmm0
jmp 0x13b3a
testq %rsi, %rsi
jne 0x13d4f
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13b1e
andq $0x0, -0x18(%r14,%rax,8)
andq $0x0, -0x30(%r14,%rax,8)
movsd 0x70(%r15,%rax,8), %xmm1
subsd (%r15,%rax,8), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm2
movsd %xmm2, (%r14,%rax,8)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x13ae0
ucomisd 0x3752a(%rip), %xmm0    # 0x4b050
jb 0x13c30
sqrtsd %xmm0, %xmm0
jmp 0x13c40
callq 0x2290
movsd %xmm0, 0x58(%rbp)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13b5d
movsd -0x68(%r14,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rax,8)
incq %rax
jmp 0x13b41
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13b6f
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x13b5f
cmpq $0x0, 0x28(%r13)
je 0x13be2
cmpl $0x0, 0x30(%rbp)
jne 0x13b96
leaq 0xa8(%r13), %rdx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x14e3b
testl %eax, %eax
js 0x13d41
leaq 0xa8(%r13), %rsi
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0xf96f
movapd %xmm0, %xmm2
xorpd 0x3750a(%rip), %xmm2    # 0x4b0c0
movlpd %xmm2, 0x60(%rbp)
movsd 0x58(%rbp), %xmm1
ucomisd %xmm1, %xmm2
ja 0x13bd8
ucomisd %xmm1, %xmm0
jbe 0x13c61
xorpd 0x374e8(%rip), %xmm1    # 0x4b0c0
movsd %xmm1, 0x60(%rbp)
jmp 0x13c65
xorpd 0x374d6(%rip), %xmm0    # 0x4b0c0
movlpd %xmm0, 0x60(%rbp)
andq $0x0, 0x68(%rbp)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13c14
movsd (%r15,%rax,8), %xmm0
subsd 0x70(%r15,%rax,8), %xmm0
movsd %xmm0, 0x30(%rsp,%rax,8)
incq %rax
jmp 0x13bf6
movq %rbp, %rdi
xorl %esi, %esi
leaq 0x30(%rsp), %rdx
callq 0x14e3b
testl %eax, %eax
jns 0x13d10
jmp 0x13d41
movq %rsi, %r15
callq 0x2290
movq 0x8(%rsp), %rdi
movq %r15, %rsi
xorl %eax, %eax
cmpq $0x3, %rax
je 0x13d1a
movsd (%r14,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rax,8)
incq %rax
jmp 0x13c42
movapd %xmm2, %xmm1
mulsd %xmm1, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x373d6(%rip), %xmm0    # 0x4b050
jb 0x13c82
sqrtsd %xmm0, %xmm0
jmp 0x13c87
callq 0x2290
movsd %xmm0, (%rsp)
leaq 0x90(%r13), %rsi
movq %r15, %rdi
callq 0xf96f
divsd (%rsp), %xmm0
movsd 0x373a0(%rip), %xmm1    # 0x4b048
ucomisd %xmm1, %xmm0
ja 0x13cc8
movsd 0x3c38a(%rip), %xmm1    # 0x50040
ucomisd %xmm0, %xmm1
movapd %xmm0, %xmm1
jbe 0x13cc8
movsd 0x3c378(%rip), %xmm1    # 0x50040
movsd %xmm1, (%rsp)
addq $0x78, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0xf96f
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
callq 0x2150
movsd 0x10(%rsp), %xmm1
ucomisd 0x37356(%rip), %xmm1    # 0x4b050
jae 0x13d0b
xorpd 0x373bc(%rip), %xmm0    # 0x4b0c0
movlpd %xmm0, 0x68(%rbp)
jmp 0x13d10
movsd %xmm0, 0x68(%rbp)
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdi
incq %rsi
movl $0xc0, %eax
addq %rax, %r12
addq %rax, %r14
addq %rax, %rbx
jmp 0x13a33
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3d03b(%rip), %rdi     # 0x50d83
callq 0x2090
jmp 0x13d68
movl 0x64(%r15), %ecx
addq $0x18, %r15
leaq 0x3cba9(%rip), %rdi     # 0x50907
movq %r15, %rdx
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270