// File: /workspace/github_c_makefile_star2_O3/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)
movq %rdi, %r14
leaq 0xa0(%rsp), %rax
movl $0x0, (%rdi)
movslq (%rax), %rcx
movq %rcx, 0x28(%rsp)
testq %rcx, %rcx
jle 0x98d3
movl %r8d, %ebp
movq %r14, 0x18(%rsp)
movq 0x38(%rax), %r13
movq 0x20(%rax), %rbx
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 (%r13,%r14,8), %rdi
movss (%rbx,%r14,4), %xmm0
movl %ebp, %esi
movq 0x60(%rsp), %rdx
callq 0x2960d
movq 0x30(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdi
movl %r12d, %esi
callq 0x10bf3
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 0x10bf3
divss 0x10(%rsp), %xmm0
movq 0x38(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movq (%r13,%r14,8), %rdi
movl %ebp, %esi
callq 0x10bf3
movss %xmm0, (%r15,%r14,4)
movss (%rbx,%r14,4), %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm1, %xmm2
movsd 0x60613(%rip), %xmm3    # 0x69e18
ucomisd %xmm2, %xmm3
jae 0x9831
movss 0xc(%rsp), %xmm2
ucomiss %xmm0, %xmm2
ja 0x9831
movq 0x20(%rsp), %rax
movl $0x1, (%rax,%r14,4)
movss 0x478f1(%rip), %xmm0    # 0x5111c
divss %xmm1, %xmm0
jmp 0x989a
leaq 0x4b6df(%rip), %rdi     # 0x54f17
movl %r14d, %esi
xorl %eax, %eax
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd (%rbx,%r14,4), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r15,%r14,4), %xmm1
movq 0x30(%rsp), %rax
xorps %xmm2, %xmm2
cvtss2sd (%rax,%r14,4), %xmm2
leaq 0x4b6cb(%rip), %rdi     # 0x54f34
movb $0x3, %al
callq 0x20f0
movss 0xc(%rsp), %xmm0
ucomiss (%r15,%r14,4), %xmm0
jbe 0x9883
incl 0x8(%rsp)
jmp 0x988a
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 0x9778
movq 0x18(%rsp), %r14
movl (%r14), %eax
movl 0x8(%rsp), %ebp
jmp 0x98db
movq 0x20(%rax), %rbx
xorl %eax, %eax
xorl %ebp, %ebp
addl %ebp, %eax
movq 0x28(%rsp), %rsi
subl %eax, %esi
leaq 0x4b66f(%rip), %rdi     # 0x54f5a
xorl %eax, %eax
callq 0x20f0
leaq 0x4b674(%rip), %rdi     # 0x54f6d
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
movl (%r14), %esi
leaq 0x4b68a(%rip), %rdi     # 0x54f96
xorl %eax, %eax
callq 0x20f0
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movss (%rbx), %xmm1
incl %esi
addq $0x4, %rbx
ucomiss %xmm1, %xmm0
ja 0x991b
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x4b678(%rip), %rdi     # 0x54fb0
movb $0x1, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x5f7aa(%rip), %xmm0    # 0x690f4
leaq 0x4b688(%rip), %rdi     # 0x54fd9
movb $0x1, %al
callq 0x20f0
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq