// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/kinetic_tnm.c
// Function: Reorient_axes
void Reorient_axes(atom *atoms, int N_atoms, struct Reference Ref)
{
  double **inertia_tot=Allocate_mat2_d(3,3);
  double **corr_sum=Allocate_mat2_d(3,3);
  double r_ave[3], mr_tot[3]; int i, k, l;

  // Compute inertia tensor and center of mass for all atoms
  for(i=0; i<3; i++)mr_tot[i]=0;
  double M_tot=Sum_inertia(mr_tot, corr_sum, atoms, Ref, 0, Ref.N_ref-1);
  for(i=0; i<3; i++)r_ave[i]=mr_tot[i]/M_tot;
  Inertia_tensor(inertia_tot, corr_sum, mr_tot, M_tot);
  float eigen_value[3], **rot=Allocate_mat2_f(3, 3);
  d_Diagonalize(3, inertia_tot, eigen_value, rot, 1);
  float det=Det_33(rot);
  printf("Determinant= %.3f\n", det);
  if(det<0){
    k=0;
    while(k<3){
      for(l=0; l<3; l++)rot[k][l]=-rot[k][l];
      det=Det_33(rot); if(det>0)break;
      for(l=0; l<3; l++)rot[k][l]=-rot[k][l];
      k++;
    }
    printf("Determinant= %.3f\n", det);
  }

  if(0){
    printf("Center of mass: ");
    for(i=0; i<3; i++)printf("  %.7f", r_ave[i]);
    printf("  M=%.1f\n", M_tot);
    printf("Inertia_values: ");
    for(k=0; k<3; k++)printf(" %.0f", eigen_value[k]);
    printf("\n");
    printf("Rotation matrix:\n");
    for(k=0; k<3; k++){
      for(l=0; l<3; l++)printf(" %.3f", rot[k][l]);
      printf("\n");
    }
  }

  // Rotate atoms coordinates
  for(i=0; i<N_atoms; i++){
    double rr[3], *r=atoms[i].r;
    for(k=0; k<3; k++){
      //double rk=0; for(l=0; l<3; l++)rk+=rot[k][l]*r[l]; rr[k]=rk;
      rr[k]=rot[k][0]*r[0]+rot[k][1]*r[1]+rot[k][2]*r[2];
    }
    for(k=0; k<3; k++)r[k]=rr[k];
  }

  if(0){
    // Test that inertia tensor is diagonal
    for(i=0; i<3; i++)mr_tot[i]=0;
    M_tot=Sum_inertia(mr_tot, corr_sum, atoms, Ref, 0, Ref.N_ref-1);
    Inertia_tensor(inertia_tot, corr_sum, mr_tot, M_tot);
    d_Diagonalize(3, inertia_tot, eigen_value, rot, 1);
    printf("Principal axes:\n");
    for(k=0; k<3; k++){
      float *v=rot[k];
      for(l=0; l<3; l++)printf(" %.3f", v[l]);
      printf("\n");
    }
    exit(8);
  }

  Empty_matrix_d(corr_sum, 3);
  Empty_matrix_d(inertia_tot, 3);
  Empty_matrix_f(rot, 3);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %esi, %ebx
movq %rdi, %r15
leaq 0xd0(%rsp), %r14
pushq $0x3
popq %rbp
movl %ebp, %edi
movl %ebp, %esi
callq 0x43878
movq %rax, 0x40(%rsp)
movl %ebp, %edi
movl %ebp, %esi
callq 0x43878
movq %rax, %rbp
xorl %eax, %eax
cmpq $0x3, %rax
je 0x211cb
andq $0x0, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x211ba
movl (%r14), %r8d
decl %r8d
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x70(%rsp), %r13
xorl %r14d, %r14d
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20914
cvtss2sd %xmm0, %xmm0
movq 0x40(%rsp), %r12
movq %r12, %rdi
movq %rbp, 0x48(%rsp)
movq %rbp, %rsi
movq %r13, %rdx
callq 0x20681
pushq $0x3
popq %rbp
movl %ebp, %edi
movl %ebp, %esi
callq 0x4379e
movq %rax, %r13
leaq 0x8c(%rsp), %rdx
pushq $0x1
popq %r8
movl %ebp, %edi
movq %r12, %rsi
movq %rax, %rcx
callq 0x444de
movq %r13, %rdi
callq 0x213aa
movss %xmm0, 0x3c(%rsp)
cvtss2sd %xmm0, %xmm0
leaq 0x316a0(%rip), %rdi     # 0x52902
movb $0x1, %al
callq 0x20f0
movss 0x3c(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x212f2
xorl %ebp, %ebp
movaps 0x2f5e0(%rip), %xmm1    # 0x50860
cmpq $0x3, %rbp
je 0x212e0
movq (%r13,%rbp,8), %r12
xorl %eax, %eax
cmpq $0x3, %rax
je 0x212a7
movss (%r12,%rax,4), %xmm0
xorps %xmm1, %xmm0
movss %xmm0, (%r12,%rax,4)
incq %rax
jmp 0x2128d
movq %r13, %rdi
callq 0x213aa
ucomiss 0x29d56(%rip), %xmm0    # 0x4b00c
ja 0x212e0
xorl %eax, %eax
movaps 0x2f59f(%rip), %xmm1    # 0x50860
cmpq $0x3, %rax
je 0x212db
movss (%r12,%rax,4), %xmm2
xorps %xmm1, %xmm2
movss %xmm2, (%r12,%rax,4)
incq %rax
jmp 0x212c1
incq %rbp
jmp 0x21280
cvtss2sd %xmm0, %xmm0
leaq 0x31617(%rip), %rdi     # 0x52902
movb $0x1, %al
callq 0x20f0
testl %ebx, %ebx
cmovlel %r14d, %ebx
movq %r15, %rax
movq 0x48(%rsp), %rdi
cmpq %rbx, %r14
je 0x21378
imulq $0x70, %r14, %rcx
movsd (%r15,%rcx), %xmm0
movsd 0x8(%r15,%rcx), %xmm1
movsd 0x10(%r15,%rcx), %xmm2
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x21357
movq (%r13,%rcx,8), %rdx
cvtss2sd (%rdx), %xmm3
mulsd %xmm0, %xmm3
cvtss2sd 0x4(%rdx), %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
cvtss2sd 0x8(%rdx), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
movsd %xmm3, 0x50(%rsp,%rcx,8)
incq %rcx
jmp 0x2131f
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x2136f
movsd 0x50(%rsp,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
jmp 0x21359
incq %r14
addq $0x70, %rax
jmp 0x21300
pushq $0x3
popq %rbx
movl %ebx, %esi
callq 0x4372a
movq 0x40(%rsp), %rdi
movl %ebx, %esi
callq 0x4372a
movq %r13, %rdi
movl %ebx, %esi
callq 0x436f0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq