// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/interactions_tnm.c
// Function: Interactions_min_M
int Interactions_min_M(struct interaction *Int_list, float thr,
		     atom *atoms, int N_atoms, int N_res)
{
  //  For each pair of residues only N_RESRES atoms
  //  with smallest distance interact
  int N_int=0, i1, i2, i1_start=0, i2_start=0;
  atom *atom1=atoms, *atom2;
  float thr2=thr*thr;
  float t_max2=thr+22; t_max2*=t_max2;
  float d, d2;
  int res1, res2, k;

  float *d2_min=malloc(N_RESRES*sizeof(float));
  int *i1_min=malloc(N_RESRES*sizeof(int));
  int *i2_min=malloc(N_RESRES*sizeof(int));

  for(res1=0; res1<N_res; res1++){

    while(atoms[i1_start].res< res1)i1_start++;
    i2_start=i1_start+1;

    for(res2=res1+1; res2<N_res; res2++){
      while(atoms[i2_start].res< res2)i2_start++;
      int n_int=0;

      // Check if contact
      for(i1=i1_start; i1<N_atoms; i1++){
	atom1=atoms+i1; if(atom1->res != res1)break;
	for(i2=i2_start; i2<N_atoms; i2++){
	  atom2=atoms+i2; if(atom2->res != res2)break;

	  // Check if covalent
	  if((NOCOV) && Covalent(res1, res2, atom1, atom2))continue;

	  // Interatomic distance
	  d=atom2->r[0]-atom1->r[0];
	  d2 =d*d; if(d2>thr2){if(d2 > t_max2)goto next2; continue;}
	  d=atom2->r[1]-atom1->r[1];
	  d2+=d*d; if(d2>thr2){if(d2 > t_max2)goto next2; continue;}
	  d=atom2->r[2]-atom1->r[2];
	  d2+=d*d; if(d2>thr2){if(d2 > t_max2)goto next2; continue;}
	  if((n_int<N_RESRES)||(d2 < d2_min[n_int-1])){
	    int j, n;
	    for(k=0; k<n_int; k++)if(d2<d2_min[k])break;
	    if(n_int<N_RESRES){n=n_int-1; n_int++;}else{n=N_RESRES-1;}
	    for(j=n; j>k; j--){
	      d2_min[j]=d2_min[j-1];
	      i1_min[j]=i1_min[j-1];
	      i2_min[j]=i2_min[j-1];
	    }
	    d2_min[k]=d2; i1_min[k]=i1; i2_min[k]=i2;
	  }
	}
      }

      // store interaction
      for(k=0; k<n_int; k++){
	Int_list[N_int].i1=i1_min[k]; Int_list[N_int].i2=i2_min[k];
	Int_list[N_int].r0=sqrt(d2_min[k]); Int_list[N_int].sec_der=1;
	N_int++;
	if(N_int >= INT_MAX){
	  printf("ERROR, too many interactions %d\n", N_int); exit(8);
	}
      }
    next2: continue;
    }
  }
  free(d2_min); free(i1_min); free(i2_min);
  return(N_int);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %r15d
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, 0x70(%rsp)
movaps %xmm0, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x14(%rsp)
addss 0x3a934(%rip), %xmm0    # 0x50dfc
mulss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
leaq 0x4dc13(%rip), %rax     # 0x640ec
movslq (%rax), %r14
shlq $0x2, %r14
movq %r14, %rdi
callq 0x21d0
movq %rax, %r12
movq %r14, %rdi
callq 0x21d0
movq %r14, %rdi
movq %rax, %r14
callq 0x21d0
movss 0x14(%rsp), %xmm2
movq %r13, %r9
movq %rax, %rbp
movslq %ebx, %rax
movq %rax, 0x68(%rsp)
testl %r15d, %r15d
movl $0x0, %eax
movl %r15d, 0x48(%rsp)
cmovgl %r15d, %eax
movl %eax, 0x40(%rsp)
leaq 0x64(%r13), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0x28(%rsp)
xorl %edi, %edi
movl $0x0, 0xc(%rsp)
movq %r13, 0x58(%rsp)
movq %r14, 0x38(%rsp)
cmpl 0x40(%rsp), %edi
je 0x1683c
movslq 0x28(%rsp), %r13
imulq $0x70, %r13, %rax
addq 0x50(%rsp), %rax
incq %r13
leaq 0x70(%rax), %rcx
cmpl %edi, (%rax)
movq %rcx, %rax
jl 0x16565
leal 0x1(%rdi), %ecx
leaq -0x1(%r13), %rax
movq %rax, 0x28(%rsp)
movl %ecx, 0x44(%rsp)
movl %ecx, %esi
movq %rdi, 0x60(%rsp)
cmpl 0x48(%rsp), %esi
jge 0x16831
movslq %r13d, %r13
imulq $0x70, %r13, %rax
decq %r13
addq 0x50(%rsp), %rax
incq %r13
leaq 0x70(%rax), %rcx
cmpl %esi, (%rax)
movq %rcx, %rax
jl 0x165a3
leaq 0x4d048(%rip), %rax     # 0x63600
movl (%rax), %eax
movl %eax, 0x4c(%rsp)
leaq 0x4db27(%rip), %rax     # 0x640ec
movl (%rax), %eax
movl %eax, 0x20(%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %rax
movl %esi, 0x24(%rsp)
cmpq 0x68(%rsp), %rax
jge 0x1677c
imulq $0x70, %rax, %r15
cmpl %edi, 0x64(%r9,%r15)
jne 0x1677c
movq %rax, 0x30(%rsp)
movq %rcx, 0x18(%rsp)
addq %r9, %r15
movq %r13, %rbx
cmpq 0x68(%rsp), %rbx
jge 0x1676a
imulq $0x70, %rbx, %r14
cmpl %esi, 0x64(%r9,%r14)
jne 0x1676a
addq %r9, %r14
cmpl $0x0, 0x4c(%rsp)
je 0x1664b
movq %r15, %rdx
movq %r14, %rcx
callq 0x171db
movl 0x24(%rsp), %esi
movq 0x60(%rsp), %rdi
movss 0x14(%rsp), %xmm2
movq 0x58(%rsp), %r9
testl %eax, %eax
jne 0x16762
movsd (%r14), %xmm0
subsd (%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
ucomiss %xmm2, %xmm0
jbe 0x16672
ucomiss 0x10(%rsp), %xmm0
jbe 0x16762
jmp 0x16825
movsd 0x8(%r14), %xmm1
subsd 0x8(%r15), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x1669f
ucomiss 0x10(%rsp), %xmm1
jbe 0x16762
jmp 0x16825
movsd 0x10(%r14), %xmm0
subsd 0x10(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm2, %xmm0
ja 0x16662
movq 0x18(%rsp), %r11
cmpl 0x20(%rsp), %r11d
jl 0x166db
movslq %r11d, %rax
movss -0x4(%r12,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x16762
testl %r11d, %r11d
movl $0x0, %eax
cmovgl %r11d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x16700
movss (%r12,%rcx,4), %xmm1
ucomiss %xmm0, %xmm1
ja 0x166fe
incq %rcx
jmp 0x166e9
movl %ecx, %eax
movq 0x30(%rsp), %r14
xorl %ecx, %ecx
movl 0x20(%rsp), %edx
cmpl %edx, %r11d
cmovll %r11d, %edx
setl %cl
movslq %edx, %rdx
cltq
decq %rdx
movq 0x38(%rsp), %r8
cmpq %rax, %rdx
jle 0x1674c
movss -0x4(%r12,%rdx,4), %xmm1
movss %xmm1, (%r12,%rdx,4)
movl -0x4(%r8,%rdx,4), %r10d
movl %r10d, (%r8,%rdx,4)
movl -0x4(%rbp,%rdx,4), %r10d
movl %r10d, (%rbp,%rdx,4)
decq %rdx
jmp 0x16722
addl %ecx, %r11d
movq %r11, 0x18(%rsp)
movss %xmm0, (%r12,%rax,4)
movl %r14d, (%r8,%rax,4)
movl %ebx, (%rbp,%rax,4)
incq %rbx
jmp 0x16600
movq 0x30(%rsp), %rax
incq %rax
movq 0x18(%rsp), %rcx
jmp 0x165d6
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movq %rcx, 0x18(%rsp)
movslq 0xc(%rsp), %rbx
imulq $0xc8, %rbx, %r15
addq 0x70(%rsp), %r15
xorl %r14d, %r14d
cmpq %r14, 0x18(%rsp)
je 0x16820
movq 0x38(%rsp), %rax
movl (%rax,%r14,4), %eax
movl %eax, (%r15)
movl (%rbp,%r14,4), %eax
movl %eax, 0x4(%r15)
cvtss2sd (%r12,%r14,4), %xmm0
ucomisd 0x34887(%rip), %xmm0    # 0x4b050
jb 0x167d1
sqrtsd %xmm0, %xmm0
jmp 0x167ea
callq 0x2290
movl 0x24(%rsp), %esi
movq 0x60(%rsp), %rdi
movss 0x14(%rsp), %xmm2
movq 0x58(%rsp), %r9
movsd %xmm0, 0x20(%r15)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r15)
leaq 0x5dad3(%rip), %rax     # 0x742d8
movslq (%rax), %rax
addq $0xc8, %r15
leaq (%rbx,%r14), %rcx
incq %rcx
incq %r14
cmpq %rax, %rcx
jl 0x1679f
jmp 0x16867
addl %r14d, 0xc(%rsp)
incl %esi
movq 0x38(%rsp), %r14
jmp 0x1658a
movl 0x44(%rsp), %eax
movl %eax, %edi
jmp 0x1654d
movq %r12, %rdi
callq 0x2050
movq %r14, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movl 0xc(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %esi
addl %r14d, %esi
leaq 0x3a65e(%rip), %rdi     # 0x50ed3
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270