// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, 0x50(%rsp)
movq %rdi, 0x20(%rsp)
movl 0x64(%rcx), %r13d
movl 0x64(%r8), %ebp
movss %xmm0, 0x30(%rsp)
mulss %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
movss %xmm1, 0x2c(%rsp)
mulss %xmm1, %xmm1
movss %xmm1, 0x14(%rsp)
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm2
movss %xmm2, 0x1c(%rsp)
jb 0x20bde
xorps %xmm0, %xmm0
sqrtss %xmm2, %xmm0
jmp 0x20bec
movaps %xmm2, %xmm0
callq 0x21c0
movss 0x1c(%rsp), %xmm2
movss %xmm0, 0x10(%rsp)
movss 0x18(%rsp), %xmm5
movss 0x362e8(%rip), %xmm0    # 0x56ee8
mulss %xmm0, %xmm5
mulss 0x14(%rsp), %xmm0
movss %xmm0, 0x38(%rsp)
movss %xmm5, 0x3c(%rsp)
testq %r12, %r12
je 0x20e77
movss 0x4(%r12), %xmm0
ucomiss %xmm2, %xmm0
ja 0x20e77
movslq (%r12), %rax
imulq $0x70, %rax, %rax
addq %rbx, %rax
cmpq %r14, %rax
je 0x20e61
movsd (%r14), %xmm1
movsd 0x8(%r14), %xmm3
subsd (%rax), %xmm1
subsd 0x8(%rax), %xmm3
mulsd %xmm1, %xmm1
mulsd %xmm3, %xmm3
addsd %xmm1, %xmm3
movsd 0x10(%r14), %xmm1
subsd 0x10(%rax), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm3, %xmm1
cvtsd2ss %xmm1, %xmm1
ucomiss %xmm2, %xmm1
jae 0x20e61
xorpd %xmm6, %xmm6
xorl %ecx, %ecx
ucomiss %xmm0, %xmm1
jbe 0x20cb9
movsd (%r14,%rcx,8), %xmm4
movapd %xmm4, %xmm3
subsd (%r15,%rcx,8), %xmm3
subsd (%rax,%rcx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm6
incq %rcx
cmpq $0x3, %rcx
jne 0x20c8e
movaps %xmm1, %xmm4
jmp 0x20ce2
movsd (%r15,%rcx,8), %xmm4
movapd %xmm4, %xmm3
subsd (%r14,%rcx,8), %xmm3
subsd (%rax,%rcx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm6
incq %rcx
cmpq $0x3, %rcx
jne 0x20cb9
movaps %xmm0, %xmm4
xorps %xmm3, %xmm3
cvtsd2ss %xmm6, %xmm3
mulss %xmm3, %xmm3
divss %xmm2, %xmm3
movaps %xmm4, %xmm6
subss %xmm3, %xmm6
ucomiss %xmm5, %xmm6
ja 0x20e61
movss %xmm6, 0x40(%rsp)
movss %xmm4, 0x8(%rsp)
movl 0x64(%rax), %edi
movl %r13d, %esi
movl %ebp, %edx
movss %xmm1, 0x34(%rsp)
callq 0x19128
testl %eax, %eax
je 0x20d61
movl $0x4, %eax
movss 0x40(%rsp), %xmm2
ucomiss 0x38(%rsp), %xmm2
movss 0x8(%rsp), %xmm1
ja 0x20e51
movss 0x14(%rsp), %xmm0
ucomiss %xmm2, %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x2c(%rsp), %xmm0
movq 0x50(%rsp), %rcx
jbe 0x20d8d
jmp 0x20e46
movss 0x18(%rsp), %xmm0
movss 0x40(%rsp), %xmm2
ucomiss %xmm2, %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x30(%rsp), %xmm0
movq 0x20(%rsp), %rcx
movss 0x8(%rsp), %xmm1
ja 0x20e46
movss %xmm0, 0x8(%rsp)
divss %xmm1, %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
ucomisd 0x302ba(%rip), %xmm0    # 0x51060
jb 0x20dae
sqrtsd %xmm0, %xmm0
jmp 0x20db3
callq 0x2290
callq 0x2250
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm2
movaps 0x35b6a(%rip), %xmm1    # 0x56930
xorps %xmm1, %xmm2
maxss %xmm0, %xmm2
movaps %xmm2, 0x40(%rsp)
movss 0x8(%rsp), %xmm0
divss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x2250
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
xorps 0x35b3b(%rip), %xmm1    # 0x56930
maxss %xmm0, %xmm1
movaps 0x40(%rsp), %xmm2
subss %xmm1, %xmm2
xorps %xmm0, %xmm0
ucomiss %xmm2, %xmm0
movq 0x20(%rsp), %rcx
jae 0x20e46
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
callq 0x2220
cvtsd2ss %xmm0, %xmm0
mulss %xmm0, %xmm0
movss 0x34(%rsp), %xmm1
maxss 0x4(%r12), %xmm1
mulss %xmm0, %xmm1
xorl %eax, %eax
movss 0xc(%rsp), %xmm0
ucomiss %xmm1, %xmm0
movq 0x20(%rsp), %rcx
jbe 0x20e51
movl $0x0, (%rcx)
movl $0x1, %eax
testb $0x3, %al
movss 0x1c(%rsp), %xmm2
movss 0x3c(%rsp), %xmm5
jne 0x20e68
movq 0x8(%r12), %r12
xorl %eax, %eax
testl %eax, %eax
je 0x20c16
movl $0x1, %eax
jmp 0x20e79
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq