// File: /workspace/github_c_makefile_star2_O2/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 %rsi, %r15
movq %rdi, 0x30(%rsp)
mulss %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movslq %edx, %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
testl %ecx, %ecx
movl $0x0, %eax
movl %ecx, 0x28(%rsp)
cmovgl %ecx, %eax
movl %eax, 0x18(%rsp)
leaq 0x64(%rsi), %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x4(%rsp)
xorl %esi, %esi
movq %r15, 0x50(%rsp)
cmpl 0x18(%rsp), %esi
je 0x1646b
movslq 0x10(%rsp), %r12
leal -0x1(%r12), %edi
imulq $0x70, %r12, %rax
addq 0x38(%rsp), %rax
incq %r12
incl %edi
leaq 0x70(%rax), %rcx
cmpl %esi, (%rax)
movq %rcx, %rax
jl 0x1627a
leal 0x1(%rsi), %r13d
leaq -0x1(%r12), %rax
movq %rax, 0x10(%rsp)
movl %r13d, 0x1c(%rsp)
movq %rsi, %rbp
movl %edi, 0x24(%rsp)
cmpl 0x28(%rsp), %r13d
jge 0x16460
movslq %r12d, %r12
imulq $0x70, %r12, %rax
decq %r12
movq 0x38(%rsp), %rcx
addq %rax, %rcx
addq %r15, %rax
movq %rax, %r8
incq %r12
leaq 0x70(%rcx), %rdx
addq $0x70, %rax
cmpl %r13d, (%rcx)
movq %rdx, %rcx
jl 0x162c4
movq %r8, 0x58(%rsp)
leaq 0x4d31a(%rip), %rax     # 0x63600
movl (%rax), %r14d
movq 0x10(%rsp), %rcx
movss 0x3ab0a(%rip), %xmm2    # 0x50e00
movl %edi, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
movq %r12, 0x60(%rsp)
cmpq 0x48(%rsp), %rcx
jge 0x163e0
imulq $0x70, %rcx, %rbx
cmpl %esi, 0x64(%r15,%rbx)
jne 0x163e0
movq %rcx, 0x40(%rsp)
addq %r15, %rbx
movq 0x58(%rsp), %r15
cmpq 0x48(%rsp), %r12
jge 0x163c9
cmpl %r13d, 0x64(%r15)
jne 0x163c9
testl %r14d, %r14d
je 0x16368
movl %ebp, %edi
movl %r13d, %esi
movq %rbx, %rdx
movq %r15, %rcx
movss %xmm2, 0x2c(%rsp)
callq 0x171db
movss 0x2c(%rsp), %xmm2
movq %rbp, %rsi
testl %eax, %eax
jne 0x163bd
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
subsd (%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
subsd 0x8(%rbx), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm0, %xmm0
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
movsd 0x10(%r15), %xmm0
subsd 0x10(%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x163bd
movaps %xmm0, %xmm2
movq 0x40(%rsp), %rax
movl %eax, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
incq %r12
addq $0x70, %r15
jmp 0x1632b
movq 0x40(%rsp), %rcx
incq %rcx
movq 0x50(%rsp), %r15
movq 0x60(%rsp), %r12
jmp 0x16304
movss 0x20(%rsp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x16454
movslq 0x4(%rsp), %rax
imulq $0xc8, %rax, %rbx
movq 0x30(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rax,%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, 0x4(%rax,%rbx)
cvtss2sd %xmm2, %xmm0
ucomisd 0x34c39(%rip), %xmm0    # 0x4b050
jb 0x1641f
sqrtsd %xmm0, %xmm0
jmp 0x16427
callq 0x2290
movq %rbp, %rsi
addq 0x30(%rsp), %rbx
movsd %xmm0, 0x20(%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%rbx)
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
leaq 0x5de88(%rip), %rcx     # 0x742d8
cmpl (%rcx), %eax
jge 0x1647e
incl %r13d
movl 0x24(%rsp), %edi
jmp 0x162a4
movl 0x1c(%rsp), %eax
movl %eax, %esi
jmp 0x1625d
movl 0x4(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3aa4e(%rip), %rdi     # 0x50ed3
movl 0x4(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270