// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %r9, %r15
movl %r8d, %r10d
movq %rcx, %r11
movq %rdx, %r14
movq %rsi, %rax
movq %rdi, 0x38(%rsp)
movq 0x108(%rsp), %r9
movl 0x100(%rsp), %r13d
movl 0xf8(%rsp), %ebp
leaq 0x4(%rsp), %rbx
andl $0x0, (%rbx)
subq $0x8, %rsp
leaq 0xb8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
movq %rbx, %rcx
movq %r15, %r8
movq %r10, 0x50(%rsp)
pushq %r10
movq %r11, 0x50(%rsp)
pushq %r11
movq %r14, %r12
pushq %r14
pushq %rax
pushq %r13
callq 0x41a63
addq $0x30, %rsp
movss %xmm0, (%rsp)
movl (%rbx), %esi
testl %esi, %esi
je 0x417ec
leaq 0x15b49(%rip), %rdi     # 0x5732e
xorl %eax, %eax
callq 0x20f0
movsd 0x88(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
testl %ebp, %ebp
movl $0x0, %r15d
cmovgl %ebp, %r15d
xorps %xmm0, %xmm0
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq %r14, %r15
je 0x41923
movq 0xf0(%rsp), %rax
movq (%rax,%r14,8), %r8
subq $0x8, %rsp
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq 0x110(%rsp), %r9
pushq 0x50(%rsp)
pushq 0x50(%rsp)
pushq %r12
pushq $0x0
movl 0x128(%rsp), %eax
pushq %rax
movaps %xmm0, 0xc0(%rsp)
movsd %xmm2, 0x98(%rsp)
movsd %xmm3, 0x90(%rsp)
movsd %xmm4, 0x88(%rsp)
movsd %xmm5, 0x80(%rsp)
callq 0x41a63
movsd 0x80(%rsp), %xmm5
movsd 0x88(%rsp), %xmm4
movsd 0x90(%rsp), %xmm3
movsd 0x98(%rsp), %xmm2
addq $0x30, %rsp
xorl %eax, %eax
ucomiss (%rsp), %xmm0
seta %al
addl %eax, %r13d
movsd 0x8(%rsp), %xmm1
xorl %eax, %eax
ucomisd 0x20(%rsp), %xmm1
cvtss2sd %xmm0, %xmm0
seta %al
addl %eax, %ebx
addsd %xmm0, %xmm5
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm4
movsd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm3
addsd %xmm1, %xmm2
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x90(%rsp), %xmm0
mulpd %xmm1, %xmm1
addpd %xmm1, %xmm0
addl 0x4(%rsp), %ebp
incq %r14
jmp 0x41823
leaq 0x10(%rsp), %rdi
movsd %xmm5, (%rdi)
leaq 0x8(%rsp), %rsi
movsd %xmm4, (%rsi)
leaq 0x78(%rsp), %rax
movsd %xmm3, (%rax)
leaq 0x70(%rsp), %rax
movhps %xmm0, (%rax)
leaq 0x18(%rsp), %r14
movsd %xmm2, (%r14)
leaq 0x80(%rsp), %r12
movlps %xmm0, (%r12)
movl 0xf8(%rsp), %r15d
movl %r15d, %edx
callq 0x41ba0
movq %r14, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x41ba0
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %r15d, %edx
callq 0x41ba0
testl %ebp, %ebp
je 0x419a4
leaq 0x159cd(%rip), %rdi     # 0x57365
movl %ebp, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x20f0
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm2
movsd 0x18(%rsp), %xmm1
movapd 0x20(%rsp), %xmm3
subsd %xmm1, %xmm3
cvtsd2ss %xmm3, %xmm5
movsd 0x80(%rsp), %xmm3
xorpd %xmm4, %xmm4
ucomisd %xmm3, %xmm4
cvtss2sd %xmm5, %xmm4
divsd %xmm3, %xmm4
cvtsd2ss %xmm4, %xmm4
jne 0x419e8
jp 0x419e8
movaps %xmm5, %xmm4
movss (%rsp), %xmm6
cvtss2sd %xmm6, %xmm5
subsd %xmm0, %xmm5
divsd %xmm2, %xmm5
movq 0x38(%rsp), %rax
movss %xmm6, (%rax)
cvtsi2ss %r13d, %xmm6
cvtsi2ss %r15d, %xmm7
divss %xmm7, %xmm6
movss %xmm6, 0x4(%rax)
unpcklpd 0x20(%rsp), %xmm5       # xmm5 = xmm5[0],mem[0]
cvtpd2ps %xmm5, %xmm5
movlpd %xmm5, 0x8(%rax)
cvtsi2ss %ebx, %xmm5
movss %xmm4, 0x14(%rax)
divss %xmm7, %xmm5
unpcklpd %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
unpcklpd %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0]
cvtpd2ps %xmm1, %xmm1
cvtpd2ps %xmm0, %xmm0
movss %xmm5, 0x10(%rax)
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movupd %xmm0, 0x18(%rax)
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq