// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %r8, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x4053c(%rip), %rdi     # 0x56ec0
callq 0x2090
testl %ebp, %ebp
jle 0x16d37
movq %rbx, %rax
movl %ebp, %ecx
movq %rcx, 0x30(%rsp)
addq $0x40, %rbx
leaq 0x8(%rax), %r15
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %r12
movq 0x28(%rsp), %rax
movsd 0x3ff82(%rip), %xmm0    # 0x56940
xorl %r13d, %r13d
leaq (,%r13,2), %r14
addq %r13, %r14
shlq $0x6, %r14
cmpl $0x0, 0x20(%rax,%r14)
js 0x16d37
addq %rax, %r14
movq 0x28(%r14), %rbp
testq %rbp, %rbp
je 0x16ae9
movq 0x28(%rbp), %rax
testq %rax, %rax
je 0x16ae9
cmpq $0x0, 0x28(%rax)
je 0x16ae9
cmpq $0x0, 0x8(%rsp)
je 0x16ac1
movslq 0x34(%r14), %rax
testq %rax, %rax
js 0x16ac1
movq 0x8(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm1
divsd 0x58(%r14), %xmm1
addsd 0x3a60b(%rip), %xmm1    # 0x51040
ucomisd %xmm1, %xmm0
jb 0x16aa5
leaq 0x3ffed(%rip), %rdi     # 0x56a2f
xorl %eax, %eax
movsd %xmm1, 0x10(%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 0x3ffe3(%rip), %rdi     # 0x56a5d
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
leaq 0x3ffee(%rip), %rdi     # 0x56a7c
movsd 0x3feaa(%rip), %xmm0    # 0x56940
movb $0x1, %al
callq 0x20f0
movsd 0x3fe9b(%rip), %xmm1    # 0x56940
movsd 0x58(%r14), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%r14)
mulsd 0x60(%r14), %xmm1
movsd %xmm1, 0x60(%r14)
movsd 0x58(%r14), %xmm0
movsd 0x60(%r14), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
ucomisd 0x3a57f(%rip), %xmm0    # 0x51060
jb 0x16b38
sqrtsd %xmm0, %xmm0
jmp 0x16b3d
cmpq $0x0, 0x18(%rsp)
je 0x16b2a
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm0
movsd %xmm0, (%r15,%rax,8)
movsd 0x38(%r12,%rax,8), %xmm0
movsd %xmm0, 0x38(%r15,%rax,8)
movsd 0xa0(%r12,%rax,8), %xmm0
movsd %xmm0, 0xa0(%r15,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x16af3
testq %rbp, %rbp
jne 0x16c74
jmp 0x16d0e
callq 0x2290
movsd %xmm0, 0x10(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x16bc9
movslq 0x38(%r14), %rax
testq %rax, %rax
js 0x16bc9
movq 0x8(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
callq 0x2130
movsd %xmm0, 0x20(%rsp)
movslq 0x38(%r14), %rax
movq 0x8(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
callq 0x2220
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0x20(%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 0x3a4aa(%rip), %xmm0    # 0x51060
jb 0x16bbe
sqrtsd %xmm0, %xmm0
jmp 0x16bc3
callq 0x2290
movsd %xmm0, 0x10(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x16bf0
movslq 0x3c(%r14), %rax
testq %rax, %rax
js 0x16bf0
movq 0x8(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd 0x68(%r14), %xmm0
movsd %xmm0, 0x68(%r14)
movsd 0x68(%r14), %xmm0
callq 0x2130
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x68(%r14), %xmm0
callq 0x2220
movsd 0x20(%rsp), %xmm4
mulsd 0x10(%rsp), %xmm0
movsd 0x60(%r14), %xmm1
xorpd 0x3a464(%rip), %xmm1    # 0x51090
xorl %eax, %eax
movsd 0x90(%rbp,%rax,8), %xmm2
mulsd %xmm4, %xmm2
movsd 0xa8(%rbp,%rax,8), %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movsd 0x78(%rbp,%rax,8), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd %xmm2, (%rbx,%rax,8)
addsd 0x8(%rbp,%rax,8), %xmm2
movsd %xmm2, -0x38(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x16c2e
leaq 0x40(%r14), %rdi
movq %rdi, %rsi
callq 0x108e3
ucomisd 0x3a3d8(%rip), %xmm0    # 0x51060
jb 0x16c90
sqrtsd %xmm0, %xmm0
jmp 0x16c95
callq 0x2290
movsd %xmm0, 0x58(%r14)
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm0
divsd 0x58(%r14), %xmm0
movsd %xmm0, 0x68(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x16c9d
cmpl $0x0, 0x30(%r14)
jne 0x16cd0
leaq 0xa8(%rbp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1678b
cmpq $0x0, 0x18(%rsp)
je 0x16d0e
movq (%r14), %rax
cmpl $0x0, 0x48(%rax)
je 0x16d0e
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movsd (%r15,%rcx,8), %xmm0
subsd (%r12,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
incq %rcx
cmpq $0x3, %rcx
jne 0x16ce7
ucomisd 0x3fc54(%rip), %xmm1    # 0x56960
ja 0x16d46
incq %r13
movl $0xc0, %eax
addq %rax, %rbx
addq %rax, %r15
addq %rax, %r12
cmpq 0x30(%rsp), %r13
movq 0x28(%rsp), %rax
movsd 0x3fc0f(%rip), %xmm0    # 0x56940
jne 0x169c1
addq $0x38, %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 0x3fdfc(%rip), %rdi     # 0x56b59
movl %r13d, %esi
xorl %eax, %eax
movsd %xmm1, 0x18(%rsp)
callq 0x20f0
xorpd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm2
movapd %xmm2, %xmm0
ucomisd %xmm1, %xmm2
jb 0x16d87
sqrtsd %xmm0, %xmm0
jmp 0x16d8c
callq 0x2290
leaq 0x3fdff(%rip), %rdi     # 0x56b92
movb $0x1, %al
callq 0x20f0
movslq 0x34(%r14), %rsi
testq %rsi, %rsi
js 0x16dbb
movq 0x8(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x3fde6(%rip), %rdi     # 0x56b9a
movb $0x1, %al
callq 0x20f0
movslq 0x38(%r14), %rsi
testq %rsi, %rsi
js 0x16ddc
movq 0x8(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x3fdd3(%rip), %rdi     # 0x56ba8
movb $0x1, %al
callq 0x20f0
movslq 0x3c(%r14), %rsi
testq %rsi, %rsi
js 0x16dfd
movq 0x8(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x3fdc2(%rip), %rdi     # 0x56bb8
movb $0x1, %al
callq 0x20f0
movl $0x8, %edi
callq 0x2270