// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movl %edx, %r14d
movq %rsi, %r15
movss %xmm0, 0xc(%rsp)
movq %rdi, 0x70(%rsp)
leaq 0x510c6(%rip), %rax     # 0x690ec
movslq (%rax), %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x21d0
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
callq 0x21d0
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
testl %r12d, %r12d
jle 0x183f4
movss 0xc(%rsp), %xmm2
movaps %xmm2, %xmm3
mulss %xmm2, %xmm3
addss 0x3ee7a(%rip), %xmm2    # 0x56eec
mulss %xmm2, %xmm2
movslq %r14d, %rax
movq %rax, 0x68(%rsp)
movq %r15, 0x38(%rsp)
leaq 0x64(%r15), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
movq $0x0, 0x30(%rsp)
movl %r14d, 0x4(%rsp)
movss %xmm2, 0xc(%rsp)
movss %xmm3, 0x1c(%rsp)
movl %r12d, (%rsp)
movl %eax, %edx
movslq 0x30(%rsp), %r15
leal -0x1(%r15), %esi
imulq $0x70, %r15, %rax
addq 0x58(%rsp), %rax
incq %r15
incl %esi
leaq 0x70(%rax), %rcx
cmpl %edx, (%rax)
movq %rcx, %rax
jl 0x180c9
leal 0x1(%rdx), %ebp
leaq -0x1(%r15), %rax
movq %rax, 0x30(%rsp)
cmpl %r12d, %ebp
movl %ebp, 0x4c(%rsp)
jge 0x183e5
movq %rdx, 0x60(%rsp)
movl %esi, 0x50(%rsp)
movslq %r15d, %r15
imulq $0x70, %r15, %rax
decq %r15
addq 0x58(%rsp), %rax
incq %r15
leaq 0x70(%rax), %rcx
cmpl %ebp, (%rax)
movq %rcx, %rax
jl 0x1810a
movl $0x0, 0x8(%rsp)
cmpl %r14d, %esi
movq 0x38(%rsp), %rcx
jge 0x18303
leaq 0x504cb(%rip), %rax     # 0x68600
movl (%rax), %eax
movl %eax, 0x54(%rsp)
leaq 0x50faa(%rip), %rax     # 0x690ec
movl (%rax), %eax
movl %eax, 0x18(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x30(%rsp), %r12
imulq $0x70, %r12, %rbx
movq 0x60(%rsp), %rax
cmpl %eax, 0x64(%rcx,%rbx)
jne 0x18303
cmpl %r14d, %r15d
jge 0x182f0
addq %rcx, %rbx
movq %r15, %r13
imulq $0x70, %r13, %r14
cmpl %ebp, 0x64(%rcx,%r14)
jne 0x182f0
addq %rcx, %r14
cmpl $0x0, 0x54(%rsp)
je 0x181bb
movq 0x60(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x18e79
movss 0x1c(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
movq 0x38(%rsp), %rcx
testl %eax, %eax
jne 0x182e2
movsd (%r14), %xmm0
subsd (%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
ucomiss %xmm3, %xmm0
jbe 0x181df
ucomiss %xmm2, %xmm0
jbe 0x182e2
jmp 0x183cd
movsd 0x8(%r14), %xmm1
subsd 0x8(%rbx), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
ucomiss %xmm3, %xmm1
jbe 0x18209
ucomiss %xmm2, %xmm1
jbe 0x182e2
jmp 0x183cd
movsd 0x10(%r14), %xmm0
subsd 0x10(%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm3, %xmm0
ja 0x181d1
movl 0x8(%rsp), %edx
cmpl 0x18(%rsp), %edx
movq 0x40(%rsp), %r10
jl 0x18247
movslq %edx, %rax
movss -0x4(%r10,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x182e2
testl %edx, %edx
jle 0x18270
movl %edx, %ecx
xorl %eax, %eax
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %r14
movss (%r10,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
ja 0x1827c
incq %rax
cmpq %rax, %rcx
jne 0x18259
movl %ecx, %eax
jmp 0x1827c
xorl %eax, %eax
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %r14
xorl %ecx, %ecx
movl 0x8(%rsp), %edi
movl 0x18(%rsp), %esi
cmpl %esi, %edi
setl %dl
cmovll %edi, %esi
leal -0x1(%rsi), %edi
cmpl %eax, %edi
jle 0x182c7
movslq %edi, %rdi
movslq %eax, %r8
addl $-0x2, %esi
movslq %esi, %rsi
movss (%r10,%rsi,4), %xmm1
movss %xmm1, (%r10,%rdi,4)
movl (%r11,%rsi,4), %r9d
movl %r9d, (%r11,%rdi,4)
movl (%r14,%rsi,4), %r9d
movl %r9d, (%r14,%rdi,4)
decq %rdi
decl %esi
cmpq %r8, %rdi
jg 0x1829e
movb %dl, %cl
addl %ecx, 0x8(%rsp)
movl %eax, %eax
movss %xmm0, (%r10,%rax,4)
movl %r12d, (%r11,%rax,4)
movl %r13d, (%r14,%rax,4)
movq 0x38(%rsp), %rcx
incq %r13
cmpq 0x68(%rsp), %r13
jl 0x18177
incq %r12
cmpq 0x68(%rsp), %r12
movl 0x4(%rsp), %r14d
jl 0x18155
movl 0x8(%rsp), %r12d
testl %r12d, %r12d
jle 0x183d2
movslq 0x10(%rsp), %rbx
movl %r12d, %r14d
imulq $0xc8, %rbx, %r12
addq 0x70(%rsp), %r12
incq %rbx
xorl %r13d, %r13d
movq 0x28(%rsp), %rax
movl (%rax,%r13,4), %eax
movl %eax, (%r12)
movq 0x20(%rsp), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x4(%r12)
movq 0x40(%rsp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
ucomisd 0x38d04(%rip), %xmm0    # 0x51060
jb 0x18364
sqrtsd %xmm0, %xmm0
jmp 0x18375
callq 0x2290
movss 0x1c(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
movsd %xmm0, 0x20(%r12)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r12)
leaq (%rbx,%r13), %rax
leaq 0x60f42(%rip), %rcx     # 0x792d8
movslq (%rcx), %rcx
cmpq %rcx, %rax
jge 0x1842f
incq %r13
addq $0xc8, %r12
cmpq %r13, %r14
jne 0x1832b
movq 0x10(%rsp), %rax
addl %r13d, %eax
movq %rax, 0x10(%rsp)
movl (%rsp), %r12d
movl 0x4(%rsp), %r14d
jmp 0x183d6
movl 0x4(%rsp), %r14d
movl (%rsp), %r12d
incl %ebp
cmpl %r12d, %ebp
movl 0x50(%rsp), %esi
jne 0x180fb
movl 0x4c(%rsp), %eax
cmpl %r12d, %eax
jne 0x180b5
jmp 0x183fd
movq $0x0, 0x10(%rsp)
movq 0x40(%rsp), %rdi
callq 0x2050
movq 0x28(%rsp), %rdi
callq 0x2050
movq 0x20(%rsp), %rdi
callq 0x2050
movq 0x10(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
leal (%rax,%r13), %esi
incl %esi
leaq 0x3eb82(%rip), %rdi     # 0x56fc3
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2270