// File: /workspace/github_cpp_makefile_star2_O1/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 $0xe8, %rsp
movl $0x28, %edi
callq 0x2160
movq %rax, %r14
movq %rax, %rdi
callq 0x6178
movl $0x20, %edi
callq 0x2160
movq %rax, %r12
movq %rax, %rdi
callq 0x6000
movl $0x58, %edi
callq 0x2160
movq %rax, %r15
movq %rax, %rdi
movq %r14, 0xb8(%rsp)
movq %r14, %rsi
movq %r12, 0xb0(%rsp)
movq %r12, %rdx
movl $0x2, %ecx
movl $0x100000, %r8d         # imm = 0x100000
xorl %r9d, %r9d
callq 0x3c98
movq 0x7d19(%rip), %rbx      # 0xafd0
leaq 0x3eaf(%rip), %rsi      # 0x716d
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 0x3e5b(%rip), %r12      # 0x7178
cmpl $0x100000, %ebp         # imm = 0x100000
ja 0x338c
movaps 0x3e14(%rip), %xmm0     # 0x7140
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movq (%r15), %rax
movq 0x8(%rax), %rax
movaps 0xc0(%rsp), %xmm0
movaps 0xd0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq *%rax
movq %rax, 0x70(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x3424
movl 0x70(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x3433
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 0x38ac
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x38ac
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 0x38ac
movl %eax, (%rcx,%r14,4)
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x38ac
shrq $0x20, %rax
movl %eax, (%rcx,%r14,4)
jmp 0x345d
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x30fe
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x3450
movl 0x74(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x345d
movq %r13, %rdi
leaq 0x74(%rsp), %rdx
callq 0x30fe
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 0x34c2
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 0x331d
movq 0x7af9(%rip), %rbx      # 0xafd0
leaq 0x3caa(%rip), %rsi      # 0x7188
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 0x7ac7(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
movaps %xmm0, -0x30(%rax)
movaps %xmm0, -0x10(%rax)
movaps %xmm0, -0x20(%rax)
movq $0x0, (%rax)
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
xorl %r12d, %r12d
movl $0x1, %ebp
movq 0x7a76(%rip), %rbx      # 0xafd0
leaq 0x3c5b(%rip), %r14      # 0x71bc
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 0x389b
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x389b
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 0x389b
movl %eax, (%rcx,%r13,4)
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
jbe 0x389b
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 0x70(%rsp), %rdi
movq 0x78(%rsp), %r8
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x3660
movq (%rdx), %r9
movq %rdx, %rax
cmpl 0x8(%r9), %ecx
je 0x3660
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x365e
movslq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x3660
movq %r10, %rax
cmpl %edi, %ecx
jne 0x3634
jmp 0x3660
xorl %eax, %eax
testq %rax, %rax
je 0x367e
cmpq $0x0, (%rax)
je 0x367e
leaq 0x70(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x39f8
incl (%rax)
jmp 0x3693
leaq 0x70(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x39f8
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 0x3812
movl $0x1e, %edx
movq %rbx, %rdi
leaq 0x3ada(%rip), %rsi      # 0x719b
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 0x70(%rsp), %rdi
movq 0x78(%rsp), %r8
xorl %edx, %edx
divq %r8
movq %rdx, %rcx
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x375f
movq (%rdx), %r9
movq %rdx, %rax
cmpl 0x8(%r9), %esi
je 0x375f
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x375d
movslq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rcx, %rdx
jne 0x375f
movq %r10, %rax
cmpl %edi, %esi
jne 0x3733
jmp 0x375f
xorl %eax, %eax
testq %rax, %rax
je 0x37bb
cmpq $0x0, (%rax)
je 0x37bb
movq %rbx, %rdi
callq 0x2240
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x3a36(%rip), %rsi      # 0x71ba
callq 0x2190
leaq 0x70(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x39f8
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 0x37eb
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 0x3704
incl %ebp
cmpl $0x1dcd6501, %ebp       # imm = 0x1DCD6501
jne 0x3561
movl $0x8, %esi
movq %r15, %rdi
callq 0x2170
movl $0x8, %esi
movq 0xb0(%rsp), %rdi
callq 0x2170
movl $0x8, %esi
movq 0xb8(%rsp), %rdi
callq 0x2170
leaq 0x70(%rsp), %rdi
callq 0x3938
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3872
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3889
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37f7(%rip), %rdi      # 0x7099
movq %r13, %rsi
xorl %eax, %eax
callq 0x21d0
leaq 0x37e6(%rip), %rdi      # 0x7099
movq %r14, %rsi
xorl %eax, %eax
callq 0x21d0
movq %rax, %rbx
movl $0x58, %esi
movq %r15, %rdi
jmp 0x392b
movq %rax, %rbx
movl $0x20, %esi
movq %r12, %rdi
jmp 0x392b
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
jmp 0x392b
jmp 0x38ec
jmp 0x38ec
jmp 0x38ec
jmp 0x38ec
movq %rax, %rbx
jmp 0x3902
jmp 0x38f5
jmp 0x38f5
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3938
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3919
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3930
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2250