// File: 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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movl $0x3, -0x4(%rbp)
movsd 0xb99e(%rip), %xmm0     # 0x100e8
movsd 0xba2e(%rip), %xmm1     # 0x10180
callq 0x2290
cvttsd2si %xmm0, %eax
movl %eax, -0x8(%rbp)
movl $0x28, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd8(%rbp)
callq 0xd5a0
jmp 0x477c
movq -0xd8(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x20, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
callq 0xd200
jmp 0x47a5
movq -0xe0(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x58, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x4(%rbp), %ecx
movl -0x8(%rbp), %r8d
xorl %r9d, %r9d
callq 0x7970
jmp 0x47e0
movq -0xe8(%rbp), %rax
movq %rax, -0x30(%rbp)
movb $0xa, -0x51(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x51(%rbp), %rsi
callq 0x2920
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20f0
leaq -0x70(%rbp), %rdi
callq 0x4bf0
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x20f0
leaq -0x88(%rbp), %rdi
callq 0x4bf0
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x496e
movq -0x30(%rbp), %rdi
movl -0x8c(%rbp), %esi
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movaps %xmm1, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movq (%rdi), %rax
movq 0x8(%rax), %rax
movaps -0xc0(%rbp), %xmm0
movaps -0xb0(%rbp), %xmm1
movq %rsp, %rcx
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
callq *%rax
movq %rax, -0xf0(%rbp)
jmp 0x4897
movq -0xf0(%rbp), %rax
movq %rax, -0x94(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x94(%rbp), %rsi
callq 0x4c10
jmp 0x48b7
leaq -0x90(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x4c10
jmp 0x48cc
jmp 0x48ce
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x483e
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x28, %esi
callq 0x21a0
jmp 0x4be4
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x20, %esi
callq 0x21a0
jmp 0x4be4
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x58, %esi
callq 0x21a0
jmp 0x4be4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x4cc0
leaq -0x70(%rbp), %rdi
callq 0x4cc0
jmp 0x4be4
movl $0x0, -0xc4(%rbp)
cmpl $0x493e0, -0xc4(%rbp)   # imm = 0x493E0
jge 0x4b60
movq -0x30(%rbp), %rax
movq %rax, -0x100(%rbp)
movl -0xc4(%rbp), %eax
movl -0x8(%rbp), %ecx
cltd
idivl %ecx
movslq %edx, %rsi
leaq -0x70(%rbp), %rdi
callq 0x4c80
movq %rax, -0xf8(%rbp)
jmp 0x49b4
movq -0xf8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x10c(%rbp)
movl -0xc4(%rbp), %eax
movl -0x8(%rbp), %ecx
cltd
idivl %ecx
movslq %edx, %rsi
leaq -0x88(%rbp), %rdi
callq 0x4c80
movq %rax, -0x108(%rbp)
jmp 0x49e7
movl -0x10c(%rbp), %esi
movq -0x100(%rbp), %rdi
movq -0x108(%rbp), %rax
movslq (%rax), %rdx
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
movq %rax, -0x118(%rbp)
jmp 0x4a10
movq -0x118(%rbp), %rax
movq %rax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, -0x124(%rbp)
movl -0xc4(%rbp), %eax
movl -0x8(%rbp), %ecx
cltd
idivl %ecx
movslq %edx, %rsi
leaq -0x70(%rbp), %rdi
callq 0x4c80
movq %rax, -0x120(%rbp)
jmp 0x4a4b
movq -0x120(%rbp), %rax
movl -0x124(%rbp), %ecx
movl %ecx, (%rax)
movl -0xc8(%rbp), %eax
movl %eax, -0x134(%rbp)
movl -0xc4(%rbp), %eax
movl -0x8(%rbp), %ecx
cltd
idivl %ecx
movslq %edx, %rsi
leaq -0x88(%rbp), %rdi
callq 0x4c80
movq %rax, -0x130(%rbp)
jmp 0x4a8a
movq -0x130(%rbp), %rax
movl -0x134(%rbp), %ecx
movl %ecx, (%rax)
movl -0xc4(%rbp), %eax
movl $0x2710, %ecx           # imm = 0x2710
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x4b4a
movq 0x13519(%rip), %rdi     # 0x17fd0
leaq 0xb6ca(%rip), %rsi      # 0x10188
callq 0x2180
movq %rax, -0x140(%rbp)
jmp 0x4acc
movq -0x140(%rbp), %rdi
movl -0xc4(%rbp), %esi
callq 0x2280
movq %rax, -0x148(%rbp)
jmp 0x4ae7
movq -0x148(%rbp), %rdi
leaq 0xb6a0(%rip), %rsi      # 0x10195
callq 0x2180
movq %rax, -0x150(%rbp)
jmp 0x4b03
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
callq *%rax
movl %eax, -0x154(%rbp)
jmp 0x4b18
movl -0x154(%rbp), %esi
movq -0x150(%rbp), %rdi
callq 0x2280
movq %rax, -0x160(%rbp)
jmp 0x4b33
movq -0x160(%rbp), %rdi
movq 0x1346f(%rip), %rsi     # 0x17fb0
callq 0x21b0
jmp 0x4b48
jmp 0x4b4a
jmp 0x4b4c
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x4978
movq -0x30(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, %rax
je 0x4b82
movq -0x168(%rbp), %rdi
movl $0x8, %esi
callq 0x21a0
movq -0x28(%rbp), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, %rax
je 0x4ba4
movq -0x170(%rbp), %rdi
movl $0x8, %esi
callq 0x21a0
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, %rax
je 0x4bc6
movq -0x178(%rbp), %rdi
movl $0x8, %esi
callq 0x21a0
leaq -0x88(%rbp), %rdi
callq 0x4cc0
leaq -0x70(%rbp), %rdi
callq 0x4cc0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0x22a0
nopl (%rax)