// File: /workspace/github_c_makefile_star2_O3/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, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x40af9(%rip), %rdi     # 0x56e81
callq 0x2090
testl %ebp, %ebp
jle 0x16736
movq %rbx, %rax
movl %ebp, %ecx
movq %rcx, 0x40(%rsp)
addq $0x40, %rbx
leaq 0x8(%rax), %r15
movq %rax, 0x38(%rsp)
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %r12
movq 0x38(%rsp), %rax
movsd 0x4056e(%rip), %xmm1    # 0x56930
xorl %r13d, %r13d
leaq (,%r13,2), %r14
addq %r13, %r14
shlq $0x6, %r14
cmpl $0x0, 0x20(%rax,%r14)
js 0x16736
addq %rax, %r14
movq 0x28(%r14), %rbp
testq %rbp, %rbp
je 0x164ee
movq 0x28(%rbp), %rax
testq %rax, %rax
je 0x164ee
cmpq $0x0, 0x28(%rax)
je 0x164ee
cmpq $0x0, 0x10(%rsp)
je 0x164c6
movslq 0x34(%r14), %rax
testq %rax, %rax
js 0x164c6
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm2
movsd 0x58(%r14), %xmm0
divsd %xmm0, %xmm2
addsd 0x3ad0b(%rip), %xmm2    # 0x51148
ucomisd %xmm2, %xmm1
jb 0x164b2
leaq 0x405f9(%rip), %rdi     # 0x56a43
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 0x405ef(%rip), %rdi     # 0x56a71
movaps 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
leaq 0x405fb(%rip), %rdi     # 0x56a90
movsd 0x40493(%rip), %xmm0    # 0x56930
movb $0x1, %al
callq 0x20f0
movsd 0x58(%r14), %xmm0
movsd 0x4047e(%rip), %xmm2    # 0x56930
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 0x3ac92(%rip), %xmm0    # 0x51178
jb 0x1653d
sqrtsd %xmm0, %xmm0
jmp 0x16542
cmpq $0x0, 0x18(%rsp)
je 0x1652f
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 0x164f8
testq %rbp, %rbp
jne 0x16675
jmp 0x1670d
callq 0x22a0
movsd %xmm0, 0x20(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x165f1
movslq 0x38(%r14), %rax
testq %rax, %rax
js 0x165d2
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 0x2230
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 0x3abb9(%rip), %xmm0    # 0x51178
jb 0x165c7
sqrtsd %xmm0, %xmm0
jmp 0x165cc
callq 0x22a0
movsd %xmm0, 0x20(%rsp)
movslq 0x3c(%r14), %rax
testq %rax, %rax
js 0x165f1
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 0x2230
movsd 0x30(%rsp), %xmm4
mulsd 0x20(%rsp), %xmm0
movsd 0x60(%r14), %xmm1
xorpd 0x3aa63(%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 0x1662f
leaq 0x40(%r14), %rdi
movq %rdi, %rsi
callq 0x10433
ucomisd 0x3aaef(%rip), %xmm0    # 0x51178
jb 0x16691
sqrtsd %xmm0, %xmm0
jmp 0x16696
callq 0x22a0
movsd %xmm0, 0x58(%r14)
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1669e
cmpl $0x0, 0x30(%r14)
jne 0x166cf
leaq 0xa8(%rbp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x16191
cmpq $0x0, 0x18(%rsp)
je 0x1670d
movq (%r14), %rax
cmpl $0x0, 0x48(%rax)
je 0x1670d
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 0x166e6
ucomisd 0x40245(%rip), %xmm1    # 0x56950
ja 0x16745
incq %r13
movl $0xc0, %eax
addq %rax, %rbx
addq %rax, %r15
addq %rax, %r12
cmpq 0x40(%rsp), %r13
movq 0x38(%rsp), %rax
movsd 0x40200(%rip), %xmm1    # 0x56930
jne 0x163c5
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 0x40411(%rip), %rdi     # 0x56b6d
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 0x16786
sqrtsd %xmm0, %xmm0
jmp 0x1678b
callq 0x22a0
leaq 0x40414(%rip), %rdi     # 0x56ba6
movb $0x1, %al
callq 0x20f0
movslq 0x34(%r14), %rsi
testq %rsi, %rsi
js 0x167ba
movq 0x10(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x403fb(%rip), %rdi     # 0x56bae
movb $0x1, %al
callq 0x20f0
movslq 0x38(%r14), %rsi
testq %rsi, %rsi
js 0x167db
movq 0x10(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x403e8(%rip), %rdi     # 0x56bbc
movb $0x1, %al
callq 0x20f0
movslq 0x3c(%r14), %rsi
testq %rsi, %rsi
js 0x167fc
movq 0x10(%rsp), %rax
movsd (%rax,%rsi,8), %xmm0
leaq 0x403d7(%rip), %rdi     # 0x56bcc
movb $0x1, %al
callq 0x20f0
movl $0x8, %edi
callq 0x2280