// File: 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
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, -0x20(%rbp)
movq 0x20(%rax), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %rcx
movq -0x88(%rbp), %rax
movl %ecx, -0x28(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x10(%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x6712
movq -0x80(%rbp), %rdi
callq 0x21a0
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x6736
movq -0x90(%rbp), %rdi
callq 0x21a0
movl -0x14(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x18(%rax)
cvtsi2sdl -0x28(%rbp), %xmm0
cvtsi2sdl -0x24(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x2130
movsd %xmm0, -0x38(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
cvtsi2sdl -0x28(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x2130
movsd %xmm0, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x692c
movq -0x88(%rbp), %rax
movq 0x20(%rax), %rdi
cvtsi2sdl -0x44(%rbp), %xmm0
movsd 0xb809(%rip), %xmm1     # 0x12008
subsd %xmm1, %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
divsd %xmm1, %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x88(%rbp), %rax
movsd %xmm0, -0x50(%rbp)
movq 0x20(%rax), %rdi
cvtsi2sdl -0x44(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
divsd %xmm1, %xmm0
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x88(%rbp), %rax
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movq 0x10(%rax), %rcx
movslq -0x44(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movsd -0x50(%rbp), %xmm0
movq 0x18(%rax), %rcx
movslq -0x44(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq -0x20(%rbp), %rdi
movsd -0x50(%rbp), %xmm0
cvttsd2si 0x8(%rax), %esi
callq 0x2b60
movq -0x88(%rbp), %rax
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x20(%rbp), %rdi
movsd -0x58(%rbp), %xmm0
cvttsd2si 0x8(%rax), %esi
callq 0x2b60
movq -0x88(%rbp), %rax
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
cvtsi2sdl -0x28(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
subsd -0x60(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm2
movsd -0x70(%rbp), %xmm1
mulsd -0x40(%rbp), %xmm1
movq %xmm1, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
mulsd -0x78(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
addsd -0x70(%rbp), %xmm1
divsd %xmm1, %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x67db
movq -0x88(%rbp), %rax
movsd (%rax), %xmm0
addq $0x90, %rsp
popq %rbp
retq