// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rsi, %r15
movq %rdi, %r14
movl %edx, (%rsi)
testl %ecx, %ecx
jne 0x2011e
cmpl $0x0, 0x43db3(%rip)     # 0x63eac
je 0x20107
leaq 0x32869(%rip), %rdi     # 0x5296b
callq 0x2090
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdi
movl %ebx, %edx
callq 0x438e8
testl %eax, %eax
je 0x20289
movq %r15, -0x48(%rbp)
leaq 0x3286c(%rip), %rdi     # 0x52995
callq 0x2090
movl %ebx, %edi
movl %ebx, %esi
callq 0x4379e
movq %rax, %r12
movq %rsp, -0x40(%rbp)
movl %ebx, %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq 0x30(%r14), %rdi
testq %rdi, %rdi
je 0x2016a
movl %ebx, %esi
callq 0x436f0
movl %ebx, %edi
movl %ebx, %esi
callq 0x4379e
movq %rax, 0x30(%r14)
movq 0x20(%r14), %rsi
pushq $0x1
popq %r8
movl %ebx, %edi
movq -0x38(%rbp), %rdx
movq %r12, %rcx
callq 0x444de
xorl %r15d, %r15d
testl %ebx, %ebx
movl $0x0, %edi
cmovgl %ebx, %edi
xorps %xmm5, %xmm5
movss 0x2ae6b(%rip), %xmm6    # 0x4b010
xorl %esi, %esi
movss -0x2c(%rbp), %xmm4
movq %rdi, -0x50(%rbp)
cmpq %rdi, %r15
je 0x2024c
movq -0x38(%rbp), %rax
movss (%rax,%r15,4), %xmm0
ucomiss %xmm0, %xmm4
jbe 0x201cf
incl %esi
incq %r15
jmp 0x201b0
ucomiss %xmm5, %xmm0
jb 0x201da
sqrtss %xmm0, %xmm0
jmp 0x201f9
movl %esi, -0x30(%rbp)
callq 0x21c0
movss 0x2ae26(%rip), %xmm6    # 0x4b010
xorps %xmm5, %xmm5
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %esi
movss -0x2c(%rbp), %xmm4
movaps %xmm6, %xmm1
divss %xmm0, %xmm1
movl %r15d, %eax
subl %esi, %eax
cltq
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x201ca
ucomiss %xmm5, %xmm0
movq (%r12,%r15,8), %rdx
movss (%rdx,%rcx,4), %xmm2
movaps %xmm0, %xmm3
mulss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm3
movq 0x28(%r14), %rdx
movq (%rdx,%rcx,8), %rdx
movsd %xmm3, (%rdx,%rax,8)
jne 0x20236
jnp 0x20247
mulss %xmm1, %xmm2
movq 0x30(%r14), %rdx
movq (%rdx,%rax,8), %rdx
movss %xmm2, (%rdx,%rcx,4)
incq %rcx
jmp 0x20209
testl %esi, %esi
je 0x20264
leaq 0x323c7(%rip), %rdi     # 0x5261e
movl %esi, %r14d
xorl %eax, %eax
callq 0x20f0
movl %r14d, %esi
movq -0x48(%rbp), %rax
subl %esi, (%rax)
movq %r12, %rdi
movl %ebx, %esi
callq 0x436f0
movq -0x40(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
pushq $0x1
popq %rdx
cmpq %rcx, %rax
je 0x20278
movq 0x28(%r14), %rsi
movq (%rsi,%rax,8), %rsi
incq %rax
movq %rdx, %rdi
cmpl %ebx, %edi
jge 0x202b9
andq $0x0, (%rsi,%rdi,8)
incq %rdi
jmp 0x202ab
incq %rdx
jmp 0x20298