// File: /workspace/github_cpp_makefile_star2_O2/ashlall[P]StreamStats/ChiSquareExperiments/VaryMemoryReal.cpp
// Function: main
int main(int argc, char* argv[])
{
  if (argc < 6)
  {
    cout << "usage: VaryMemoryReal lower-memory upper-memory filename1 filename2 num_bins\n";
    exit(1);
  }

  double lower = atof(argv[1]), upper = atof(argv[2]);
  char *filename1 = argv[3], *filename2 = argv[4];
  int num_buckets = atoi(argv[5]);

  if (lower <=0 || upper <= 0)
  {
    cout << "The memory must be greater than 0.\n";
    exit(1);
  }
  if (num_buckets <= 0)
  {
    cout << "The number of buckets must be greater than 0.\n";
    exit(1);
  }

  // finds the number of times the experiment will run
  double memory_percent;
  int repeats = 0;
  double mem = lower;
  while (mem <= (upper + 0.0000001)) // accounts for rounding error
  {
    repeats++;
    mem *= sqrt(10);
  }

  double actual_values[repeats];
  double GK_values[repeats];
  double QD_values[repeats];
  double RS_values[repeats];
  double percents[repeats];
  std::vector<double> data1, data2;

  // creates and initializes the log file
  ofstream data_file;
  char str[100];
  name_file(str, argv, 0);
  data_file.open(str);
  ifstream input_file;
  char output[100];
  int stream_size1 = 0, stream_size2 = 0;

  std::default_random_engine generator(1);
  std::uniform_real_distribution<double> distribution(0.0, 1.0);

  input_file.open(filename1);
  while (!input_file.eof())
  {
    input_file >> output;
    data1.push_back(atof(output) + (distribution(generator) * 0.000000001));
    stream_size1++;
  }
  input_file.close();

  input_file.open(filename2);
  while (!input_file.eof())
  {
    input_file >> output;
    data2.push_back(atof(output) + (distribution(generator) * 0.000000001));
    stream_size2++;
  }
  input_file.close();

  data_file << filename1 << endl << filename2 << endl;
  data_file << "num_buckets: " << num_buckets << endl;
  data_file << "stream 1 size: " << stream_size1 << endl;
  data_file << "stream 2 size: " << stream_size2 << endl;
  memory_percent = lower;
  int i = 0;
  while (memory_percent <= (upper + 0.00000001)) //accounts for rounding
  {
    data_file << "memory percent: " << memory_percent << endl;
    percents[i] = memory_percent;
    int sample_size1 = memory_percent * stream_size1;
    int sample_size2 = memory_percent * stream_size2;

    // calculates GK statistic
    ChiSquareContinuous GK_sketch1(sample_size1,1);
    for (std::vector<double>::iterator j = data1.begin(); j != data1.end();j++)
      GK_sketch1.insert(*j);
    ChiSquareContinuous GK_sketch2(sample_size2,1);
    for (std::vector<double>::iterator j = data2.begin(); j != data2.end();j++)
      GK_sketch2.insert(*j);
    
    double GK_stat = GK_sketch1.two_sample_statistic(GK_sketch2, num_buckets);
    GK_values[i] = GK_stat;
    data_file << "GK = " << GK_stat << endl;

    // calculates real statistic
    double *upper_intervals = GK_sketch1.get_upper();
    double *lower_intervals = GK_sketch1.get_lower();
    double constant_1 = sqrt((double)stream_size2/stream_size1);
    double constant_2 = sqrt((double)stream_size1/stream_size2);
    double chi_squared = 0;

    for (int i = 0; i < num_buckets; i++)
    {
      double frequency1 = 0, frequency2 = 0;
      for (std::vector<double>::iterator j = data1.begin(); j!=data1.end();j++)
	{
        if (*j <= upper_intervals[i+1] && *j > lower_intervals[i+1])
          frequency1++;
      }
      for (std::vector<double>::iterator j = data2.begin(); j!=data2.end();j++)
      {
        if (*j <= upper_intervals[i+1] && *j > lower_intervals[i+1])
	  frequency2++;
      }

      double lambda = frequency1 * constant_1 - frequency2 * constant_2;
      chi_squared += (lambda * lambda) / (frequency1 + frequency2);
    }

    actual_values[i] = chi_squared;
    data_file << "actual = " << chi_squared << endl;

    memory_percent *= sqrt(10);
    i++;
  }
  data_file.close();

  // creates pvalue file
  name_file(str, argv, 3);
  data_file.open(str);
  
  // creates table file
  ofstream data2_file;
  name_file(str, argv, 1);
  data2_file.open(str);

  // creates extra file
  ofstream data3_file;
  name_file(str, argv, 2);
  data3_file.open(str);

  int deg_freedom = num_buckets;
  if (stream_size1 != stream_size2)
    deg_freedom--;
  for (int i = 0; i < repeats; i++)
  {
    // adds values to the table
    data2_file << percents[i] * 100 << "\t";
    double error = abs(pochisq(GK_values[i], deg_freedom) - pochisq(actual_values[i], deg_freedom));
    data2_file << error << endl;

    // adds values to pvalue file
    data_file << pochisq(actual_values[i], deg_freedom) << " actual" << endl;
    data_file << pochisq(GK_values[i], deg_freedom) << " GK" << endl;

    // adds values to extra file
    data3_file << percents[i] * 100 << "\t";
    error = abs(GK_values[i] - actual_values[i]) / actual_values[i]; 
    data3_file << error << endl;
  }
  data3_file.close();
  data2_file.close();
  data_file.close();
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9b8, %rsp            # imm = 0x9B8
cmpl $0x5, %edi
jle 0x4f44
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
callq 0x2220
movsd %xmm0, -0x50(%rbp)
movq 0x10(%rbx), %rdi
callq 0x2220
movsd %xmm0, -0x58(%rbp)
movq 0x18(%rbx), %rax
movq %rax, -0x70(%rbp)
movq 0x20(%rbx), %rax
movq %rax, -0x30(%rbp)
movq 0x28(%rbx), %rdi
callq 0x2290
movsd -0x58(%rbp), %xmm1
xorpd %xmm0, %xmm0
ucomisd -0x50(%rbp), %xmm0
jae 0x4f54
ucomisd %xmm1, %xmm0
jae 0x4f54
movl %eax, %r15d
testl %eax, %eax
jle 0x4f64
movsd 0x3a46(%rip), %xmm0     # 0x8168
addsd -0x58(%rbp), %xmm0
xorl %eax, %eax
movsd -0x50(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x4741
mulsd 0x3a34(%rip), %xmm1     # 0x8170
incq %rax
jmp 0x472e
movq %rbx, -0x60(%rbp)
movq %rsp, %rcx
movq %rax, -0xe8(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
xorpd %xmm0, %xmm0
movapd %xmm0, -0xb0(%rbp)
andq $0x0, -0xa0(%rbp)
movapd %xmm0, -0x90(%rbp)
andq $0x0, -0x80(%rbp)
leaq -0x570(%rbp), %rdi
callq 0x22f0
leaq -0x170(%rbp), %rbx
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0x5028
leaq -0x570(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2050
leaq -0x778(%rbp), %rdi
callq 0x2200
andq $0x0, -0x100(%rbp)
movq $0x1, -0xd0(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0xf8(%rbp)
leaq -0x778(%rbp), %rdi
pushq $0x8
popq %rdx
movq -0x70(%rbp), %rsi
callq 0x2140
movl $0x0, -0x38(%rbp)
leaq -0x9e0(%rbp), %r13
leaq -0xd0(%rbp), %r14
leaq -0xb0(%rbp), %rbx
leaq -0x370(%rbp), %r12
movq -0x778(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x758(%rbp,%rax)
jne 0x48a7
leaq -0x778(%rbp), %rdi
movq %r13, %rsi
callq 0x5307
movq %r13, %rdi
callq 0x2220
movsd %xmm0, -0x40(%rbp)
leaq -0x100(%rbp), %rdi
movq %r14, %rsi
callq 0x532a
mulsd 0x38fe(%rip), %xmm0     # 0x8188
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x370(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7274
incl -0x38(%rbp)
jmp 0x4842
leaq -0x778(%rbp), %rdi
callq 0x2080
leaq -0x778(%rbp), %rdi
pushq $0x8
popq %rdx
movq -0x30(%rbp), %rsi
callq 0x2140
movl $0x0, -0x34(%rbp)
leaq -0x9e0(%rbp), %r14
leaq -0xd0(%rbp), %r12
leaq -0x90(%rbp), %rbx
leaq -0x370(%rbp), %r13
movq -0x778(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x758(%rbp,%rax)
jne 0x494e
leaq -0x778(%rbp), %rdi
movq %r14, %rsi
callq 0x5307
movq %r14, %rdi
callq 0x2220
movsd %xmm0, -0x40(%rbp)
leaq -0x100(%rbp), %rdi
movq %r12, %rsi
callq 0x532a
mulsd 0x3857(%rip), %xmm0     # 0x8188
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x370(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7274
incl -0x34(%rbp)
jmp 0x48e9
leaq -0x778(%rbp), %rdi
callq 0x2080
leaq -0x570(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
leaq 0x39b0(%rip), %rsi      # 0x833d
leaq -0x570(%rbp), %rdi
callq 0x21e0
movq %rax, %rdi
movl %r15d, %esi
callq 0x2320
movq %rax, %rdi
callq 0x20f0
leaq 0x3998(%rip), %rsi      # 0x834b
leaq -0x570(%rbp), %rdi
callq 0x21e0
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x2320
movq %rax, %rdi
callq 0x20f0
leaq 0x3982(%rip), %rsi      # 0x835b
leaq -0x570(%rbp), %rdi
callq 0x21e0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x2320
movq %rax, %rdi
callq 0x20f0
movsd -0x58(%rbp), %xmm0
addsd 0x3773(%rip), %xmm0     # 0x8178
movsd %xmm0, -0x58(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm1
cvtsi2sdl -0x34(%rbp), %xmm2
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd %xmm1, -0xe0(%rbp)
movapd %xmm1, %xmm0
movsd %xmm2, -0xd8(%rbp)
divsd %xmm2, %xmm0
sqrtsd %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl %r15d, %r14d
xorl %ebx, %ebx
leaq -0x570(%rbp), %r12
leaq -0x370(%rbp), %r13
movsd -0x50(%rbp), %xmm1
movl %r15d, -0x74(%rbp)
movsd %xmm1, -0x50(%rbp)
movsd -0x58(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x4cd1
movq %r12, %rdi
leaq 0x38eb(%rip), %rsi      # 0x836b
callq 0x21e0
movq %rax, %rdi
movsd -0x50(%rbp), %xmm0
callq 0x22a0
movq %rax, %rdi
callq 0x20f0
movq -0xc8(%rbp), %rax
movsd -0x50(%rbp), %xmm0
movq %rbx, -0xf0(%rbp)
movsd %xmm0, (%rax,%rbx,8)
mulsd -0xe0(%rbp), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x3c8c
movsd -0x50(%rbp), %xmm0
mulsd -0xd8(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x30(%rbp)
movq -0xb0(%rbp), %r15
movq -0x350(%rbp), %rbx
cmpq -0xa8(%rbp), %r15
je 0x4b0b
movsd (%r15), %xmm0
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
addq $0x8, %r15
jmp 0x4aef
cvtsi2sdl -0x30(%rbp), %xmm0
leaq -0x978(%rbp), %rdi
pushq $0x1
popq %rsi
callq 0x3c8c
movq -0x90(%rbp), %r15
movq -0x958(%rbp), %rbx
cmpq -0x88(%rbp), %r15
je 0x4b49
movsd (%r15), %xmm0
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
addq $0x8, %r15
jmp 0x4b2d
movq %r13, %rdi
leaq -0x978(%rbp), %rsi
movl -0x74(%rbp), %r15d
movl %r15d, %edx
callq 0x4406
movq -0xf0(%rbp), %rbx
movq -0xc0(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
movq %r12, %rdi
leaq 0x3800(%rip), %rsi      # 0x837c
movsd %xmm0, -0x30(%rbp)
callq 0x21e0
movsd -0x30(%rbp), %xmm0
movq %rax, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x20f0
movq -0x360(%rbp), %rax
movq -0x358(%rbp), %rcx
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %r8
xorpd %xmm4, %xmm4
xorl %r9d, %r9d
cmpq %r14, %r9
je 0x4c69
incq %r9
xorpd %xmm0, %xmm0
movq %rdx, %r10
cmpq %rsi, %r10
je 0x4c0b
movsd (%r10), %xmm1
movsd (%rax,%r9,8), %xmm2
ucomisd %xmm1, %xmm2
jb 0x4c05
ucomisd (%rcx,%r9,8), %xmm1
jbe 0x4c05
addsd 0x340b(%rip), %xmm0     # 0x8010
addq $0x8, %r10
jmp 0x4bdf
xorpd %xmm1, %xmm1
movq %rdi, %r10
cmpq %r8, %r10
je 0x4c3e
movsd (%r10), %xmm2
movsd (%rax,%r9,8), %xmm3
ucomisd %xmm2, %xmm3
jb 0x4c38
ucomisd (%rcx,%r9,8), %xmm2
jbe 0x4c38
addsd 0x33d8(%rip), %xmm1     # 0x8010
addq $0x8, %r10
jmp 0x4c12
movsd -0x70(%rbp), %xmm2
mulsd %xmm1, %xmm2
movapd %xmm0, %xmm3
mulsd -0x40(%rbp), %xmm3
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm3
addsd %xmm1, %xmm0
divsd %xmm0, %xmm3
addsd %xmm3, %xmm4
jmp 0x4bcc
movq -0xb8(%rbp), %rax
movsd %xmm4, (%rax,%rbx,8)
movq %r12, %rdi
leaq 0x3703(%rip), %rsi      # 0x8382
movsd %xmm4, -0x30(%rbp)
callq 0x21e0
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22a0
movq %rax, %rdi
callq 0x20f0
movsd -0x50(%rbp), %xmm0
mulsd 0x34c5(%rip), %xmm0     # 0x8170
movsd %xmm0, -0x50(%rbp)
incq %rbx
leaq -0x978(%rbp), %rdi
callq 0x3d82
movq %r13, %rdi
callq 0x3d82
movsd -0x50(%rbp), %xmm1
jmp 0x4a62
leaq -0x570(%rbp), %rdi
callq 0x2250
leaq -0x170(%rbp), %rbx
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
callq 0x5028
leaq -0x570(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2050
leaq -0x370(%rbp), %rdi
callq 0x22f0
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
callq 0x5028
leaq -0x370(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2050
leaq -0x978(%rbp), %rdi
callq 0x22f0
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
callq 0x5028
leaq -0x978(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2050
xorl %eax, %eax
movl -0x34(%rbp), %ecx
cmpl %ecx, -0x38(%rbp)
setne %al
subl %eax, %r15d
xorl %r14d, %r14d
leaq 0x3615(%rip), %rbx      # 0x838c
leaq -0x570(%rbp), %r12
leaq -0x978(%rbp), %r13
subq $0x1, -0xe8(%rbp)
jb 0x4ec7
movq -0xc8(%rbp), %rax
movsd (%rax,%r14), %xmm0
mulsd 0x33d8(%rip), %xmm0     # 0x8180
leaq -0x370(%rbp), %rdi
movsd %xmm0, -0x30(%rbp)
callq 0x22a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
movq -0xc0(%rbp), %rax
movsd (%rax,%r14), %xmm0
movaps %xmm0, -0x70(%rbp)
movl %r15d, %edi
callq 0x3870
movaps %xmm0, -0x50(%rbp)
movq -0xb8(%rbp), %rax
movsd (%rax,%r14), %xmm0
movsd %xmm0, -0x40(%rbp)
movl %r15d, %edi
callq 0x3870
movapd -0x50(%rbp), %xmm1
subsd %xmm0, %xmm1
andpd 0x33b4(%rip), %xmm1     # 0x81c0
movapd %xmm1, %xmm0
leaq -0x370(%rbp), %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x20f0
movsd -0x40(%rbp), %xmm0
movl %r15d, %edi
callq 0x3870
movq %r12, %rdi
callq 0x22a0
movq %rax, %rdi
leaq 0x354b(%rip), %rsi      # 0x838e
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movapd -0x70(%rbp), %xmm0
movl %r15d, %edi
callq 0x3870
movq %r12, %rdi
callq 0x22a0
movq %rax, %rdi
leaq 0x3527(%rip), %rsi      # 0x8396
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %r13, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
movapd -0x70(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
subsd %xmm1, %xmm0
andpd 0x3316(%rip), %xmm0     # 0x81c0
divsd %xmm1, %xmm0
movq %r13, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x20f0
addq $0x8, %r14
jmp 0x4d85
leaq -0x978(%rbp), %rdi
callq 0x2250
leaq -0x370(%rbp), %rdi
callq 0x2250
leaq -0x570(%rbp), %rdi
callq 0x2250
leaq -0x978(%rbp), %rdi
callq 0x2270
leaq -0x370(%rbp), %rdi
callq 0x2270
leaq -0x778(%rbp), %rdi
callq 0x2090
leaq -0x570(%rbp), %rdi
callq 0x2270
leaq -0x90(%rbp), %rdi
callq 0x7112
leaq -0xb0(%rbp), %rdi
callq 0x7112
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7085(%rip), %rdi      # 0xbfd0
leaq 0x334a(%rip), %rsi      # 0x829c
jmp 0x4f72
movq 0x7075(%rip), %rdi      # 0xbfd0
leaq 0x3388(%rip), %rsi      # 0x82ea
jmp 0x4f72
movq 0x7065(%rip), %rdi      # 0xbfd0
leaq 0x339c(%rip), %rsi      # 0x830e
callq 0x21e0
pushq $0x1
popq %rdi
callq 0x2210
jmp 0x4fb3
jmp 0x4f83
movq %rax, %rbx
jmp 0x4fc2
jmp 0x4fac
jmp 0x4f8c
movq %rax, %rbx
jmp 0x4ffc
movq %rax, %rbx
jmp 0x5008
jmp 0x4fac
jmp 0x4fb3
jmp 0x4fac
jmp 0x4fe1
jmp 0x4fac
jmp 0x4fac
jmp 0x4fac
jmp 0x4fb3
jmp 0x4fac
jmp 0x4fac
jmp 0x4fd0
movq %rax, %rbx
jmp 0x4ff0
jmp 0x4fd0
movq %rax, %rbx
leaq -0x978(%rbp), %rdi
callq 0x2270
leaq -0x370(%rbp), %rdi
callq 0x2270
jmp 0x4ff0
movq %rax, %rbx
leaq -0x978(%rbp), %rdi
callq 0x3d82
jmp 0x4fe4
movq %rax, %rbx
leaq -0x370(%rbp), %rdi
callq 0x3d82
leaq -0x778(%rbp), %rdi
callq 0x2090
leaq -0x570(%rbp), %rdi
callq 0x2270
leaq -0x90(%rbp), %rdi
callq 0x7112
leaq -0xb0(%rbp), %rdi
callq 0x7112
movq %rbx, %rdi
callq 0x2340