// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/kinetic_tnm.c
// Function: Set_rot_shift
void  Set_rot_shift(struct axe *axes, int N_axes, atom *atoms, int N_atoms)
{
  int k, j;
  double v[3], offset[3], shift[3];
  // Axes vector and shift
  for(k=0; k<N_axes; k++){
    struct axe *axe=axes+k;
    double *r1=(atoms+axe->bond->previous->i_atom)->r;
    double *r2=(atoms+axe->bond->i_atom)->r;
    double v2=0;
    for(j=0; j<3; j++){
      axe->offset[j]=r1[j];               // Get offset as r1
      offset[j]=axe->offset[j];
      v[j]=r2[j]-r1[j]; v2+=v[j]*v[j];    // Get axe as (r1-r2)/|r1-r2|
    }
    v2=1./sqrt(v2);
    for(j=0; j<3; j++)axe->vers[j]=v[j]*v2;

    if(axe->type=='l'){ // bond length
      for(j=0; j<3; j++){
	// Bonds used as dof have a standard length LBOND=1
	axe->shift[j]=axe->vers[j];
	axe->rot[j]=0;
      }
    }else if(axe->type=='a'){ // bond angle
      if(axe->bond->previous->previous==NULL){
	printf("ERROR in bond angle %d\n", k); exit(8);
      }
      // Compute normal vector
      double *r0=(atoms+axe->bond->previous->previous->i_atom)->r;
      double v0[3], normal[3];
      for(j=0; j<3; j++)v0[j]=r1[j]-r0[j];
      Vector_product_d(normal, v, v0);
      // Normalize:
      double v2=0; for(j=0; j<3; j++)v2+=normal[j]*normal[j];
      if(v2<=0){
	printf("ERROR for bond angle %d, zero norm (%.6f)\n",k, v2);
	exit(8);
      }
      v2=1./sqrt(v2); for(j=0; j<3; j++)normal[j]*=v2;
      Vector_product_d(shift, normal, offset);
      for(j=0; j<3; j++){
	axe->shift[j]=-shift[j];
	axe->rot[j]=normal[j];
      }
    }else{   // torsion
      Vector_product_d(shift, axe->vers, offset);
      for(j=0; j<3; j++){
	axe->shift[j]=-shift[j]; axe->rot[j]=axe->vers[j];
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, 0x8(%rsp)
movq %rdi, (%rsp)
testl %esi, %esi
jle 0x21164
movl %esi, %eax
movq %rax, 0x28(%rsp)
movq (%rsp), %rax
leaq 0x30(%rax), %r13
leaq 0x78(%rax), %rbp
leaq 0x60(%rax), %r12
xorl %ebx, %ebx
xorpd %xmm3, %xmm3
imulq $0x158, %rbx, %rax      # imm = 0x158
movq (%rsp), %rcx
leaq (%rcx,%rax), %r14
movq 0x8(%rcx,%rax), %rax
movq 0x28(%rax), %rcx
movslq 0x20(%rcx), %rcx
imulq $0x70, %rcx, %r15
movq 0x8(%rsp), %rcx
addq %rcx, %r15
movslq 0x20(%rax), %rax
imulq $0x70, %rax, %rax
addq %rcx, %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd (%r15,%rcx), %xmm1
movsd %xmm1, (%r13,%rcx)
movsd %xmm1, 0x50(%rsp,%rcx)
movsd (%rax,%rcx), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, 0x70(%rsp,%rcx)
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
addq $0x8, %rcx
cmpq $0x18, %rcx
jne 0x20f55
ucomisd %xmm3, %xmm0
jb 0x20f95
sqrtsd %xmm0, %xmm0
jmp 0x20f9a
callq 0x22a0
movsd 0x301a6(%rip), %xmm1    # 0x51148
divsd %xmm0, %xmm1
leaq 0x78(%r14), %rsi
xorl %eax, %eax
movsd 0x70(%rsp,%rax,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x20fac
movzbl (%r14), %eax
cmpl $0x61, %eax
je 0x21001
cmpl $0x6c, %eax
jne 0x21090
movq $-0x3, %rax
xorpd %xmm3, %xmm3
movsd 0x18(%rbp,%rax,8), %xmm0
movsd %xmm0, (%rbp,%rax,8)
movq $0x0, -0x18(%rbp,%rax,8)
incq %rax
jne 0x20fe2
jmp 0x21148
movq 0x8(%r14), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
testq %rax, %rax
je 0x21188
movslq 0x20(%rax), %rax
imulq $0x70, %rax, %rax
addq 0x8(%rsp), %rax
xorl %ecx, %ecx
movsd (%r15,%rcx,8), %xmm0
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x90(%rsp,%rcx,8)
incq %rcx
cmpq $0x3, %rcx
jne 0x21025
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x103bb
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd 0x10(%rsp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x2105f
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x21176
ucomisd %xmm1, %xmm0
jb 0x210d4
sqrtsd %xmm0, %xmm0
jmp 0x210d9
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x103bb
xorl %eax, %eax
xorpd %xmm3, %xmm3
movapd 0x2ffe3(%rip), %xmm1    # 0x51090
movsd 0x30(%rsp,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, -0x18(%rbp,%rax,8)
movsd (%rbp,%rax,8), %xmm0
movsd %xmm0, -0x30(%rbp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x210ad
jmp 0x21148
callq 0x22a0
movsd 0x30067(%rip), %xmm1    # 0x51148
divsd %xmm0, %xmm1
xorl %eax, %eax
movsd 0x10(%rsp,%rax,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x210e7
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x103bb
xorl %eax, %eax
movapd 0x2ff72(%rip), %xmm1    # 0x51090
movsd 0x30(%rsp,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, (%r12,%rax,8)
movsd 0x10(%rsp,%rax,8), %xmm0
movsd %xmm0, -0x18(%r12,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x2111e
xorpd %xmm3, %xmm3
incq %rbx
movl $0x158, %eax            # imm = 0x158
addq %rax, %r13
addq %rax, %rbp
addq %rax, %r12
cmpq 0x28(%rsp), %rbx
jne 0x20f1c
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37584(%rip), %rdi     # 0x58701
movl %ebx, %esi
movb $0x1, %al
callq 0x20f0
jmp 0x21198
leaq 0x3755a(%rip), %rdi     # 0x586e9
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280