// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r12
movslq %edx, %r13
leaq (,%r13,4), %rdi
callq 0x21d0
movq %rax, %r14
leaq 0x28057(%rip), %rdi     # 0x54884
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, 0x38(%rsp)
movq %rbx, %rcx
callq 0x2280
xorl %ebx, %ebx
testl %r13d, %r13d
movl $0x0, %r13d
cmovgl %ebp, %r13d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, (%rsp)
movsd %xmm0, 0x18(%rsp)
cmpq %rbx, %r13
je 0x2c8bf
movss (%r15,%rbx,4), %xmm2
cvtss2sd %xmm2, %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
mulss %xmm2, %xmm2
cvtss2sd %xmm2, %xmm2
movsd (%rsp), %xmm1
addsd %xmm2, %xmm1
movsd %xmm1, (%rsp)
callq 0x2120
mulsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
incq %rbx
jmp 0x2c864
xorl %eax, %eax
movsd 0x8(%rsp), %xmm0
cmpq %rax, %r13
je 0x2c8e5
cvtss2sd (%r15,%rax,4), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r15,%rax,4)
incq %rax
jmp 0x2c8c7
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd (%rsp), %xmm3
divsd %xmm1, %xmm3
cvtsi2sd %ebp, %xmm2
movsd 0x2373e(%rip), %xmm1    # 0x50040
divsd %xmm2, %xmm1
addsd %xmm3, %xmm1
movsd %xmm1, 0x40(%rsp)
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
callq 0x2120
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x248a9(%rip), %xmm0    # 0x511f0
movss %xmm0, 0x24(%rsp)
movss 0x231bf(%rip), %xmm0    # 0x4fb14
movss 0x2489f(%rip), %xmm1    # 0x511fc
ucomiss %xmm0, %xmm1
jb 0x2cb0d
movss %xmm0, 0x34(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
mulsd 0x1e6f2(%rip), %xmm0    # 0x4b070
movsd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x2c9c7
cvtss2sd (%r12,%rbx,4), %xmm0
movsd (%rsp), %xmm1
callq 0x20a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rbx,4)
cvtss2sd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incq %rbx
jmp 0x2c98f
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
movsd %xmm0, (%rsp)
cmpq %rbx, %r13
je 0x2ca06
cvtss2sd (%r15,%rbx,4), %xmm0
movsd %xmm0, 0x28(%rsp)
cvtss2sd (%r14,%rbx,4), %xmm0
callq 0x2120
mulsd 0x28(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %rbx
jmp 0x2c9cd
movsd 0x8(%rsp), %xmm0
callq 0x2120
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm3
subsd %xmm1, %xmm3
cvtss2sd 0x24(%rsp), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x2ca3a
cvtsd2ss %xmm3, %xmm1
movss %xmm1, 0x24(%rsp)
xorpd %xmm4, %xmm4
xorl %eax, %eax
cmpq %rax, %r13
je 0x2ca68
cvtss2sd (%r15,%rax,4), %xmm1
cvtss2sd (%r14,%rax,4), %xmm2
divsd 0x8(%rsp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm4
incq %rax
jmp 0x2ca40
ucomisd %xmm3, %xmm0
movss 0x34(%rsp), %xmm0
ja 0x2ca7a
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
xorl %eax, %eax
cmpq %rax, %r13
je 0x2ca9e
movss (%r15,%rax,4), %xmm0
divss (%r14,%rax,4), %xmm0
movss %xmm0, (%r14,%rax,4)
incq %rax
jmp 0x2ca82
divsd 0x40(%rsp), %xmm4
movsd %xmm4, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
leaq 0x54(%rsp), %rcx
leaq 0x50(%rsp), %r8
movsd %xmm3, 0x28(%rsp)
callq 0xfe18
cvtss2sd %xmm0, %xmm3
movq 0x38(%rsp), %rdi
leaq 0x27de5(%rip), %rsi     # 0x548bb
movsd 0x48(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd (%rsp), %xmm2
movb $0x4, %al
callq 0x2190
movss 0x34(%rsp), %xmm0
addss 0x246f8(%rip), %xmm0    # 0x511f4
movss 0x8(%rsp), %xmm1
movss %xmm1, 0x14(%rsp)
jmp 0x2c955
cvtss2sd 0x14(%rsp), %xmm0
leaq 0x27db8(%rip), %rsi     # 0x548d2
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x2190
movss 0x1e4df(%rip), %xmm0    # 0x4b010
subss 0x24(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x27dac(%rip), %rsi     # 0x548ee
movq %rbx, %rdi
movb $0x1, %al
callq 0x2190
movq %r14, %rdi
callq 0x2050
movss 0x14(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq