// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq %r8, %rbp
movl %ecx, %ebx
movl %edx, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
movss %xmm0, 0xc(%rsp)
movq %rdi, 0x70(%rsp)
xorl %eax, %eax
movl %eax, 0x24(%rsp)
movl $0x0, 0x4(%rsp)
movl %eax, 0x20(%rsp)
movslq %ecx, %r14
leaq (,%r14,4), %rdi
callq 0x21d0
movq %rax, %r15
movl %ebx, %r13d
testl %r14d, %r14d
jle 0x1792d
leaq (,%r13,4), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2110
movq %r15, 0x40(%rsp)
testl %ebx, %ebx
movl 0x8(%rsp), %r14d
movss 0xc(%rsp), %xmm2
jle 0x17cea
movss 0x3f59f(%rip), %xmm0    # 0x56eec
addss %xmm2, %xmm0
mulss %xmm2, %xmm2
mulss %xmm0, %xmm0
movss %xmm0, 0x1c(%rsp)
movslq %r14d, %rax
movq %rax, 0x80(%rsp)
movq 0x28(%rsp), %rax
addq $0x64, %rax
movq %rax, 0x78(%rsp)
movl $0x1, %eax
movq %rax, 0x68(%rsp)
xorl %edx, %edx
movl $0x0, 0x4(%rsp)
movq $0x0, 0x48(%rsp)
movq %rbp, 0x38(%rsp)
movss %xmm2, 0xc(%rsp)
movq %r13, 0x30(%rsp)
movslq 0x48(%rsp), %r15
leal -0x1(%r15), %esi
imulq $0x70, %r15, %rax
addq 0x78(%rsp), %rax
movslq (%rax), %rcx
incq %r15
incl %esi
addq $0x70, %rax
cmpq %rcx, %rdx
jg 0x179b7
movl %esi, 0x5c(%rsp)
movl %esi, 0x24(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movq %rdx, %rbx
movl %r14d, %ecx
movq %rbp, %r8
callq 0x18d9f
leaq -0x1(%r15), %rcx
movq %rcx, 0x48(%rsp)
testl %eax, %eax
movq %rbx, 0x10(%rsp)
js 0x17cae
leaq 0x1(%rbx), %rax
cmpq %r13, %rax
movq %rbx, %rdx
jae 0x17cd9
movl 0x24(%rsp), %eax
movl %eax, 0x58(%rsp)
movq 0x68(%rsp), %rcx
movq %rdx, %r8
movq %rbp, %rbx
movq %r8, %rax
movq %rcx, %rbp
movslq %r15d, %r15
imulq $0x70, %r15, %rcx
decq %r15
movq 0x78(%rsp), %rdx
addq %rcx, %rdx
addq 0x28(%rsp), %rcx
movq %rcx, %r12
incq %r15
leaq 0x70(%rdx), %rsi
addq $0x70, %rcx
cmpl %eax, (%rdx)
movq %rsi, %rdx
jle 0x17a3e
movl %r15d, 0x20(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl %ebp, %edx
movl %r14d, %ecx
movq %rbx, %r8
callq 0x18d9f
testl %eax, %eax
movq %rbp, 0x50(%rsp)
js 0x17c03
movss 0x3f46c(%rip), %xmm3    # 0x56ef0
cmpl %r14d, 0x5c(%rsp)
movss 0xc(%rsp), %xmm2
movq 0x10(%rsp), %rdx
movq %rbp, %r8
jge 0x17bc2
leaq 0x50b5c(%rip), %rax     # 0x68600
movl (%rax), %eax
movl %eax, 0x60(%rsp)
movq 0x48(%rsp), %rbp
movss 0x3f439(%rip), %xmm3    # 0x56ef0
imulq $0x70, %rbp, %rbx
movq 0x28(%rsp), %rax
movl 0x64(%rax,%rbx), %eax
cmpq %rax, %rdx
jne 0x17bc2
cmpl %r14d, %r15d
jge 0x17bac
addq 0x28(%rsp), %rbx
movq %r12, %r13
movq %r15, %r14
movl 0x64(%r13), %eax
cmpq %rax, %r8
jne 0x17bac
cmpl $0x0, 0x60(%rsp)
je 0x17b25
movl %edx, %edi
movl %r8d, %esi
movq %rbx, %rdx
movq %r13, %rcx
movss %xmm3, 0x64(%rsp)
callq 0x18e79
movss 0x64(%rsp), %xmm3
movq 0x50(%rsp), %r8
movq 0x10(%rsp), %rdx
movss 0xc(%rsp), %xmm2
testl %eax, %eax
jne 0x17b97
movsd (%r13), %xmm0
subsd (%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
mulss %xmm1, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x17b4b
ucomiss 0x1c(%rsp), %xmm1
jbe 0x17b97
jmp 0x17c4d
movsd 0x8(%r13), %xmm0
subsd 0x8(%rbx), %xmm0
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm2, %xmm0
jbe 0x17b73
ucomiss 0x1c(%rsp), %xmm0
jbe 0x17b97
jmp 0x17c4d
movsd 0x10(%r13), %xmm1
subsd 0x10(%rbx), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
ucomiss %xmm2, %xmm1
ja 0x17b3f
ucomiss %xmm1, %xmm3
jbe 0x17b97
movaps %xmm1, %xmm3
incq %r14
addq $0x70, %r13
cmpq 0x80(%rsp), %r14
jl 0x17ae1
incq %rbp
cmpq 0x80(%rsp), %rbp
movl 0x8(%rsp), %r14d
jl 0x17ab7
ucomiss %xmm3, %xmm2
jbe 0x17c46
movslq 0x4(%rsp), %rax
imulq $0xc8, %rax, %rbx
movq 0x70(%rsp), %rcx
movl 0x58(%rsp), %eax
movl %eax, (%rcx,%rbx)
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rcx,%rbx)
xorps %xmm0, %xmm0
cvtss2sd %xmm3, %xmm0
ucomisd 0x3946a(%rip), %xmm0    # 0x51060
movq 0x30(%rsp), %r13
jb 0x17c5e
sqrtsd %xmm0, %xmm0
jmp 0x17c6d
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax,%rbp,4)
movq 0x10(%rsp), %rdx
movq %rbp, %r8
movq %rbx, %rbp
jne 0x17c3c
leaq 0x3f44a(%rip), %rdi     # 0x5706a
movq %rbp, %rsi
movl %r8d, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x50(%rsp), %r8
movq 0x10(%rsp), %rdx
movq 0x40(%rsp), %rax
movl $0x1, (%rax,%r8,4)
jmp 0x17c9f
movq 0x38(%rsp), %rbp
jmp 0x17c57
movq 0x38(%rsp), %rbp
movl 0x8(%rsp), %r14d
movq 0x30(%rsp), %r13
jmp 0x17c9f
callq 0x2290
movq 0x50(%rsp), %r8
movq 0x10(%rsp), %rdx
addq 0x70(%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 0x61642(%rip), %rcx     # 0x792d8
cmpl (%rcx), %eax
movq 0x38(%rsp), %rbp
jge 0x17d0a
leaq 0x1(%r8), %rcx
cmpq %r13, %rcx
jne 0x17a1e
jmp 0x17cd9
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax,%rbx,4)
je 0x17cbe
movq %rbx, %rdx
jmp 0x17cd9
leaq 0x3f3a5(%rip), %rdi     # 0x5706a
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %rdx
incq %rdx
incq 0x68(%rsp)
cmpq %r13, %rdx
jne 0x179a5
movq 0x40(%rsp), %rdi
callq 0x2050
movl 0x4(%rsp), %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f2b2(%rip), %rdi     # 0x56fc3
movl 0x4(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2270