// File: /workspace/github_c_makefile_star2_O0/ugobas[P]tnm/kinetic_tnm.c
// Function: Compute_kinetic
int Compute_kinetic(struct Jacobian *J,
		    struct axe *axe1, int naxe1,
		    atom *atoms1, int natoms1,
		    struct Reference Ref, int SQR)
{
  int N_kin=0;
  int test=(J->Jacobian_ar[0][0]==0);
  // Put center of mass at the origin
  Center_atoms(atoms1, natoms1, Ref);
  // Reorient the Cartesian axes along the principal axes
  //Reorient_axes(atoms1, natoms1, Ref);

  Set_rot_shift(axe1, naxe1, atoms1, natoms1);
  Eckart_main(axe1, naxe1, atoms1, natoms1, Ref);
  Internal_Jacobian(*J, Ref, atoms1, natoms1, axe1, naxe1);
  if(test){
    Eckart_test(J->Jacobian_ar, naxe1, atoms1, natoms1, Ref, axe1);
    if(0)Correlate_Jacobian(*J, Ref, axe1, naxe1);
  }
  Kinetic_energy_fast(J, axe1, naxe1, atoms1, natoms1, Ref);
  //Kinetic_energy_Jac(*J, Ref, naxe1);

  if(SQR){
    // Compute T_sqrt with Cholevsky decomposition
    // Empty matrices
    if(J->T_sqrt_inv){
      Empty_matrix_f(J->T_sqrt_inv, naxe1); J->T_sqrt_inv=NULL;
    }
    if(J->T_sqrt_inv_tr!=NULL){
      Empty_matrix_f(J->T_sqrt_inv_tr, naxe1); J->T_sqrt_inv_tr=NULL;
    }
    if(J->T_sqrt_tr!=NULL){
      Empty_matrix_f(J->T_sqrt_tr, N_kin); J->T_sqrt_tr=NULL;
    }
    if(J->Jtilde_ar!=NULL){
      Empty_matrix_f(J->Jtilde_ar, naxe1); J->Jtilde_ar=NULL;
    }

    // Compute T_sqrt with Cholevsky decomposition
    Kinetic_sqrt(J, &(N_kin), naxe1, T_COMPUTE, E_MIN);    
    // Transpose matrix to accelerate computation
    if(J->T_sqrt_inv){
      J->T_sqrt_inv_tr=Transpose_matrix(J->T_sqrt_inv, N_kin, naxe1);
    }else{
      J->T_sqrt_tr=Transpose_matrix_d2f(J->T_sqrt, N_kin, N_kin);
    }
  }
  J->N_kin=N_kin;

  // Show results
  if(DBG){
    int i, j, m=10;
    printf("Masses: ");
    for(i=0; i<3*m; i+=3)printf("%.0f ", Ref.mass_coord[i]);
    printf("\nJacobian: ");
    for(i=0; i<m; i++)printf("%.1f ", J->Jacobian_ar[i][0]);
    printf("\nKinetic energy matrix. ");
    printf("%d dofs atoms= %d cart= %d Nkin= %d\n",
	   naxe1, natoms1, Ref.N_Cart, N_kin);
    for(i=0; i<m; i++){
      for(j=0; j<m; j++)printf("%.2g\t", J->T_sqrt[i][j]);
      printf("\n");
    }
    printf("Axis properties\n");
    struct axe *axe=axe1;
    for(i=0; i<30; i++){
      printf("%c%d\t%s-%s\t%d-%d\t%d-%d\t%.2g\t%.2g\t%.2g\t%.2g\t%.2g\n",
	     axe->type, i,
	     axe->bond->previous->atom->name, axe->bond->atom->name,
	     axe->first_atom, axe->last_atom,
	     axe->first_kin, axe->last_kin,
	     axe->rot[0],
	     axe->local_shift[0], axe->local_rot[0],
	     axe->global_shift[0], axe->global_rot[0]);
      axe++;
    }
  }
  START=0;

  return(N_kin);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx), %rcx
