// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %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
xorl %ebx, %ebx
movq %rax, %rdi
movq %r14, 0x68(%rsp)
movq %r14, %rsi
movq %r12, 0x60(%rsp)
movq %r12, %rdx
movl $0x3, %ecx
movl $0x1000, %r8d           # imm = 0x1000
xorl %r9d, %r9d
callq 0x3be8
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r14
movaps %xmm0, (%r14)
movq %rbx, 0x10(%r14)
leaq 0x5c(%rsp), %r12
leaq 0x20(%rsp), %r13
movq %rbx, 0x10(%r13)
movaps %xmm0, (%r13)
leaq 0x58(%rsp), %rbp
movaps 0x42c5(%rip), %xmm0     # 0x70f0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movq (%r15), %rax
movq 0x8(%rax), %rax
movaps 0x70(%rsp), %xmm0
movaps 0x80(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %ebx, %esi
callq *%rax
movq %rax, 0x58(%rsp)
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x2e7a
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x2e85
movq %r14, %rdi
movq %rbp, %rdx
callq 0x30d8
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x2ea2
movl 0x5c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x2ead
movq %r13, %rdi
movq %r12, %rdx
callq 0x30d8
incl %ebx
cmpl $0x1000, %ebx           # imm = 0x1000
jne 0x2e24
xorl %ebp, %ebp
movl $0xd1b71759, %r13d      # imm = 0xD1B71759
movq 0x8106(%rip), %r12      # 0xafd0
leaq 0x423c(%rip), %rbx      # 0x710d
movl %ebp, %r14d
andl $0xfff, %r14d           # imm = 0xFFF
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x3062
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x3062
leal (,%r14,4), %edx
movl (%rax,%rdx), %esi
movslq (%rcx,%rdx), %rdx
movq (%r15), %rax
movq %r15, %rdi
callq *0x28(%rax)
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x3062
movl %eax, (%rcx,%r14,4)
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
jbe 0x3062
movl %ebp, %edx
imulq %r13, %rdx
shrq $0x2d, %rdx
imull $0x2710, %edx, %edx     # imm = 0x2710
shrq $0x20, %rax
movl %eax, (%rcx,%r14,4)
cmpl %ebp, %edx
jne 0x2fe9
movl $0xc, %edx
movq %r12, %rdi
leaq 0x4176(%rip), %rsi      # 0x7100
callq 0x2190
movq %r12, %rdi
movl %ebp, %esi
callq 0x2240
movq %rax, %r14
movl $0xf, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2190
movq (%r15), %rax
movq %r15, %rdi
callq *0x40(%rax)
movq %r14, %rdi
movl %eax, %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 $0x493e0, %ebp          # imm = 0x493E0
jne 0x2ed1
movl $0x8, %esi
movq %r15, %rdi
callq 0x2170
movl $0x8, %esi
movq 0x60(%rsp), %rdi
callq 0x2170
movl $0x8, %esi
movq 0x68(%rsp), %rdi
callq 0x2170
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3039
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3050
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40b4(%rip), %rdi      # 0x711d
movq %r14, %rsi
xorl %eax, %eax
callq 0x21d0
movq %rax, %rbx
movl $0x58, %esi
movq %r15, %rdi
jmp 0x30ca
movq %rax, %rbx
movl $0x20, %esi
movq %r12, %rdi
jmp 0x30ca
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
jmp 0x30ca
jmp 0x309e
jmp 0x309e
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x30b8
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x30cf
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2250
nop