// File: /workspace/github_cpp_makefile_star2_O2/ashlall[P]StreamStats/ChiSquareExperiments/./../SourceCode/ChiSquare/ChiSquareContinuous.cpp
// Function: ChiSquareContinuous::two_sample_statistic(ChiSquareContinuous const&, int)
double ChiSquareContinuous::two_sample_statistic(const ChiSquareContinuous& distribution_2, int num_buckets)
{
  QuantileSketch *quantile_sketch_2 = distribution_2.quantile_sketch;
  int stream_size_1 = quantile_sketch->get_stream_size();
  int stream_size_2 = quantile_sketch_2->get_stream_size();
  double frequency_1 = (double)stream_size_1 / num_buckets;
  chi_squared = 0;

  // 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];

  double constant_1 = sqrt((double)stream_size_2/stream_size_1);
  double constant_2 = sqrt((double)stream_size_1/stream_size_2);
  for (int i = 1; i <= num_buckets; i++)
  {
    // Finds the upper and lower values from quantile_sketch that mark the bin.
    double lower_interval = quantile_sketch->getQuantile(((double)i-1)/num_buckets);
    double upper_interval = quantile_sketch->getQuantile((double)i/num_buckets);
    
    upper_bins[i] = upper_interval;
    lower_bins[i] = lower_interval;

    // Finds the upper and lower quantiles from quantile_sketch_2 that 
    // correspond to lower_interval and upper_interval. 
    double lower_value, upper_value;
    lower_value = (quantile_sketch_2->reverseQuantile(lower_interval, memory))/memory;
    upper_value = (quantile_sketch_2->reverseQuantile(upper_interval, memory))/memory;

    // Calculates the chi-squared statistic with an adjusted formula for the 
    // two-sample case.
    double frequency_2 = stream_size_2 * (upper_value - lower_value);
    double value = frequency_1 * constant_1 - frequency_2 * constant_2;
    chi_squared += (value * value) / (frequency_1 + frequency_2);
  }
  return chi_squared;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebx
movq %rdi, %r14
movq 0x20(%rsi), %r15
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x10(%rax)
movq %rax, %r12
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x30(%rbp)
cvtsi2sd %ebx, %xmm0
movsd %xmm0, -0x48(%rbp)
andq $0x0, (%r14)
movq 0x10(%r14), %rdi
callq 0x21b0
movq 0x18(%r14), %rdi
callq 0x21b0
leal 0x1(%rbx), %eax
shlq $0x3, %rax
cmpl $-0x1, %ebx
pushq $-0x1
popq %r13
cmovgeq %rax, %r13
movq %r13, %rdi
callq 0x2030
movq %rax, 0x10(%r14)
movq %r13, %rdi
callq 0x2030
movsd -0x30(%rbp), %xmm1
movq %rax, 0x18(%r14)
cvtsi2sd %r12d, %xmm0
movsd %xmm0, -0x60(%rbp)
divsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jb 0x48b7
sqrtsd %xmm0, %xmm0
jmp 0x48c5
callq 0x2160
xorpd %xmm2, %xmm2
movsd -0x30(%rbp), %xmm1
movsd %xmm0, -0x38(%rbp)
movapd %xmm1, %xmm0
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
divsd -0x60(%rbp), %xmm1
ucomisd %xmm2, %xmm1
jb 0x48e9
sqrtsd %xmm1, %xmm0
jmp 0x48f2
movapd %xmm1, %xmm0
callq 0x2160
movsd %xmm0, -0x50(%rbp)
xorl %r13d, %r13d
testl %ebx, %ebx
cmovlel %r13d, %ebx
movsd -0x38(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
cmpq %r13, %rbx
je 0x49e2
movq 0x20(%r14), %rdi
leaq 0x1(%r13), %r12
cvtsi2sd %r12d, %xmm0
movsd %xmm0, -0x30(%rbp)
cvtsi2sd %r13d, %xmm0
divsd -0x48(%rbp), %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, -0x40(%rbp)
movq 0x20(%r14), %rdi
movsd -0x30(%rbp), %xmm0
divsd -0x48(%rbp), %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, -0x30(%rbp)
movq 0x10(%r14), %rax
movsd %xmm0, 0x8(%rax,%r13,8)
movq 0x18(%r14), %rax
movsd -0x40(%rbp), %xmm0
movsd %xmm0, 0x8(%rax,%r13,8)
cvttsd2si 0x8(%r14), %esi
movq %r15, %rdi
callq 0x28d4
movsd 0x8(%r14), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
cvttsd2si %xmm1, %esi
movq %r15, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x28d4
divsd 0x8(%r14), %xmm0
subsd -0x40(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm2
subsd %xmm1, %xmm2
mulsd %xmm2, %xmm2
addsd -0x58(%rbp), %xmm0
divsd %xmm0, %xmm2
addsd (%r14), %xmm2
movsd %xmm2, (%r14)
movq %r12, %r13
jmp 0x490f
movsd (%r14), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq