// File: ashlall[P]StreamStats/ChiSquareExperiments/./DataGenerator.cpp
// Function: DataGenerator::get_stat_two_sample(DataGenerator const&, int, double*, double*)
double DataGenerator::get_stat_two_sample(const DataGenerator& other, int num_buckets, double *upper_intervals, double *lower_intervals)
{
  if (num_buckets <= 0)
    throw ParameterError();
  double *stream2 = other.stream;
  int stream2_size = other.stream_size;
  double constant_1 = sqrt((double)stream2_size/stream_size);
  double constant_2 = sqrt((double)stream_size/stream2_size);
  double chi_squared = 0;
  for (int i = 0; i < num_buckets; i++)
  {
    double frequency1 = 0, frequency2 = 0;
    for (int j = 0; j < stream_size; j++)
    {
      if (stream[j] <= upper_intervals[i+1] && stream[j] > lower_intervals[i+1])
	frequency1++;
    }
    for (int j = 0; j < stream2_size; j++)
    {
      if (stream2[j] <= upper_intervals[i+1] && stream2[j] > lower_intervals[i+1])
	frequency2++;
    }
    double lambda = frequency1 * constant_1 - frequency2 * constant_2;
    chi_squared += (lambda * lambda) / (frequency1 + frequency2);
  }
  return chi_squared;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x14(%rbp)
jg 0x2979
movl $0x1, %edi
callq 0x20c0
movq %rax, %rdi
leaq 0x172e0(%rip), %rsi     # 0x19c50
xorl %eax, %eax
movl %eax, %edx
callq 0x22f0
movq -0x80(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, -0x30(%rbp)
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, -0x34(%rbp)
cvtsi2sdl -0x34(%rbp), %xmm0
cvtsi2sdl 0x8(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x2130
movq -0x80(%rbp), %rax
movsd %xmm0, -0x40(%rbp)
cvtsi2sdl 0x8(%rax), %xmm0
cvtsi2sdl -0x34(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x2130
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2b4b
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movq -0x80(%rbp), %rcx
movl -0x6c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x2a6f
movq -0x80(%rbp), %rax
movq (%rax), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x20(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x2a62
movq -0x80(%rbp), %rax
movq (%rax), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x2a62
movsd 0xf5b0(%rip), %xmm0     # 0x12008
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x2a64
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x29f8
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x2ae3
movq -0x30(%rbp), %rax
movslq -0x70(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x20(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x2ad6
movq -0x30(%rbp), %rax
movslq -0x70(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x2ad6
movsd 0xf53c(%rip), %xmm0     # 0x12008
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
jmp 0x2ad8
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x2a76
movsd -0x60(%rbp), %xmm0
movsd -0x40(%rbp), %xmm2
movsd -0x68(%rbp), %xmm1
mulsd -0x48(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
mulsd -0x78(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
addsd -0x68(%rbp), %xmm1
divsd %xmm1, %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x29d5
movsd -0x50(%rbp), %xmm0
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)