movss (%rcx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
sete %cl
setnp %dl
andb %dl, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
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)
callq 0x2e160
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
callq 0x2e2e0
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %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)
callq 0x2e7c0
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %r8
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movq %rax, -0x50(%rbp)
movups %xmm2, 0x68(%rax)
movups %xmm1, 0x58(%rax)
movups %xmm0, 0x48(%rax)
movq 0x40(%r8), %r9
movq %r9, 0x40(%rax)
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups 0x20(%r8), %xmm2
movups 0x30(%r8), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x2f210
cmpl $0x0, -0x30(%rbp)
je 0x2ddd0
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %r8
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)
callq 0x2f3d0
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rcx
movl -0x24(%rbp), %r8d
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)
callq 0x2f4a0
cmpl $0x0, -0x28(%rbp)
je 0x2df13
movq -0x8(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x2de36
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x69240
movq -0x8(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x2de5d
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x69240
movq -0x8(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x2de84
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x69240
movq -0x8(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x2deab
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x69240
movq -0x8(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0x5e1fb(%rip), %rax     # 0x8c0b4
movss (%rax), %xmm0
leaq -0x2c(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2fab0
movq -0x8(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x2def3
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdi
movl -0x2c(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x2fdb0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x2df11
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x2c(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x2fe40
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0x2df13
movl -0x2c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x6e3a8(%rip)     # 0x9c2cc
je 0x2e147
movl $0xa, -0x3c(%rbp)
leaq 0x4f873(%rip), %rdi     # 0x7d7ab
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
imull $0x3, -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2df7f
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x34(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x4f847(%rip), %rdi     # 0x7d7b4
movb $0x1, %al
callq 0x10d0
movl -0x34(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x34(%rbp)
jmp 0x2df46
leaq 0x4f834(%rip), %rdi     # 0x7d7ba
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x2dfcd
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x4a44b(%rip), %rdi     # 0x78406
movb $0x1, %al
callq 0x10d0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2df94
leaq 0x4f7f2(%rip), %rdi     # 0x7d7c6
movb $0x0, %al
callq 0x10d0
movq -0x58(%rbp), %rax
movl -0x14(%rbp), %esi
movl -0x24(%rbp), %edx
movl 0x4(%rax), %ecx
movl -0x2c(%rbp), %r8d
leaq 0x4f7ec(%rip), %rdi     # 0x7d7df
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x2e063
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x2e04a
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x4f7cc(%rip), %rdi     # 0x7d804
movb $0x1, %al
callq 0x10d0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2e010
leaq 0x50027(%rip), %rdi     # 0x7e078
movb $0x0, %al
callq 0x10d0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2e001
leaq 0x4f7a0(%rip), %rdi     # 0x7d80a
movb $0x0, %al
callq 0x10d0
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x1e, -0x34(%rbp)
jge 0x2e145
movq -0x48(%rbp), %rax
movsbl (%rax), %esi
movl -0x34(%rbp), %edx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rcx
addq $0x18, %rcx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %r8
addq $0x18, %r8
movq -0x48(%rbp), %rax
movl 0x10(%rax), %r9d
movq -0x48(%rbp), %rax
movl 0x14(%rax), %r11d
movq -0x48(%rbp), %rax
movl 0x18(%rax), %r10d
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x48(%rbp), %rdi
movsd 0x48(%rdi), %xmm0
movq -0x48(%rbp), %rdi
movsd 0xc0(%rdi), %xmm1
movq -0x48(%rbp), %rdi
movsd 0xd8(%rdi), %xmm2
movq -0x48(%rbp), %rdi
movsd 0x90(%rdi), %xmm3
movq -0x48(%rbp), %rdi
movsd 0xa8(%rdi), %xmm4
leaq 0x4f706(%rip), %rdi     # 0x7d81b
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x5, %al
callq 0x10d0
movq -0x48(%rbp), %rax
addq $0x158, %rax            # imm = 0x158
movq %rax, -0x48(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2e080
jmp 0x2e147
movl $0x0, 0x5dd1f(%rip)     # 0x8be70
movl -0x2c(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)