// File: obliviousram[P]PathOHeap/src/OHeapReadPathEviction.cpp
// Function: OHeapReadPathEviction::readAndRemove(int, long)
Block OHeapReadPathEviction::readAndRemove(int leaf, long t) {
  if (leaf != -1 && t != -1) {
    Block ret;    
    for (int l = 0; l < this->num_levels; l++) {
      int p = this->P(leaf, l);
      Bucket bucket_on_path = this->storage->readBucket(p);
      std::optional<Block> possible_ret = bucket_on_path.removeBlock(t);
      if (possible_ret.has_value()) {
        ret = possible_ret.value();
        bucket_on_path.addBlock(Block());  // for obliviousness
      }
      this->storage->writeBucket(p, bucket_on_path);
    }
    return ret;
  } else {
    // dummy ops
    leaf = this->rand_gen->getRandomLeaf();
    for (int l = 0; l < num_levels; l++) {
      int p = this->P(leaf, l);
      Bucket b = this->storage->readBucket(p);
      this->storage->writeBucket(p, b);
    }
    return Block();
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x168(%rbp)
movq %rdi, -0x160(%rbp)
movq %rsi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movq %rcx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x158(%rbp)
cmpl $-0x1, -0xc(%rbp)
je 0x8f7f
cmpq $-0x1, -0x18(%rbp)
je 0x8f7f
movq -0x168(%rbp), %rdi
callq 0x26b0
movl $0x0, -0x1c(%rbp)
movq -0x158(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x8f7a
movq -0x158(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x1c(%rbp), %edx
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
movl %eax, %ecx
movq -0x158(%rbp), %rax
movl %ecx, -0x20(%rbp)
movq 0x30(%rax), %rsi
movl -0x20(%rbp), %edx
movq (%rsi), %rax
movq 0x8(%rax), %rax
leaq -0x70(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq *%rax
movq -0x170(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
callq 0x2e00
jmp 0x8eb0
leaq -0xb0(%rbp), %rdi
callq 0x9e40
testb $0x1, %al
jne 0x8ec5
jmp 0x8f46
leaq -0xb0(%rbp), %rdi
callq 0x9e60
movq %rax, -0x178(%rbp)
jmp 0x8eda
movq -0x168(%rbp), %rax
movq -0x178(%rbp), %rcx
movl 0x30(%rcx), %edx
movl %edx, 0x30(%rax)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0xf8(%rbp), %rdi
callq 0x26b0
jmp 0x8f12
leaq -0x70(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x2db0
jmp 0x8f24
jmp 0x8f46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x9ae0
jmp 0x907a
movq -0x158(%rbp), %rax
movq 0x30(%rax), %rdi
movl -0x20(%rbp), %esi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq -0x70(%rbp), %rdx
callq *%rax
jmp 0x8f63
leaq -0x70(%rbp), %rdi
callq 0x9ae0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x8e47
jmp 0x906a
movq -0x158(%rbp), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0xc(%rbp)
movl $0x0, -0xfc(%rbp)
movq -0x158(%rbp), %rcx
movl -0xfc(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x905e
movq -0x158(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0xfc(%rbp), %edx
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
movl %eax, %ecx
movq -0x158(%rbp), %rax
movl %ecx, -0x100(%rbp)
movq 0x30(%rax), %rsi
movl -0x100(%rbp), %edx
movq (%rsi), %rax
movq 0x8(%rax), %rax
leaq -0x150(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq *%rax
movq -0x158(%rbp), %rax
movq -0x180(%rbp), %rdx
movq 0x30(%rax), %rdi
movl -0x100(%rbp), %esi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x901e
leaq -0x150(%rbp), %rdi
callq 0x9ae0
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x8f9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x9ae0
jmp 0x907a
movq -0x168(%rbp), %rdi
callq 0x26b0
movq -0x160(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0xb8(%rbp), %rdi
callq 0x22a0
nopw %cs:(%rax,%rax)