// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %r9, %rbp
movl %r8d, %r14d
movl %ecx, 0x8(%rsp)
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movslq 0xb8(%rsp), %rbx
leal (%rbx,%rbx,2), %esi
movl %r8d, %edi
callq 0x4379e
movq %rax, 0x18(%rsp)
movl %r14d, %edi
movl %r14d, %esi
callq 0x43878
movq %r12, 0x30(%rsp)
movq %rax, (%r12)
movslq 0x8(%rsp), %rcx
movq %rcx, 0x20(%rsp)
imulq $0x158, %rcx, %rax      # imm = 0x158
testl %r14d, %r14d
movl $0x0, %r12d
movl %r14d, 0x14(%rsp)
cmovgl %r14d, %r12d
xorl %edx, %edx
leaq (%r15,%rax), %rcx
movq %rcx, 0x40(%rsp)
movq %r15, 0x28(%rsp)
movq %rax, 0x38(%rsp)
addq %rax, %r15
addq $0x48, %r15
movq %r12, 0x48(%rsp)
cmpq %r12, %rdx
je 0x204b2
imulq $0x158, %rdx, %rax      # imm = 0x158
addq 0x40(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x2042a
cvtsd2ss -0x18(%r15,%rcx,8), %xmm0
movss %xmm0, 0x60(%rsp,%rcx,4)
cvtsd2ss (%r15,%rcx,8), %xmm0
movss %xmm0, 0x54(%rsp,%rcx,4)
incq %rcx
jmp 0x20406
movslq 0x18(%rax), %r14
imulq $0xc, %r14, %r12
movq 0x18(%rsp), %rax
movq %rdx, 0x8(%rsp)
addq (%rax,%rdx,8), %r12
cmpq %rbx, %r14
jge 0x20499
movq 0xb0(%rsp), %rax
movslq (%rax,%r14,4), %rax
imulq $0x70, %rax, %rax
addq %rbp, %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x2047e
movsd (%rax,%rcx,8), %xmm0
cvtss2sd 0x60(%rsp,%rcx,4), %xmm1
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x6c(%rsp,%rcx,4)
incq %rcx
jmp 0x2045a
movq %r12, %rdi
leaq 0x54(%rsp), %rsi
leaq 0x6c(%rsp), %rdx
callq 0xf8a4
addq $0xc, %r12
incq %r14
jmp 0x20440
movq 0x8(%rsp), %rdx
incq %rdx
addq $0x158, %r15            # imm = 0x158
movq 0x48(%rsp), %r12
jmp 0x203ef
pushq $0x1
popq %rax
xorl %ecx, %ecx
movl 0x14(%rsp), %ebp
movq 0x30(%rsp), %r11
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %r14
movq 0x20(%rsp), %r15
cmpq %r12, %rcx
je 0x2054f
movq (%rbx,%rcx,8), %rdx
leaq (%rcx,%r15), %rsi
imulq $0x158, %rsi, %rsi      # imm = 0x158
movslq 0x18(%r14,%rsi), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%r11), %rdi
xorl %r8d, %r8d
cmpq %rax, %r8
je 0x20547
movq (%rbx,%r8,8), %r9
leaq (%r8,%r15), %r10
imulq $0x158, %r10, %r10      # imm = 0x158
imull $0x3, 0x18(%r14,%r10), %r10d
movslq %r10d, %r10
xorpd %xmm0, %xmm0
cmpq %rsi, %r10
jge 0x20538
movss (%r9,%r10,4), %xmm1
mulss (%rdx,%r10,4), %xmm1
mulss (%r13,%r10,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r10
jmp 0x20513
movq (%rdi,%r8,8), %r9
movsd %xmm0, (%r9,%rcx,8)
incq %r8
jmp 0x204f2
incq %rcx
incq %rax
jmp 0x204cf
pushq $0x1
popq %rax
xorl %ecx, %ecx
cmpq %r12, %rcx
je 0x20582
movq %rax, %rdx
cmpl %ebp, %edx
jge 0x2057a
movq (%r11), %rsi
movq (%rsi,%rcx,8), %rdi
movsd (%rdi,%rdx,8), %xmm0
movq (%rsi,%rdx,8), %rsi
movsd %xmm0, (%rsi,%rcx,8)
incq %rdx
jmp 0x2055c
incq %rcx
incq %rax
jmp 0x20554
leaq 0x53e63(%rip), %rax     # 0x743ec
cmpl $0x0, (%rax)
je 0x20664
movq 0x38(%rsp), %rax
addq %r14, %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rbp
leaq 0x32013(%rip), %rbx     # 0x525c0
xorl %r15d, %r15d
cmpq $0xa, %r15
je 0x2060a
movq 0x20(%rsp), %rax
addq %r15, %rax
imulq $0x158, %rax, %rax      # imm = 0x158
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %r14
addq $0x18, %r14
movq 0x8(%rsp), %r13
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x20602
movl (%r14), %ecx
movl (%r13), %r8d
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x20f0
incq %r12
addq $0x158, %r13            # imm = 0x158
jmp 0x205da
incq %r15
incq %rbp
jmp 0x205b0
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x2060
pushq $0x1
popq %r14
xorl %r12d, %r12d
leaq 0x31fab(%rip), %r15     # 0x525cd
movl 0x14(%rsp), %ebp
cmpq $0x14, %r12
je 0x20664
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x20655
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %r15, %rdi
movb $0x1, %al
callq 0x20f0
incq %r13
jmp 0x2062f
movl %ebx, %edi
callq 0x2060
incq %r12
incq %r14
jmp 0x20626
movq 0x18(%rsp), %rdi
movl %ebp, %esi
callq 0x436f0
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq