// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/kinetic_tnm.c
// Function: Kinetic_sqrt
int Kinetic_sqrt(struct Jacobian *J, int *N_kin,
		 int N, int CONTROL, float E_MIN)
{
  // 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;
  (*N_kin)=N;

  if(CONTROL==0){
    if(START)printf("Kinetic_sqrt with Cholevsky decomposition\n");
    if(choldc(J->T_sqrt, J->T, N)==0){
      //J->T_sqrt_inv=Cholevsky_inversion_d2f(J->T_sqrt, N);
      for(k=0; k<N; k++){
	double *Lk=J->T_sqrt[k]+k+1;
	for(l=k+1; l<N; l++){*Lk=0; Lk++;}
      }
      return(0);
    }
  }

  // New decomposition diagonalizing kinetic energy
  printf("Square root of kinetic energy through diagonalization\n");
  {
    float **eigen_vector=Allocate_mat2_f(N, N);
    float eigen_value[N], t, t_inv;
    int num=0, ik;
    if(J->T_sqrt_inv)Empty_matrix_f(J->T_sqrt_inv, N);
    J->T_sqrt_inv=Allocate_mat2_f(N, N);
    d_Diagonalize(N, J->T, eigen_value, eigen_vector, 1);
    for(k=0; k<N; k++){
      if(eigen_value[k]<E_MIN){t=0; num++; continue;}
      else{t=sqrt(eigen_value[k]); t_inv=1./t; ik=k-num;}
      for(l=0; l<N; l++){
	J->T_sqrt[l][ik]=t*eigen_vector[k][l];
	if(t)J->T_sqrt_inv[ik][l]=eigen_vector[k][l]*t_inv;
      }
    }
    if(num)printf("WARNING, kinetic energy has %d null modes\n", num);
    (*N_kin)-=num;
    Empty_matrix_f(eigen_vector, N);
  }

  return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movss %xmm0, -0x2c(%rbp)
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r15
movl %edx, (%rsi)
testl %ecx, %ecx
jne 0x2279b
cmpl $0x0, 0x46737(%rip)     # 0x68eac
je 0x22783
leaq 0x362b7(%rip), %rdi     # 0x58a35
callq 0x2090
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdi
movl %r13d, %edx
callq 0x49810
testl %eax, %eax
je 0x22904
movq %rbx, -0x50(%rbp)
leaq 0x362b9(%rip), %rdi     # 0x58a5f
callq 0x2090
movl %r13d, %edi
movl %r13d, %esi
callq 0x496ae
movq %rax, -0x38(%rbp)
movq %rsp, -0x48(%rbp)
movl %r13d, %r12d
movl %r13d, %ebx
movq %rsp, %r13
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq 0x30(%r15), %rdi
testq %rdi, %rdi
je 0x227e9
movl %ebx, %esi
callq 0x4960c
movl %ebx, %edi
movl %ebx, %esi
callq 0x496ae
movq %rax, 0x30(%r15)
movq 0x20(%r15), %rsi
movl %ebx, %edi
movq %r13, %rdx
movq -0x38(%rbp), %rcx
movl $0x1, %r8d
callq 0x4a4d2
movl %ebx, -0x3c(%rbp)
testl %ebx, %ebx
jle 0x228c3
xorl %ebx, %ebx
xorps %xmm5, %xmm5
movss 0x2e7ea(%rip), %xmm6    # 0x51010
xorl %r14d, %r14d
movss -0x2c(%rbp), %xmm4
movss (%r13,%rbx,4), %xmm0
ucomiss %xmm0, %xmm4
jbe 0x2283f
incl %r14d
jmp 0x228b5
ucomiss %xmm5, %xmm0
jb 0x2284a
sqrtss %xmm0, %xmm0
jmp 0x2285f
callq 0x21c0
movss 0x2e7b9(%rip), %xmm6    # 0x51010
xorps %xmm5, %xmm5
movss -0x2c(%rbp), %xmm4
movaps %xmm6, %xmm1
divss %xmm0, %xmm1
movl %ebx, %edx
subl %r14d, %edx
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rax
movq 0x28(%r15), %rcx
movslq %edx, %rdx
xorl %esi, %esi
ucomiss %xmm5, %xmm0
movss (%rax,%rsi,4), %xmm2
movaps %xmm0, %xmm3
mulss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm3
movq (%rcx,%rsi,8), %rdi
movsd %xmm3, (%rdi,%rdx,8)
jne 0x2289c
jnp 0x228ad
mulss %xmm1, %xmm2
movq 0x30(%r15), %rdi
movq (%rdi,%rdx,8), %rdi
movss %xmm2, (%rdi,%rsi,4)
incq %rsi
cmpq %rsi, %r12
jne 0x2287c
incq %rbx
cmpq %r12, %rbx
jne 0x2282e
jmp 0x228c6
xorl %r14d, %r14d
testl %r14d, %r14d
je 0x228dc
leaq 0x35e16(%rip), %rdi     # 0x586e8
movl %r14d, %esi
xorl %eax, %eax
callq 0x20f0
movq -0x50(%rbp), %rax
subl %r14d, (%rax)
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x4960c
movq -0x48(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r13d, %r13d
jle 0x228f3
movq 0x28(%r15), %rbx
movl %r13d, %r14d
addl $-0x2, %r13d
movq %r14, %r15
negq %r15
movl $0x1, %r12d
cmpq %r14, %r12
jae 0x22944
leaq (,%r12,8), %rdi
movl %r13d, %eax
leaq 0x8(,%rax,8), %rdx
addq -0x8(%rbx,%r12,8), %rdi
xorl %esi, %esi
callq 0x2110
decl %r13d
leaq (%r15,%r12), %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x22920
jmp 0x228f3