// File: /workspace/github_c_makefile_star2_O1/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 0x15478
movl %esi, %eax
movq %rax, 0x28(%rsp)
leaq 0x90(%rdi), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%rdi), %rbx
leaq 0xa8(%rdi), %r14
xorl %r15d, %r15d
xorl %esi, %esi
movq %rdi, 0x10(%rsp)
movq %rsi, %rax
shlq $0x6, %rax
leaq (%rax,%rax,2), %r13
movslq 0x20(%rdi,%r13), %rax
testq %rax, %rax
js 0x15478
movq 0x30(%rsp), %rcx
addq %r13, %rcx
addq %rdi, %r13
imulq $0x70, %rax, %r12
addq 0x38(%rsp), %r12
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x151a5
movq 0x28(%r13), %rbp
testq %rbp, %rbp
je 0x15205
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
subsd 0x8(%rbp,%rax,8), %xmm1
movsd %xmm1, 0x38(%rbx,%rax,8)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x151c8
ucomisd 0x3be6e(%rip), %xmm0    # 0x51060
movq %rsi, 0x20(%rsp)
movsd %xmm0, 0x8(%rsp)
jb 0x15268
sqrtsd %xmm0, %xmm0
jmp 0x15277
testq %rsi, %rsi
jne 0x15497
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq %r15, -0x18(%r14,%rax,8)
movq %r15, -0x30(%r14,%rax,8)
movsd 0x70(%r12,%rax,8), %xmm1
subsd (%r12,%rax,8), %xmm1
cvtsd2ss %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm1, %xmm2
movsd %xmm2, (%r14,%rax,8)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x15214
ucomisd 0x3be07(%rip), %xmm0    # 0x51060
jb 0x15434
sqrtsd %xmm0, %xmm0
jmp 0x15444
movq %rcx, 0x18(%rsp)
callq 0x2290
movq 0x18(%rsp), %rcx
movsd %xmm0, 0x58(%r13)
xorl %eax, %eax
movsd -0x68(%r14,%rax,8), %xmm0
divsd 0x58(%r13), %xmm0
movsd %xmm0, (%r14,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1527f
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
cmpq $0x0, 0x28(%rbp)
je 0x15340
cmpl $0x0, 0x30(%r13)
jne 0x152d7
leaq 0xa8(%rbp), %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1678b
testl %eax, %eax
js 0x15489
leaq 0x40(%r13), %r12
leaq 0xa8(%rbp), %rsi
movq %r12, %rdi
callq 0x108e3
movapd %xmm0, %xmm2
xorpd 0x3bd9a(%rip), %xmm2    # 0x51090
movlpd %xmm2, 0x60(%r13)
movsd 0x58(%r13), %xmm1
ucomisd %xmm1, %xmm2
ja 0x15316
ucomisd %xmm1, %xmm0
jbe 0x1531c
xorpd 0x3bd7a(%rip), %xmm1    # 0x51090
movsd %xmm1, 0x60(%r13)
movsd 0x60(%r13), %xmm1
mulsd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x3bd28(%rip), %xmm0    # 0x51060
jb 0x15394
sqrtsd %xmm0, %xmm0
jmp 0x15399
movsd 0x58(%r13), %xmm0
xorps 0x3bd43(%rip), %xmm0    # 0x51090
movlps %xmm0, 0x60(%r13)
movq $0x0, 0x68(%r13)
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm0
subsd 0x70(%r12,%rax,8), %xmm0
movsd %xmm0, 0x40(%rsp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1535c
movq %r13, %rdi
xorl %esi, %esi
leaq 0x40(%rsp), %rdx
callq 0x1678b
testl %eax, %eax
jns 0x15428
jmp 0x15489
callq 0x2290
movsd %xmm0, 0x8(%rsp)
leaq 0x90(%rbp), %rsi
movq %r12, %rdi
callq 0x108e3
divsd 0x8(%rsp), %xmm0
movsd 0x3bc84(%rip), %xmm1    # 0x51040
ucomisd %xmm1, %xmm0
ja 0x153dc
movsd 0x40d26(%rip), %xmm1    # 0x560f0
ucomisd %xmm0, %xmm1
movapd %xmm0, %xmm1
jbe 0x153dc
movsd 0x40d14(%rip), %xmm1    # 0x560f0
movsd %xmm1, 0x18(%rsp)
addq $0x78, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x108e3
movsd %xmm0, 0x8(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x2150
movsd 0x8(%rsp), %xmm1
ucomisd 0x3bc50(%rip), %xmm1    # 0x51060
jae 0x15422
xorpd 0x3bc76(%rip), %xmm0    # 0x51090
movlpd %xmm0, 0x68(%r13)
jmp 0x15428
movsd %xmm0, 0x68(%r13)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
jmp 0x1545f
movq %rsi, %r12
callq 0x2290
movq %r12, %rsi
movq 0x10(%rsp), %rdi
xorl %eax, %eax
movsd (%r14,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x15446
incq %rsi
movl $0xc0, %eax
addq %rax, %rbx
addq %rax, %r14
cmpq 0x28(%rsp), %rsi
jne 0x15176
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x419e2(%rip), %rdi     # 0x56e72
callq 0x2090
jmp 0x154b1
movl 0x64(%r12), %ecx
addq $0x18, %r12
leaq 0x414fc(%rip), %rdi     # 0x569a3
movq %r12, %rdx
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2270