// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movl 0xe0(%rbp), %edx
movslq %edx, %rax
movq %rsp, %rsi
leaq 0xf(,%rdx,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rsi
movq %rsi, %rsp
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
movq 0x48(%rbp), %r12
movq 0x38(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movl 0x10(%rbp), %r13d
movss %xmm0, -0x54(%rbp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq 0xd0(%rbp), %rcx
movq %rcx, -0x60(%rbp)
leaq (%rax,%rax,2), %rbx
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq 0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq (,%rdx,4), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rsi, -0x50(%rbp)
movq %r12, -0x68(%rbp)
testl %edx, %edx
jle 0x2aa95
xorl %r12d, %r12d
movq %r13, %r14
movq -0x40(%rbp), %r13
movq -0x68(%rbp), %rdi
movq %r13, %rsi
movl %r14d, %edx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x2a28b
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rdx
movss %xmm0, (%rsi,%r12,4)
incq %r12
cmpq %r12, %rdx
jne 0x2aa4d
testl %edx, %edx
movq %r14, %r13
jle 0x2aa95
movq %r15, %rdi
xorl %esi, %esi
movq -0x70(%rbp), %rdx
callq 0x2110
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq %r15, %rdi
movss -0x54(%rbp), %xmm0
callq 0x2a7b8
leaq 0x2ee92(%rip), %rdi     # 0x5993b
movl -0x30(%rbp), %esi
movl %eax, %edx
movsd -0x78(%rbp), %xmm0
movb $0x1, %al
callq 0x20f0
testl %r13d, %r13d
jle 0x2ac20
xorl %r12d, %r12d
xorl %r14d, %r14d
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdi
movq -0x60(%rbp), %r8
xorps %xmm2, %xmm2
movl $0xaaaaaaab, %r9d       # imm = 0xAAAAAAAB
cmpl $0x0, (%r8,%r12,4)
je 0x2abd1
movss (%rdi,%r12,4), %xmm0
ucomiss %xmm2, %xmm0
jne 0x2aafa
jnp 0x2abd1
testl %esi, %esi
jle 0x2ab44
movq -0x68(%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 %r9, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%r15,%rdx,4)
cvtss2sd %xmm1, %xmm1
je 0x2ab35
addsd %xmm1, %xmm4
jmp 0x2ab39
addsd %xmm1, %xmm3
addq $0x2, %rcx
cmpq %rbx, %rcx
jl 0x2ab10
jmp 0x2ab4c
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
movapd %xmm4, %xmm1
addsd %xmm3, %xmm1
mulsd -0x48(%rbp), %xmm1
ucomisd %xmm1, %xmm4
jbe 0x2abd1
cvtss2sd %xmm0, %xmm0
movq -0x80(%rbp), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax,%r12,4), %xmm1
leaq 0x2edfe(%rip), %rdi     # 0x59975
movl %r12d, %esi
movb $0x2, %al
movsd %xmm3, -0x90(%rbp)
movsd %xmm4, -0x88(%rbp)
callq 0x20f0
leaq 0x2ee10(%rip), %rdi     # 0x599a8
movsd -0x88(%rbp), %xmm0
movsd -0x90(%rbp), %xmm1
movb $0x2, %al
callq 0x20f0
movl $0xaaaaaaab, %r9d       # imm = 0xAAAAAAAB
xorps %xmm2, %xmm2
movq -0x60(%rbp), %r8
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, (%r8,%r12,4)
movl %eax, (%rdi,%r12,4)
incl %r14d
incq %r12
cmpq %r13, %r12
jne 0x2aade
movl -0x2c(%rbp), %edx
addl %r14d, %edx
leaq 0x2ede0(%rip), %rdi     # 0x599ca
movl -0x30(%rbp), %r12d
movl %r12d, %esi
movl %edx, -0x2c(%rbp)
movsd -0x48(%rbp), %xmm0
movb $0x1, %al
callq 0x20f0
testl %r14d, %r14d
je 0x2ac39
incl %r12d
movl %r12d, -0x30(%rbp)
cmpl $0xa, %r12d
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %rsi
jne 0x2aa3f
jmp 0x2ac39
leaq 0x2eda3(%rip), %rdi     # 0x599ca
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
movsd -0x48(%rbp), %xmm0
movb $0x1, %al
callq 0x20f0
movl -0x2c(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop