// File: /workspace/github_cpp_makefile_star2_O2/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, -0x40(%rbp)
movl %esi, %r14d
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
cvtsi2sd %eax, %xmm0
cvtsi2sd %r14d, %xmm1
movsd %xmm0, -0x58(%rbp)
movsd %xmm1, -0x38(%rbp)
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movq 0x10(%rbx), %rdi
callq 0x21b0
movq 0x18(%rbx), %rdi
callq 0x21b0
leal 0x1(%r14), %eax
shlq $0x3, %rax
cmpl $-0x1, %r14d
pushq $-0x1
popq %r15
cmovgeq %rax, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x18(%rbx)
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
cmpq %r12, %r14
je 0x4320
leaq 0x1(%r12), %r15
cvtsi2sd %r15d, %xmm0
movsd %xmm0, -0x30(%rbp)
cvtsi2sd %r12d, %xmm0
divsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq 0x4332
movsd %xmm0, -0x28(%rbp)
movsd -0x30(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq 0x4332
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 0x28d4
movsd 0x8(%rbx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movq 0x20(%rbx), %rdi
cvttsd2si %xmm1, %esi
movsd -0x30(%rbp), %xmm0
callq 0x28d4
divsd 0x8(%rbx), %xmm0
subsd -0x28(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
divsd %xmm1, %xmm0
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
movq %r15, %r12
jmp 0x4253
movsd (%rbx), %xmm0
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop