// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0x28, %edi
callq 0x2160
movq %rax, %r14
movq %rax, %rdi
callq 0x5ff4
movl $0x20, %edi
callq 0x2160
movq %rax, %r12
movq %rax, %rdi
callq 0x5e7c
movl $0x58, %edi
callq 0x2160
movq %rax, %r15
movq %rax, %rdi
movq %r14, 0x70(%rsp)
movq %r14, %rsi
movq %r12, 0x68(%rsp)
movq %r12, %rdx
movl $0x2, %ecx
movl $0x100000, %r8d         # imm = 0x100000
xorl %r9d, %r9d
callq 0x3be8
movq 0x7d47(%rip), %rbx      # 0xafd0
leaq 0x3ed7(%rip), %rsi      # 0x7167
movl $0xa, %edx
movq %rbx, %rdi
callq 0x2190
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
xorl %eax, %eax
leaq 0x30(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq 0x50(%rsp), %r13
movq %rax, 0x10(%r13)
movaps %xmm0, (%r13)
movl $0x1, %ebp
leaq 0x3e83(%rip), %r12      # 0x7172
cmpl $0x100000, %ebp         # imm = 0x100000
ja 0x335a
movaps 0x3df2(%rip), %xmm0     # 0x70f0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movq (%r15), %rax
movq 0x8(%rax), %rax
movaps 0xb0(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq *%rax
movq %rax, 0x78(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x33f2
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x3401
movl %ebp, %r14d
andl $0xfffff, %r14d         # imm = 0xFFFFF
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x382f
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x382f
leal (,%r14,4), %edx
movl (%rax,%rdx), %esi
movslq (%rcx,%rdx), %rdx
movq (%r15), %rax
movq %r15, %rdi
callq *0x28(%rax)
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x382f
movl %eax, (%rcx,%r14,4)
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x382f
shrq $0x20, %rax
movl %eax, (%rcx,%r14,4)
jmp 0x342b
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x30d8
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x341e
movl 0x7c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x342b
movq %r13, %rdi
leaq 0x7c(%rsp), %rdx
callq 0x30d8
movl %ebp, %eax
imulq $0x431bde83, %rax, %rax # imm = 0x431BDE83
shrq $0x31, %rax
imull $0x7a120, %eax, %eax    # imm = 0x7A120
movl $0x1, %ecx
subl %eax, %ecx
addl %ebp, %ecx
cmpl $0x1, %ecx
jne 0x3490
movl $0xf, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2190
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2240
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
incl %ebp
cmpl $0x2dc6c1, %ebp         # imm = 0x2DC6C1
jne 0x32ef
movq 0x7b2b(%rip), %rbx      # 0xafd0
leaq 0x3cd6(%rip), %rsi      # 0x7182
movl $0x12, %edx
movq %rbx, %rdi
callq 0x2190
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq 0x7af9(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0xa8(%rsp), %rax
movq $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)
xorl %r12d, %r12d
movl $0x1, %ebp
movq 0x7aaf(%rip), %rbx      # 0xafd0
leaq 0x3c8e(%rip), %r14      # 0x71b6
movl %ebp, %r13d
andl $0xfffff, %r13d         # imm = 0xFFFFF
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x381e
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x381e
leal (,%r13,4), %edx
movl (%rax,%rdx), %esi
movslq (%rcx,%rdx), %rdx
movq (%r15), %rax
movq %r15, %rdi
callq *0x28(%rax)
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x381e
movl %eax, (%rcx,%r13,4)
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x381e
shrq $0x20, %rax
movl %eax, (%rcx,%r13,4)
movq (%r15), %rax
movq %r15, %rdi
callq *0x40(%rax)
movl %eax, %ecx
movl %eax, 0x2c(%rsp)
cltq
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %r8
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0x3626
movq %rdx, %rsi
movq (%rax), %r9
cmpl 0x8(%r9), %ecx
je 0x3613
movq (%r9), %r9
testq %r9, %r9
je 0x3626
movslq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0x3626
cmpl %edi, %ecx
jne 0x35f6
leaq 0x78(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x3936
incl (%rax)
jmp 0x363b
leaq 0x78(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x3936
movl $0x1, (%rax)
movl 0x2c(%rsp), %eax
cmpl %r12d, %eax
cmovgl %eax, %r12d
imull $0x68c26139, %ebp, %eax # imm = 0x68C26139
rorl $0x5, %eax
cmpl $0x218d, %eax           # imm = 0x218D
ja 0x379b
movl $0x1e, %edx
movq %rbx, %rdi
leaq 0x3b2c(%rip), %rsi      # 0x7195
callq 0x2190
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2240
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movl $0x0, 0x28(%rsp)
xorl %esi, %esi
movslq %esi, %rax
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %r8
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0x3744
movq %rdx, %rcx
movq (%rax), %r9
cmpl 0x8(%r9), %esi
je 0x36f3
movq (%r9), %r9
testq %r9, %r9
je 0x3744
movslq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rcx, %rdx
jne 0x3744
cmpl %edi, %esi
jne 0x36d6
movq %rbx, %rdi
callq 0x2240
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x3aa7(%rip), %rsi      # 0x71b4
callq 0x2190
leaq 0x78(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3936
movl (%rax), %esi
movq %r13, %rdi
callq 0x2240
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2150
jmp 0x3774
movq %rbx, %rdi
callq 0x2240
movq %rax, %r13
movl $0x2, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2190
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movl 0x28(%rsp), %eax
leal 0x1(%rax), %esi
movl %esi, 0x28(%rsp)
cmpl %r12d, %eax
jl 0x36ac
incl %ebp
cmpl $0x1dcd6501, %ebp       # imm = 0x1DCD6501
jne 0x3528
movl $0x8, %esi
movq %r15, %rdi
callq 0x2170
movl $0x8, %esi
movq 0x68(%rsp), %rdi
callq 0x2170
movl $0x8, %esi
movq 0x70(%rsp), %rdi
callq 0x2170
leaq 0x78(%rsp), %rdi
callq 0x38bc
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x37f5
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x380c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38f8(%rip), %rdi      # 0x711d
movq %r13, %rsi
xorl %eax, %eax
callq 0x21d0
leaq 0x38e7(%rip), %rdi      # 0x711d
movq %r14, %rsi
xorl %eax, %eax
callq 0x21d0
movq %rax, %rbx
movl $0x58, %esi
movq %r15, %rdi
jmp 0x38ae
movq %rax, %rbx
movl $0x20, %esi
movq %r12, %rdi
jmp 0x38ae
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
jmp 0x38ae
jmp 0x386f
jmp 0x386f
jmp 0x386f
jmp 0x386f
movq %rax, %rbx
jmp 0x3885
jmp 0x3878
jmp 0x3878
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x38bc
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x389c
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x38b3
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2250
nop