// File: /workspace/github_c_makefile_star2_O1/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, 0x20(%rsp)
movl %r8d, %ebp
movl %ecx, %ebx
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, %r15
movl 0xa8(%rsp), %eax
leal (%rax,%rax,2), %esi
movl %r8d, %edi
callq 0x496ae
movq %rax, 0x8(%rsp)
movl %ebp, %edi
movl %ebp, %esi
callq 0x49798
movq %r15, 0x28(%rsp)
movq %rax, (%r15)
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
movl %ebp, %eax
movq %rax, 0x30(%rsp)
movl %ebp, 0x4(%rsp)
testl %ebp, %ebp
jle 0x22b5a
imulq $0x158, 0x10(%rsp), %rax # imm = 0x158
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x38(%rsp)
movl 0xa8(%rsp), %edx
movslq %edx, %r15
leaq (%rcx,%rax), %r12
addq $0x48, %r12
xorl %eax, %eax
movq %rax, %r14
imulq $0x158, %rax, %rax      # imm = 0x158
addq 0x38(%rsp), %rax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsd2ss -0x18(%r12,%rcx,8), %xmm0
movss %xmm0, 0x50(%rsp,%rcx,4)
xorps %xmm0, %xmm0
cvtsd2ss (%r12,%rcx,8), %xmm0
movss %xmm0, 0x44(%rsp,%rcx,4)
incq %rcx
cmpq $0x3, %rcx
jne 0x22a96
movslq 0x18(%rax), %rbp
movl 0xa8(%rsp), %eax
cmpl %eax, %ebp
jge 0x22b42
leaq (,%rbp,2), %rbx
addq %rbp, %rbx
shlq $0x2, %rbx
movq 0x8(%rsp), %rax
addq (%rax,%r14,8), %rbx
movq 0xa0(%rsp), %rax
movslq (%rax,%rbp,4), %rax
imulq $0x70, %rax, %rax
addq 0x20(%rsp), %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 0x22afc
movq %rbx, %rdi
leaq 0x44(%rsp), %rsi
leaq 0x5c(%rsp), %rdx
callq 0x10818
addq $0xc, %rbx
incq %rbp
cmpq %r15, %rbp
jne 0x22ae5
movq %r14, %rax
incq %rax
addq $0x158, %r12            # imm = 0x158
cmpq 0x30(%rsp), %rax
jne 0x22a85
cmpl $0x0, 0x4(%rsp)
movq 0x28(%rsp), %r11
movq 0x8(%rsp), %rbp
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r15
jle 0x22c05
movq (%r11), %rax
movl $0x1, %ecx
xorl %edx, %edx
movq (%rbp,%rdx,8), %rsi
leaq (%rdx,%rbx), %rdi
imulq $0x158, %rdi, %rdi      # imm = 0x158
movslq 0x18(%r12,%rdi), %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
leaq (%r8,%rbx), %r9
imulq $0x158, %r9, %r9        # imm = 0x158
movslq 0x18(%r12,%r9), %r9
leaq (%r9,%r9,2), %r9
xorps %xmm0, %xmm0
cmpl %edi, %r9d
jge 0x22be8
movq (%rbp,%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 0x22bc5
movq (%rax,%r8,8), %r9
movsd %xmm0, (%r9,%rdx,8)
incq %r8
cmpq %rcx, %r8
jne 0x22ba4
incq %rdx
incq %rcx
cmpq %r15, %rdx
jne 0x22b88
cmpl $0x0, 0x4(%rsp)
jle 0x22c48
movl $0x1, %eax
xorl %ecx, %ecx
movq %rcx, %rdx
incq %rcx
cmpq %r15, %rcx
jae 0x22c40
movq (%r11), %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, %r15
jne 0x22c28
incq %rax
cmpq %r15, %rcx
jne 0x22c13
leaq 0x5679d(%rip), %rax     # 0x793ec
cmpl $0x0, (%rax)
je 0x22d2c
imulq $0x158, %rbx, %rax      # imm = 0x158
addq %r12, %rax
addq $0x18, %rax
movq %rax, 0x20(%rsp)
movl $0x1, %ebp
leaq 0x35a13(%rip), %rbx     # 0x5868a
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
addq %r15, %rax
imulq $0x158, %rax, %rax      # imm = 0x158
addq %rax, %r12
addq $0x18, %r12
movq 0x20(%rsp), %r14
xorl %r13d, %r13d
movl (%r12), %ecx
movl (%r14), %r8d
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x20f0
incq %r13
addq $0x158, %r14            # imm = 0x158
cmpq %r13, %rbp
jne 0x22c98
incq %r15
incq %rbp
cmpq $0xa, %r15
movq 0x18(%rsp), %r12
jne 0x22c7a
movl $0xa, %edi
callq 0x2060
movl $0x1, %r14d
xorl %r15d, %r15d
leaq 0x359ae(%rip), %rbx     # 0x58697
movq 0x28(%rsp), %r13
movq 0x8(%rsp), %rbp
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 0x22cf6
movl $0xa, %edi
callq 0x2060
incq %r15
incq %r14
cmpq $0x14, %r15
jne 0x22cf3
movq %rbp, %rdi
movl 0x4(%rsp), %esi
callq 0x4960c
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq