// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/anharmonic_tnm.c
// Function: Compute_corr_inv
double **Compute_corr_inv(int npara, int nsam)
{
  //C_ab= sum_i=1^4 i^a*i^b = sum_i=1^4 i^(a+b)
  double **Corr=Allocate_mat2_d(npara, npara); int a,b,k;
  for(a=0; a<npara; a++){
    for(b=0; b<=a; b++){
      double C=0;
      for(k=1; k<nsam; k++)C+=pow(k,a+b);
      Corr[a][b]=C; Corr[b][a]=C;
    }
  }
  Corr[0][0]=nsam+1;
  double **Corr_inv=Allocate_mat2_d(npara, npara);
  if(choldc(Corr, Corr, npara)==0){ //
    printf("Inverting correlation through Cholevsky decomposition\n");
    double **L_inv=Cholevsky_inversion(Corr, npara);
    for(k=0; k<npara; k++){
      double *v=L_inv[k];
      for(a=0; a<npara; a++)for(b=0; b<=a; b++)Corr_inv[a][b]+=v[a]*v[b];
    }
    Empty_matrix_d(L_inv, npara);
  }else{
    printf("Inverting correlation through diagonalization\n");
    float **eigen_vector=Allocate_mat2_f(npara, npara), eigen_value[npara];
    d_Diagonalize(npara, Corr, eigen_value, eigen_vector, 1);
    int num=0;
    for(k=0; k<npara; k++){
      if(eigen_value[k]<=0){num++; continue;}
      double t=1./eigen_value[k];
      float *v=eigen_vector[k];
      for(a=0; a<npara; a++){
	for(b=0; b<=a; b++)Corr_inv[a][b]+=t*v[a]*v[b];
      }
    }
    if(num)printf("WARNING, correlation has %d null modes\n", num);
    Empty_matrix_f(eigen_vector, npara);
  }
  for(a=0; a<npara; a++)for(b=0; b<a; b++)Corr_inv[b][a]=Corr_inv[a][b];
  Empty_matrix_d(Corr, npara);
  return(Corr_inv);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, -0x38(%rbp)
movl %edi, %ebx
movl %edi, %esi
callq 0x49804
movq %rax, %r14
movl %ebx, %eax
movq %rax, -0x30(%rbp)
movl %ebx, -0x44(%rbp)
testl %ebx, %ebx
jle 0x1b20c
movq -0x38(%rbp), %rax
decl %eax
movl %eax, -0x48(%rbp)
movl $0x1, %r13d
xorl %ebx, %ebx
movsd 0x35fcd(%rip), %xmm0    # 0x51148
xorl %r12d, %r12d
movq %r14, %r15
xorpd %xmm1, %xmm1
cmpl $0x2, -0x38(%rbp)
jl 0x1b1de
leal (%r12,%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, -0x50(%rbp)
xorpd %xmm1, %xmm1
movl -0x48(%rbp), %r14d
movapd %xmm0, %xmm2
movsd %xmm2, -0x58(%rbp)
movsd %xmm1, -0x40(%rbp)
movapd %xmm2, %xmm0
movsd -0x50(%rbp), %xmm1
callq 0x20a0
movsd -0x58(%rbp), %xmm2
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd 0x35f73(%rip), %xmm0    # 0x51148
addsd %xmm0, %xmm2
decl %r14d
jne 0x1b1a7
movq %r15, %r14
movq (%r15,%rbx,8), %rax
movsd %xmm1, (%rax,%r12,8)
movq (%r15,%r12,8), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %r12
cmpq %r13, %r12
jne 0x1b17e
incq %rbx
incq %r13
cmpq -0x30(%rbp), %rbx
jne 0x1b17b
movq -0x38(%rbp), %rax
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq (%r14), %rax
movsd %xmm0, (%rax)
movl -0x44(%rbp), %ebx
movl %ebx, %edi
movl %ebx, %esi
callq 0x49804
movq %rax, %r15
movq %r14, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x4987c
testl %eax, %eax
je 0x1b33e
leaq 0x3cf82(%rip), %rdi     # 0x581cd
callq 0x2090
movl %ebx, %edi
movl %ebx, %esi
callq 0x4971a
movq %rax, %r12
movq %rsp, -0x40(%rbp)
movq %rsp, %r13
movq -0x30(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movl %ebx, %edi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
movl $0x1, %r8d
callq 0x4a559
testl %ebx, %ebx
jle 0x1b32e
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x35ea3(%rip), %xmm1    # 0x51148
xorl %esi, %esi
movq -0x30(%rbp), %r10
movss (%r13,%rax,4), %xmm2
ucomiss %xmm2, %xmm0
jae 0x1b312
xorps %xmm3, %xmm3
cvtss2sd %xmm2, %xmm3
movapd %xmm1, %xmm2
divsd %xmm3, %xmm2
movq (%r12,%rax,8), %rcx
movl $0x1, %edx
xorl %edi, %edi
xorps %xmm3, %xmm3
cvtss2sd (%rcx,%rdi,4), %xmm3
mulsd %xmm2, %xmm3
movq (%r15,%rdi,8), %r8
xorl %r9d, %r9d
xorps %xmm4, %xmm4
cvtss2sd (%rcx,%r9,4), %xmm4
mulsd %xmm3, %xmm4
addsd (%r8,%r9,8), %xmm4
movsd %xmm4, (%r8,%r9,8)
incq %r9
cmpq %r9, %rdx
jne 0x1b2e4
incq %rdi
incq %rdx
cmpq %r10, %rdi
jne 0x1b2d1
jmp 0x1b314
incl %esi
incq %rax
cmpq %r10, %rax
jne 0x1b2ab
testl %esi, %esi
je 0x1b32e
leaq 0x3ce7e(%rip), %rdi     # 0x581a5
xorl %eax, %eax
callq 0x20f0
movq %r12, %rdi
movl %ebx, %esi
callq 0x49678
movq -0x40(%rbp), %rsp
jmp 0x1b3ac
leaq 0x3ceb6(%rip), %rdi     # 0x581fb
callq 0x2090
movq %r14, %rdi
movl %ebx, %esi
callq 0x49bb8
testl %ebx, %ebx
movq -0x30(%rbp), %r10
jle 0x1b3a2
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movl $0x1, %esi
xorl %edi, %edi
movq (%r15,%rdi,8), %r8
xorl %r9d, %r9d
movsd (%rdx,%rdi,8), %xmm0
mulsd (%rdx,%r9,8), %xmm0
addsd (%r8,%r9,8), %xmm0
movsd %xmm0, (%r8,%r9,8)
incq %r9
cmpq %r9, %rsi
jne 0x1b370
incq %rdi
incq %rsi
cmpq %r10, %rdi
jne 0x1b369
incq %rcx
cmpq %r10, %rcx
jne 0x1b35e
movq %rax, %rdi
movl %ebx, %esi
callq 0x496ae
testl %ebx, %ebx
movq -0x30(%rbp), %rdi
jle 0x1b3df
xorl %eax, %eax
testq %rax, %rax
je 0x1b3d7
movq (%r15,%rax,8), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
movq (%r15,%rdx,8), %rsi
movsd %xmm0, (%rsi,%rax,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1b3c1
incq %rax
cmpq %rdi, %rax
jne 0x1b3b6
movq %r14, %rdi
movl %ebx, %esi
callq 0x496ae
movq %r15, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq