// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x3, %esi
movl %esi, %edi
callq 0x694a0
movq %rax, -0x18(%rbp)
movl $0x3, %esi
movl %esi, %edi
callq 0x694a0
movq %rax, -0x20(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x3, -0x64(%rbp)
jge 0x31712
movslq -0x64(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp,%rax,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x316f4
movq -0xc0(%rbp), %rax
leaq -0x60(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x8(%rbp), %rdx
movl (%rax), %r8d
subl $0x1, %r8d
xorl %ecx, %ecx
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
xorl %ecx, %ecx
callq 0x307d0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x3, -0x64(%rbp)
jge 0x31788
movslq -0x64(%rbp), %rax
movsd -0x60(%rbp,%rax,8), %xmm0
divsd -0x78(%rbp), %xmm0
movslq -0x64(%rbp), %rax
movsd %xmm0, -0x40(%rbp,%rax,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3175e
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x60(%rbp), %rdx
movsd -0x78(%rbp), %xmm0
callq 0x30340
movl $0x3, %esi
movl %esi, %edi
callq 0x69360
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x84(%rbp), %rdx
movq -0x90(%rbp), %rcx
movl $0x3, %edi
movl $0x1, %r8d
callq 0x6ac30
movq -0x90(%rbp), %rdi
callq 0x31a70
movss %xmm0, -0x94(%rbp)
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x4c4b4(%rip), %rdi     # 0x7dcae
movb $0x1, %al
callq 0x10d0
xorps %xmm0, %xmm0
ucomiss -0x94(%rbp), %xmm0
jbe 0x3191a
movl $0x0, -0x68(%rbp)
cmpl $0x3, -0x68(%rbp)
jge 0x31900
movl $0x0, -0x6c(%rbp)
cmpl $0x3, -0x6c(%rbp)
jge 0x31877
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x31829
movq -0x90(%rbp), %rdi
callq 0x31a70
movss %xmm0, -0x94(%rbp)
movss -0x94(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x3189d
jmp 0x31900
movl $0x0, -0x6c(%rbp)
cmpl $0x3, -0x6c(%rbp)
jge 0x318f2
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x318a4
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x31818
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x4c39b(%rip), %rdi     # 0x7dcae
movb $0x1, %al
callq 0x10d0
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x31a2e
movq -0x8(%rbp), %rax
movslq -0x64(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movl $0x0, -0x68(%rbp)
cmpl $0x3, -0x68(%rbp)
jge 0x319e9
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq -0xb8(%rbp), %rax
movsd (%rax), %xmm2
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0xb8(%rbp), %rax
mulsd 0x8(%rax), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movq -0x90(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0xb8(%rbp), %rax
movsd 0x10(%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x68(%rbp), %rax
movsd %xmm0, -0xb0(%rbp,%rax,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x3194a
movl $0x0, -0x68(%rbp)
cmpl $0x3, -0x68(%rbp)
jge 0x31a1e
movslq -0x68(%rbp), %rax
movsd -0xb0(%rbp,%rax,8), %xmm0
movq -0xb8(%rbp), %rax
movslq -0x68(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x319f0
jmp 0x31a20
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x31921
movq -0x20(%rbp), %rdi
movl $0x3, %esi
callq 0x692a0
movq -0x18(%rbp), %rdi
movl $0x3, %esi
callq 0x692a0
movq -0x90(%rbp), %rdi
movl $0x3, %esi
callq 0x69240
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)