// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq $0x0, (%rbx)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x4fd9
callq 0x21f0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x4ff1
callq 0x21f0
movslq %r15d, %rax
leaq 0x8(,%rax,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
movq %rax, 0x18(%rbx)
cvtsi2sd %r12d, %xmm2
movapd %xmm2, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
movsd %xmm2, -0x50(%rbp)
jb 0x504a
sqrtsd %xmm0, %xmm3
jmp 0x5061
callq 0x2190
xorpd %xmm4, %xmm4
movsd -0x50(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
movapd %xmm0, %xmm3
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
ucomisd %xmm4, %xmm0
jb 0x5078
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x5090
movsd %xmm3, -0x38(%rbp)
callq 0x2190
movsd -0x38(%rbp), %xmm3
movsd -0x30(%rbp), %xmm1
movapd %xmm0, %xmm4
testl %r15d, %r15d
jle 0x519a
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, -0x58(%rbp)
divsd %xmm0, %xmm1
movl %r15d, %r15d
xorl %r13d, %r13d
mulsd %xmm1, %xmm3
movsd %xmm1, -0x30(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm4, -0x60(%rbp)
movq 0x20(%rbx), %rdi
leaq 0x1(%r13), %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd -0x58(%rbp), %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, -0x40(%rbp)
movq 0x20(%rbx), %rdi
movsd -0x48(%rbp), %xmm0
divsd -0x58(%rbp), %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movsd %xmm0, -0x48(%rbp)
movq 0x10(%rbx), %rax
movsd %xmm0, 0x8(%rax,%r13,8)
movq 0x18(%rbx), %rax
movsd -0x40(%rbp), %xmm0
movsd %xmm0, 0x8(%rax,%r13,8)
cvttsd2si 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x2966
movsd 0x8(%rbx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
cvttsd2si %xmm1, %esi
movq %r14, %rdi
movsd -0x48(%rbp), %xmm0
callq 0x2966
movsd -0x50(%rbp), %xmm5
movsd -0x60(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
divsd 0x8(%rbx), %xmm0
subsd -0x40(%rbp), %xmm0
mulsd %xmm5, %xmm0
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm2
movsd -0x30(%rbp), %xmm1
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm0
divsd %xmm0, %xmm2
addsd (%rbx), %xmm2
movsd %xmm2, (%rbx)
movq %r12, %r13
cmpq %r12, %r15
jne 0x50c3
movsd (%rbx), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop