// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/buildup.c
// Function: Make_frame
int Make_frame(struct bond *bond, struct bond *previous, double *pz)
{
  double dr_dr1=
    bond->dz[0]*pz[0]+bond->dz[1]*pz[1]+bond->dz[2]*pz[2];

  double norm=0; int j;
  for(j=0; j<3; j++){
    double dx=-pz[j]+dr_dr1*bond->dz[j]; // Standard definition
    //double dx=pz[j]-dr_dr1*bond->dz[j];
    bond->dx[j]=dx; norm+=dx*dx;
  }
  if(norm>0){ 
    norm=1./sqrt(norm);
    for(j=0; j<3; j++)bond->dx[j]*=norm;
  }else{  // Collinear bonds, theta=0 or pi. Use previous axis
    //double dx3[3]; for(j=0; j<3; j++)dx3[j]=bond->dx[j];
    if(previous==NULL){printf("ERROR no previous bond\n"); exit(8);}
    for(j=0; j<3; j++)bond->dx[j]=previous->dx[j];
  } 
  
  Vector_product_d(bond->dy, bond->dz, bond->dx);
  norm=0; for(j=0; j<3; j++)norm+=bond->dy[j]*bond->dy[j];
  if(norm>0){
    norm= 1./sqrt(norm);
    for(j=0; j<3; j++)bond->dy[j]*=norm;
  }else{
    printf("ERROR in Make_frame norm_y= %.2g atom= %s res=%d\n"
	   "dy=%.2g %.2g %.2g dx= %.2g %.2g %.2g dz= %.2g %.2g %.2g\n",
	   norm, bond->atom->name, bond->atom->res,
	   bond->dy[0], bond->dy[1], bond->dy[2],
	   bond->dx[0], bond->dx[1], bond->dx[2],
	   bond->dz[0], bond->dz[1], bond->dz[2]);
    return(-1);
  }
  return(0);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movsd 0xa8(%rdi), %xmm0
movsd 0xb0(%rdi), %xmm1
mulsd 0x8(%rdx), %xmm1
mulsd (%rdx), %xmm0
addsd %xmm1, %xmm0
movsd 0xb8(%rdi), %xmm1
mulsd 0x10(%rdx), %xmm1
leaq 0xa8(%rdi), %r14
addsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd 0xa8(%rbx,%rax,8), %xmm2
mulsd %xmm1, %xmm2
subsd (%rdx,%rax,8), %xmm2
movsd %xmm2, 0x90(%rbx,%rax,8)
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x167d2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16810
jb 0x16838
sqrtsd %xmm0, %xmm0
jmp 0x1683d
testq %rsi, %rsi
je 0x1694a
xorl %eax, %eax
movsd 0x90(%rsi,%rax,8), %xmm0
movsd %xmm0, 0x90(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1681b
jmp 0x1686a
callq 0x2290
movsd 0x3a7fb(%rip), %xmm1    # 0x51040
divsd %xmm0, %xmm1
xorl %eax, %eax
movsd 0x90(%rbx,%rax,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rbx,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x1684b
leaq 0x78(%rbx), %r15
leaq 0x90(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1086b
xorpd %xmm0, %xmm0
movl $0xf, %eax
movsd (%rbx,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x12, %rax
jne 0x16889
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x168b1
jb 0x16912
sqrtsd %xmm0, %xmm0
jmp 0x16917
movq (%rbx), %rsi
movl 0x64(%rsi), %edx
addq $0x18, %rsi
movsd 0x78(%rbx), %xmm1
movsd 0x80(%rbx), %xmm2
movsd 0x88(%rbx), %xmm3
movsd 0x90(%rbx), %xmm4
movsd 0x98(%rbx), %xmm5
movsd 0xa0(%rbx), %xmm6
movsd 0xa8(%rbx), %xmm7
movups 0xb0(%rbx), %xmm8
movups %xmm8, (%rsp)
leaq 0x4037d(%rip), %rdi     # 0x56c81
movb $0x8, %al
callq 0x20f0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x16940
callq 0x2290
movsd 0x3a721(%rip), %xmm1    # 0x51040
divsd %xmm0, %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
movsd (%r15,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
cmpq $0x3, %rcx
jne 0x16927
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq
leaq 0x40558(%rip), %rdi     # 0x56ea9
callq 0x2090
movl $0x8, %edi
callq 0x2270