// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/interactions_tnm.c
// Function: Interactions_all_ref
int Interactions_all_ref(struct interaction *Int_list, float thr,
			atom *atoms, int N_atoms, int N_res, char *REF)
{
  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;
  int CB1=0, CB2=0; //i1_min, i2_min, 
  int *notfound=malloc(N_res*sizeof(int));

  for(res1=0; res1<N_res; res1++)notfound[res1]=0;

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

    while(atoms[i1_start].res< res1)i1_start++;
    CB1=i1_start;
    if(Find_atom(atoms, &CB1, res1, N_atoms, REF)<0){
      if(notfound[res1]==0)
	printf("WARNING, %s atom not found, res= %d\n", REF, res1);
      continue;
    }
    i2_start=i1_start+1;

    for(res2=res1+1; res2<N_res; res2++){
      while(atoms[i2_start].res< res2)i2_start++;
      CB2=i2_start;
      if(Find_atom(atoms, &CB2, res2, N_atoms, REF)<0){
	if(notfound[res2]==0)
	  printf("WARNING, %s atom not found, res= %d\n", REF, res2);
	notfound[res2]=1;
	continue;
      }
      d2_min=10000;

      // 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(d2 < d2_min)d2_min=d2; //i1_min=i1; i2_min=i2;}
	}
      }

      // store interaction
      if(d2_min < thr2){
	// Interacting atoms set as CB (CA if Gly)
	Int_list[N_int].i1=CB1; Int_list[N_int].i2=CB2;
	//Int_list[N_int].r0=sqrt(Dist2(atoms[CB1].r, atoms[CB2].r));
	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;
    }
  }
  free(notfound);
  return(N_int);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %rbx
movl %edx, %r14d
movq %rsi, %r13
movss %xmm0, 0xc(%rsp)
movq %rdi, 0x60(%rsp)
movslq %ecx, %r15
leaq (,%r15,4), %rdi
callq 0x21e0
testl %r15d, %r15d
movq %rax, 0x38(%rsp)
jle 0x175c8
movq %rbx, 0x28(%rsp)
movss 0xc(%rsp), %xmm0
movaps %xmm0, %xmm1
addss 0x3fca7(%rip), %xmm0    # 0x56eac
mulss %xmm1, %xmm1
movss %xmm1, 0x1c(%rsp)
mulss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movl %r15d, %ecx
movq %rcx, 0x68(%rsp)
leaq (,%rcx,4), %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2110
movslq %r14d, %rax
movq %rax, 0x78(%rsp)
decq %r15
movq %r15, 0x80(%rsp)
leaq 0x64(%r13), %rax
movq %rax, 0x70(%rsp)
movl $0x1, %r15d
xorl %edx, %edx
movl $0x0, 0x4(%rsp)
movq $0x0, 0x40(%rsp)
movq %r13, 0x30(%rsp)
movl %r14d, 0x8(%rsp)
movslq 0x40(%rsp), %r12
leal -0x1(%r12), %esi
imulq $0x70, %r12, %rax
addq 0x70(%rsp), %rax
movslq (%rax), %rcx
incq %r12
incl %esi
addq $0x70, %rax
cmpq %rcx, %rdx
jg 0x17285
movl %esi, 0x54(%rsp)
movl %esi, 0x24(%rsp)
movq %r13, %rdi
leaq 0x24(%rsp), %rsi
movq %rdx, %rbp
movl %r14d, %ecx
movq 0x28(%rsp), %r8
callq 0x1867d
leaq -0x1(%r12), %rcx
movq %rcx, 0x40(%rsp)
testl %eax, %eax
movq %rbp, 0x10(%rsp)
movq %r15, 0x88(%rsp)
js 0x17580
cmpq 0x80(%rsp), %rbp
movq %rbp, %rdx
jge 0x175ad
movl 0x24(%rsp), %eax
movl %eax, 0x50(%rsp)
movq %r15, %rsi
movl %edx, %r15d
movslq %r12d, %r12
imulq $0x70, %r12, %rax
decq %r12
movq 0x70(%rsp), %rcx
addq %rax, %rcx
addq %r13, %rax
movq %rax, %rbp
incq %r12
leaq 0x70(%rcx), %rdx
addq $0x70, %rax
cmpl %r15d, (%rcx)
movq %rdx, %rcx
jle 0x17309
movl %r12d, 0x20(%rsp)
movl %esi, %r15d
movq %r13, %rdi
movq %rsi, %rbx
leaq 0x20(%rsp), %rsi
movl %r15d, %edx
movl %r14d, %ecx
movq 0x28(%rsp), %r8
callq 0x1867d
testl %eax, %eax
movq %rbx, 0x48(%rsp)
js 0x174e6
movss 0x3fb59(%rip), %xmm4    # 0x56eb0
cmpl %r14d, 0x54(%rsp)
movss 0xc(%rsp), %xmm2
movss 0x1c(%rsp), %xmm3
movq 0x10(%rsp), %rdx
movq %rbx, %rsi
jge 0x174a6
leaq 0x51283(%rip), %rax     # 0x68600
movl (%rax), %eax
movl %eax, 0x58(%rsp)
movq 0x40(%rsp), %rax
movss 0x3fb20(%rip), %xmm4    # 0x56eb0
movq %rax, 0x90(%rsp)
imulq $0x70, %rax, %rbx
movl 0x64(%r13,%rbx), %eax
cmpq %rax, %rdx
jne 0x174a6
cmpl %r14d, %r12d
jge 0x17486
addq %r13, %rbx
movq %rbp, %r13
movq %r12, %r14
movl 0x64(%r13), %eax
cmpq %rax, %rsi
jne 0x17486
cmpl $0x0, 0x58(%rsp)
je 0x17406
movl %edx, %edi
movl %r15d, %esi
movq %rbx, %rdx
movq %r13, %rcx
movss %xmm4, 0x5c(%rsp)
callq 0x18742
movss 0x5c(%rsp), %xmm4
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rdx
movss 0x1c(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
testl %eax, %eax
jne 0x17474
movsd (%r13), %xmm0
subsd (%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
mulss %xmm1, %xmm1
ucomiss %xmm3, %xmm1
jbe 0x1742a
ucomiss %xmm2, %xmm1
jbe 0x17474
jmp 0x17524
movsd 0x8(%r13), %xmm0
subsd 0x8(%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm3, %xmm0
jbe 0x17450
ucomiss %xmm2, %xmm0
jbe 0x17474
jmp 0x17524
movsd 0x10(%r13), %xmm1
subsd 0x10(%rbx), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
ucomiss %xmm3, %xmm1
ja 0x17420
ucomiss %xmm1, %xmm4
jbe 0x17474
movaps %xmm1, %xmm4
incq %r14
addq $0x70, %r13
cmpq 0x78(%rsp), %r14
jl 0x173bc
movq 0x90(%rsp), %rax
incq %rax
cmpq 0x78(%rsp), %rax
movl 0x8(%rsp), %r14d
movq 0x30(%rsp), %r13
jl 0x17390
ucomiss %xmm4, %xmm3
jbe 0x17570
movslq 0x4(%rsp), %rax
imulq $0xc8, %rax, %rbx
movq 0x60(%rsp), %rcx
movl 0x50(%rsp), %eax
movl %eax, (%rcx,%rbx)
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rcx,%rbx)
xorps %xmm0, %xmm0
cvtss2sd %xmm4, %xmm0
ucomisd 0x39c9a(%rip), %xmm0    # 0x51178
jb 0x17530
sqrtsd %xmm0, %xmm0
jmp 0x1753f
movq 0x38(%rsp), %rbp
cmpl $0x0, (%rbp,%rbx,4)
movq 0x10(%rsp), %rdx
movq %rbx, %rsi
jne 0x1751a
leaq 0x3fb29(%rip), %rdi     # 0x5702a
movq 0x28(%rsp), %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl $0x1, (%rbp,%rsi,4)
jmp 0x17570
movl 0x8(%rsp), %r14d
movq 0x30(%rsp), %r13
jmp 0x17570
callq 0x22a0
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq 0x60(%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 0x61d70(%rip), %rcx     # 0x792d8
cmpl (%rcx), %eax
jge 0x175f0
incq %rsi
cmpq 0x68(%rsp), %rsi
jne 0x172f4
jmp 0x175ad
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax,%rbp,4)
je 0x17590
movq %rbp, %rdx
jmp 0x175ad
leaq 0x3fa93(%rip), %rdi     # 0x5702a
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x88(%rsp), %r15
incq %r15
cmpq 0x68(%rsp), %rdx
jne 0x17272
jmp 0x175d0
movl $0x0, 0x4(%rsp)
movq 0x38(%rsp), %rdi
callq 0x2050
movl 0x4(%rsp), %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f98c(%rip), %rdi     # 0x56f83
movl 0x4(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280