// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/allostery.c
// Function: Test_site
int Test_site(struct site_results *res, int *atom_site,
	      float *profile, float **coupling, int Na,
	      int *site_res, int **site_res_ran, int numran,
	      int nr, int *atomres)
{

  int n_not=0;

  // Observed site
  double p_ave, p_dev, c_ave, c_dev;
  int n_notfound=0;
  p_ave=Ave_site(&p_dev, &c_ave, &c_dev, &n_notfound,
		 site_res, atomres, nr, atom_site,
		 profile, coupling, Na);
  if(n_notfound){
    printf("WARNING, %d reference atoms of binding site not found\n",
	   n_notfound);
  } 

  // Sum over num random sites 
  double ran_prof1=0, ran_prof2=0;
  double ran_pair1=0, ran_pair2=0;
  double ran_sig1=0,  ran_sig2=0;
  int num_p_gt=0, num_c_gt=0;
  for(int kran=0; kran<numran; kran++){
    double ave_prof, sigprof, ave_pair, sigpair;
    ave_prof=Ave_site(&sigprof, &ave_pair, &sigpair, &n_notfound,
		      site_res_ran[kran], atomres, nr,
		      NULL, profile, coupling, Na);
    if(ave_prof > p_ave)num_p_gt++;
    if(ave_pair > c_ave)num_c_gt++;
    ran_prof1+=ave_prof;
    ran_prof2+=ave_prof*ave_prof;
    ran_sig1+=sigprof;
    ran_sig2+=sigprof*sigprof;
    ran_pair1+=ave_pair;
    ran_pair2+=ave_pair*ave_pair;
    n_not+=n_notfound;
  } 
  Ave_St_dev(&ran_prof1, &ran_prof2, numran);
  Ave_St_dev(&ran_pair1, &ran_pair2, numran);
  Ave_St_dev(&ran_sig1, &ran_sig2, numran);
  if(n_not)
    printf("WARNING, %d atoms not found in %d random sites\n", n_not, numran);
  
  float Z_p=(p_ave-ran_prof1)/(ran_prof2);
  float Z_c=(c_ave-ran_pair1);
  if(ran_pair2)Z_c/=ran_pair2;
  //float Z_sig= (p_dev - ran_sig1)/(ran_sig2);
  res->prof=p_ave; res->Z_p=Z_p; res->P_p= (float)num_p_gt/numran;
  res->coup=c_ave; res->Z_c=Z_c; res->P_c= (float)num_c_gt/numran;
  res->p_ran1=ran_prof1; res->p_ran2=ran_prof2;
  res->c_ran1=ran_pair1; res->c_ran2=ran_pair2;

  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %r10
movl %r8d, %r12d
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %rax
movq %rdi, %r14
movq 0xf8(%rsp), %r9
movl 0xf0(%rsp), %r11d
movl 0xe8(%rsp), %r15d
leaq 0x24(%rsp), %rbx
movl $0x0, (%rbx)
subq $0x8, %rsp
leaq 0xa8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movq %rbx, %rcx
movq %r10, %r8
pushq %r12
pushq %r13
pushq %rbp
pushq %rax
pushq %r11
callq 0x476db
addq $0x30, %rsp
movaps %xmm0, %xmm4
movl (%rbx), %esi
testl %esi, %esi
movss %xmm0, 0xc(%rsp)
je 0x472f3
leaq 0x160d8(%rip), %rdi     # 0x5d3be
xorl %eax, %eax
callq 0x20f0
movss 0xc(%rsp), %xmm4
xorpd %xmm6, %xmm6
testl %r15d, %r15d
jle 0x47446
movq %r14, 0x68(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl %r15d, %eax
movq %rax, 0x28(%rsp)
xorpd %xmm5, %xmm5
xorpd %xmm3, %xmm3
movq %r13, 0x78(%rsp)
movq %r12, %r13
xorl %r12d, %r12d
movq %rbp, %r15
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r14d, %r14d
xorpd %xmm7, %xmm7
xorpd %xmm6, %xmm6
movsd %xmm7, 0x18(%rsp)
movapd %xmm6, 0x30(%rsp)
movsd %xmm3, 0x10(%rsp)
movapd %xmm5, 0x40(%rsp)
movq 0xe0(%rsp), %rax
movq (%rax,%r12,8), %r8
subq $0x8, %rsp
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x98(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
movq 0x100(%rsp), %r9
pushq %r13
pushq 0x88(%rsp)
pushq %r15
pushq $0x0
movl 0x118(%rsp), %eax
pushq %rax
callq 0x476db
movsd 0x48(%rsp), %xmm7
movapd 0x60(%rsp), %xmm6
movsd 0x40(%rsp), %xmm3
movapd 0x70(%rsp), %xmm5
movss 0x3c(%rsp), %xmm4
addq $0x30, %rsp
xorl %eax, %eax
ucomiss %xmm4, %xmm0
seta %al
addl %eax, %ebx
movsd 0x80(%rsp), %xmm1
xorl %eax, %eax
ucomisd 0x50(%rsp), %xmm1
seta %al
cvtss2sd %xmm0, %xmm0
addl %eax, %r14d
movaps %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm7
movsd 0x88(%rsp), %xmm2
addsd %xmm2, %xmm3
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
addpd %xmm0, %xmm5
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
mulpd %xmm2, %xmm2
addpd %xmm2, %xmm6
addl 0x24(%rsp), %ebp
incq %r12
cmpq %r12, 0x28(%rsp)
jne 0x4733e
xorpd %xmm8, %xmm8
movl 0xe8(%rsp), %r15d
cmpl $0x1, %r15d
jne 0x47460
xorpd %xmm7, %xmm7
jmp 0x475a0
xorpd %xmm5, %xmm5
xorps %xmm9, %xmm9
xorpd %xmm7, %xmm7
xorpd %xmm8, %xmm8
xorps %xmm10, %xmm10
jmp 0x47616
xorps %xmm4, %xmm4
cvtsi2sd %r15d, %xmm4
leal -0x1(%r15), %eax
cvtsi2sd %eax, %xmm9
movapd %xmm4, %xmm0
unpcklpd %xmm4, %xmm0            # xmm0 = xmm0[0],xmm4[0]
divpd %xmm0, %xmm5
movapd %xmm4, %xmm0
mulsd %xmm5, %xmm0
mulsd %xmm5, %xmm0
subsd %xmm0, %xmm7
divsd %xmm9, %xmm7
ucomisd %xmm8, %xmm7
movapd %xmm5, 0x40(%rsp)
movapd %xmm4, 0x50(%rsp)
movsd %xmm9, 0x28(%rsp)
jb 0x474bb
movsd %xmm3, 0x10(%rsp)
xorps %xmm3, %xmm3
sqrtsd %xmm7, %xmm3
jmp 0x474f2
movsd %xmm3, 0x10(%rsp)
movapd %xmm7, %xmm0
movapd %xmm6, 0x30(%rsp)
callq 0x22a0
xorpd %xmm8, %xmm8
movsd 0x28(%rsp), %xmm9
movapd 0x50(%rsp), %xmm4
movapd 0x30(%rsp), %xmm6
movapd 0x40(%rsp), %xmm5
movapd %xmm0, %xmm3
movapd %xmm5, %xmm1
unpckhpd %xmm5, %xmm1            # xmm1 = xmm1[1],xmm5[1]
movapd %xmm4, %xmm2
mulsd %xmm1, %xmm2
movapd %xmm6, %xmm0
unpckhpd %xmm6, %xmm0            # xmm0 = xmm0[1],xmm6[1]
mulsd %xmm1, %xmm2
subsd %xmm2, %xmm0
divsd %xmm9, %xmm0
ucomisd %xmm8, %xmm0
movsd %xmm3, 0x18(%rsp)
jb 0x4752f
xorps %xmm8, %xmm8
sqrtsd %xmm0, %xmm8
jmp 0x47552
movapd %xmm6, 0x30(%rsp)
callq 0x22a0
movsd 0x28(%rsp), %xmm9
movapd 0x50(%rsp), %xmm4
movapd 0x30(%rsp), %xmm6
movapd %xmm0, %xmm8
movsd 0x10(%rsp), %xmm0
divsd %xmm4, %xmm0
mulsd %xmm0, %xmm4
mulsd %xmm0, %xmm4
subsd %xmm4, %xmm6
divsd %xmm9, %xmm6
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm6
jae 0x4758e
movapd %xmm6, %xmm0
movsd %xmm8, 0x10(%rsp)
callq 0x22a0
movsd 0x10(%rsp), %xmm8
movss 0xc(%rsp), %xmm4
movapd 0x40(%rsp), %xmm5
movsd 0x18(%rsp), %xmm7
cvtsi2ss %ebx, %xmm10
xorps %xmm9, %xmm9
cvtsi2ss %r14d, %xmm9
testl %ebp, %ebp
je 0x4760d
leaq 0x15e3c(%rip), %rdi     # 0x5d3f5
movl %ebp, %esi
movl %r15d, %edx
xorl %eax, %eax
movapd %xmm5, 0x40(%rsp)
movsd %xmm7, 0x18(%rsp)
movsd %xmm8, 0x10(%rsp)
movss %xmm9, 0x30(%rsp)
movss %xmm10, 0x50(%rsp)
callq 0x20f0
movss 0x50(%rsp), %xmm10
movss 0x30(%rsp), %xmm9
movsd 0x10(%rsp), %xmm8
movsd 0x18(%rsp), %xmm7
movapd 0x40(%rsp), %xmm5
movss 0xc(%rsp), %xmm4
movq 0x68(%rsp), %r14
xorpd %xmm6, %xmm6
movsd 0x70(%rsp), %xmm1
movapd %xmm5, %xmm0
unpckhpd %xmm5, %xmm0            # xmm0 = xmm0[1],xmm5[1]
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
xorps %xmm3, %xmm3
cvtsd2ss %xmm2, %xmm3
ucomisd %xmm6, %xmm8
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
divsd %xmm8, %xmm2
cvtsd2ss %xmm2, %xmm2
jne 0x4764f
jp 0x4764f
movaps %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtss2sd %xmm4, %xmm3
movss %xmm4, (%r14)
xorps %xmm4, %xmm4
cvtsi2ss %r15d, %xmm4
subsd %xmm5, %xmm3
divsd %xmm7, %xmm3
divss %xmm4, %xmm10
movss %xmm10, 0x4(%r14)
unpcklpd %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
cvtpd2ps %xmm3, %xmm1
movlpd %xmm1, 0x8(%r14)
movss %xmm2, 0x14(%r14)
divss %xmm4, %xmm9
xorps %xmm1, %xmm1
cvtsd2ss %xmm5, %xmm1
movss %xmm9, 0x10(%r14)
movss %xmm1, 0x18(%r14)
xorps %xmm1, %xmm1
cvtsd2ss %xmm7, %xmm1
movss %xmm1, 0x1c(%r14)
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%r14)
xorps %xmm0, %xmm0
cvtsd2ss %xmm8, %xmm0
movss %xmm0, 0x24(%r14)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq