// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movl %ecx, %ebp
movl %edx, %ebx
movq %rsi, 0x30(%rsp)
movq %rdi, 0x48(%rsp)
movslq %edx, %r13
leaq (,%r13,8), %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
movslq %ebp, %r15
leaq (,%r15,4), %rdi
callq 0x21d0
movq %rax, 0x10(%rsp)
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %ebp, %ecx
movq %rcx, 0x40(%rsp)
movq 0x10(%rsp), %rcx
cmpq %rax, 0x40(%rsp)
je 0x1e1b0
movl %ebx, (%rcx,%rax,4)
incq %rax
jmp 0x1e1a1
xorl %r15d, %r15d
testl %ebx, %ebx
cmovlel %r15d, %ebx
movq 0x30(%rsp), %rsi
cmpq %rbx, %r15
je 0x1e22e
movq %r15, %rax
shlq $0x5, %rax
movq 0x48(%rsp), %rcx
movslq (%rcx,%rax), %r12
movq %r12, %rdi
shlq $0x2, %rdi
callq 0x21d0
movq 0x30(%rsp), %rsi
movq 0x20(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x1e209
andl $0x0, (%rax,%rdx,4)
incq %rdx
jmp 0x1e1fb
imulq $0x70, %r15, %rax
movslq 0x64(%rsi,%rax), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
cmpq %rcx, %r15
jge 0x1e229
movq 0x10(%rsp), %rcx
movl %r15d, (%rcx,%rax,4)
incq %r15
jmp 0x1e1be
xorl %r14d, %r14d
pushq $-0x1
popq %rax
movss 0x2cdd8(%rip), %xmm1    # 0x4b014
movl %eax, 0xc(%rsp)
movl $0x0, 0x1c(%rsp)
movl %ebp, 0x38(%rsp)
movq %r13, 0x60(%rsp)
cmpq 0x40(%rsp), %r14
je 0x1e3a7
leaq 0x1(%r14), %rax
movq %rax, 0x58(%rsp)
movl %eax, %r12d
cmpl %ebp, %r12d
jge 0x1e39d
movq 0x10(%rsp), %rax
movslq (%rax,%r14,4), %rcx
pushq $-0x1
popq %rbx
cmpq %r13, %rcx
jge 0x1e367
imulq $0x70, %rcx, %rax
movq %rax, 0x50(%rsp)
movslq 0x64(%rsi,%rax), %rax
cmpq %rax, %r14
jl 0x1e367
movl %eax, %eax
cmpq %rax, %r14
jne 0x1e35f
addq %rsi, 0x50(%rsp)
movq %rcx, 0x28(%rsp)
movq %rcx, %rax
shlq $0x5, %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax), %r15
addq $0x10, %r15
xorl %r13d, %r13d
movq (%r15), %r15
testq %r15, %r15
je 0x1e355
movslq (%r15), %rax
imulq $0x70, %rax, %rcx
movl 0x64(%rsi,%rcx), %eax
cmpl %r12d, %eax
jne 0x1e336
leaq 0x45314(%rip), %rax     # 0x63600
cmpl $0x0, (%rax)
je 0x1e319
addq %rsi, %rcx
movl %r14d, %edi
movl %r12d, %esi
movq 0x50(%rsp), %rdx
movss %xmm1, 0x3c(%rsp)
callq 0x171db
movss 0x3c(%rsp), %xmm1
movq 0x30(%rsp), %rsi
testl %eax, %eax
jne 0x1e343
movss 0x4(%r15), %xmm0
testl %ebx, %ebx
js 0x1e328
ucomiss %xmm0, %xmm1
jbe 0x1e343
movq 0x28(%rsp), %rax
movl %eax, %ebx
movl %r13d, 0xc(%rsp)
jmp 0x1e346
cmpl %ebp, %eax
movl %ebp, %ecx
cmovll %eax, %ecx
cmpl %r12d, %eax
cmovgl %ecx, %ebp
movaps %xmm1, %xmm0
addq $0x8, %r15
incl %r13d
movaps %xmm0, %xmm1
jmp 0x1e2c9
movq 0x60(%rsp), %r13
movq 0x28(%rsp), %rcx
incq %rcx
jmp 0x1e27d
testl %ebx, %ebx
js 0x1e386
movl %ebx, %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movslq 0xc(%rsp), %rcx
movl $0x1, (%rax,%rcx,4)
incl 0x1c(%rsp)
leal -0x1(%rbp), %eax
movl 0x38(%rsp), %ecx
cmpl %ecx, %ebp
movl %ecx, %ebp
cmovll %eax, %r12d
incl %r12d
jmp 0x1e268
movq 0x58(%rsp), %r14
jmp 0x1e251
leaq 0x34140(%rip), %rdi     # 0x524ee
movl 0x1c(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %rdi
callq 0x2050
movq 0x20(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq