// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %r8, 0x28(%rsp)
movl %ecx, %ebp
movl %edx, 0xc(%rsp)
movq %rsi, %r15
movq %rdi, 0x50(%rsp)
movss 0x3afb8(%rip), %xmm1    # 0x50dfc
movss %xmm0, 0x8(%rsp)
addss %xmm0, %xmm1
movss %xmm1, 0x4(%rsp)
movslq %ecx, %rbx
leaq (,%rbx,4), %rdi
callq 0x21d0
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %r14d
movl %ebp, 0x44(%rsp)
cmovgl %ebp, %r14d
movq %rax, %rbp
cmpq %rcx, %r14
je 0x15e88
andl $0x0, (%rbp,%rcx,4)
incq %rcx
jmp 0x15e79
movss 0x8(%rsp), %xmm2
mulss %xmm2, %xmm2
movss 0x4(%rsp), %xmm0
mulss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movslq 0xc(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x64(%r15), %rax
movq %rax, 0x58(%rsp)
xorl %r8d, %r8d
movq $0x0, 0x38(%rsp)
movl $0x0, (%rsp)
movq %r15, 0x30(%rsp)
movss %xmm2, 0x8(%rsp)
movq %rbp, 0x20(%rsp)
movq %r14, 0x68(%rsp)
cmpq %r14, %r8
je 0x161cf
movslq 0x38(%rsp), %rbx
leal -0x1(%rbx), %eax
imulq $0x70, %rbx, %rcx
addq 0x58(%rsp), %rcx
movslq (%rcx), %rdx
incq %rbx
incl %eax
addq $0x70, %rcx
cmpq %rdx, %r8
jg 0x15ef7
movl %eax, 0x1c(%rsp)
movq %r15, %rdi
leaq 0x1c(%rsp), %rsi
movl %r8d, %edx
movl 0xc(%rsp), %ecx
movq %r8, %r13
movq 0x28(%rsp), %r8
callq 0x1712c
leaq -0x1(%rbx), %rcx
movq %rcx, 0x38(%rsp)
testl %eax, %eax
movq %r13, 0x10(%rsp)
js 0x161a1
movl 0x1c(%rsp), %eax
movl %eax, 0x40(%rsp)
movq %r13, %r12
movq %r13, %r8
movq %r12, %r14
leaq 0x1(%r14), %r12
cmpl 0x44(%rsp), %r12d
jge 0x1619a
movslq %ebx, %rbx
imulq $0x70, %rbx, %rcx
decq %rbx
movq 0x58(%rsp), %rax
addq %rcx, %rax
addq %r15, %rcx
movq %rcx, %r13
movslq (%rax), %rdx
incq %rbx
addq $0x70, %rax
addq $0x70, %rcx
cmpq %rdx, %r14
jge 0x15f73
movl %ebx, 0x18(%rsp)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
movl %r12d, %edx
movl 0xc(%rsp), %ecx
movq 0x28(%rsp), %r8
callq 0x1712c
testl %eax, %eax
js 0x16128
leaq 0x4d64b(%rip), %rax     # 0x63600
movl (%rax), %eax
movl %eax, 0x48(%rsp)
movq 0x38(%rsp), %rcx
movss 0x3ae38(%rip), %xmm3    # 0x50e00
movss 0x8(%rsp), %xmm2
movq 0x10(%rsp), %r8
cmpq 0x60(%rsp), %rcx
jge 0x160e2
imulq $0x70, %rcx, %rbp
movl 0x64(%r15,%rbp), %eax
cmpq %rax, %r8
jne 0x160e2
movq %rcx, 0x70(%rsp)
addq %r15, %rbp
movq %r13, %r14
movq %rbx, %r15
cmpq 0x60(%rsp), %r15
jge 0x160be
movl 0x64(%r14), %eax
cmpq %rax, %r12
jne 0x160be
cmpl $0x0, 0x48(%rsp)
je 0x16049
movl %r8d, %edi
movl %r12d, %esi
movq %rbp, %rdx
movq %r14, %rcx
movss %xmm3, 0x4c(%rsp)
callq 0x171db
movss 0x4c(%rsp), %xmm3
movq 0x10(%rsp), %r8
movss 0x8(%rsp), %xmm2
testl %eax, %eax
jne 0x160b2
movsd (%r14), %xmm0
subsd (%rbp), %xmm0
cvtsd2ss %xmm0, %xmm1
mulss %xmm1, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x16069
ucomiss 0x4(%rsp), %xmm1
jbe 0x160b2
jmp 0x160d0
movsd 0x8(%r14), %xmm0
subsd 0x8(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm2, %xmm0
jbe 0x1608e
ucomiss 0x4(%rsp), %xmm0
jbe 0x160b2
jmp 0x160d0
movsd 0x10(%r14), %xmm1
subsd 0x10(%rbp), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
ucomiss %xmm2, %xmm1
ja 0x16060
ucomiss %xmm1, %xmm3
jbe 0x160b2
movaps %xmm1, %xmm3
incq %r15
addq $0x70, %r14
jmp 0x15ffe
movq 0x70(%rsp), %rcx
incq %rcx
movq 0x30(%rsp), %r15
jmp 0x15fd3
movq %r12, %r14
movq 0x30(%rsp), %r15
movq 0x20(%rsp), %rbp
jmp 0x15f4f
ucomiss %xmm3, %xmm2
movq %r12, %r14
movq 0x20(%rsp), %rbp
jbe 0x15f4f
movslq (%rsp), %rax
imulq $0xc8, %rax, %r14
movq 0x50(%rsp), %rcx
movl 0x40(%rsp), %eax
movl %eax, (%rcx,%r14)
movl 0x18(%rsp), %eax
movl %eax, 0x4(%rcx,%r14)
cvtss2sd %xmm3, %xmm0
ucomisd 0x34f30(%rip), %xmm0    # 0x4b050
jb 0x1615e
sqrtsd %xmm0, %xmm0
jmp 0x16168
cmpl $0x0, 0x4(%rbp,%r14,4)
movq 0x10(%rsp), %r8
jne 0x16150
leaq 0x3ae3e(%rip), %rdi     # 0x50f7a
movq 0x28(%rsp), %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %r8
movl $0x1, 0x4(%rbp,%r14,4)
jmp 0x15f4c
callq 0x2290
movq 0x10(%rsp), %r8
addq 0x50(%rsp), %r14
movsd %xmm0, 0x20(%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r14)
movl (%rsp), %eax
incl %eax
movl %eax, (%rsp)
leaq 0x5e148(%rip), %rcx     # 0x742d8
cmpl (%rcx), %eax
jl 0x15f4c
jmp 0x161e9
movq 0x68(%rsp), %r14
jmp 0x161c7
cmpl $0x0, (%rbp,%r13,4)
movq %r13, %r8
jne 0x161c7
leaq 0x3adc7(%rip), %rdi     # 0x50f7a
movq 0x28(%rsp), %rsi
movl %r8d, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %r8
incq %r8
jmp 0x15edd
movq %rbp, %rdi
callq 0x2050
movl (%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3ace3(%rip), %rdi     # 0x50ed3
movl (%rsp), %esi
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270