// File: /workspace/github_cpp_makefile_star2_O1/ashlall[P]StreamStats/ChiSquareExperiments/./../SourceCode/ChiSquare/ChiSquareContinuous.cpp
// Function: ChiSquareContinuous::calculate_statistic_ifNormal(int, double, double)
double ChiSquareContinuous::calculate_statistic_ifNormal(int num_buckets, double mean, double SD)
{	
  int stream_size = quantile_sketch->get_stream_size();
  double expected_frequency = (double)stream_size/num_buckets;
  
  // Reallocates the memory for upper_bins and lower_bins.
  delete upper_bins;
  delete lower_bins;
  upper_bins = new double[num_buckets+1];
  lower_bins = new double[num_buckets+1];
  for (int i = 1; i <= num_buckets; i++)
  {
    // Finds the upper and lower values from the normal distribution that
    // mark the interval, or bin.
    double lower_interval = normal_cdf_inverse(((double)i-1)/num_buckets, mean, SD);
    double upper_interval = normal_cdf_inverse((double)i/num_buckets, mean, SD);

    upper_bins[i] = upper_interval;
    lower_bins[i] = lower_interval;

    // Finds the upper and lower quantiles from the quantile_sketch at the
    // lower_interval and upper_interval values.
    double lower_value, upper_value;
    lower_value = (quantile_sketch->reverseQuantile(lower_interval, memory))/memory;
    upper_value = (quantile_sketch->reverseQuantile(upper_interval, memory))/memory;

    double observed_frequency = stream_size * (upper_value - lower_value);
    double lambda = fabs(observed_frequency - expected_frequency);
    chi_squared = chi_squared + ((lambda*lambda)/expected_frequency);
  }		
  return chi_squared;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movsd %xmm1, -0x48(%rbp)
movsd %xmm0, -0x38(%rbp)
movl %esi, %r14d
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r15
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x48dc
callq 0x21f0
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x48ea
callq 0x21f0
movslq %r14d, %rax
leaq 0x8(,%rax,8), %rax
cmpl $-0x1, %r14d
movq $-0x1, %r12
cmovgeq %rax, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x18(%rbx)
testl %r14d, %r14d
movsd -0x38(%rbp), %xmm1
jle 0x4a1e
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
cvtsi2sd %r14d, %xmm3
movsd %xmm0, -0x58(%rbp)
movsd %xmm3, -0x40(%rbp)
divsd %xmm3, %xmm0
movsd %xmm0, -0x50(%rbp)
movl %r14d, %r14d
xorl %r12d, %r12d
leaq 0x1(%r12), %r15
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd -0x40(%rbp), %xmm0
movsd -0x48(%rbp), %xmm2
callq 0x4a30
movsd %xmm0, -0x28(%rbp)
movsd -0x30(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq 0x4a30
movsd %xmm0, -0x30(%rbp)
movq 0x10(%rbx), %rax
movsd %xmm0, 0x8(%rax,%r12,8)
movq 0x18(%rbx), %rax
movsd -0x28(%rbp), %xmm0
movsd %xmm0, 0x8(%rax,%r12,8)
movq 0x20(%rbx), %rdi
cvttsd2si 0x8(%rbx), %esi
callq 0x2966
movsd 0x8(%rbx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movq 0x20(%rbx), %rdi
cvttsd2si %xmm1, %esi
movsd -0x30(%rbp), %xmm0
callq 0x2966
movsd -0x38(%rbp), %xmm1
divsd 0x8(%rbx), %xmm0
subsd -0x28(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd -0x50(%rbp), %xmm3
subsd %xmm3, %xmm0
mulsd %xmm0, %xmm0
divsd %xmm3, %xmm0
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
movq %r15, %r12
cmpq %r15, %r14
jne 0x4950
movsd (%rbx), %xmm0
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop