// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq $0x0, 0x28(%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x1c4df
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq $0xc0, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jge 0x1bf6a
jmp 0x1c4df
movq -0x18(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq 0x20(%rcx), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1bfb4
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd %xmm0, 0x8(%rax,%rcx,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1bf88
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x1c0ec
cmpl $0x0, -0x1c(%rbp)
je 0x1bffb
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
addq $0x18, %rdx
movq -0x28(%rbp), %rax
movl 0x64(%rax), %ecx
leaq 0x5fabd(%rip), %rdi     # 0x7baa7
movb $0x0, %al
callq 0x10d0
movl $0x8, %edi
callq 0x1210
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1c09d
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rax,%rcx,8)
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd 0x70(%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x44(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd %xmm0, 0xa8(%rax,%rcx,8)
movss -0x44(%rbp), %xmm0
mulss -0x44(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1c00a
movsd -0x40(%rbp), %xmm0
callq 0x1220
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1c0e7
movsd -0x40(%rbp), %xmm1
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd 0xa8(%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rax,%rcx,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1c0b3
jmp 0x1c4d1
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1c15c
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd 0x8(%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movslq -0x20(%rbp), %rcx
subsd 0x8(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd %xmm0, 0x40(%rax,%rcx,8)
movsd -0x58(%rbp), %xmm0
movsd -0x58(%rbp), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1c0fb
movsd -0x50(%rbp), %xmm0
callq 0x1220
movq -0x30(%rbp), %rax
movsd %xmm0, 0x58(%rax)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1c1af
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd 0x40(%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
divsd 0x58(%rax), %xmm0
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd %xmm0, 0xa8(%rax,%rcx,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1c176
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1c1db
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rax,%rcx,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1c1b6
movq -0x38(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x1c2a6
movq -0x30(%rbp), %rax
movsd 0x58(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, 0x60(%rax)
movq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rax)
movl $0x0, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jge 0x1c273
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
movsd 0x70(%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movslq -0x20(%rbp), %rax
movsd %xmm0, -0x70(%rbp,%rax,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1c226
movq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x1d700
cmpl $0x0, %eax
jge 0x1c2a1
leaq 0x5f874(%rip), %rdi     # 0x7bb04
movb $0x0, %al
callq 0x10d0
movl $0x8, %edi
callq 0x1210
jmp 0x1c4d1
movq -0x30(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x1c2e7
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x38(%rbp), %rdx
addq $0xa8, %rdx
callq 0x1d700
cmpl $0x0, %eax
jge 0x1c2e5
leaq 0x5f830(%rip), %rdi     # 0x7bb04
movb $0x0, %al
callq 0x10d0
movl $0x8, %edi
callq 0x1210
jmp 0x1c2e7
movq -0x30(%rbp), %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rsi
addq $0xa8, %rsi
callq 0x14c00
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, 0x60(%rax)
movq -0x30(%rbp), %rax
movsd 0x60(%rax), %xmm0
movq -0x30(%rbp), %rax
ucomisd 0x58(%rax), %xmm0
jbe 0x1c347
movq -0x30(%rbp), %rax
movsd 0x58(%rax), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, 0x60(%rax)
jmp 0x1c3a1
movq -0x30(%rbp), %rax
movsd 0x60(%rax), %xmm1
movq -0x30(%rbp), %rax
movsd 0x58(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1c39f
movq -0x30(%rbp), %rax
movsd 0x58(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, 0x60(%rax)
jmp 0x1c3a1
movsd -0x50(%rbp), %xmm1
movq -0x30(%rbp), %rax
movsd 0x60(%rax), %xmm0
movq -0x30(%rbp), %rax
movsd 0x60(%rax), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x1220
movsd %xmm0, -0x78(%rbp)
movq -0x30(%rbp), %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rsi
addq $0x90, %rsi
callq 0x14c00
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
divsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
movsd 0x59c28(%rip), %xmm1    # 0x76048
ucomisd %xmm1, %xmm0
jbe 0x1c438
movsd 0x59c1a(%rip), %xmm0    # 0x76048
movsd %xmm0, -0x88(%rbp)
jmp 0x1c45c
movsd 0x59bf0(%rip), %xmm0    # 0x76030
ucomisd -0x88(%rbp), %xmm0
jbe 0x1c45a
movsd 0x59bde(%rip), %xmm0    # 0x76030
movsd %xmm0, -0x88(%rbp)
jmp 0x1c45c
movq -0x30(%rbp), %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rsi
addq $0x78, %rsi
callq 0x14c00
movsd %xmm0, -0x90(%rbp)
movsd -0x90(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1c4a2
movsd -0x88(%rbp), %xmm0
callq 0x1110
movq -0x30(%rbp), %rax
movsd %xmm0, 0x68(%rax)
jmp 0x1c4cf
movsd -0x88(%rbp), %xmm0
callq 0x1110
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, 0x68(%rax)
jmp 0x1c4d1
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1bf39
xorl %eax, %eax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)