// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movq %rdi, %rbx
movq 0x20(%rsi), %r14
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r13
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %rax, -0x38(%rbp)
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x30(%rbp)
movq $0x0, (%rbx)
movq 0x10(%rbx), %rdi
callq 0x21f0
movq 0x18(%rbx), %rdi
callq 0x21f0
leal 0x1(%r15), %r12d
leaq (,%r12,8), %rax
cmpl $-0x1, %r15d
movq $-0x1, %r13
cmovgeq %rax, %r13
movq %r13, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
movq %r13, %rdi
callq 0x2030
movsd -0x30(%rbp), %xmm2
movq %rax, 0x18(%rbx)
cvtsi2sdl -0x38(%rbp), %xmm1
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
movsd %xmm1, -0x58(%rbp)
jb 0x4e7a
sqrtsd %xmm0, %xmm3
jmp 0x4e91
callq 0x2190
xorpd %xmm4, %xmm4
movsd -0x58(%rbp), %xmm1
movsd -0x30(%rbp), %xmm2
movapd %xmm0, %xmm3
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
ucomisd %xmm4, %xmm0
jb 0x4ea8
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x4ec0
movsd %xmm3, -0x40(%rbp)
callq 0x2190
movsd -0x40(%rbp), %xmm3
movsd -0x30(%rbp), %xmm2
movapd %xmm0, %xmm4
testl %r15d, %r15d
jle 0x4fd0
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, -0x38(%rbp)
divsd %xmm0, %xmm2
decq %r12
xorl %r13d, %r13d
mulsd %xmm2, %xmm3
movsd %xmm2, -0x30(%rbp)
movsd %xmm3, -0x40(%rbp)
movsd %xmm4, -0x60(%rbp)
movq 0x20(%rbx), %rdi
leaq 0x1(%r13), %r15
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd -0x38(%rbp), %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, -0x48(%rbp)
movq 0x20(%rbx), %rdi
movsd -0x50(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, -0x50(%rbp)
movq 0x10(%rbx), %rax
movsd %xmm0, 0x8(%rax,%r13,8)
movq 0x18(%rbx), %rax
movsd -0x48(%rbp), %xmm0
movsd %xmm0, 0x8(%rax,%r13,8)
cvttsd2si 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x293a
movsd 0x8(%rbx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
cvttsd2si %xmm1, %esi
movq %r14, %rdi
movsd -0x50(%rbp), %xmm0
callq 0x293a
movsd -0x60(%rbp), %xmm2
movsd -0x40(%rbp), %xmm3
movapd %xmm0, %xmm1
divsd 0x8(%rbx), %xmm1
subsd -0x48(%rbp), %xmm1
mulsd -0x58(%rbp), %xmm1
mulsd %xmm1, %xmm2
movapd %xmm3, %xmm0
subsd %xmm2, %xmm0
movsd -0x30(%rbp), %xmm2
mulsd %xmm0, %xmm0
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
movq %r15, %r13
cmpq %r15, %r12
jne 0x4ef3
jmp 0x4fd4
movsd (%rbx), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop