// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rsi, 0x30(%rsp)
movq %rdi, 0x48(%rsp)
movslq %edx, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, 0x28(%rsp)
movslq %ebp, %rbx
leaq (,%rbx,4), %rdi
callq 0x21d0
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
testl %ebx, %ebx
jle 0x20495
xorl %ecx, %ecx
movl %r14d, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x20489
movq %rdx, 0x38(%rsp)
movl %r14d, 0x14(%rsp)
testl %r14d, %r14d
movq %rax, 0x20(%rsp)
jle 0x20513
movl 0x14(%rsp), %r14d
movq 0x30(%rsp), %rax
leaq 0x64(%rax), %r15
xorl %r12d, %r12d
movq 0x48(%rsp), %r13
movslq (%r13), %rbp
leaq (,%rbp,4), %rbx
movq %rbx, %rdi
callq 0x21d0
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%r12,8)
testq %rbp, %rbp
jle 0x204ee
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2110
movslq (%r15), %rax
movq 0x20(%rsp), %rdx
movslq (%rdx,%rax,4), %rcx
cmpq %rcx, %r12
jge 0x20503
movl %r12d, (%rdx,%rax,4)
incq %r12
addq $0x20, %r13
addq $0x70, %r15
cmpq %r12, %r14
jne 0x204bf
cmpl $0x0, 0xc(%rsp)
jle 0x206c4
movss 0x30aee(%rip), %xmm0    # 0x51014
movss %xmm0, 0x1c(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movl $0x0, 0x10(%rsp)
movl 0x14(%rsp), %edx
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rax
leaq 0x1(%rbp), %rcx
cmpq %rax, %rcx
movq %rcx, 0x60(%rsp)
jae 0x206af
movq 0x20(%rsp), %rax
movslq (%rax,%rbp,4), %rbx
movl %ecx, %r15d
movq %rbx, 0x40(%rsp)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpl %edx, %ebx
jge 0x20669
movl 0xc(%rsp), %eax
movl %eax, %r14d
imulq $0x70, %rbx, %rax
movq %rax, 0x58(%rsp)
movslq 0x64(%rsi,%rax), %rax
cmpq %rax, %rbp
jl 0x20670
movl %eax, %eax
cmpq %rax, %rbp
movq %rbx, 0x50(%rsp)
jne 0x20653
movq %rbx, %rax
shlq $0x5, %rax
movq 0x48(%rsp), %rcx
movq 0x10(%rcx,%rax), %r13
testq %r13, %r13
je 0x20653
addq %rsi, 0x58(%rsp)
xorl %r12d, %r12d
movslq (%r13), %rax
imulq $0x70, %rax, %rcx
movl 0x64(%rsi,%rcx), %eax
cmpl %r15d, %eax
jne 0x20633
leaq 0x4801c(%rip), %rax     # 0x68600
cmpl $0x0, (%rax)
je 0x20608
addq %rsi, %rcx
movl %edi, %ebx
movl %ebp, %edi
movl %r15d, %esi
movq 0x58(%rsp), %rdx
callq 0x18e79
movl %ebx, %edi
movq 0x30(%rsp), %rsi
testl %eax, %eax
jne 0x20643
testl %edi, %edi
js 0x20619
movss 0x1c(%rsp), %xmm0
ucomiss 0x4(%r13), %xmm0
jbe 0x20643
movss 0x4(%r13), %xmm0
movss %xmm0, 0x1c(%rsp)
movq 0x50(%rsp), %rax
movl %eax, %edi
movl %r12d, 0x18(%rsp)
jmp 0x20643
cmpl %r14d, %eax
movl %r14d, %ecx
cmovll %eax, %ecx
cmpl %r15d, %eax
cmovgl %ecx, %r14d
incl %r12d
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x205cc
movq 0x50(%rsp), %rbx
incq %rbx
movl 0x14(%rsp), %edx
cmpl %edx, %ebx
jne 0x20583
jmp 0x20670
movl 0xc(%rsp), %eax
movl %eax, %r14d
testl %edi, %edi
js 0x2068f
movl %edi, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movslq 0x18(%rsp), %rcx
movl $0x1, (%rax,%rcx,4)
incl 0x10(%rsp)
movq 0x40(%rsp), %rbx
leal -0x1(%r14), %eax
movl 0xc(%rsp), %ecx
cmpl %ecx, %r14d
cmovll %eax, %r15d
incl %r15d
cmpl %ecx, %r15d
jl 0x2056f
movq 0x38(%rsp), %rax
movq 0x60(%rsp), %rbp
cmpq %rax, %rbp
jne 0x2054c
jmp 0x206cc
movl $0x0, 0x10(%rsp)
leaq 0x37efb(%rip), %rdi     # 0x585ce
movl 0x10(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movq 0x20(%rsp), %rdi
callq 0x2050
movq 0x28(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq