// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, 0x38(%rsp)
movq $0x0, 0x28(%rdi)
testl %esi, %esi
jle 0x15042
movl %esi, %eax
movq %rax, 0x28(%rsp)
leaq 0x90(%rdi), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%rdi), %r14
leaq 0xa8(%rdi), %r12
xorl %r15d, %r15d
xorl %esi, %esi
movq %rdi, 0x8(%rsp)
movq %rsi, %rax
shlq $0x6, %rax
leaq (%rax,%rax,2), %r13
movslq 0x20(%rdi,%r13), %rax
testq %rax, %rax
js 0x15042
movq 0x30(%rsp), %rcx
addq %r13, %rcx
addq %rdi, %r13
imulq $0x70, %rax, %rbx
addq 0x38(%rsp), %rbx
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm0
movsd %xmm0, (%r14,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x14d6d
movq 0x28(%r13), %rbp
testq %rbp, %rbp
je 0x14dd7
leaq 0x40(%r13), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%r14,%rax,8), %xmm1
subsd 0x8(%rbp,%rax,8), %xmm1
movsd %xmm1, 0x38(%r14,%rax,8)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x14d99
ucomisd 0x3c3b3(%rip), %xmm0    # 0x51178
movq %rsi, 0x18(%rsp)
movsd %xmm0, (%rsp)
jb 0x14e38
sqrtsd %xmm0, %xmm0
jmp 0x14e47
testq %rsi, %rsi
jne 0x15061
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq %r15, -0x18(%r12,%rax,8)
movq %r15, -0x30(%r12,%rax,8)
movsd 0x70(%rbx,%rax,8), %xmm1
subsd (%rbx,%rax,8), %xmm1
cvtsd2ss %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm1, %xmm2
movsd %xmm2, (%r12,%rax,8)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x14de6
ucomisd 0x3c34f(%rip), %xmm0    # 0x51178
jb 0x14f3e
sqrtsd %xmm0, %xmm0
jmp 0x14f4e
movq %rcx, 0x20(%rsp)
callq 0x22a0
movq 0x20(%rsp), %rcx
movsd %xmm0, 0x58(%r13)
xorl %eax, %eax
movsd -0x68(%r12,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x14e4f
xorpd %xmm1, %xmm1
movupd %xmm1, (%rcx)
movq $0x0, 0x10(%rcx)
cmpq $0x0, 0x28(%rbp)
je 0x14ef0
cmpl $0x0, 0x30(%r13)
jne 0x14ea1
leaq 0xa8(%rbp), %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x16191
testl %eax, %eax
js 0x15053
leaq 0xa8(%rbp), %rsi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x10433
movapd %xmm0, %xmm2
xorpd 0x3c1cf(%rip), %xmm2    # 0x51090
movlpd %xmm2, 0x60(%r13)
movsd 0x58(%r13), %xmm1
ucomisd %xmm1, %xmm2
ja 0x14ee5
ucomisd %xmm1, %xmm0
jbe 0x14f6e
xorpd 0x3c1ab(%rip), %xmm1    # 0x51090
movsd %xmm1, 0x60(%r13)
jmp 0x14f72
xorpd 0x3c198(%rip), %xmm0    # 0x51090
movlpd %xmm0, 0x60(%r13)
movq $0x0, 0x68(%r13)
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm0
subsd 0x70(%rbx,%rax,8), %xmm0
movsd %xmm0, 0x40(%rsp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x14f08
movq %r13, %rdi
xorl %esi, %esi
leaq 0x40(%rsp), %rdx
callq 0x16191
testl %eax, %eax
jns 0x1501f
jmp 0x15053
movq %rsi, %rbx
callq 0x22a0
movq %rbx, %rsi
movq 0x8(%rsp), %rdi
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x14f50
jmp 0x15029
movapd %xmm2, %xmm1
mulsd %xmm1, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x3c1f1(%rip), %xmm0    # 0x51178
jb 0x14f8f
sqrtsd %xmm0, %xmm0
jmp 0x14f94
callq 0x22a0
movsd %xmm0, (%rsp)
leaq 0x90(%rbp), %rsi
movq %rbx, %rdi
callq 0x10433
divsd (%rsp), %xmm0
movsd 0x3c193(%rip), %xmm1    # 0x51148
ucomisd %xmm1, %xmm0
ja 0x14fd5
movsd 0x41135(%rip), %xmm1    # 0x560f8
ucomisd %xmm0, %xmm1
movapd %xmm0, %xmm1
jbe 0x14fd5
movsd 0x41123(%rip), %xmm1    # 0x560f8
movsd %xmm1, (%rsp)
addq $0x78, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x10433
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
callq 0x2150
movsd 0x10(%rsp), %xmm1
ucomisd 0x3c171(%rip), %xmm1    # 0x51178
jae 0x15019
xorpd 0x3c07f(%rip), %xmm0    # 0x51090
movlpd %xmm0, 0x68(%r13)
jmp 0x1501f
movsd %xmm0, 0x68(%r13)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
incq %rsi
movl $0xc0, %eax
addq %rax, %r14
addq %rax, %r12
cmpq 0x28(%rsp), %rsi
jne 0x14d3e
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x41dd9(%rip), %rdi     # 0x56e33
callq 0x2090
jmp 0x15079
movl 0x64(%rbx), %ecx
addq $0x18, %rbx
leaq 0x41948(%rip), %rdi     # 0x569b7
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280