// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm1, -0x50(%rbp)
movsd %xmm0, -0x48(%rbp)
movl %esi, %r14d
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r15
movq 0x10(%rbx), %rdi
callq 0x21f0
movq 0x18(%rbx), %rdi
callq 0x21f0
leal 0x1(%r14), %r13d
leaq (,%r13,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
jle 0x4854
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x40(%rbp)
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
decq %r13
xorl %r15d, %r15d
leaq 0x1(%r15), %r14
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, -0x38(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
divsd -0x40(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
callq 0x4868
movsd %xmm0, -0x30(%rbp)
movsd -0x38(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
callq 0x4868
movsd %xmm0, -0x38(%rbp)
movq 0x10(%rbx), %rax
movsd %xmm0, 0x8(%rax,%r15,8)
movq 0x18(%rbx), %rax
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax,%r15,8)
movq 0x20(%rbx), %rdi
cvttsd2si 0x8(%rbx), %esi
callq 0x293a
movsd 0x8(%rbx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x20(%rbx), %rdi
cvttsd2si %xmm1, %esi
movsd -0x38(%rbp), %xmm0
callq 0x293a
divsd 0x8(%rbx), %xmm0
subsd -0x30(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
divsd %xmm1, %xmm0
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
movq %r14, %r15
cmpq %r14, %r13
jne 0x4785
jmp 0x4858
movsd (%rbx), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop