// File: /workspace/github_cpp_makefile_star2_O2/obliviousram[P]PathOHeap/src/JobForStatisticalSimulation.cpp
// Function: JobForStatisticalSimulation::run()
void JobForStatisticalSimulation::run() {
  int bucket_size = 2;
  int num_blocks = pow(2, 20);
  const int frequency = 500000;

  std::array<std::byte, Block::BLOCK_SIZE> sample_data;
  sample_data.fill(std::byte{0xa});

  UntrustedStorageInterface* storage = new ServerStorage();
  RandForOHeapInterface* random = new RandomForOHeap();
  OHeapInterface* oheap = new OHeapReadPathEviction(storage, random, bucket_size, num_blocks, false);

  std::cout << "Warming up" << std::endl;
  // do inserts until heap is full, then increaseKeys
  
  // as with Job2, need to keep around leaves and ts
  auto leaves = std::vector<int>();
  auto ts = std::vector<int>();

  for (int i = 1; i <= 3000000; i++) {
    if (i <= num_blocks) {
      std::pair<int, int> leaf_t = oheap->insertBlock(i, sample_data);
      leaves.push_back(leaf_t.first);
      ts.push_back(leaf_t.second);
    } else {
      std::pair<int, int> leaf_t = oheap->increaseKey(leaves.at(i % num_blocks), ts.at(i % num_blocks));
      leaves.at(i % num_blocks) = leaf_t.first;
      ts.at(i % num_blocks) = leaf_t.second;
    }

    if (i % frequency == 0) {
      std::cout << "Accesses made: " << i << std::endl;
    }
  }

  std::cout << "Start recording..." << std::endl;

  int max_stash_size = 0;
  auto histogram = std::unordered_map<int, int>();
  int stash_size;

  for (int i = 1; i <= 500000000; i++) {
    std::pair<int, int> leaf_t = oheap->increaseKey(leaves.at(i % num_blocks), ts.at(i % num_blocks));
    leaves.at(i % num_blocks) = leaf_t.first;
    ts.at(i % num_blocks) = leaf_t.second;

    stash_size = oheap->getStashSize();
    if (histogram.find(stash_size) != histogram.end()) {
      histogram[stash_size]++;
    } else {
      histogram[stash_size] = 1;
    }

    max_stash_size = (stash_size > max_stash_size) ? stash_size : max_stash_size;
    
    if (i % frequency == 0) {
      std::cout << "Accesses made (after warmup): " << i << std::endl;

      for (int i = 0; i <= max_stash_size; i++) {
        if (histogram.find(i) != histogram.end()) {
          std::cout << i << "," << histogram[i] << std::endl;
        } else {
          std::cout << i << ",0" << std::endl;
        }
      }
    }
  }

  delete oheap;
  delete random;
  delete storage;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
pushq $0x28
popq %rdi
callq 0x2150
movq %rax, %r14
movq %rax, %rdi
callq 0x5990
pushq $0x20
popq %rdi
callq 0x2150
movq %rax, %r15
movq %rax, %rdi
callq 0x583c
pushq $0x58
popq %rdi
callq 0x2150
pushq $0x2
popq %rcx
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x100000, %r8d         # imm = 0x100000
xorl %r9d, %r9d
callq 0x396c
movq %r15, 0x50(%rsp)
movq %r14, 0x58(%rsp)
movq 0x7e9d(%rip), %rdi      # 0xafd0
leaq 0x4033(%rip), %rsi      # 0x716d
callq 0x2140
movq %rax, %rdi
callq 0x20b0
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x60(%rsp), %r12
andq $0x0, 0x10(%r12)
movaps %xmm0, (%r12)
pushq $0x1
popq %rbx
movl $0x7a120, %r14d         # imm = 0x7A120
movl %ebx, %r13d
movl %r13d, %eax
xorl %edx, %edx
divl %r14d
cmpl $0x2dc6c1, %r13d        # imm = 0x2DC6C1
je 0x32a6
movl %eax, %ebp
cmpl $0x100000, %r13d        # imm = 0x100000
ja 0x3200
movaps 0x3fa6(%rip), %xmm0     # 0x7140
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rax), %rax
movaps 0x80(%rsp), %xmm0
movaps 0x90(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl %r13d, %esi
callq *%rax
movq %rax, 0xa0(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2eee
movq %r12, %rdi
leaq 0xa4(%rsp), %rsi
callq 0x2eee
jmp 0x3266
movl %ebx, %r14d
movl %r13d, %ebx
andl $0xfffff, %ebx          # imm = 0xFFFFF
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f0c
movl (%rax), %r15d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2f0c
movslq (%rax), %rdx
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
movl %r15d, %esi
callq *0x28(%rax)
movq %rax, %r15
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f0c
movl %r15d, (%rax)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2f0c
shrq $0x20, %r15
movl %r15d, (%rax)
movl %r14d, %ebx
movl $0x7a120, %r14d         # imm = 0x7A120
imull $0x7a120, %ebp, %eax    # imm = 0x7A120
movl %ebx, %ecx
subl %eax, %ecx
addl %r13d, %ecx
cmpl $0x1, %ecx
jne 0x329e
movq 0x7d51(%rip), %rdi      # 0xafd0
leaq 0x3ef2(%rip), %rsi      # 0x7178
callq 0x2140
movq %rax, %rdi
movl %r13d, %esi
callq 0x2230
movq %rax, %rdi
callq 0x20b0
incl %r13d
jmp 0x3173
movq 0x7d23(%rip), %rdi      # 0xafd0
leaq 0x3ed4(%rip), %rsi      # 0x7188
callq 0x2140
movq %rax, %rdi
callq 0x20b0
leaq 0xd0(%rsp), %rax
andq $0x0, -0x10(%rax)
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
pushq $0x1
popq %r15
xorl %ebp, %ebp
leaq 0xa0(%rsp), %rbx
leaq 0x24(%rsp), %r13
cmpl $0x1dcd6501, %r15d      # imm = 0x1DCD6501
je 0x347a
movl %r15d, %r12d
andl $0xfffff, %r15d         # imm = 0xFFFFF
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x2f0c
movl (%rax), %r14d
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x2f0c
movslq (%rax), %rdx
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
movl %r14d, %esi
callq *0x28(%rax)
movq %rax, %r14
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x2f0c
movl %r14d, (%rax)
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x2f0c
shrq $0x20, %r14
movl %r14d, (%rax)
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x35e4
testq %rax, %rax
je 0x339d
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3660
incl (%rax)
jmp 0x33b0
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3660
movl $0x1, (%rax)
movl 0x20(%rsp), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
movl %r12d, %r15d
movl %r12d, %eax
xorl %edx, %edx
movl $0x7a120, %ecx          # imm = 0x7A120
divl %ecx
testl %edx, %edx
je 0x33d4
incl %r15d
jmp 0x32ff
movq 0x7bf5(%rip), %rdi      # 0xafd0
leaq 0x3db9(%rip), %rsi      # 0x719b
callq 0x2140
movq %rax, %rdi
movl %r12d, %esi
callq 0x2230
movq %rax, %rdi
callq 0x20b0
xorl %eax, %eax
movl %r12d, %r15d
movq 0x7bca(%rip), %r12      # 0xafd0
movl %eax, 0x24(%rsp)
cmpl %ebp, %eax
jg 0x33cc
movq %rbx, %rdi
movq %r13, %rsi
callq 0x35e4
movl 0x24(%rsp), %esi
testq %rax, %rax
je 0x3453
movq %r12, %rdi
callq 0x2230
movq %rax, %rdi
leaq 0x3d86(%rip), %rsi      # 0x71ba
callq 0x2140
movq %rax, %r14
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3660
movl (%rax), %esi
movq %r14, %rdi
callq 0x2230
jmp 0x346a
movq %r12, %rdi
callq 0x2230
movq %rax, %rdi
leaq 0x3d57(%rip), %rsi      # 0x71bc
callq 0x2140
movq %rax, %rdi
callq 0x20b0
movl 0x24(%rsp), %eax
incl %eax
jmp 0x3406
pushq $0x8
popq %rbx
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x2160
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x2160
movq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x2160
leaq 0xa0(%rsp), %rdi
callq 0x353c
leaq 0x60(%rsp), %rdi
callq 0x2f2e
leaq 0x30(%rsp), %rdi
callq 0x2f2e
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x58
popq %rsi
movq 0x28(%rsp), %rdi
jmp 0x34f8
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r15, %rdi
jmp 0x34f8
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x2160
jmp 0x3534
jmp 0x3507
jmp 0x3510
jmp 0x3507
jmp 0x3507
movq %rax, %rbx
jmp 0x3520
jmp 0x3510
jmp 0x3510
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x353c
leaq 0x60(%rsp), %rdi
callq 0x2f2e
leaq 0x30(%rsp), %rdi
callq 0x2f2e
movq %rbx, %rdi
callq 0x2240