// File: /workspace/github_c_makefile_star2_O1/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, -0x68(%rbp)
movq %rsi, %rsp
movq %rsp, %r14
subq %rcx, %r14
movq %r14, %rsp
movq 0x48(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq 0x38(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movl 0x10(%rbp), %r15d
movss %xmm0, -0x44(%rbp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movq 0xd0(%rbp), %rcx
movq %rcx, -0x50(%rbp)
leaq (%rax,%rax,2), %rbx
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movq 0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq (,%rdx,4), %rax
movq %rax, -0x70(%rbp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rdx, -0x38(%rbp)
movq %r14, -0x88(%rbp)
movq %r15, -0x90(%rbp)
movl %r12d, -0x2c(%rbp)
movl %r13d, -0x48(%rbp)
testl %edx, %edx
movq -0x78(%rbp), %r13
movq -0x58(%rbp), %r14
jle 0x2ae1c
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x2a5eb
movq -0x38(%rbp), %rdx
movq -0x68(%rbp), %rax
movss %xmm0, (%rax,%r12,4)
incq %r12
cmpq %r12, %rdx
jne 0x2adf2
testl %edx, %edx
movq -0x88(%rbp), %r14
jle 0x2ae39
movq %r14, %rdi
xorl %esi, %esi
movq -0x70(%rbp), %rdx
callq 0x2110
movq -0x38(%rbp), %rdx
movq %r14, %rdi
movq -0x68(%rbp), %rsi
movss -0x44(%rbp), %xmm0
callq 0x2ab3f
leaq 0x2eb42(%rip), %rdi     # 0x59993
movl -0x2c(%rbp), %esi
movl %eax, %edx
movsd -0x80(%rbp), %xmm0
movb $0x1, %al
callq 0x20f0
testl %r15d, %r15d
jle 0x2af9c
xorl %r12d, %r12d
xorl %r10d, %r10d
movq -0x38(%rbp), %r9
movq -0x50(%rbp), %rdi
xorps %xmm2, %xmm2
movl $0xaaaaaaab, %r8d       # imm = 0xAAAAAAAB
movl -0x2c(%rbp), %esi
cmpl $0x0, (%rdi,%r12,4)
je 0x2af8e
movss (%r13,%r12,4), %xmm0
ucomiss %xmm2, %xmm0
jne 0x2aea2
jnp 0x2af8e
testl %r9d, %r9d
jle 0x2aeed
movq -0x58(%rbp), %rax
movq (%rax,%r12,8), %rax
xorpd %xmm3, %xmm3
xorl %ecx, %ecx
xorpd %xmm4, %xmm4
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
leal 0x1(%rcx), %edx
imulq %r8, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%r14,%rdx,4)
cvtss2sd %xmm1, %xmm1
je 0x2aede
addsd %xmm1, %xmm4
jmp 0x2aee2
addsd %xmm1, %xmm3
addq $0x2, %rcx
cmpq %rbx, %rcx
jl 0x2aeb9
jmp 0x2aef5
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
movapd %xmm4, %xmm1
addsd %xmm3, %xmm1
mulsd -0x60(%rbp), %xmm1
ucomisd %xmm1, %xmm4
jbe 0x2af8e
cvtss2sd %xmm0, %xmm0
movq -0x98(%rbp), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax,%r12,4), %xmm1
leaq 0x2eaa6(%rip), %rdi     # 0x599cd
movl %r12d, %esi
movb $0x2, %al
movq %r13, %r15
movl %r10d, %r13d
movsd %xmm3, -0x40(%rbp)
movsd %xmm4, -0xa0(%rbp)
callq 0x20f0
leaq 0x2eab5(%rip), %rdi     # 0x59a00
movsd -0xa0(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movb $0x2, %al
callq 0x20f0
movl %r13d, %r10d
movq %r15, %r13
movq -0x90(%rbp), %r15
movl -0x2c(%rbp), %esi
movl $0xaaaaaaab, %r8d       # imm = 0xAAAAAAAB
xorps %xmm2, %xmm2
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %r9
xorl %eax, %eax
movl %eax, (%rdi,%r12,4)
movl %eax, (%r13,%r12,4)
incl %r10d
incq %r12
cmpq %r15, %r12
jne 0x2ae85
jmp 0x2afa2
xorl %r10d, %r10d
movl -0x2c(%rbp), %esi
movl %r10d, -0x40(%rbp)
movl -0x48(%rbp), %r13d
addl %r10d, %r13d
leaq 0x2ea6e(%rip), %rdi     # 0x59a22
movl %r13d, %edx
movsd -0x60(%rbp), %xmm0
movb $0x1, %al
movl %esi, %r12d
callq 0x20f0
movq -0x38(%rbp), %rdx
cmpl $0x0, -0x40(%rbp)
je 0x2afdd
incl %r12d
cmpl $0xa, %r12d
jne 0x2addb
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop