// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movq %rsi, %r14
movss %xmm0, 0x10(%rsp)
movq %rdi, 0x78(%rsp)
leaq 0x517de(%rip), %rax     # 0x690ec
movslq (%rax), %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x21e0
movq %rax, %r12
movq %rbx, %rdi
callq 0x21e0
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
movl %ebp, %ebx
callq 0x21e0
movq %rax, %r13
testl %ebp, %ebp
jle 0x17cef
movss 0x10(%rsp), %xmm2
movaps %xmm2, %xmm3
mulss %xmm2, %xmm3
addss 0x3f555(%rip), %xmm2    # 0x56eac
mulss %xmm2, %xmm2
movslq %r15d, %rax
movq %rax, 0x70(%rsp)
leaq 0x64(%r14), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
movq $0x0, 0x38(%rsp)
movl %r15d, 0xc(%rsp)
movq %r14, %r10
movq %r14, 0x68(%rsp)
movss %xmm2, 0x10(%rsp)
movq 0x28(%rsp), %r11
movss %xmm3, 0x24(%rsp)
movl %ebx, 0x8(%rsp)
movq %r13, 0x40(%rsp)
movq %r12, 0x80(%rsp)
movl %eax, %edx
movslq 0x38(%rsp), %r14
leal -0x1(%r14), %esi
imulq $0x70, %r14, %rax
addq 0x58(%rsp), %rax
incq %r14
incl %esi
leaq 0x70(%rax), %rcx
cmpl %edx, (%rax)
movq %rcx, %rax
jl 0x179c3
leal 0x1(%rdx), %ebp
leaq -0x1(%r14), %rax
movq %rax, 0x38(%rsp)
cmpl %ebx, %ebp
movl %ebp, 0x4c(%rsp)
jge 0x17cdc
movl %esi, 0x50(%rsp)
movq %rdx, 0x30(%rsp)
movslq %r14d, %r14
imulq $0x70, %r14, %rax
decq %r14
addq 0x58(%rsp), %rax
incq %r14
leaq 0x70(%rax), %rcx
cmpl %ebp, (%rax)
movq %rcx, %rax
jl 0x17a03
cmpl %r15d, 0x50(%rsp)
jge 0x17cca
leaq 0x50bdd(%rip), %rax     # 0x68600
movl (%rax), %eax
movl %eax, 0x54(%rsp)
leaq 0x516bc(%rip), %rax     # 0x690ec
movl (%rax), %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x38(%rsp), %rax
imulq $0x70, %rax, %rbx
cmpl %edx, 0x64(%r10,%rbx)
jne 0x17bf6
movq %rax, 0x60(%rsp)
cmpl %r15d, %r14d
jge 0x17bde
addq %r10, %rbx
movq %r14, %r15
imulq $0x70, %r15, %r13
cmpl %ebp, 0x64(%r10,%r13)
jne 0x17bde
addq %r10, %r13
cmpl $0x0, 0x54(%rsp)
je 0x17ab4
movq 0x30(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x18742
movq 0x30(%rsp), %rdx
movss 0x24(%rsp), %xmm3
movq 0x28(%rsp), %r11
movss 0x10(%rsp), %xmm2
movq 0x68(%rsp), %r10
testl %eax, %eax
jne 0x17bd0
movsd (%r13), %xmm0
subsd (%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
ucomiss %xmm3, %xmm0
jbe 0x17ad9
ucomiss %xmm2, %xmm0
jbe 0x17bd0
jmp 0x17cc1
movsd 0x8(%r13), %xmm1
subsd 0x8(%rbx), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
ucomiss %xmm3, %xmm1
jbe 0x17b03
ucomiss %xmm2, %xmm1
jbe 0x17bd0
jmp 0x17cc1
movsd 0x10(%r13), %xmm0
subsd 0x10(%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm3, %xmm0
ja 0x17acb
movl 0x14(%rsp), %ecx
cmpl 0x20(%rsp), %ecx
jl 0x17b3c
movslq %ecx, %rax
movss -0x4(%r12,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x17bd0
testl %ecx, %ecx
movl %ecx, %edx
jle 0x17b5d
movl %edx, %ecx
xorl %eax, %eax
movss (%r12,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
ja 0x17b5f
incq %rax
cmpq %rax, %rcx
jne 0x17b46
movl %ecx, %eax
jmp 0x17b5f
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x20(%rsp), %esi
cmpl %esi, %edx
movl %edx, %edi
setl %dl
cmovll %edi, %esi
leal -0x1(%rsi), %edi
cmpl %eax, %edi
movq 0x40(%rsp), %r13
jle 0x17baf
movslq %edi, %rdi
movslq %eax, %r8
addl $-0x2, %esi
movslq %esi, %rsi
movss (%r12,%rsi,4), %xmm1
movss %xmm1, (%r12,%rdi,4)
movl (%r11,%rsi,4), %r9d
movl %r9d, (%r11,%rdi,4)
movl (%r13,%rsi,4), %r9d
movl %r9d, (%r13,%rdi,4)
decq %rdi
decl %esi
cmpq %r8, %rdi
jg 0x17b84
movb %dl, %cl
addl %ecx, 0x14(%rsp)
movl %eax, %eax
movss %xmm0, (%r12,%rax,4)
movq 0x60(%rsp), %rcx
movl %ecx, (%r11,%rax,4)
movl %r15d, (%r13,%rax,4)
movq 0x30(%rsp), %rdx
incq %r15
cmpq 0x70(%rsp), %r15
jl 0x17a66
movq 0x60(%rsp), %rax
incq %rax
cmpq 0x70(%rsp), %rax
movl 0xc(%rsp), %r15d
jl 0x17a43
movl 0x14(%rsp), %eax
testl %eax, %eax
jle 0x17cd6
movslq 0x18(%rsp), %rdi
movl %eax, %ebx
imulq $0xc8, %rdi, %r15
addq 0x78(%rsp), %r15
incq %rdi
xorl %r13d, %r13d
movl (%r11,%r13,4), %eax
movl %eax, (%r15)
movq 0x40(%rsp), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x4(%r15)
xorps %xmm0, %xmm0
cvtss2sd (%r12,%r13,4), %xmm0
ucomisd 0x39538(%rip), %xmm0    # 0x51178
jb 0x17c48
sqrtsd %xmm0, %xmm0
jmp 0x17c76
movq %rdi, %r12
callq 0x22a0
movq %r12, %rdi
movq 0x80(%rsp), %r12
movq 0x30(%rsp), %rdx
movss 0x24(%rsp), %xmm3
movq 0x28(%rsp), %r11
movss 0x10(%rsp), %xmm2
movq 0x68(%rsp), %r10
movsd %xmm0, 0x20(%r15)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r15)
leaq (%rdi,%r13), %rax
leaq 0x61643(%rip), %rcx     # 0x792d8
movslq (%rcx), %rcx
cmpq %rcx, %rax
jge 0x17d2f
incq %r13
addq $0xc8, %r15
cmpq %r13, %rbx
jne 0x17c1b
movq 0x18(%rsp), %rax
addl %r13d, %eax
movq %rax, 0x18(%rsp)
movl 0x8(%rsp), %ebx
movl 0xc(%rsp), %r15d
incl %ebp
cmpl %ebx, %ebp
jne 0x179f4
jmp 0x17cdc
movl 0x8(%rsp), %ebx
jmp 0x17cca
movl 0x4c(%rsp), %eax
cmpl %ebx, %eax
movq 0x40(%rsp), %r13
jne 0x179af
jmp 0x17cfd
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %r11
movq %r12, %rdi
movq %r11, %rbx
callq 0x2050
movq %rbx, %rdi
callq 0x2050
movq %r13, %rdi
callq 0x2050
movq 0x18(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
leal (%rax,%r13), %esi
incl %esi
leaq 0x3f242(%rip), %rdi     # 0x56f83
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280