// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
movl %r8d, %r15d
movq %rdi, 0x18(%rsp)
andl $0x0, (%rdi)
movl %ecx, %r12d
movq %rdx, 0x58(%rsp)
movl 0xa0(%rsp), %eax
movq 0xd8(%rsp), %rbx
movq 0xc0(%rsp), %r14
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x120(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq 0xe0(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movq 0x110(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movq 0xe8(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movq 0x118(%rsp), %rcx
movq %rcx, 0x38(%rsp)
cvtsi2ss %r8d, %xmm0
movss %xmm0, 0x14(%rsp)
movq 0x108(%rsp), %r13
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rsp)
testl %eax, %eax
movl $0x0, %ecx
movl %eax, 0x10(%rsp)
cmovgl %eax, %ecx
movq %rcx, 0x60(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x4(%rsp)
cmpq %rbp, 0x60(%rsp)
je 0x93e8
movq (%rbx,%rbp,8), %rdi
movss (%r14,%rbp,4), %xmm0
movl %r15d, %esi
movq 0x58(%rsp), %rdx
callq 0x26b01
movq 0x28(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movl %r12d, %esi
callq 0x100f2
divss 0xc(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movq 0x40(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movl %r12d, %esi
callq 0x100f2
divss 0xc(%rsp), %xmm0
movq 0x38(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movq (%rbx,%rbp,8), %rdi
movl %r15d, %esi
callq 0x100f2
movss %xmm0, (%r13,%rbp,4)
movss (%r14,%rbp,4), %xmm1
cvtss2sd %xmm1, %xmm2
movsd 0x5badd(%rip), %xmm3    # 0x64e18
ucomisd %xmm2, %xmm3
jae 0x9366
movss 0x8(%rsp), %xmm2
ucomiss %xmm0, %xmm2
ja 0x9366
movq 0x20(%rsp), %rax
movl $0x1, (%rax,%rbp,4)
movss 0x41cb0(%rip), %xmm0    # 0x4b010
divss %xmm1, %xmm0
jmp 0x93c2
leaq 0x45b02(%rip), %rdi     # 0x4ee6f
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
cvtss2sd (%r14,%rbp,4), %xmm0
cvtss2sd (%r13,%rbp,4), %xmm1
movq 0x28(%rsp), %rax
cvtss2sd (%rax,%rbp,4), %xmm2
leaq 0x45af8(%rip), %rdi     # 0x4ee8c
movb $0x3, %al
callq 0x20f0
movss 0x8(%rsp), %xmm0
ucomiss (%r13,%rbp,4), %xmm0
jbe 0x93af
incl 0x4(%rsp)
jmp 0x93b6
movq 0x18(%rsp), %rax
incl (%rax)
movq 0x20(%rsp), %rax
andl $0x0, (%rax,%rbp,4)
xorps %xmm0, %xmm0
movq 0x30(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movss (%r13,%rbp,4), %xmm0
divss 0x14(%rsp), %xmm0
movss %xmm0, (%r13,%rbp,4)
incq %rbp
jmp 0x92a8
movq 0x18(%rsp), %r15
movl (%r15), %eax
movl 0x4(%rsp), %ebx
addl %ebx, %eax
movl 0x10(%rsp), %esi
subl %eax, %esi
leaq 0x45aaf(%rip), %rdi     # 0x4eeb2
xorl %eax, %eax
callq 0x20f0
leaq 0x45ab4(%rip), %rdi     # 0x4eec5
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
movl (%r15), %esi
leaq 0x45aca(%rip), %rdi     # 0x4eeee
xorl %eax, %eax
callq 0x20f0
pushq $-0x1
popq %rsi
xorps %xmm0, %xmm0
movss (%r14), %xmm1
incl %esi
addq $0x4, %r14
ucomiss %xmm1, %xmm0
ja 0x9431
cvtss2sd %xmm1, %xmm0
leaq 0x45abc(%rip), %rdi     # 0x4ef08
movb $0x1, %al
callq 0x20f0
cvtss2sd 0x5ac99(%rip), %xmm0    # 0x640f4
leaq 0x45acf(%rip), %rdi     # 0x4ef31
movb $0x1, %al
callq 0x20f0
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq