// File: /workspace/github_c_makefile_star2_O1/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 0x21d0
movq %rax, %r14
leaq 0x2abc8(%rip), %rdi     # 0x5a980
movl $0x36, %esi
movl $0x1, %edx
movq %r13, 0x38(%rsp)
movq %r13, %rcx
callq 0x2280
movl %ebp, %r13d
testl %ebx, %ebx
jle 0x2fe44
xorpd %xmm1, %xmm1
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, 0x8(%rsp)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm4
movsd %xmm4, 0x10(%rsp)
callq 0x2120
movsd 0x10(%rsp), %xmm4
movsd 0x8(%rsp), %xmm3
movsd 0x20(%rsp), %xmm1
mulsd 0x30(%rsp), %xmm0
addsd %xmm0, %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x2fde4
jmp 0x2fe50
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
xorpd %xmm1, %xmm1
testl %ebp, %ebp
jle 0x2fe75
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 0x2fe56
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
divsd %xmm0, %xmm4
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0x26260(%rip), %xmm2    # 0x560f0
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 0x25cb3(%rip), %xmm1    # 0x55b7c
movss 0x2740f(%rip), %xmm0    # 0x572e0
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 0x2115a(%rip), %xmm1    # 0x51058
xorpd %xmm2, %xmm2
testl %ebp, %ebp
jle 0x2ff48
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 0x2ff0e
movsd %xmm2, 0x10(%rsp)
testl %ebp, %ebp
xorps %xmm0, %xmm0
jle 0x2ff9e
xorl %ebx, %ebx
movsd %xmm0, 0x8(%rsp)
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)
movsd 0x8(%rsp), %xmm0
incq %rbx
cmpq %rbx, %r13
jne 0x2ff57
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %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 0x2fff1
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 0x3002b
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 0x30001
testl %ebp, %ebp
jle 0x3004b
xorl %eax, %eax
movss (%r15,%rax,4), %xmm0
divss (%r14,%rax,4), %xmm0
movss %xmm0, (%r14,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x30031
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 0x10d91
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
movq 0x38(%rsp), %rdi
leaq 0x2a936(%rip), %rsi     # 0x5a9b7
movsd 0x48(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movb $0x4, %al
callq 0x2190
movss 0x2c(%rsp), %xmm0
addss 0x2723c(%rip), %xmm0    # 0x572e4
movss 0x2723c(%rip), %xmm1    # 0x572ec
ucomiss %xmm0, %xmm1
jae 0x2fee3
xorps %xmm0, %xmm0
cvtss2sd 0x18(%rsp), %xmm0
leaq 0x2a905(%rip), %rsi     # 0x5a9ce
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x2190
movss 0x20f30(%rip), %xmm0    # 0x51010
subss 0x1c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x2a8f9(%rip), %rsi     # 0x5a9ea
movq %rbx, %rdi
movb $0x1, %al
callq 0x2190
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