// File: obliviousram[P]oblivious-avl-tree/src/OAVLTreeDeterministicEviction.cpp
// Function: OAVLTreeDeterministicEviction::evictByLeaf(int)
void OAVLTreeDeterministicEviction::evictByLeaf(int leaf) {
    for (int l = 0; l < num_levels; l++) {
      std::vector<Block> blocks = this->storage->readBucket(this->P(leaf, l)).getBlocks();
      for (const Block& b : blocks) {
        if (b.id != -1) {
          this->stash.push_back(b);
        }
      }
    }
    for (int l = num_levels-1; l >= 0; l--) {
      auto evictedId = std::vector<int>();
      Bucket bucket;
      int Pxl = P(leaf, l);
      int counter = 0;

      for (const Block& be_evicted : this->stash) {
        if (counter >= this->bucket_size) {
          break;
        }
        if (Pxl == this->P(be_evicted.leaf_id, l)) {
          bucket.addBlock(be_evicted);
          evictedId.push_back(be_evicted.id);
          counter++;
        }
      }

      // removing from the stash; careful not to modify the stash while iterating over it
      for (int i = 0; i < evictedId.size(); i++) {
        for (int j = 0; j < this->stash.size(); j++) {
          if (this->stash.at(j).id == evictedId.at(i)) {
            this->stash.erase(this->stash.begin() + j);
            break;
          }
        }
      }

      // pad out the bucket with dummy blocks
      while (counter < bucket_size) {
        bucket.addBlock(Block());
        counter++;
      }

      this->storage->writeBucket(Pxl, bucket);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x160(%rbp)
movl $0x0, -0x10(%rbp)
movq -0x160(%rbp), %rcx
movl -0x10(%rbp), %eax
cmpl 0x20(%rcx), %eax
jge 0xc78f
movq -0x160(%rbp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rcx, -0x170(%rbp)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq 0x10(%rax), %rax
callq *%rax
movq -0x170(%rbp), %rsi
movl %eax, %edx
movq (%rsi), %rax
movq 0x8(%rax), %rax
leaq -0x40(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq *%rax
movq -0x168(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4700
jmp 0xc6c7
leaq -0x40(%rbp), %rdi
callq 0xcfd0
leaq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x4a70
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x4aa0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4ad0
testb $0x1, %al
jne 0xc705
jmp 0xc778
leaq -0x60(%rbp), %rdi
callq 0x4b10
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
je 0xc768
movq -0x160(%rbp), %rdi
addq $0x30, %rdi
movq -0x70(%rbp), %rsi
callq 0x4b80
jmp 0xc732
jmp 0xc768
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xcfd0
jmp 0xcbc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4900
jmp 0xcbc6
jmp 0xc76a
leaq -0x60(%rbp), %rdi
callq 0x4b30
jmp 0xc6f2
leaq -0x28(%rbp), %rdi
callq 0x4900
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xc664
movq -0x160(%rbp), %rax
movl 0x20(%rax), %eax
subl $0x1, %eax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jl 0xcbbd
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x6c30
leaq -0xa8(%rbp), %rdi
callq 0x41c0
jmp 0xc7d5
movq -0x160(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x74(%rbp), %edx
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movl %eax, -0x174(%rbp)
jmp 0xc7f3
movq -0x160(%rbp), %rax
movl -0x174(%rbp), %ecx
movl %ecx, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
addq $0x30, %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x4a70
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x4aa0
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x4ad0
testb $0x1, %al
jne 0xc85d
jmp 0xc97a
leaq -0xc0(%rbp), %rdi
callq 0x4b10
movq -0x160(%rbp), %rcx
movq %rax, -0xd0(%rbp)
movl -0xb0(%rbp), %eax
cmpl 0x1c(%rcx), %eax
jl 0xc8a9
jmp 0xc97a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xcbaf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xcba3
movq -0x160(%rbp), %rdi
movl -0xac(%rbp), %eax
movl %eax, -0x17c(%rbp)
movq -0xd0(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x74(%rbp), %edx
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movl %eax, -0x178(%rbp)
jmp 0xc8da
movl -0x17c(%rbp), %eax
movl -0x178(%rbp), %ecx
cmpl %ecx, %eax
jne 0xc967
movq -0xd0(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0x3720
jmp 0xc8ff
leaq -0xa8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x45c0
jmp 0xc914
leaq -0x100(%rbp), %rdi
callq 0x3830
movq -0xd0(%rbp), %rsi
addq $0xc, %rsi
leaq -0x90(%rbp), %rdi
callq 0x6d00
jmp 0xc939
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xc967
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x3830
jmp 0xcba3
jmp 0xc969
leaq -0xc0(%rbp), %rdi
callq 0x4b30
jmp 0xc841
movl $0x0, -0x104(%rbp)
movslq -0x104(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x188(%rbp), %rax
cmpq %rcx, %rax
jae 0xcaec
movl $0x0, -0x108(%rbp)
movq -0x160(%rbp), %rdi
movslq -0x108(%rbp), %rax
movq %rax, -0x190(%rbp)
addq $0x30, %rdi
callq 0x4b50
movq %rax, %rcx
movq -0x190(%rbp), %rax
cmpq %rcx, %rax
jae 0xcad6
movq -0x160(%rbp), %rdi
addq $0x30, %rdi
movslq -0x108(%rbp), %rsi
callq 0xd150
movq %rax, -0x198(%rbp)
jmp 0xca0c
movq -0x198(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x1a4(%rbp)
movslq -0x104(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0xd190
movq %rax, -0x1a0(%rbp)
jmp 0xca38
movl -0x1a4(%rbp), %eax
movq -0x1a0(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xcac0
movq -0x160(%rbp), %rdi
addq $0x30, %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x4a70
movq %rax, -0x120(%rbp)
movslq -0x108(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x4c60
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x4ca0
movq -0x1b8(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x4bf0
movq %rax, -0x1b0(%rbp)
jmp 0xcab0
movq -0x1b0(%rbp), %rax
movq %rax, -0x128(%rbp)
jmp 0xcad6
jmp 0xcac2
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0xc9bb
jmp 0xcad8
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xc984
jmp 0xcaee
movq -0x160(%rbp), %rcx
movl -0xb0(%rbp), %eax
cmpl 0x1c(%rcx), %eax
jge 0xcb5a
leaq -0x158(%rbp), %rdi
callq 0x37e0
jmp 0xcb0e
leaq -0xa8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x45c0
jmp 0xcb23
leaq -0x158(%rbp), %rdi
callq 0x3830
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xcaee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x3830
jmp 0xcba3
movq -0x160(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0xac(%rbp), %esi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq -0xa8(%rbp), %rdx
callq *%rax
jmp 0xcb7d
leaq -0xa8(%rbp), %rdi
callq 0xcfd0
leaq -0x90(%rbp), %rdi
callq 0x6f40
movl -0x74(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xc79f
leaq -0xa8(%rbp), %rdi
callq 0xcfd0
leaq -0x90(%rbp), %rdi
callq 0x6f40
jmp 0xcbc6
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x33a0
nop