// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/screened_interactions.c
// Function: Protect_interactions
int **Protect_interactions(struct ranked *rank, atom *atoms,
			   int N_atoms, int nres)
{
  int **protect=malloc(N_atoms*sizeof(int *));
  int N_protect=0, i1=0, i;
  int *ii2=malloc(N_atoms*sizeof(int));
  int *i1start=malloc(nres*sizeof(int));
  for(i=0; i<nres; i++)i1start[i]=N_atoms;
  for(i1=0; i1<N_atoms; i1++){
    int n=rank[i1].num;
    protect[i1]=malloc(n*sizeof(int));
    for(i=0; i<n; i++)protect[i1][i]=0;
    if(i1<i1start[atoms[i1].res])i1start[atoms[i1].res]=i1;
    ii2[i1]=0;
  }

  float dmin=100;
  int i1min=-1, imin=-1, res1, res2;
  for(res1=0; res1<nres; res1++){
    for(res2=res1+1; res2<nres; res2++){
      // Find strongest res1-res2 contact
      // Find the next value of res2
      int res2next=nres; i1min=-1;
      for(i1=i1start[res1]; i1<N_atoms; i1++){
	if(atoms[i1].res > res1)break;
	if(atoms[i1].res == res1){
	  struct store *store=rank[i1].first; i=0;
	  while(store!=NULL){
	    int res2atom=atoms[store->i].res;
	    if(res2atom == res2){
	      if(NOCOV && Covalent(res1, res2, atoms+i1, atoms+store->i))
		goto next;
	      if((i1min<0)|| (store->x< dmin)){
		i1min=i1; dmin=store->x; imin=i;
	      }
	    }else if(res2atom > res2){
	      if(res2atom  < res2next)res2next=res2atom;
	      //ii2[i1]=i; break;
	    }
	  next:
	    store=store->next; i++;
	  }
	}
      } // end pair
      if(i1min >=0){protect[i1min][imin]=1; N_protect++;}

      // Prepare next value
      if(res2next<nres)res2=res2next-1;
    }
  }
  printf("%d contacts protected from screening (MIN)\n", N_protect);
  free(ii2); free(i1start);
  return(protect);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %ebx
movq %rsi, 0x28(%rsp)
movq %rdi, 0x58(%rsp)
movl %edx, 0xc(%rsp)
movslq %edx, %rdi
shlq $0x3, %rdi
callq 0x21e0
movq %rax, 0x18(%rsp)
movslq %ebx, %r14
leaq (,%r14,4), %rdi
callq 0x21e0
movq %rax, %rbp
movl %ebx, %eax
movq %rax, 0x40(%rsp)
movq %r14, 0x68(%rsp)
testl %r14d, %r14d
jle 0x1fe44
leal 0x3(%rbx), %eax
movq 0x40(%rsp), %rcx
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
shlq $0x4, %rax
movdqa 0x312a6(%rip), %xmm1    # 0x51010
movdqa 0x312ae(%rip), %xmm2    # 0x51020
xorl %ecx, %ecx
movdqa 0x312b4(%rip), %xmm3    # 0x51030
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x363f4(%rip), %xmm5    # 0x56180
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x1fdc8
movl 0xc(%rsp), %edx
movl %edx, (%rbp,%rcx)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x1fdde
movl 0xc(%rsp), %edx
movl %edx, 0x4(%rbp,%rcx)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edx
testb $0x1, %dl
je 0x1fe1d
movl 0xc(%rsp), %edx
movl %edx, 0x8(%rbp,%rcx)
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x1fe2f
movl 0xc(%rsp), %edx
movl %edx, 0xc(%rbp,%rcx)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x1fd8c
movq %rbx, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
movq %rbp, 0x48(%rsp)
jle 0x1fec1
movl 0xc(%rsp), %r14d
movq 0x28(%rsp), %rax
leaq 0x64(%rax), %r15
xorl %r12d, %r12d
movq 0x58(%rsp), %r13
movslq (%r13), %rbp
leaq (,%rbp,4), %rbx
movq %rbx, %rdi
callq 0x21e0
movq 0x18(%rsp), %rcx
movq %rax, (%rcx,%r12,8)
testq %rbp, %rbp
jle 0x1fe9a
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2110
movslq (%r15), %rax
movq 0x48(%rsp), %rbp
movslq (%rbp,%rax,4), %rcx
cmpq %rcx, %r12
jge 0x1feb1
movl %r12d, (%rbp,%rax,4)
incq %r12
addq $0x20, %r13
addq $0x70, %r15
cmpq %r12, %r14
jne 0x1fe6b
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
jle 0x20078
movss 0x3124a(%rip), %xmm1    # 0x51120
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rdx
leaq 0x1(%r14), %rax
cmpq 0x68(%rsp), %rax
movq %rax, 0x70(%rsp)
jge 0x2005b
movslq (%rbp,%r14,4), %rbx
movl %eax, %r15d
movq %rbx, 0x50(%rsp)
cmpl 0xc(%rsp), %ebx
jge 0x2000e
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %ecx, %ebp
imulq $0x70, %rbx, %rax
movq %rax, 0x60(%rsp)
movslq 0x64(%rdx,%rax), %rax
cmpq %rax, %r14
jl 0x20015
movl %eax, %eax
cmpq %rax, %r14
movq %rbx, 0x30(%rsp)
jne 0x1fff4
movq %rbx, %rax
shlq $0x5, %rax
movq 0x58(%rsp), %rcx
movq 0x10(%rcx,%rax), %r13
testq %r13, %r13
je 0x1fff4
addq %rdx, 0x60(%rsp)
xorl %r12d, %r12d
movslq (%r13), %rax
imulq $0x70, %rax, %rcx
movl 0x64(%rdx,%rcx), %eax
cmpl %r15d, %eax
jne 0x1ffcf
leaq 0x4867f(%rip), %rax     # 0x68600
cmpl $0x0, (%rax)
je 0x1ffb2
addq %rdx, %rcx
movl %r14d, %edi
movl %esi, %ebx
movl %r15d, %esi
movq 0x60(%rsp), %rdx
movss %xmm1, 0x3c(%rsp)
callq 0x18742
movss 0x3c(%rsp), %xmm1
movl %ebx, %esi
movq 0x28(%rsp), %rdx
testl %eax, %eax
jne 0x1ffdc
movss 0x4(%r13), %xmm0
testl %esi, %esi
js 0x1ffc1
ucomiss %xmm0, %xmm1
jbe 0x1ffdc
movq 0x30(%rsp), %rax
movl %eax, %esi
movl %r12d, 0x14(%rsp)
jmp 0x1ffdf
cmpl %ebp, %eax
movl %ebp, %ecx
cmovll %eax, %ecx
cmpl %r15d, %eax
cmovgl %ecx, %ebp
movaps %xmm1, %xmm0
incl %r12d
movq 0x8(%r13), %r13
movaps %xmm0, %xmm1
testq %r13, %r13
jne 0x1ff69
jmp 0x1fff7
movaps %xmm1, %xmm0
movq 0x30(%rsp), %rbx
incq %rbx
movaps %xmm0, %xmm1
cmpl 0xc(%rsp), %ebx
jne 0x1ff20
jmp 0x20018
movl %ecx, %ebp
movaps %xmm1, %xmm0
jmp 0x20041
movaps %xmm1, %xmm0
testl %esi, %esi
js 0x20037
movl %esi, %eax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movslq 0x14(%rsp), %rcx
movl $0x1, (%rax,%rcx,4)
incl 0x10(%rsp)
movq 0x20(%rsp), %rcx
movq 0x50(%rsp), %rbx
leal -0x1(%rbp), %eax
cmpl %ecx, %ebp
cmovll %eax, %r15d
incl %r15d
movaps %xmm0, %xmm1
cmpl %ecx, %r15d
jl 0x1ff0f
jmp 0x2005e
movaps %xmm1, %xmm0
movaps %xmm0, %xmm1
movq 0x70(%rsp), %r14
cmpq 0x40(%rsp), %r14
movq 0x48(%rsp), %rbp
jne 0x1feee
jmp 0x20080
movl $0x0, 0x10(%rsp)
leaq 0x38507(%rip), %rdi     # 0x5858e
movl 0x10(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movq %rbp, %rdi
callq 0x2050
movq 0x18(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq