// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/main_tnm.c
// Function: Compute_sigma2
int Compute_sigma2(int *N_disc_freq, int *N_disc_out,
		   struct Normal_Mode NM,
		   float *mass_sum_sqrt, int naxe1, int N_Cart,
		   float COLL_THR)
{
  int N_disc_coll=0; *N_disc_freq=0;

  for(int ia=0; ia<NM.N; ia++){

    NM.Max_dev[ia]=
      Compute_Max_dev(NM.Cart[ia], NM.omega2[ia], N_Cart, mass_sum_sqrt);
    NM.Tors_coll[ia]=Collectivity_norm2(NM.Tors[ia], naxe1)/naxe1;
    NM.MW_Tors_coll[ia]=Collectivity_norm2(NM.MW_Tors[ia],naxe1)/naxe1;
    NM.Cart_coll[ia]=Collectivity_norm2(NM.Cart[ia], N_Cart); //_Renyi
    if((NM.omega2[ia]<=E_THR)||(NM.Cart_coll[ia] < COLL_THR)){
      printf("WARNING, discarding mode %d ", ia);
      printf(" omega^2= %.3g Coll= %.0f RMSD= %.2g\n",
	     NM.omega2[ia], NM.Cart_coll[ia], NM.Max_dev[ia]);
      if(NM.Cart_coll[ia] < COLL_THR){N_disc_coll++;}
      else{(*N_disc_freq)++;}
      NM.select[ia]=0; NM.sigma2[ia]=0;
    }else{
      NM.select[ia]=1;
      NM.sigma2[ia]=1./NM.omega2[ia];
    }
    NM.Cart_coll[ia]/=N_Cart;
  }
  printf("%d modes selected\n", NM.N-(N_disc_coll+*N_disc_freq));
  printf("%d modes discarded for low collectivity ", N_disc_coll);
  printf("and %d for low frequency\n", *N_disc_freq);
  int amin=0; while(NM.omega2[amin]<0)amin++;
  printf("Smallest positive eigenvalue: %.2g a=%d ",NM.omega2[amin],amin);
  printf("Threshold for selection: %.2g*<omega^2>\n", E_MIN);

  // Ugo_mod 05/21
  /* float thr_outlier=2.5, thr_mode=0.5;
     printf("Remove modes that move > %.2f outlier dof (rmsf > %.1f)\n",
     thr_mode, thr_outlier);
     *N_disc_out=Filter_modes_outliers(NM, Ref_kin, thr_outlier, thr_mode);
     printf("%d modes removed because they mostly move outliers\n",
     *N_disc_out);*/

  return(N_disc_coll);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movss %xmm0, 0xc(%rsp)
movl %ecx, %r12d
movq %rdx, 0x60(%rsp)
leaq 0xa0(%rsp), %rax
movq %rdi, 0x18(%rsp)
movl $0x0, (%rdi)
movslq (%rax), %rcx
movq %rcx, 0x28(%rsp)
testq %rcx, %rcx
jle 0x98e0
movl %r8d, %ebp
movq 0x38(%rax), %rbx
movq 0x20(%rax), %r13
movq 0x28(%rax), %rcx
movq %rcx, 0x58(%rsp)
movq 0x80(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq 0x40(%rax), %rcx
movq %rcx, 0x50(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %xmm0
movss %xmm0, 0x10(%rsp)
movq 0x70(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x48(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x78(%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x68(%rax), %r15
movq 0xc0(%rax), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %r8d, %xmm0
movss %xmm0, 0x14(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x8(%rsp)
movq (%rbx,%r14,8), %rdi
movss (%r13,%r14,4), %xmm0
movl %ebp, %esi
movq 0x60(%rsp), %rdx
callq 0x29957
movq 0x30(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdi
movl %r12d, %esi
callq 0x110b6
divss 0x10(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %rdi
movl %r12d, %esi
callq 0x110b6
divss 0x10(%rsp), %xmm0
movq 0x38(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movq (%rbx,%r14,8), %rdi
movl %ebp, %esi
callq 0x110b6
movss %xmm0, (%r15,%r14,4)
movss (%r13,%r14,4), %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm1, %xmm2
movsd 0x605fb(%rip), %xmm3    # 0x69e18
ucomisd %xmm2, %xmm3
jae 0x9849
movss 0xc(%rsp), %xmm2
ucomiss %xmm0, %xmm2
ja 0x9849
movq 0x20(%rsp), %rax
movl $0x1, (%rax,%r14,4)
movss 0x477cd(%rip), %xmm0    # 0x51010
divss %xmm1, %xmm0
jmp 0x98b3
leaq 0x4b69b(%rip), %rdi     # 0x54eeb
movl %r14d, %esi
xorl %eax, %eax
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd (%r13,%r14,4), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r15,%r14,4), %xmm1
movq 0x30(%rsp), %rax
xorps %xmm2, %xmm2
cvtss2sd (%rax,%r14,4), %xmm2
leaq 0x4b686(%rip), %rdi     # 0x54f08
movb $0x3, %al
callq 0x20f0
movss 0xc(%rsp), %xmm0
ucomiss (%r15,%r14,4), %xmm0
jbe 0x989c
incl 0x8(%rsp)
jmp 0x98a3
movq 0x18(%rsp), %rax
incl (%rax)
movq 0x20(%rsp), %rax
movl $0x0, (%rax,%r14,4)
xorps %xmm0, %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movss (%r15,%r14,4), %xmm0
divss 0x14(%rsp), %xmm0
movss %xmm0, (%r15,%r14,4)
incq %r14
cmpq %r14, 0x28(%rsp)
jne 0x9790
jmp 0x98e8
movl $0x0, 0x8(%rsp)
movq 0x18(%rsp), %r14
movl (%r14), %eax
movl 0x8(%rsp), %ebx
addl %ebx, %eax
movq 0x28(%rsp), %rsi
subl %eax, %esi
leaq 0x4b62a(%rip), %rdi     # 0x54f2e
xorl %eax, %eax
callq 0x20f0
leaq 0x4b62f(%rip), %rdi     # 0x54f41
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
movl (%r14), %esi
leaq 0x4b645(%rip), %rdi     # 0x54f6a
xorl %eax, %eax
callq 0x20f0
leaq 0xa0(%rsp), %rax
movq 0x20(%rax), %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movss (%rax), %xmm1
incl %esi
addq $0x4, %rax
ucomiss %xmm1, %xmm0
ja 0x9940
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x4b627(%rip), %rdi     # 0x54f84
movb $0x1, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x5f785(%rip), %xmm0    # 0x690f4
leaq 0x4b637(%rip), %rdi     # 0x54fad
movb $0x1, %al
callq 0x20f0
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq