// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/screened_interactions.c
// Function: Axis_distance_S
int Axis_distance_S(float *d2_nb, float *d2_bo, float S_NB, float S_BO,
		    struct store *store1, float d2_ij,
		    atom *atom_i, atom *atom_j, atom *atoms)
{
  int resi=atom_i->res, resj=atom_j->res;
  float d2, d2_jk, d2_k, d2_c, S, S2;
  float S2_NB=S_NB*S_NB;
  float S2_BO=S_BO*S_BO;
  float S4_NB=4*S2_NB;
  float S4_BO=4*S2_BO;
  float d_ij=sqrt(d2_ij);
  while(store1 != NULL){
    if(store1->x > d2_ij)break;
    atom *atom_k=atoms+store1->i;
    if(atom_k!=atom_j){
      d2_jk=Dist2(atom_j->r, atom_k->r);
      if(d2_jk >= d2_ij)goto next2;
      if(d2_jk > store1->x){
	d2_k=d2_jk;
	d2_c=Scalar(atom_j->r, atom_i->r, atom_k->r);
      }else{
	d2_k=store1->x;
	d2_c=Scalar(atom_i->r, atom_j->r, atom_k->r);
      }
      d2=d2_k-d2_c*d2_c/d2_ij;
      // d2 is the squared distance between atom k and the axis ij
      if(d2 > S4_NB)goto next2; // No screening if d > 2*S
      int bond=Bond(atom_k->res, resi, store1->x, resj, d2_jk);
      if(bond){
	if(d2 > S4_BO)goto next2;
	if(d2 < S2_BO){*d2_bo=0; return(1);} // There is shadow on the axis
	S=S_BO; S2=S2_BO;
      }else{
	if(d2 < S2_NB){*d2_nb=0; return(1);}
	S=S_NB; S2=S2_NB;
      }
      float ang=asin(sqrt(d2/d2_k)); if(ang<0)ang=-ang;
      float ang1=asin(S/d_ij); if(ang1<0)ang1=-ang1;
      float ang2=ang-ang1, s2;
      if(ang2<=0){*d2_nb=0; return(1);}
      else{s2=sin(ang2); s2*=s2;} // s2=sin(ang2)^2
      if(d2_jk > store1->x){d2=s2*d2_jk;}
      else{d2=s2*store1->x;}
      if(d2<S2){*d2_nb=0; return(1);}

      /*float d2_ic=d2_k-d2;       // Sq. dist. from atom i to projection of k
      // Sq. distance from bottom of atom k to axis
      if(bond){d2=(d2+S2_BO-2*S_BO*sqrt(d2));}
      else{d2=(d2+S2_NB-2*S_NB*sqrt(d2));}
      float d2a_ij=d2*d2_ij/d2_ic; // Sq. dist. from shadow to axis at j
      if(bond){if(d2a_ij < *d2_bo)*d2_bo=d2a_ij;}
      else{if(d2a_ij < *d2_nb)*d2_nb=d2a_ij;}*/
    }
  next2:
    store1=store1->next;
  }
  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r14
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movss %xmm1, 0x18(%rsp)
movss %xmm0, 0x1c(%rsp)
movl 0x64(%rcx), %ebp
movl 0x64(%r8), %ebx
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
movss %xmm2, 0x10(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, 0x20(%rsp)
jb 0x20543
xorps %xmm0, %xmm0
sqrtss %xmm2, %xmm0
jmp 0x20551
movaps %xmm2, %xmm0
callq 0x21d0
movss 0x10(%rsp), %xmm2
movss %xmm0, 0x14(%rsp)
testq %r13, %r13
je 0x207ad
movss 0x1c(%rsp), %xmm0
mulss %xmm0, %xmm0
movss 0x18(%rsp), %xmm1
mulss %xmm1, %xmm1
movss 0x3692c(%rip), %xmm3    # 0x56ea8
movss %xmm0, 0x2c(%rsp)
mulss %xmm3, %xmm0
movss %xmm0, 0x3c(%rsp)
movss %xmm1, 0x28(%rsp)
mulss %xmm1, %xmm3
movss %xmm3, 0x30(%rsp)
movss 0x4(%r13), %xmm0
ucomiss %xmm2, %xmm0
ja 0x207ad
movslq (%r13), %rax
imulq $0x70, %rax, %rax
addq %r14, %rax
cmpq %r15, %rax
je 0x207a0
movsd (%r15), %xmm1
movsd 0x8(%r15), %xmm3
subsd (%rax), %xmm1
subsd 0x8(%rax), %xmm3
mulsd %xmm1, %xmm1
mulsd %xmm3, %xmm3
addsd %xmm1, %xmm3
movsd 0x10(%r15), %xmm1
subsd 0x10(%rax), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm3, %xmm1
cvtsd2ss %xmm1, %xmm1
ucomiss %xmm2, %xmm1
jae 0x207a0
xorpd %xmm5, %xmm5
xorl %ecx, %ecx
ucomiss %xmm0, %xmm1
jbe 0x20635
movsd (%r15,%rcx,8), %xmm4
movapd %xmm4, %xmm3
subsd (%r12,%rcx,8), %xmm3
subsd (%rax,%rcx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm5
incq %rcx
cmpq $0x3, %rcx
jne 0x2060a
movaps %xmm1, %xmm4
jmp 0x2065e
movsd (%r12,%rcx,8), %xmm4
movapd %xmm4, %xmm3
subsd (%r15,%rcx,8), %xmm3
subsd (%rax,%rcx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm5
incq %rcx
cmpq $0x3, %rcx
jne 0x20635
movaps %xmm0, %xmm4
xorps %xmm3, %xmm3
cvtsd2ss %xmm5, %xmm3
mulss %xmm3, %xmm3
divss %xmm2, %xmm3
movaps %xmm4, %xmm5
subss %xmm3, %xmm5
ucomiss 0x3c(%rsp), %xmm5
ja 0x207a0
movss %xmm5, 0x40(%rsp)
movss %xmm4, 0xc(%rsp)
movl 0x64(%rax), %edi
movl %ebp, %esi
movl %ebx, %edx
movss %xmm1, 0x38(%rsp)
callq 0x18966
testl %eax, %eax
je 0x206d4
movss 0x40(%rsp), %xmm3
ucomiss 0x30(%rsp), %xmm3
movss 0x10(%rsp), %xmm2
movss 0xc(%rsp), %xmm1
ja 0x207a0
movss 0x28(%rsp), %xmm0
ucomiss %xmm3, %xmm0
movss 0x18(%rsp), %xmm2
jbe 0x206f5
jmp 0x207d0
movss 0x2c(%rsp), %xmm0
movss 0x40(%rsp), %xmm3
ucomiss %xmm3, %xmm0
movss 0x1c(%rsp), %xmm2
movss 0xc(%rsp), %xmm1
ja 0x207be
movss %xmm2, 0xc(%rsp)
movss %xmm0, 0x34(%rsp)
divss %xmm1, %xmm3
xorps %xmm0, %xmm0
cvtss2sd %xmm3, %xmm0
ucomisd 0x30a64(%rip), %xmm0    # 0x51178
jb 0x2071c
sqrtsd %xmm0, %xmm0
jmp 0x20721
callq 0x22a0
callq 0x2260
movaps %xmm0, 0x40(%rsp)
movss 0xc(%rsp), %xmm0
divss 0x14(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x2260
movaps 0x40(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvtpd2ps %xmm1, %xmm1
movapd %xmm1, %xmm0
xorpd 0x308d8(%rip), %xmm0    # 0x51030
maxps %xmm1, %xmm0
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
subss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jae 0x207be
cvtss2sd %xmm0, %xmm0
callq 0x2230
cvtsd2ss %xmm0, %xmm0
movss 0x38(%rsp), %xmm1
maxss 0x4(%r13), %xmm1
mulss %xmm0, %xmm0
mulss %xmm0, %xmm1
movss 0x34(%rsp), %xmm0
ucomiss %xmm1, %xmm0
movss 0x10(%rsp), %xmm2
ja 0x207be
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x2059c
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rax
movl $0x0, (%rax)
movl $0x1, %eax
jmp 0x207af
movq 0x50(%rsp), %rax
jmp 0x207c3