// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x45452
leaq -0x90(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x3a7fb(%rip), %rsi     # 0x7fc09
movb $0x0, %al
callq 0x1200
leaq -0x90(%rbp), %rdi
leaq 0x36f4f(%rip), %rsi     # 0x7c372
callq 0x11b0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x3a7e4(%rip), %rsi     # 0x7fc1b
movb $0x0, %al
callq 0x1150
movq -0x40(%rbp), %rdi
leaq 0x3a720(%rip), %rsi     # 0x7fb69
movb $0x0, %al
callq 0x1150
jmp 0x45464
movq -0x28(%rbp), %rdi
leaq 0x3a7f0(%rip), %rsi     # 0x7fc4d
movb $0x0, %al
callq 0x1150
movss 0x36f74(%rip), %xmm0    # 0x7c3e0
movss %xmm0, -0xa4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xa8(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xac(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xb8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc8(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4556d
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0xf8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x10f0
movaps %xmm0, %xmm2
movsd -0xf8(%rbp), %xmm0
movsd -0xc8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x454b2
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x455ad
movsd -0xb8(%rbp), %xmm1
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x45574
movsd -0xc0(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm1
mulsd -0xb8(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdl -0x14(%rbp), %xmm2
movsd 0x30a72(%rip), %xmm1    # 0x76048
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xc8(%rbp), %xmm0
divsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0xb8(%rbp), %xmm0
callq 0x10f0
movaps %xmm0, %xmm1
movsd -0x100(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movss 0x30a3e(%rip), %xmm0    # 0x76068
movss %xmm0, -0x94(%rbp)
movss 0x36daa(%rip), %xmm0    # 0x7c3e4
ucomiss -0x94(%rbp), %xmm0
jb 0x459da
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x309fd(%rip), %xmm1    # 0x76058
divsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xd8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xe0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xe8(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x456f2
movq -0x8(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd -0xd0(%rbp), %xmm1
callq 0x1080
cvtsd2ss %xmm0, %xmm0
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x4568f
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x4575e
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x108(%rbp)
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x10f0
movaps %xmm0, %xmm2
movsd -0x108(%rbp), %xmm0
movsd -0xe0(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x456f9
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0xe0(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
movsd -0xd8(%rbp), %xmm0
callq 0x10f0
movsd -0x118(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x110(%rbp), %xmm0
subsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0xe0(%rbp), %xmm1
movss -0xa4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x457ec
movsd -0xe0(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xa4(%rbp)
movss -0x94(%rbp), %xmm0
movss %xmm0, -0xa8(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x45864
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm1
cvtss2sd %xmm1, %xmm1
divsd -0xd8(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0xf0(%rbp), %xmm0
movsd -0xf0(%rbp), %xmm2
movsd -0xe8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x457f3
movsd -0xc0(%rbp), %xmm1
movsd -0xe8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x458c1
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
divss (%rax,%rcx,4), %xmm0
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x45887
movq -0x38(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
leaq -0x9c(%rbp), %rcx
leaq -0xa0(%rbp), %r8
callq 0x15750
movss %xmm0, -0x98(%rbp)
movss -0x98(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movaps 0x36b96(%rip), %xmm0    # 0x7c490
pand %xmm0, %xmm1
movss -0xac(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x45933
movss -0x98(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0x36b6d(%rip), %xmm1    # 0x7c490
pand %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xac(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x45975
movq -0x40(%rbp), %rdi
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd -0xe0(%rbp), %xmm1
movsd -0xe8(%rbp), %xmm2
movss -0x98(%rbp), %xmm3
cvtss2sd %xmm3, %xmm3
leaq 0x3a318(%rip), %rsi     # 0x7fc84
movb $0x4, %al
callq 0x1150
jmp 0x459af
movq -0x28(%rbp), %rdi
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd -0xe0(%rbp), %xmm1
movsd -0xe8(%rbp), %xmm2
movss -0x98(%rbp), %xmm3
cvtss2sd %xmm3, %xmm3
leaq 0x3a2f1(%rip), %rsi     # 0x7fc99
movb $0x4, %al
callq 0x1150
jmp 0x459b1
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x39383(%rip), %xmm1    # 0x7ed48
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x94(%rbp)
jmp 0x45632
movq -0x28(%rbp), %rdi
movss -0xa8(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x3a2bf(%rip), %rsi     # 0x7fcb0
movb $0x1, %al
callq 0x1150
movq -0x28(%rbp), %rdi
movss 0x3068c(%rip), %xmm0    # 0x76090
subss -0xa4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x3a2b5(%rip), %rsi     # 0x7fccc
movb $0x1, %al
callq 0x1150
cmpl $0x0, -0x2c(%rbp)
je 0x45a42
movq -0x40(%rbp), %rdi
callq 0x10a0
leaq -0x90(%rbp), %rsi
leaq 0x31ab3(%rip), %rdi     # 0x774ee
movb $0x0, %al
callq 0x10d0
movq -0x38(%rbp), %rdi
callq 0x1040
movss -0xa8(%rbp), %xmm0
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax)