// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/Fit_B_factors.c
// Function: Filter_modes_outliers
int Filter_modes_outliers(struct Normal_Mode NM, struct Reference Ref_kin,
			  float t_out, float t_mode)
{
  int discard_tot=0;
  int i, N_ref=Ref_kin.N_ref;
  float B_TNM[N_ref];
  int outlier[N_ref]; 

  for(int iter=0; iter<10; iter++){
    for(i=0; i<N_ref; i++){
      B_TNM[i]=Compute_correlation(NM.Cart,NM.sigma2,NM.N,i,i);
    }
    for(i=0; i<N_ref; i++)outlier[i]=0;
    int nout=Find_outliers_mean(outlier, B_TNM, N_ref, t_out);
    printf("%d iter %d outliers with fluctuations > %.2f of the mean\n",
	   iter, nout, t_out);
    int discard=0;
    for(int k=0; k<NM.N; k++){
      if((NM.select[k]==0)||(NM.sigma2[k]==0))continue;
      double B_out=0, B_no_out=0;
      for(int j=0; j<3*N_ref; j++){
	float v2=NM.Cart[k][j]; v2*=v2; j++;
	i=j/3; if(outlier[i]){B_out+=v2;}else{B_no_out+=v2;}
      }
      if(B_out>t_mode*(B_out+B_no_out)){
	printf("Discard: mode %d, sigma2= %.3g Collectivity= %.3g ",
	       k, NM.sigma2[k], NM.Cart_coll[k]);
	printf("Outliers= %.3g No outliers= %.3g\n", B_out, B_no_out);
	NM.select[k]=0; NM.sigma2[k]=0; discard++;
      }
    }
    discard_tot+=discard;
    printf("%d iter %d modes discarded for outliers represent > %.2f motion\n",
	   iter, discard_tot, t_mode);
    if(discard==0)break;
  }
  return(discard_tot);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl 0xe0(%rbp), %edx
movslq %edx, %rax
movq %rsp, %rsi
leaq 0xf(,%rdx,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rsi
movq %rsi, -0x70(%rbp)
movq %rsi, %rsp
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
movq 0x48(%rbp), %r12
movq 0x38(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movl 0x10(%rbp), %ecx
movss %xmm0, -0x3c(%rbp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq 0xd0(%rbp), %rsi
leaq (%rax,%rax,2), %rbx
cvtss2sd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
xorl %r14d, %r14d
testl %eax, %eax
movl $0x0, %edi
movq %rdx, -0x80(%rbp)
cmovgl %edx, %edi
testl %ecx, %ecx
movl $0x0, %eax
movl %ecx, -0x40(%rbp)
cmovgl %ecx, %eax
movq %rax, -0x50(%rbp)
movq 0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
pushq $0x3
popq %r13
movl $0x0, -0x2c(%rbp)
movq %rdi, -0x68(%rbp)
movq %rsi, -0x58(%rbp)
cmpl $0xa, %r14d
je 0x27fd4
movq %r14, -0x48(%rbp)
xorl %r14d, %r14d
cmpq %r14, %rdi
je 0x27e52
movq %r12, %rdi
movq -0x38(%rbp), %rsi
movl -0x40(%rbp), %edx
movl %r14d, %ecx
movl %r14d, %r8d
callq 0x276bb
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rax
movss %xmm0, (%rax,%r14,4)
incq %r14
jmp 0x27e25
xorl %eax, %eax
movq -0x48(%rbp), %r14
cmpq %rax, %rdi
je 0x27e67
andl $0x0, (%r15,%rax,4)
incq %rax
jmp 0x27e58
movq %r15, %rdi
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
movss -0x3c(%rbp), %xmm0
callq 0x27b9f
leaq 0x2ba18(%rip), %rdi     # 0x5389b
movl %r14d, %esi
movl %eax, %edx
movsd -0x78(%rbp), %xmm0
movb $0x1, %al
callq 0x20f0
xorl %r14d, %r14d
xorl %r8d, %r8d
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdi
xorps %xmm2, %xmm2
cmpq %rdi, %r14
je 0x27f9c
cmpl $0x0, (%rsi,%r14,4)
je 0x27f94
movq -0x38(%rbp), %rax
movss (%rax,%r14,4), %xmm0
ucomiss %xmm2, %xmm0
jne 0x27ece
jnp 0x27f94
xorpd %xmm3, %xmm3
xorl %ecx, %ecx
xorpd %xmm4, %xmm4
cmpq %rbx, %rcx
jge 0x27f0d
movq (%r12,%r14,8), %rax
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
leal 0x1(%rcx), %eax
xorl %edx, %edx
divl %r13d
cmpl $0x0, (%r15,%rax,4)
cvtss2sd %xmm1, %xmm1
je 0x27f03
addsd %xmm1, %xmm3
jmp 0x27f07
addsd %xmm1, %xmm4
addq $0x2, %rcx
jmp 0x27ed8
movapd %xmm3, %xmm1
addsd %xmm4, %xmm1
mulsd -0x60(%rbp), %xmm1
ucomisd %xmm1, %xmm3
jbe 0x27f94
cvtss2sd %xmm0, %xmm0
movq -0x88(%rbp), %rax
cvtss2sd (%rax,%r14,4), %xmm1
leaq 0x2b99d(%rip), %rdi     # 0x538d5
movl %r14d, %esi
movb $0x2, %al
movl %r8d, -0x30(%rbp)
movsd %xmm3, -0x90(%rbp)
movsd %xmm4, -0x98(%rbp)
callq 0x20f0
leaq 0x2b9ab(%rip), %rdi     # 0x53908
movsd -0x90(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
movb $0x2, %al
callq 0x20f0
movl -0x30(%rbp), %r8d
xorps %xmm2, %xmm2
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
andl $0x0, (%rsi,%r14,4)
movq -0x38(%rbp), %rax
andl $0x0, (%rax,%r14,4)
incl %r8d
incq %r14
jmp 0x27ea5
movl -0x2c(%rbp), %edx
addl %r8d, %edx
leaq 0x2b981(%rip), %rdi     # 0x5392a
movq -0x48(%rbp), %r14
movl %r14d, %esi
movl %edx, -0x2c(%rbp)
movsd -0x60(%rbp), %xmm0
movb $0x1, %al
movl %r8d, -0x30(%rbp)
callq 0x20f0
incl %r14d
cmpl $0x0, -0x30(%rbp)
movq -0x68(%rbp), %rdi
jne 0x27e14
movl -0x2c(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop