// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/buildup.c
// Function: Compare_build_up
void Compare_build_up(struct bond *bonds, int N_atoms,
		      double *d_phi, int N_axes,
		      struct bond *bonds2)
{
  struct bond *bond, *previous; int i, j, error=0;
  double dr_dr;

  printf("Testing differences atom by atom\n");

  for(i=0; i<N_atoms; i++){
    bond=bonds+i;
    if(bond->i_atom<0)break;
    previous=bond->previous;
    if((previous==NULL)||(previous->previous==NULL)||
       (previous->previous->previous==NULL)){
      if(bonds2){
	for(j=0; j<3; j++){
	  bond->r[j]=bonds2[i].r[j];
	  bond->dr[j]=bonds2[i].dr[j];
	  bond->dz[j]=bonds2[i].dz[j];
	}
      }
      if(previous){
	goto test;
      }else{
	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 r_sin_theta=
      sqrt(bond->l*bond->l-bond->r_cos_theta*bond->r_cos_theta);
    if((d_phi)&&(bond->angle >= 0)){
      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;
      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];
    }
    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;
    }

  test:
    
    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);

    if(bond->atom->main==0)continue;
    if(bonds2){
      double d2=0;
      for(j=0; j<3; j++){
	double d=bond->r[j]-bonds2[i].r[j]; d2+=d*d;
      }
      if(d2>0.005){
	printf("ERROR in Compare_build_up, bond %d atom %s %c%d chain %d",
	       i, bond->atom->name, bond->atom->aa, bond->atom->res,
	       bond->atom->chain);
	printf("d=%.4g\n", sqrt(d2));
	if(bond->len>=0)
	  printf("Len: %.2f %d\n", d_phi[bond->len], bond->len);
	if(bond->angle>=0)
	  printf("Angle: %.2f %d\n", d_phi[bond->angle], bond->angle);
	if(bond->torsion>=0)
	  printf("Torsion: %.2f %d\n", d_phi[bond->torsion], bond->torsion);
	error++; exit(8);
      }
    }

  }
  if(error)exit(8);
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %rbx
movq %rdx, 0x10(%rsp)
movl %esi, %r14d
movq %rdi, %r15
leaq 0x3bda2(%rip), %rdi     # 0x50dd1
callq 0x2090
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
movq %r15, %rax
movq %r14, 0x40(%rsp)
leaq 0x40(%r15), %r12
addq $0x8, %r15
movq %rbx, 0x18(%rsp)
leaq 0x8(%rbx), %r13
movsd 0x3b822(%rip), %xmm1    # 0x50880
xorl %ebp, %ebp
movq %rax, 0x38(%rsp)
cmpq 0x40(%rsp), %rbp
je 0x153eb
imulq $0xc0, %rbp, %r14
cmpl $0x0, 0x20(%rax,%r14)
js 0x153eb
addq %rax, %r14
movq 0x28(%r14), %rbx
testq %rbx, %rbx
je 0x15198
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x15198
cmpq $0x0, 0x28(%rax)
je 0x15198
cmpq $0x0, 0x10(%rsp)
je 0x15169
movslq 0x34(%r14), %rax
testq %rax, %rax
js 0x15169
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm2
movsd 0x58(%r14), %xmm0
divsd %xmm0, %xmm2
addsd 0x35f68(%rip), %xmm2    # 0x4b048
ucomisd %xmm2, %xmm1
jb 0x15155
leaq 0x3b8a6(%rip), %rdi     # 0x50993
xorl %eax, %eax
movapd %xmm2, 0x20(%rsp)
callq 0x20f0
movq (%r14), %rax
movq 0x28(%r14), %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 0x3b89c(%rip), %rdi     # 0x509c1
movaps 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
leaq 0x3b8a8(%rip), %rdi     # 0x509e0
movsd 0x3b740(%rip), %xmm0    # 0x50880
movb $0x1, %al
callq 0x20f0
movsd 0x58(%r14), %xmm0
movsd 0x3b72b(%rip), %xmm2    # 0x50880
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
movhpd 0x60(%r14), %xmm0       # xmm0 = xmm0[0],mem[0]
mulpd %xmm2, %xmm0
movupd %xmm0, 0x58(%r14)
movsd 0x58(%r14), %xmm0
movsd 0x60(%r14), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
ucomisd 0x35ec7(%rip), %xmm0    # 0x4b050
jb 0x152ab
sqrtsd %xmm0, %xmm0
jmp 0x152b0
cmpq $0x0, 0x18(%rsp)
je 0x151dc
xorl %eax, %eax
cmpq $0x3, %rax
je 0x151dc
movsd (%r13,%rax,8), %xmm0
movsd %xmm0, (%r15,%rax,8)
movsd 0x38(%r13,%rax,8), %xmm0
movsd %xmm0, 0x38(%r15,%rax,8)
movsd 0xa0(%r13,%rax,8), %xmm0
movsd %xmm0, 0xa0(%r15,%rax,8)
incq %rax
jmp 0x151a2
testq %rbx, %rbx
je 0x15288
leaq 0x40(%r14), %rdi
movq %rdi, %rsi
callq 0xf96f
ucomisd 0x35e57(%rip), %xmm0    # 0x4b050
jb 0x15201
sqrtsd %xmm0, %xmm0
jmp 0x15206
callq 0x2290
movsd %xmm0, 0x58(%r14)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1522a
movsd (%r12,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x68(%r12,%rax,8)
incq %rax
jmp 0x1520e
cmpl $0x0, 0x30(%r14)
jne 0x15243
leaq 0xa8(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14e3b
cmpq $0x0, 0x18(%rsp)
je 0x15288
movq (%r14), %rax
cmpl $0x0, 0x48(%rax)
je 0x15288
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x1527a
movsd (%r15,%rcx,8), %xmm0
subsd (%r13,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
incq %rcx
jmp 0x1525a
ucomisd 0x3b61e(%rip), %xmm1    # 0x508a0
ja 0x153fa
incq %rbp
movl $0xc0, %eax
addq %rax, %r12
addq %rax, %r15
addq %rax, %r13
movq 0x38(%rsp), %rax
movsd 0x3b5da(%rip), %xmm1    # 0x50880
jmp 0x15065
callq 0x2290
movsd %xmm0, 0x20(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x1535f
movslq 0x38(%r14), %rax
testq %rax, %rax
js 0x15340
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
callq 0x2130
movsd %xmm0, 0x30(%rsp)
movslq 0x38(%r14), %rax
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
callq 0x2220
movsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm2
mulsd 0x60(%r14), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x60(%r14)
movsd 0x58(%r14), %xmm0
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm0
subsd %xmm2, %xmm0
ucomisd 0x35d23(%rip), %xmm0    # 0x4b050
jb 0x15335
sqrtsd %xmm0, %xmm0
jmp 0x1533a
callq 0x2290
movsd %xmm0, 0x20(%rsp)
movslq 0x3c(%r14), %rax
testq %rax, %rax
js 0x1535f
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd 0x68(%r14), %xmm0
movsd %xmm0, 0x68(%r14)
movsd 0x68(%r14), %xmm0
callq 0x2130
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x68(%r14), %xmm0
callq 0x2220
movsd 0x30(%rsp), %xmm4
mulsd 0x20(%rsp), %xmm0
movsd 0x60(%r14), %xmm1
xorpd 0x35d25(%rip), %xmm1    # 0x4b0c0
xorl %eax, %eax
cmpq $0x3, %rax
je 0x151e5
movsd 0x90(%rbx,%rax,8), %xmm2
mulsd %xmm4, %xmm2
movsd 0xa8(%rbx,%rax,8), %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movsd 0x78(%rbx,%rax,8), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd %xmm2, (%r12,%rax,8)
addsd 0x8(%rbx,%rax,8), %xmm2
movsd %xmm2, -0x38(%r12,%rax,8)
incq %rax
jmp 0x1539d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rax), %rdx
movsbl 0x68(%rax), %ecx
movl 0x64(%rax), %r8d
movl 0x6c(%rax), %r9d
leaq 0x3b6ac(%rip), %rdi     # 0x50abd
movl %ebp, %esi
xorl %eax, %eax
movsd %xmm1, 0x18(%rsp)
callq 0x20f0
xorpd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm2
movapd %xmm2, %xmm0
ucomisd %xmm1, %xmm2
jb 0x1543a
sqrtsd %xmm0, %xmm0
jmp 0x1543f
callq 0x2290
leaq 0x3b6b0(%rip), %rdi     # 0x50af6
movb $0x1, %al
callq 0x20f0
movslq 0x34(%r14), %rsi
testq %rsi, %rsi
js 0x1546e
movq 0x10(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x3b697(%rip), %rdi     # 0x50afe
movb $0x1, %al
callq 0x20f0
movslq 0x38(%r14), %rsi
testq %rsi, %rsi
js 0x1548f
movq 0x10(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x3b684(%rip), %rdi     # 0x50b0c
movb $0x1, %al
callq 0x20f0
movslq 0x3c(%r14), %rsi
testq %rsi, %rsi
js 0x154b0
movq 0x10(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x3b673(%rip), %rdi     # 0x50b1c
movb $0x1, %al
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270