// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/confchange.c
// Function: Test_Null
float Test_Null(float *wminus2, float *P, int N, char *name, FILE *file1)
{
  /* Tests the null model P0(q) \propto w^-q varying q
     Returns the value of q that yields best correlation */

  int Newfile=0, i;
  float *x=malloc(N*sizeof(float));
  FILE *file2; char filename[80];
  if(Newfile){
    sprintf(filename, "Null_model_%s.dat", name);
    file2=fopen(filename, "w");
    fprintf(file2, "# Correlation between c_a^2 and P0_q(a)=w_a^(-q)\n");
    fprintf(file2, "# q Corr\n");
  }else{
    fprintf(file1, "# P_q(a)=w^(-q) d_KL(w^-q,c_a^2) d2 r(w^q*c_a^2,w^-2)\n");
  }

  float q, rho, slope, offset;
  float dmin=1000, q_dmin=0, rho_opt=0;
  //float q_rho=0;
  double P1=0, P2=0, dP=0;
  for(i=0; i<N; i++){P1+=P[i]; P2+=P[i]*P[i]; dP+=P[i]*log(P[i]);}
  for(i=0; i<N; i++)P[i]/=P1;
  P2=P2/(P1*P1)-1./N; dP=dP/P1-log(P1);
  //printf("P2= %.3f -S[P]=%.3f N=%d\n", P2, dP, N);
  for(q=0.5; q<=8; q+=0.25){
    double  q1=q/2., x1=0, d_KL=0, d2=0, d;
    for(i=0; i<N; i++){x[i]=pow(wminus2[i], q1); x1+=x[i];}
    for(i=0; i<N; i++)d_KL+=P[i]*log(x[i]);
    d_KL=dP-(d_KL-log(x1));
    if(d_KL < dmin){dmin=d_KL; q_dmin=q;}
    for(i=0; i<N; i++){d=P[i]-x[i]/x1; d2+=d*d;} d2/=P2;
    for(i=0; i<N; i++)x[i]=P[i]/x[i];
    rho= Corr_coeff(x, wminus2, N, &slope, &offset);
    if(fabs(rho)<rho_opt)rho_opt=fabs(rho); //q_rho=q;
    if(Newfile){
      fprintf(file2, "%.3f\t%.3f\t%.3f\t%.4f\n", q, d_KL, d2, rho);
    }else{
      fprintf(file1, "q=%.2f\t%.3f\t%.3f\t%.4f\n",q,d_KL,d2,rho);
    }
  }
  fprintf(file1, "Null model qopt       %.3f\n", q_dmin);
  fprintf(file1, "r^2[P_qopt,P_obs]     %.3f\n#\n", 1-dmin);
  if(Newfile){
    fclose(file2);
    printf("Writing %s\n", filename);
  }
  free(x);
  return(q_dmin);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r13
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r12
movslq %edx, %rbx
leaq (,%rbx,4), %rdi
callq 0x21e0
movq %rax, %r14
leaq 0x2ade4(%rip), %rdi     # 0x5a924
movl $0x36, %esi
movl $0x1, %edx
movq %r13, 0x38(%rsp)
movq %r13, %rcx
callq 0x2290
movl %ebp, %r13d
xorpd %xmm1, %xmm1
testl %ebx, %ebx
jle 0x2fbf5
xorl %ebx, %ebx
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
movsd %xmm1, 0x20(%rsp)
movss (%r15,%rbx,4), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
addsd %xmm0, %xmm3
movsd %xmm3, 0x10(%rsp)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm4
movsd %xmm4, 0x8(%rsp)
callq 0x2120
movsd 0x8(%rsp), %xmm4
movsd 0x10(%rsp), %xmm3
movsd 0x20(%rsp), %xmm1
mulsd 0x30(%rsp), %xmm0
addsd %xmm0, %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x2fb70
testl %ebp, %ebp
jle 0x2fbfd
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rax,4), %xmm0
divsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x2fbd4
jmp 0x2fbfd
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
divsd %xmm0, %xmm4
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0x264e0(%rip), %xmm2    # 0x560f8
divsd %xmm0, %xmm2
addsd %xmm4, %xmm2
movsd %xmm2, 0x40(%rsp)
divsd %xmm3, %xmm1
movsd %xmm1, 0x20(%rsp)
movapd %xmm3, %xmm0
callq 0x2120
movsd 0x20(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movss 0x25f73(%rip), %xmm1    # 0x55bc4
movss 0x27647(%rip), %xmm0    # 0x572a0
movss %xmm0, 0x1c(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
movaps %xmm1, %xmm0
movss %xmm0, 0x2c(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
movaps %xmm0, %xmm1
mulsd 0x214ea(%rip), %xmm1    # 0x51170
xorpd %xmm2, %xmm2
testl %ebp, %ebp
jle 0x2fd2c
xorl %ebx, %ebx
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtss2sd (%r12,%rbx,4), %xmm0
callq 0x20a0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rbx,4)
cvtss2sd %xmm0, %xmm0
addsd %xmm0, %xmm2
incq %rbx
cmpq %rbx, %r13
jne 0x2fc9a
testl %ebp, %ebp
jle 0x2fd2c
movsd %xmm2, 0x10(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rbx,4), %xmm0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
cvtss2sd (%r14,%rbx,4), %xmm0
callq 0x2120
mulsd 0x30(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incq %rbx
cmpq %rbx, %r13
jne 0x2fce9
movsd 0x10(%rsp), %xmm2
jmp 0x2fd36
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
movapd %xmm2, %xmm0
callq 0x2120
movsd 0x8(%rsp), %xmm2
subsd %xmm0, %xmm2
movsd 0x20(%rsp), %xmm1
subsd %xmm2, %xmm1
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movsd %xmm1, 0x8(%rsp)
jbe 0x2fd87
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x2c(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
testl %ebp, %ebp
movsd 0x10(%rsp), %xmm2
xorpd %xmm3, %xmm3
jle 0x2fde1
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rax,4), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r14,%rax,4), %xmm1
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm3
incq %rax
cmpq %rax, %r13
jne 0x2fd97
testl %ebp, %ebp
jle 0x2fde1
xorl %eax, %eax
movss (%r15,%rax,4), %xmm0
divss (%r14,%rax,4), %xmm0
movss %xmm0, (%r14,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x2fdc7
divsd 0x40(%rsp), %xmm3
movsd %xmm3, 0x10(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
leaq 0x54(%rsp), %rcx
leaq 0x50(%rsp), %r8
callq 0x108db
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
movq 0x38(%rsp), %rdi
leaq 0x2ab44(%rip), %rsi     # 0x5a95b
movsd 0x48(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movb $0x4, %al
callq 0x21a0
movss 0x2c(%rsp), %xmm0
addss 0x27466(%rip), %xmm0    # 0x572a4
movss 0x27466(%rip), %xmm1    # 0x572ac
ucomiss %xmm0, %xmm1
jae 0x2fc6b
xorps %xmm0, %xmm0
cvtss2sd 0x18(%rsp), %xmm0
leaq 0x2ab13(%rip), %rsi     # 0x5a972
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x21a0
movss 0x212a6(%rip), %xmm0    # 0x5111c
subss 0x1c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x2ab07(%rip), %rsi     # 0x5a98e
movq %rbx, %rdi
movb $0x1, %al
callq 0x21a0
movq %r14, %rdi
callq 0x2050
movss 0x18(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq