// File: /workspace/github_cpp_makefile_star2_O2/obliviousram[P]PathOHeap/src/Job2.cpp
// Function: Job2::run()
void Job2::run() {
  int bucket_size = 3;
  int num_blocks = pow(2, 12);
  UntrustedStorageInterface* storage = new ServerStorage();
  RandForOHeapInterface* random = new RandomForOHeap();
  OHeapInterface* oheap = new OHeapReadPathEviction(storage, random, bucket_size, num_blocks, false);

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

  // Sample computation: fill the heap, then start overwriting.

  // overwrite requires knowing the leaf and t of the block to be overwritten, so keep them around as the heap is filled
  auto leaves = std::vector<int>();
  auto ts = std::vector<int>();

  for (int i = 0; i < num_blocks; i++) {
    std::pair<int, int> leaf_t = oheap->insertBlock(i, sample_data);
    leaves.push_back(leaf_t.first);
    ts.push_back(leaf_t.second);
  }

  for (int i = 0; i < 300000; 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;

    if (i % 10000 == 0) {
      std::cout << "After block " << i << " stash size is " << oheap->getStashSize() << std::endl;
    }
  }

  delete oheap;
  delete random;
  delete storage;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
pushq $0x28
popq %rdi
callq 0x2150
movq %rax, %r14
movq %rax, %rdi
callq 0x5990
pushq $0x20
popq %rdi
callq 0x2150
movq %rax, %r12
movq %rax, %rdi
callq 0x583c
pushq $0x58
popq %rdi
callq 0x2150
movq %rax, %r15
pushq $0x3
popq %rcx
xorl %ebx, %ebx
movq %rax, %rdi
movq %r14, 0x38(%rsp)
movq %r14, %rsi
movq %r12, 0x30(%rsp)
movq %r12, %rdx
movl $0x1000, %r8d           # imm = 0x1000
xorl %r9d, %r9d
callq 0x396c
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
leaq 0x60(%rsp), %r12
andq $0x0, 0x10(%r12)
leaq 0x2c(%rsp), %r13
movaps %xmm0, (%r12)
leaq 0x28(%rsp), %rbp
cmpl $0x1000, %ebx           # imm = 0x1000
je 0x2d84
movaps 0x4412(%rip), %xmm0     # 0x7140
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq (%r15), %rax
movq 0x8(%rax), %rax
movaps 0x80(%rsp), %xmm0
movaps 0x90(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq *%rax
movq %rax, 0x28(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2eee
movq %r12, %rdi
movq %r13, %rsi
callq 0x2eee
incl %ebx
jmp 0x2d1f
xorl %ebp, %ebp
leaq 0x60(%rsp), %r13
movl %ebp, %eax
xorl %edx, %edx
movl $0x2710, %ecx           # imm = 0x2710
divl %ecx
cmpl $0x493e0, %ebp          # imm = 0x493E0
je 0x2e58
movl %eax, %r14d
movl %ebp, %ebx
andl $0xfff, %ebx            # imm = 0xFFF
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f0c
movl (%rax), %r12d
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2f0c
movslq (%rax), %rdx
movq (%r15), %rax
movq %r15, %rdi
movl %r12d, %esi
callq *0x28(%rax)
movq %rax, %r12
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f0c
movl %r12d, (%rax)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2f0c
imull $0x2710, %r14d, %ecx    # imm = 0x2710
shrq $0x20, %r12
movl %r12d, (%rax)
cmpl %ebp, %ecx
jne 0x2e51
movq 0x81c2(%rip), %rdi      # 0xafd0
leaq 0x433b(%rip), %rsi      # 0x7150
callq 0x2140
movq %rax, %rdi
movl %ebp, %esi
callq 0x2230
movq %rax, %rdi
leaq 0x432f(%rip), %rsi      # 0x715d
callq 0x2140
movq %rax, %rbx
movq (%r15), %rax
movq %r15, %rdi
callq *0x40(%rax)
movq %rbx, %rdi
movl %eax, %esi
callq 0x2230
movq %rax, %rdi
callq 0x20b0
incl %ebp
jmp 0x2d8b
pushq $0x8
popq %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2160
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x2160
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x2160
leaq 0x60(%rsp), %rdi
callq 0x2f2e
leaq 0x40(%rsp), %rdi
callq 0x2f2e
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x58
popq %rsi
movq %r15, %rdi
jmp 0x2ec5
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r12, %rdi
jmp 0x2ec5
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x2160
jmp 0x2ee5
jmp 0x2ece
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x2f2e
leaq 0x40(%rsp), %rdi
callq 0x2f2e
movq %rbx, %rdi
callq 0x2240
nop