// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/kinetic_tnm.c
// Function: Kinetic_energy
int Kinetic_energy(double ***T_mat, float *mass_coord,
		   struct axe *axe, int ini_axe, int N_axes,
		   atom *atoms, int *atom_num, int N_ref)
{
  // Compute kinetic energy tensor T=T_sqrt T_sqrt^T (only internal dof)
  // Lower tridiagonal matrix T_sqrt[i][j] with j<=i
  int k, l, i, j, ini_ref, N_Cart=3*N_ref;
  float **J_ar=Allocate_mat2_f(N_axes, N_Cart), *J, dr[3];
  *T_mat=Allocate_mat2_d(N_axes, N_axes);

  // Local Jacobian (only atoms upstream of k)
  for(l=0; l<N_axes; l++){
    struct axe *l_axe=axe+ini_axe+l;
    float offset[3], rot[3];
    for(j=0; j<3; j++){
      offset[j]=l_axe->offset[j]; rot[j]=l_axe->rot[j];
    }
    ini_ref=l_axe->first_kin;
    J=J_ar[l]+3*ini_ref;
    for(i=ini_ref; i<N_ref; i++){
      for(j=0; j<3; j++)dr[j]=atoms[atom_num[i]].r[j]-offset[j];
      Vector_product(J, rot, dr); J+=3;
    }
  }

  for(l=0; l<N_axes; l++){
    float *Jl=J_ar[l];
    //~ struct axe *l_axe=axe+ini_axe+l;
    int end_cart=3*(axe[ini_axe+l].first_kin);

    for(k=0; k<=l; k++){
      double sum=0; float *Jk=J_ar[k];
      int ini_cart=3*(axe[ini_axe+k].first_kin);
      for(j=ini_cart; j<end_cart; j++)sum+=Jk[j]*Jl[j]*mass_coord[j];
      (*T_mat)[k][l]=sum;
    }
  }
  for(l=0; l<N_axes; l++){
    for(k=l+1; k<N_axes; k++)(*T_mat)[k][l]=(*T_mat)[l][k];
  }
  if(DEBUG){
    for(l=0; l<10; l++){
      for(k=0; k<=l; k++){
	printf("%d %d %d %d\n", l, k,
	       axe[ini_axe+l].first_kin, axe[ini_axe+k].first_kin);
      }
    }
    printf("\n");
    for(l=0; l<20; l++){
      for(k=0; k<=l; k++)printf("%.2g ", (*T_mat)[l][k]);
      printf("\n");
    }
  }

  Empty_matrix_f(J_ar, N_axes);
  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbp
movl %r8d, %r14d
movl %ecx, %ebx
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
movl 0xa8(%rsp), %eax
leal (%rax,%rax,2), %esi
movl %r8d, %edi
callq 0x4971a
movq %rax, 0x18(%rsp)
movl %r14d, %edi
movl %r14d, %esi
callq 0x49804
movq %r15, 0x10(%rsp)
movq %rax, (%r15)
movslq %ebx, %r15
testl %r14d, %r14d
movl %r14d, 0x4(%rsp)
movq %r12, 0x28(%rsp)
movq %r15, 0x20(%rsp)
jle 0x225ad
imulq $0x158, %r15, %rax      # imm = 0x158
leaq (%r12,%rax), %rcx
movq %rcx, 0x30(%rsp)
movl %r14d, %r14d
movl 0xa8(%rsp), %edx
movslq %edx, %rbx
leaq (%r12,%rax), %r15
addq $0x48, %r15
xorl %eax, %eax
movq %r14, 0x38(%rsp)
movq %rax, 0x8(%rsp)
imulq $0x158, %rax, %rax      # imm = 0x158
addq 0x30(%rsp), %rax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsd2ss -0x18(%r15,%rcx,8), %xmm0
movss %xmm0, 0x50(%rsp,%rcx,4)
xorps %xmm0, %xmm0
cvtsd2ss (%r15,%rcx,8), %xmm0
movss %xmm0, 0x44(%rsp,%rcx,4)
incq %rcx
cmpq $0x3, %rcx
jne 0x223fa
movslq 0x18(%rax), %r14
cmpl %edx, %r14d
jge 0x2249c
leaq (%r14,%r14,2), %r12
shlq $0x2, %r12
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
addq (%rax,%rcx,8), %r12
movq 0xa0(%rsp), %rax
movslq (%rax,%r14,4), %rax
imulq $0x70, %rax, %rax
addq %rbp, %rax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtss2sd 0x50(%rsp,%rcx,4), %xmm0
movsd (%rax,%rcx,8), %xmm1
subsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x5c(%rsp,%rcx,4)
incq %rcx
cmpq $0x3, %rcx
jne 0x22456
movq %r12, %rdi
leaq 0x44(%rsp), %rsi
leaq 0x5c(%rsp), %rdx
callq 0x10368
addq $0xc, %r12
incq %r14
cmpq %rbx, %r14
jne 0x22441
movq 0x8(%rsp), %rax
incq %rax
addq $0x158, %r15            # imm = 0x158
movq 0x38(%rsp), %r14
cmpq %r14, %rax
movl 0xa8(%rsp), %edx
jne 0x223e7
movl 0x4(%rsp), %r11d
testl %r11d, %r11d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
jle 0x225ad
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x1, %ecx
xorl %edx, %edx
movq 0x18(%rsp), %rbx
movq (%rbx,%rdx,8), %rsi
leaq (%rdx,%r15), %rdi
imulq $0x158, %rdi, %rdi      # imm = 0x158
movslq 0x18(%r12,%rdi), %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
leaq (%r8,%r15), %r9
imulq $0x158, %r9, %r9        # imm = 0x158
movslq 0x18(%r12,%r9), %r9
leaq (%r9,%r9,2), %r9
xorps %xmm0, %xmm0
cmpl %edi, %r9d
jge 0x2254a
movq (%rbx,%r8,8), %r10
movss (%r10,%r9,4), %xmm1
mulss (%rsi,%r9,4), %xmm1
mulss (%r13,%r9,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r9
cmpq %r9, %rdi
jne 0x22527
movq (%rax,%r8,8), %r9
movsd %xmm0, (%r9,%rdx,8)
incq %r8
cmpq %rcx, %r8
jne 0x22507
incq %rdx
incq %rcx
cmpq %r14, %rdx
jne 0x224ec
testl %r11d, %r11d
jle 0x225ad
movl $0x1, %eax
xorl %ecx, %ecx
movq %rcx, %rdx
incq %rcx
cmpq %r14, %rcx
jae 0x225a5
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rax, %r8
movsd (%rdi,%r8,8), %xmm0
movq (%rsi,%r8,8), %r9
movsd %xmm0, (%r9,%rdx,8)
incq %r8
cmpq %r8, %r14
jne 0x2258d
incq %rax
cmpq %r14, %rcx
jne 0x22573
leaq 0x56e38(%rip), %rax     # 0x793ec
cmpl $0x0, (%rax)
je 0x2268d
imulq $0x158, %r15, %rax      # imm = 0x158
addq %r12, %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
movl $0x1, %r13d
leaq 0x36083(%rip), %rbx     # 0x58660
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
addq %r15, %rax
imulq $0x158, %rax, %rax      # imm = 0x158
leaq (%r12,%rax), %rbp
addq $0x18, %rbp
movq 0x8(%rsp), %r14
xorl %r12d, %r12d
movl (%rbp), %ecx
movl (%r14), %r8d
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x20f0
incq %r12
addq $0x158, %r14            # imm = 0x158
cmpq %r12, %r13
jne 0x225ff
incq %r15
incq %r13
cmpq $0xa, %r15
movq 0x28(%rsp), %r12
jne 0x225e0
movl $0xa, %edi
callq 0x2060
movl $0x1, %r14d
xorl %r15d, %r15d
leaq 0x3601e(%rip), %rbx     # 0x5866d
movq 0x10(%rsp), %r13
xorl %r12d, %r12d
movq (%r13), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%r12,8), %xmm0
movq %rbx, %rdi
movb $0x1, %al
callq 0x20f0
incq %r12
cmpq %r12, %r14
jne 0x22657
movl $0xa, %edi
callq 0x2060
incq %r15
incq %r14
cmpq $0x14, %r15
jne 0x22654
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x49678
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq