// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
cmpl $0x2, %esi
jl 0x155fd
movq %rdx, %r13
movq %rdi, %r14
movl %esi, %eax
movq %rax, 0x30(%rsp)
leaq 0x100(%rdi), %rbx
movl $0x1, %r15d
movsd 0x41874(%rip), %xmm1    # 0x56930
movq %rdx, 0x18(%rsp)
leaq (%r15,%r15,2), %rbp
shlq $0x6, %rbp
cmpl $0x0, 0x20(%r14,%rbp)
js 0x155fd
addq %r14, %rbp
movq 0x28(%rbp), %r12
testq %r12, %r12
je 0x1560c
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x15438
cmpq $0x0, 0x28(%rax)
je 0x15438
testq %r13, %r13
je 0x151d6
movslq 0x34(%rbp), %rax
testq %rax, %rax
js 0x151d6
movsd (%r13,%rax,8), %xmm2
movsd 0x58(%rbp), %xmm0
divsd %xmm0, %xmm2
addsd 0x3c01c(%rip), %xmm2    # 0x51148
ucomisd %xmm2, %xmm1
jb 0x151a2
leaq 0x4190a(%rip), %rdi     # 0x56a43
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 0x418ff(%rip), %rdi     # 0x56a71
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
leaq 0x4190a(%rip), %rdi     # 0x56a90
movsd 0x417a2(%rip), %xmm0    # 0x56930
movb $0x1, %al
callq 0x20f0
movsd 0x58(%rbp), %xmm0
movsd 0x4178e(%rip), %xmm2    # 0x56930
mulsd %xmm2, %xmm0
movsd %xmm0, 0x58(%rbp)
mulsd 0x60(%rbp), %xmm2
movsd %xmm2, 0x60(%rbp)
mulsd 0x4177b(%rip), %xmm0    # 0x56938
ucomisd %xmm0, %xmm2
ja 0x151d1
xorpd 0x3bec5(%rip), %xmm0    # 0x51090
ucomisd %xmm2, %xmm0
jbe 0x151d6
movsd %xmm0, 0x60(%rbp)
movsd 0x58(%rbp), %xmm0
movsd 0x60(%rbp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
ucomisd 0x3bf84(%rip), %xmm0    # 0x51178
jb 0x151fc
sqrtsd %xmm0, %xmm0
jmp 0x15201
callq 0x22a0
movsd %xmm0, 0x10(%rsp)
testq %r13, %r13
je 0x15303
movslq 0x38(%rbp), %rax
testq %rax, %rax
js 0x152ae
movsd (%r13,%rax,8), %xmm0
callq 0x2130
movsd %xmm0, 0x20(%rsp)
movslq 0x38(%rbp), %rax
movsd (%r13,%rax,8), %xmm0
callq 0x2230
movsd 0x20(%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 0x416ce(%rip), %xmm1    # 0x56938
ucomisd %xmm1, %xmm2
ja 0x1527e
xorpd 0x3be18(%rip), %xmm1    # 0x51090
ucomisd %xmm2, %xmm1
jbe 0x15287
movsd %xmm1, 0x60(%rbp)
movapd %xmm1, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm0
subsd %xmm2, %xmm0
ucomisd 0x3bedd(%rip), %xmm0    # 0x51178
jb 0x152a3
sqrtsd %xmm0, %xmm0
jmp 0x152a8
callq 0x22a0
movsd %xmm0, 0x10(%rsp)
movslq 0x3c(%rbp), %rax
testq %rax, %rax
js 0x15303
movsd (%r13,%rax,8), %xmm0
addsd 0x68(%rbp), %xmm0
movss 0x53565(%rip), %xmm2    # 0x68830
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x152de
subsd %xmm1, %xmm0
jmp 0x152d2
movsd %xmm0, 0x68(%rbp)
xorps 0x3bd46(%rip), %xmm2    # 0x51030
cvtss2sd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x15303
addsd %xmm1, %xmm0
ucomisd %xmm0, %xmm2
ja 0x152f4
movsd %xmm0, 0x68(%rbp)
movsd 0x68(%rbp), %xmm0
callq 0x2130
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x68(%rbp), %xmm0
callq 0x2230
movsd 0x20(%rsp), %xmm4
mulsd 0x10(%rsp), %xmm0
movsd 0x60(%rbp), %xmm1
xorpd 0x3bd54(%rip), %xmm1    # 0x51090
leaq 0xa8(%r12), %rax
movq %rax, 0x28(%rsp)
leaq 0x40(%rbp), %r13
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, (%rbx,%rax,8)
addsd 0x8(%r12,%rax,8), %xmm2
movsd %xmm2, -0x38(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1534f
movsd 0x8(%rbp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x15452
movsd 0x10(%rbp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x15452
movsd 0x18(%rbp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x15452
movq %r13, %rdi
movq %r13, %rsi
callq 0x10433
ucomisd 0x3bd9f(%rip), %xmm0    # 0x51178
jb 0x153e1
sqrtsd %xmm0, %xmm0
jmp 0x153e6
callq 0x22a0
movq 0x18(%rsp), %r13
movsd %xmm0, 0x58(%rbp)
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x153f2
cmpl $0x0, 0x30(%rbp)
movsd 0x4151a(%rip), %xmm1    # 0x56930
jne 0x15438
movq %rbp, %rdi
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq 0x16191
movsd 0x41500(%rip), %xmm1    # 0x56930
testl %eax, %eax
js 0x1562b
incq %r15
addq $0xc0, %rbx
cmpq 0x30(%rsp), %r15
jne 0x150c1
jmp 0x155fd
movq (%rbp), %rsi
addq $0x18, %rsi
leaq 0x4164e(%rip), %rdi     # 0x56aaf
movl %r15d, %edx
xorl %eax, %eax
callq 0x20f0
movl 0x34(%rbp), %esi
leaq 0x41650(%rip), %rdi     # 0x56ac5
xorl %eax, %eax
callq 0x20f0
movslq 0x34(%rbp), %rax
testq %rax, %rax
js 0x1549d
movq 0x18(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
leaq 0x40cda(%rip), %rdi     # 0x56170
movb $0x1, %al
callq 0x20f0
movl 0x38(%rbp), %esi
leaq 0x41627(%rip), %rdi     # 0x56ace
xorl %eax, %eax
callq 0x20f0
movslq 0x38(%rbp), %rax
testq %rax, %rax
js 0x154e4
movq 0x18(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x20(%rsp)
callq 0x2130
movapd %xmm0, %xmm1
leaq 0x41604(%rip), %rdi     # 0x56adb
movsd 0x20(%rsp), %xmm0
movb $0x2, %al
callq 0x20f0
movl 0x3c(%rbp), %esi
leaq 0x415fc(%rip), %rdi     # 0x56aea
xorl %eax, %eax
callq 0x20f0
movslq 0x3c(%rbp), %rax
testq %rax, %rax
js 0x15516
movq 0x18(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
leaq 0x41614(%rip), %rdi     # 0x56b23
movb $0x1, %al
callq 0x20f0
movl $0xa, %edi
callq 0x2060
movsd 0x58(%rbp), %xmm0
leaq 0x415cd(%rip), %rdi     # 0x56af9
movb $0x1, %al
callq 0x20f0
movsd 0x60(%rbp), %xmm0
leaq 0x415c2(%rip), %rdi     # 0x56b01
movb $0x1, %al
callq 0x20f0
leaq 0x415c8(%rip), %rdi     # 0x56b15
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
movsd 0x68(%rbp), %xmm0
leaq 0x415c3(%rip), %rdi     # 0x56b29
movb $0x1, %al
callq 0x20f0
movsd 0x90(%r12), %xmm0
movsd 0x98(%r12), %xmm1
movsd 0xa0(%r12), %xmm2
leaq 0x415a2(%rip), %rdi     # 0x56b34
movb $0x3, %al
callq 0x20f0
movsd 0x78(%r12), %xmm0
movsd 0x80(%r12), %xmm1
movsd 0x88(%r12), %xmm2
leaq 0x4158c(%rip), %rdi     # 0x56b47
movb $0x3, %al
callq 0x20f0
movsd 0xa8(%r12), %xmm0
movsd 0xb0(%r12), %xmm1
movsd 0xb8(%r12), %xmm2
leaq 0x41573(%rip), %rdi     # 0x56b5a
movb $0x3, %al
callq 0x20f0
movl $0xa, %edi
callq 0x2060
jmp 0x153c6
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x41401(%rip), %rdi     # 0x56a14
movl %r15d, %esi
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0
leaq 0x41815(%rip), %rdi     # 0x56e47
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090