// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/interactions_tnm.c
// Function: Interactions_min
int Interactions_min(struct interaction *Int_list, float thr,
		     atom *atoms, int N_atoms, int N_res)
{
  // All pairs of atoms tested, only the pair at minimal 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, d2_min;
  int res1, res2, ij;

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

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

    for(res2=res1+1; res2<N_res; res2++){ // Omit i+1 because covalent
      while(atoms[i2_start].res< res2)i2_start++;

      int i1_min=i1_start, i2_min=i2_start, ncheck=0;
      d2_min=10000; ij++;

      // Check if contact
      for(i1=i1_start; i1<N_atoms; i1++){
	atom1=atoms+i1; if(atom1->res != res1)break;
	//if(atom1->name[0]=='H')continue;
	//int ib; if(ij<IJ_MIN)ib=Backbone(atom1->name);
	for(i2=i2_start; i2<N_atoms; i2++){
	  atom2=atoms+i2; if(atom2->res != res2)break;
	  //if(atom2->name[0]=='H')continue;

	  // Check if covalent
	  if((NOCOV) && Covalent(res1, res2, atom1, atom2))continue;
	  //if((ij<IJ_MIN)&&(ib)&&(Backbone(atom2->name)))continue;
	  ncheck++;

	  // Interatomic distance
	  d=atom2->r[0]-atom1->r[0];
	  d2 =d*d; // if(d2>thr2){continue;}//if(d2 > t_max2)goto next2;  
	  d=atom2->r[1]-atom1->r[1];
	  d2+=d*d; // if(d2>thr2){continue;} //if(d2 > t_max2)goto next2; 
	  d=atom2->r[2]-atom1->r[2];
	  d2+=d*d; // if(d2>thr2){continue;} //if(d2 > t_max2)goto next2; 
	  if(d2 < d2_min){d2_min=d2; i1_min=i1; i2_min=i2;}
	}
      }

      /*  if((res1==29)&&(res2==104)){
	atom2=atoms+i1_min;
	printf("d=%.5g ", sqrt(d2_min));
	printf("a1: %s %d r %d  ",atom2->name, i1_min, atom2->res);
	atom2=atoms+i2_min;
	printf("a1: %s %d r %d\n",atom2->name, i2_min, atom2->res);
	exit(8);
      }
      */

      // store interaction
      if(d2_min < thr2){
	Int_list[N_int].i1=i1_min; Int_list[N_int].i2=i2_min;
	Int_list[N_int].r0=sqrt(d2_min); 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;
    }
  }
  return(N_int);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, 0x38(%rsp)
movl %ecx, 0x4(%rsp)
testl %ecx, %ecx
jle 0x178b2
movl %edx, %r12d
movaps %xmm0, %xmm2
mulss %xmm0, %xmm2
movslq %edx, %rax
movq %rax, 0x58(%rsp)
leaq 0x64(%rsi), %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
movl $0x0, (%rsp)
movq $0x0, 0x18(%rsp)
movq %rsi, 0x48(%rsp)
movss %xmm2, 0x14(%rsp)
movl %edx, 0x2c(%rsp)
movslq 0x18(%rsp), %rbp
leal -0x1(%rbp), %r15d
imulq $0x70, %rbp, %rax
addq 0x40(%rsp), %rax
incq %rbp
incl %r15d
leaq 0x70(%rax), %rcx
cmpl %edi, (%rax)
movq %rcx, %rax
jl 0x17679
leal 0x1(%rdi), %eax
leaq -0x1(%rbp), %rcx
movq %rcx, 0x18(%rsp)
cmpl 0x4(%rsp), %eax
movq %rax, 0x60(%rsp)
jge 0x178a1
movl %eax, %ebx
movq %rdi, 0x20(%rsp)
movl %r15d, 0x10(%rsp)
movslq %ebp, %rbp
imulq $0x70, %rbp, %rax
decq %rbp
movq 0x40(%rsp), %rcx
addq %rax, %rcx
addq %rsi, %rax
movq %rax, %r13
incq %rbp
leaq 0x70(%rcx), %rdx
addq $0x70, %rax
cmpl %ebx, (%rcx)
movq %rdx, %rcx
jl 0x176c6
cmpl %r12d, %r15d
jge 0x177f8
leaq 0x50f15(%rip), %rax     # 0x68600
movl (%rax), %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rsp), %rcx
movl %ebp, 0xc(%rsp)
movl %r15d, 0x8(%rsp)
movss 0x3f7a9(%rip), %xmm3    # 0x56eb0
imulq $0x70, %rcx, %r15
cmpl %edi, 0x64(%rsi,%r15)
jne 0x17809
movq %rcx, 0x50(%rsp)
cmpl %r12d, %ebp
jge 0x177d9
addq %rsi, %r15
movq %r13, %r12
movq %rbp, %r14
cmpl %ebx, 0x64(%r12)
jne 0x177d9
cmpl $0x0, 0x30(%rsp)
je 0x1776c
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movq %r15, %rdx
movq %r12, %rcx
movss %xmm3, 0x34(%rsp)
callq 0x18742
movss 0x34(%rsp), %xmm3
movq 0x20(%rsp), %rdi
movss 0x14(%rsp), %xmm2
testl %eax, %eax
jne 0x177c7
movsd (%r12), %xmm0
movsd 0x8(%r12), %xmm1
subsd (%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
subsd 0x8(%r15), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm0, %xmm0
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
movsd 0x10(%r12), %xmm0
subsd 0x10(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm0, %xmm3
jbe 0x177c7
movaps %xmm0, %xmm3
movq 0x50(%rsp), %rax
movl %eax, 0x8(%rsp)
movl %r14d, 0xc(%rsp)
incq %r14
addq $0x70, %r12
cmpq 0x58(%rsp), %r14
jl 0x1772d
movq 0x50(%rsp), %rcx
incq %rcx
cmpq 0x58(%rsp), %rcx
movl 0x2c(%rsp), %r12d
movq 0x48(%rsp), %rsi
jl 0x17707
jmp 0x17809
movss 0x3f6b0(%rip), %xmm3    # 0x56eb0
movl %r15d, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
ucomiss %xmm3, %xmm2
jbe 0x1784d
movl (%rsp), %esi
movslq %esi, %rax
imulq $0xc8, %rax, %r14
movq 0x38(%rsp), %rax
movl 0x8(%rsp), %ecx
movl %ecx, (%rax,%r14)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%rax,%r14)
xorps %xmm0, %xmm0
cvtss2sd %xmm3, %xmm0
ucomisd 0x39938(%rip), %xmm0    # 0x51178
movl 0x10(%rsp), %r15d
jb 0x17854
sqrtsd %xmm0, %xmm0
jmp 0x17867
movl 0x10(%rsp), %r15d
jmp 0x17895
callq 0x22a0
movq 0x20(%rsp), %rdi
movl (%rsp), %esi
movss 0x14(%rsp), %xmm2
addq 0x38(%rsp), %r14
movsd %xmm0, 0x20(%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r14)
incl %esi
leaq 0x61a4f(%rip), %rax     # 0x792d8
cmpl (%rax), %esi
jge 0x178cb
movl %esi, (%rsp)
movq 0x48(%rsp), %rsi
incl %ebx
cmpl 0x4(%rsp), %ebx
jne 0x176b1
movq 0x60(%rsp), %rdi
cmpl 0x4(%rsp), %edi
jne 0x17667
jmp 0x178b9
movl $0x0, (%rsp)
movl (%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f6b1(%rip), %rdi     # 0x56f83
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280