// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/buildup.c
// Function: Build_up
void Build_up(struct bond *bonds, int N_atoms, double *d_phi, int N_axes)
{
  float COSMAX=0.999; // Maximum value of cos(theta) to avoid collinear bonds
  struct bond *bond, *previous; int i, j;

  for(i=1; i<N_atoms; i++){
    bond=bonds+i;
    if(bond->i_atom<0)break;
    previous=bond->previous;
    if(previous==NULL){
      printf("ERROR, build up can not be applied at bond %d\n", i);
      return;
    }
    if((previous->previous==NULL)||
     (previous->previous->previous==NULL))continue;
    if((d_phi)&&(bond->len >= 0)){
      double factor=1.+d_phi[bond->len]/bond->l; //
      if(factor <= 0.1){
	printf("WARNING, bond length too short with buildup! ");
	printf("atoms: %s%d%c  %s%d%c f= %.2g\n",
	       bond->atom->name, bond->atom->res, bond->atom->aa,
	       bond->previous->atom->name, bond->previous->atom->res,
	       bond->previous->atom->aa, factor);
	factor=0.1;
	printf("Setting shrink factor to %.2g\n", factor);
      }
      bond->l*=factor;
      bond->r_cos_theta*=factor;
      double lcosmax=bond->l*COSMAX;
      if(bond->r_cos_theta>lcosmax){bond->r_cos_theta=lcosmax;}
      else if(bond->r_cos_theta<-lcosmax){bond->r_cos_theta=-lcosmax;}
    }

    double r_sin_theta=
      sqrt(bond->l*bond->l-bond->r_cos_theta*bond->r_cos_theta);
    if((d_phi)&&(bond->angle >= 0)){
      // Cos(theta)->Cos(theta+d_phi)
      double cos_a=cos(d_phi[bond->angle]);
      double sin_a=sin(d_phi[bond->angle]);
      bond->r_cos_theta=bond->r_cos_theta*cos_a-r_sin_theta*sin_a;
      double lcosmax=bond->l*COSMAX;
      if(bond->r_cos_theta>lcosmax){bond->r_cos_theta=lcosmax;}
      else if(bond->r_cos_theta<-lcosmax){bond->r_cos_theta=-lcosmax;}
      r_sin_theta=
	sqrt(bond->l*bond->l-bond->r_cos_theta*bond->r_cos_theta);
    }

    if((d_phi)&&(bond->torsion >= 0)){
      bond->phi += d_phi[bond->torsion];
      while(bond->phi > TWOPI){bond->phi-=TWOPI;}
      while(bond->phi <-TWOPI){bond->phi+=TWOPI;}
    }
    double ax= r_sin_theta*cos(bond->phi);
    double ay= r_sin_theta*sin(bond->phi);
    double az= -bond->r_cos_theta;

    for(j=0; j<3; j++){
      double dr=
	az*previous->dz[j]+
	ax*previous->dx[j]+
	ay*previous->dy[j];
      bond->dr[j]=dr;
      bond->r[j]=previous->r[j]+dr;
    }
    if(isnan(bond->r[0])||isnan(bond->r[1])||isnan(bond->r[2])){
      printf("ERROR nan, atom %s%d ", bond->atom->name, i);
      printf("len: %d ", bond->len);
      if(bond->len>=0)printf(" %.2g", d_phi[bond->len]);
      printf("  angle: %d ", bond->angle);
      if(bond->angle>=0){
	printf(" %.3f cos=%.3f",d_phi[bond->angle], cos(d_phi[bond->angle]));
      }
      printf("  torsion: %d ", bond->torsion);
      if(bond->torsion>=0)printf(" %.3f", d_phi[bond->torsion]);
      printf("\n");
      printf("r= %.3f", bond->l);
      printf(" r*cos(theta)= %.3f", bond->r_cos_theta);
      printf(" r*sin(theta)= %.3f", r_sin_theta);
      printf(" phi= %.3f", bond->phi);
      double *v=previous->dx; printf(" x: %.3f %.3f %.3f",v[0],v[1],v[2]);
      v=previous->dy; printf(" y: %.3f %.3f %.3f",v[0],v[1],v[2]);
      v=previous->dz; printf(" z: %.3f %.3f %.3f",v[0],v[1],v[2]);
      printf("\n"); 
      //for(i=0; i<N_axes; i++)printf("%.2f ",d_phi[i]); printf("\n"); 
      //exit(8);
    }
    double dr_dr=Scalar_product_3_d(bond->dr, bond->dr);
    bond->l=sqrt(dr_dr);
    for(j=0; j<3; j++){
      bond->dz[j]=bond->dr[j]/bond->l;
    }

    // Make new orthogonal frame
    if(bond->terminal==0 && Make_frame(bond, previous, previous->dz)<0){
      printf("Could not make last frame, exiting\n");
      return;
    }
  }
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %esi
jl 0x15a10
movq %rdx, %rbx
movq %rdi, %r14
movl %esi, %eax
movq %rax, 0x20(%rsp)
leaq 0x100(%rdi), %r13
movl $0x1, %r15d
movsd 0x4144c(%rip), %xmm1    # 0x56940
leaq (%r15,%r15,2), %rbp
shlq $0x6, %rbp
cmpl $0x0, 0x20(%r14,%rbp)
js 0x15a10
addq %r14, %rbp
movq 0x28(%rbp), %r12
testq %r12, %r12
je 0x15a1f
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x1585a
cmpq $0x0, 0x28(%rax)
je 0x1585a
testq %rbx, %rbx
je 0x15603
movslq 0x34(%rbp), %rax
testq %rax, %rax
js 0x15603
movsd (%rbx,%rax,8), %xmm2
divsd 0x58(%rbp), %xmm2
addsd 0x3bae7(%rip), %xmm2    # 0x51040
ucomisd %xmm2, %xmm1
jb 0x155ca
leaq 0x414c9(%rip), %rdi     # 0x56a2f
xorl %eax, %eax
movsd %xmm2, 0x10(%rsp)
callq 0x20f0
movq (%rbp), %rax
movq 0x28(%rbp), %rdi
leaq 0x18(%rax), %rsi
movl 0x64(%rax), %edx
movsbl 0x68(%rax), %ecx
movq (%rdi), %rax
leaq 0x18(%rax), %r8
movl 0x64(%rax), %r9d
movsbl 0x68(%rax), %eax
movl %eax, (%rsp)
leaq 0x414be(%rip), %rdi     # 0x56a5d
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
leaq 0x414c9(%rip), %rdi     # 0x56a7c
movsd 0x41385(%rip), %xmm0    # 0x56940
movb $0x1, %al
callq 0x20f0
movsd 0x41376(%rip), %xmm2    # 0x56940
movsd 0x58(%rbp), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x58(%rbp)
mulsd 0x60(%rbp), %xmm2
movsd %xmm2, 0x60(%rbp)
mulsd 0x4135e(%rip), %xmm0    # 0x56948
ucomisd %xmm0, %xmm2
ja 0x155fe
xorpd 0x3ba98(%rip), %xmm0    # 0x51090
ucomisd %xmm2, %xmm0
jbe 0x15603
movsd %xmm0, 0x60(%rbp)
movsd 0x58(%rbp), %xmm0
movsd 0x60(%rbp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
ucomisd 0x3ba3f(%rip), %xmm0    # 0x51060
jb 0x15629
sqrtsd %xmm0, %xmm0
jmp 0x1562e
callq 0x2290
movsd %xmm0, 0x10(%rsp)
testq %rbx, %rbx
je 0x156d8
movslq 0x38(%rbp), %rax
testq %rax, %rax
js 0x156d8
movsd (%rbx,%rax,8), %xmm0
callq 0x2130
movsd %xmm0, 0x18(%rsp)
movslq 0x38(%rbp), %rax
movsd (%rbx,%rax,8), %xmm0
callq 0x2220
movsd 0x18(%rsp), %xmm2
mulsd 0x60(%rbp), %xmm2
mulsd 0x10(%rsp), %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, 0x60(%rbp)
movsd 0x58(%rbp), %xmm0
movapd %xmm0, %xmm1
mulsd 0x412b5(%rip), %xmm1    # 0x56948
ucomisd %xmm1, %xmm2
ja 0x156a7
xorpd 0x3b9ef(%rip), %xmm1    # 0x51090
ucomisd %xmm2, %xmm1
jbe 0x156ac
movsd %xmm1, 0x60(%rbp)
movsd 0x60(%rbp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
ucomisd 0x3b99b(%rip), %xmm0    # 0x51060
jb 0x156cd
sqrtsd %xmm0, %xmm0
jmp 0x156d2
callq 0x2290
movsd %xmm0, 0x10(%rsp)
testq %rbx, %rbx
je 0x15730
movslq 0x3c(%rbp), %rax
testq %rax, %rax
js 0x15730
movsd (%rbx,%rax,8), %xmm0
addsd 0x68(%rbp), %xmm0
movss 0x53138(%rip), %xmm2    # 0x68830
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1570b
subsd %xmm1, %xmm0
jmp 0x156ff
movsd %xmm0, 0x68(%rbp)
xorps 0x41219(%rip), %xmm2    # 0x56930
cvtss2sd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x15730
addsd %xmm1, %xmm0
ucomisd %xmm0, %xmm2
ja 0x15721
movsd %xmm0, 0x68(%rbp)
movsd 0x68(%rbp), %xmm0
callq 0x2130
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x68(%rbp), %xmm0
callq 0x2220
movsd 0x18(%rsp), %xmm4
mulsd 0x10(%rsp), %xmm0
movsd 0x60(%rbp), %xmm1
xorpd 0x3b927(%rip), %xmm1    # 0x51090
xorl %eax, %eax
movsd 0x90(%r12,%rax,8), %xmm2
mulsd %xmm4, %xmm2
movsd 0xa8(%r12,%rax,8), %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movsd 0x78(%r12,%rax,8), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd %xmm2, (%r13,%rax,8)
addsd 0x8(%r12,%rax,8), %xmm2
movsd %xmm2, -0x38(%r13,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1576b
movsd 0x8(%rbp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x15874
movsd 0x10(%rbp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x15874
movsd 0x18(%rbp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x15874
leaq 0x40(%rbp), %rdi
movq %rdi, %rsi
callq 0x108e3
ucomisd 0x3b867(%rip), %xmm0    # 0x51060
jb 0x15801
sqrtsd %xmm0, %xmm0
jmp 0x15806
callq 0x2290
movsd 0x41132(%rip), %xmm1    # 0x56940
movsd %xmm0, 0x58(%rbp)
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm0
divsd 0x58(%rbp), %xmm0
movsd %xmm0, 0x68(%r13,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x15815
cmpl $0x0, 0x30(%rbp)
jne 0x1585a
leaq 0xa8(%r12), %rdx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1678b
movsd 0x410ee(%rip), %xmm1    # 0x56940
testl %eax, %eax
js 0x15a3e
incq %r15
addq $0xc0, %r13
cmpq 0x20(%rsp), %r15
jne 0x154f4
jmp 0x15a10
movq (%rbp), %rsi
addq $0x18, %rsi
leaq 0x41218(%rip), %rdi     # 0x56a9b
movl %r15d, %edx
xorl %eax, %eax
callq 0x20f0
movl 0x34(%rbp), %esi
leaq 0x4121a(%rip), %rdi     # 0x56ab1
xorl %eax, %eax
callq 0x20f0
movslq 0x34(%rbp), %rax
testq %rax, %rax
js 0x158ba
movsd (%rbx,%rax,8), %xmm0
leaq 0x408b5(%rip), %rdi     # 0x56168
movb $0x1, %al
callq 0x20f0
movl 0x38(%rbp), %esi
leaq 0x411f6(%rip), %rdi     # 0x56aba
xorl %eax, %eax
callq 0x20f0
movslq 0x38(%rbp), %rax
testq %rax, %rax
js 0x158fc
movsd (%rbx,%rax,8), %xmm0
movsd %xmm0, 0x18(%rsp)
callq 0x2130
movapd %xmm0, %xmm1
leaq 0x411d8(%rip), %rdi     # 0x56ac7
movsd 0x18(%rsp), %xmm0
movb $0x2, %al
callq 0x20f0
movl 0x3c(%rbp), %esi
leaq 0x411d0(%rip), %rdi     # 0x56ad6
xorl %eax, %eax
callq 0x20f0
movslq 0x3c(%rbp), %rax
testq %rax, %rax
js 0x15929
movsd (%rbx,%rax,8), %xmm0
leaq 0x411ed(%rip), %rdi     # 0x56b0f
movb $0x1, %al
callq 0x20f0
movl $0xa, %edi
callq 0x2060
movsd 0x58(%rbp), %xmm0
leaq 0x411a6(%rip), %rdi     # 0x56ae5
movb $0x1, %al
callq 0x20f0
movsd 0x60(%rbp), %xmm0
leaq 0x4119b(%rip), %rdi     # 0x56aed
movb $0x1, %al
callq 0x20f0
leaq 0x411a1(%rip), %rdi     # 0x56b01
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
movsd 0x68(%rbp), %xmm0
leaq 0x4119c(%rip), %rdi     # 0x56b15
movb $0x1, %al
callq 0x20f0
movsd 0x90(%r12), %xmm0
movsd 0x98(%r12), %xmm1
movsd 0xa0(%r12), %xmm2
leaq 0x4117b(%rip), %rdi     # 0x56b20
movb $0x3, %al
callq 0x20f0
movsd 0x78(%r12), %xmm0
movsd 0x80(%r12), %xmm1
movsd 0x88(%r12), %xmm2
leaq 0x41165(%rip), %rdi     # 0x56b33
movb $0x3, %al
callq 0x20f0
movsd 0xa8(%r12), %xmm0
movsd 0xb0(%r12), %xmm1
movsd 0xb8(%r12), %xmm2
leaq 0x4114c(%rip), %rdi     # 0x56b46
movb $0x3, %al
callq 0x20f0
movl $0xa, %edi
callq 0x2060
jmp 0x157e5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40fda(%rip), %rdi     # 0x56a00
movl %r15d, %esi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0
leaq 0x41441(%rip), %rdi     # 0x56e86
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090