// File: /workspace/github_c_makefile_star2_O2/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, 0x14(%rsp)
movq %r8, %rbp
movl %ecx, 0x8(%rsp)
movq %rdx, (%rsp)
movl %esi, %ebx
movq %rdi, %r14
pushq $0x3
popq %r12
movl %r12d, %edi
movl %r12d, %esi
callq 0x43878
movq %rax, %r13
movl %r12d, %edi
movl %r12d, %esi
callq 0x43878
movq %rax, %r15
movl %r12d, %edi
movl %r12d, %esi
callq 0x43878
leaq 0x40(%rsp), %rcx
movq %r14, %rdi
movq (%rsp), %rsi
movl 0x8(%rsp), %edx
movq %rax, (%rsp)
movq %rax, %r8
movq %r13, %r9
callq 0x2427e
movq %r13, 0x8(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x2447f
xorl %r12d, %r12d
testl %ebx, %ebx
cmovlel %r12d, %ebx
leaq 0x20(%rsp), %r13
movq %r14, 0x18(%rsp)
cmpq %rbx, %r12
je 0x2419e
xorl %eax, %eax
cmpq $0x3, %rax
je 0x24161
movsd (%r14,%rax,8), %xmm0
movsd %xmm0, 0x20(%rsp,%rax,8)
incq %rax
jmp 0x2414a
movq %r13, %rdi
movq %r13, %rsi
leaq 0x40(%rsp), %rdx
callq 0xfac1
movq %r13, %rdi
movq %r15, %rsi
callq 0xfa15
xorl %eax, %eax
cmpq $0x3, %rax
je 0x24195
movsd 0x20(%rsp,%rax,8), %xmm0
movsd %xmm0, (%r14,%rax,8)
incq %rax
jmp 0x2417e
incq %r12
addq $0x70, %r14
jmp 0x24143
xorl %r14d, %r14d
movl 0x14(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %r14d
addq $0x30, %rbp
leaq 0x40(%rsp), %r12
subq $0x1, %r14
jb 0x241e8
leaq 0x18(%rbp), %rdi
movq %r15, %rsi
callq 0xfa15
movq %rbp, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xfac1
movq %rbp, %rdi
movq %r15, %rsi
callq 0xfa15
addq $0x158, %rbp            # imm = 0x158
jmp 0x241b4
cmpl $0x0, 0xc0(%rsp)
je 0x24248
leaq 0x60(%rsp), %rax
xorl %ecx, %ecx
movq 0x18(%rsp), %r14
cmpq $0x3, %rcx
je 0x24228
movq (%r15,%rcx,8), %rdx
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x2421f
cvtsd2ss (%rdx,%rsi,8), %xmm0
movss %xmm0, (%rax,%rsi,4)
incq %rsi
jmp 0x2420a
incq %rcx
addq $0xc, %rax
jmp 0x241fe
addq $0x20, %r14
leaq 0x60(%rsp), %r12
subq $0x1, %rbx
jb 0x24248
movq %r14, %rdi
movq %r12, %rsi
callq 0xfb87
addq $0x70, %r14
jmp 0x24231
pushq $0x3
popq %rbx
movq 0x8(%rsp), %rdi
movl %ebx, %esi
callq 0x4372a
movq (%rsp), %rdi
movl %ebx, %esi
callq 0x4372a
movq %r15, %rdi
movl %ebx, %esi
callq 0x4372a
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq