// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Principal_axis_frame
void Principal_axis_frame(atom *atoms, int N_atoms, int *atom_ref,
			  int N_atom_ref,struct axe *axes, int N_axes,
			  int ANISOU)
{
  double **inertia=Allocate_mat2_d(3,3);
  double **rotation=Allocate_mat2_d(3,3);
  double **corr=Allocate_mat2_d(3,3);
  double r_ave[3], ri[3];
  int i, j;

  /******************************************************************/
  /*              Transforming to principal axis frame              */
  /******************************************************************/
  // Compute center of mass and inertia tensor for reference atoms
  Inertia_tensor_old(atoms, atom_ref, N_atom_ref, r_ave, corr, inertia);

  // Transform all atoms and axes to principal axis frame of references
  Get_rotation(inertia, rotation);
  for(i=0; i<N_atoms; i++){
    double *r=atoms[i].r;
    for(j=0; j<3; j++)ri[j]=r[j];
    Subtract_vector_3_d(ri, ri, r_ave);
    Rotate_d(ri, rotation);
    for(j=0; j<3; j++)r[j]=ri[j];
  }
  for(i=0; i<N_axes; i++){
    Rotate_d(axes[i].rot, rotation);
    Subtract_vector_3_d(axes[i].offset, axes[i].offset, r_ave);
    Rotate_d(axes[i].offset, rotation);
  }

  // Transfrom anisotropic structure factors
  if(ANISOU){
    float rot[3][3];
    for(i=0; i<3; i++)for(j=0; j<3; j++)rot[i][j]=rotation[i][j];
    for(i=0; i<N_atoms; i++){
      Transform_matrix(atoms[i].anisou, (float **)rot);
    }
  }

  // Cleaning
  Empty_matrix_d(inertia, 3);
  Empty_matrix_d(corr, 3);
  Empty_matrix_d(rotation, 3);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, 0xc(%rsp)
movq %r8, %r13
movl %ecx, (%rsp)
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %r14
movl $0x3, %edi
movl $0x3, %esi
callq 0x49798
movq %rax, %r12
movl $0x3, %edi
movl $0x3, %esi
callq 0x49798
movq %rax, %rbx
movl $0x3, %edi
movl $0x3, %esi
callq 0x49798
leaq 0x40(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
movl (%rsp), %edx
movq %rax, 0x10(%rsp)
movq %rax, %r8
movq %r12, %r9
callq 0x26e4c
movq %r12, 0x18(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x27034
movl %ebp, %r12d
movl %ebp, 0x8(%rsp)
testl %ebp, %ebp
jle 0x26d6d
imulq $0x70, %r12, %rax
movq %rax, (%rsp)
xorl %ebp, %ebp
leaq 0x20(%rsp), %r15
movq 0x10(%r14,%rbp), %rax
movq %rax, 0x30(%rsp)
movups (%r14,%rbp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq %r15, %rdi
movq %r15, %rsi
leaq 0x40(%rsp), %rdx
callq 0x10a37
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1098a
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r14,%rbp)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%r14,%rbp)
addq $0x70, %rbp
cmpq %rbp, (%rsp)
jne 0x26d20
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0x26db1
movl %eax, %ebp
addq $0x30, %r13
leaq 0x40(%rsp), %r15
leaq 0x18(%r13), %rdi
movq %rbx, %rsi
callq 0x1098a
movq %r13, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x10a37
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1098a
addq $0x158, %r13            # imm = 0x158
decq %rbp
jne 0x26d80
cmpl $0x0, 0xc0(%rsp)
je 0x26e0f
leaq 0x60(%rsp), %rax
xorl %ecx, %ecx
movq (%rbx,%rcx,8), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rdx,%rsi,8), %xmm0
movss %xmm0, (%rax,%rsi,4)
incq %rsi
cmpq $0x3, %rsi
jne 0x26dc8
incq %rcx
addq $0xc, %rax
cmpq $0x3, %rcx
jne 0x26dc2
cmpl $0x0, 0x8(%rsp)
jle 0x26e0f
addq $0x20, %r14
leaq 0x60(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x10af1
addq $0x70, %r14
decq %r12
jne 0x26dfb
movq 0x18(%rsp), %rdi
movl $0x3, %esi
callq 0x49642
movq 0x10(%rsp), %rdi
movl $0x3, %esi
callq 0x49642
movq %rbx, %rdi
movl $0x3, %esi
callq 0x49642
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq