// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %esi, 0x24(%rsp)
movq %rdi, %r14
movl 0x20(%rdi), %r12d
testl %r12d, %r12d
jle 0x51f9
leaq 0x30(%r14), %rbx
xorl %ebp, %ebp
movq %rsp, %r15
movq (%r14), %rax
movq 0x8(%r14), %r13
movq %r14, %rdi
movl 0x24(%rsp), %esi
movl %ebp, %edx
callq *0x10(%rax)
movq (%r13), %rcx
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r15, %rdi
leaq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x2f50
movq %r12, %rdi
callq 0x304c
movq (%rsp), %r13
movq 0x8(%rsp), %r12
cmpq %r12, %r13
je 0x51e6
cmpl $-0x1, 0xc(%r13)
je 0x51e0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3124
addq $0x30, %r13
jmp 0x51c9
movq %r15, %rdi
callq 0x304c
incl %ebp
movl 0x20(%r14), %r12d
cmpl %r12d, %ebp
jl 0x5184
testl %r12d, %r12d
jle 0x53ec
leaq 0x30(%r14), %r15
leaq 0x30(%rsp), %r13
movq %r15, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r13, %rdi
callq 0x2b38
leal -0x1(%r12), %edx
movq (%r14), %rax
movq %r14, %rdi
movl 0x24(%rsp), %esi
movl %edx, 0x2c(%rsp)
callq *0x10(%rax)
movl %eax, 0x28(%rsp)
movq %r12, 0x50(%rsp)
movq 0x30(%r14), %rbp
movq 0x38(%r14), %rbx
cmpq %rbx, %rbp
je 0x52e9
xorl %r15d, %r15d
xorl %r12d, %r12d
movl 0x1c(%r14), %r13d
cmpl %r13d, %r15d
jge 0x52d2
movl 0x8(%rbp), %esi
movq (%r14), %rax
movq %r14, %rdi
movl 0x2c(%rsp), %edx
callq *0x10(%rax)
movl %r15d, %r12d
cmpl %eax, 0x28(%rsp)
jne 0x52d2
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x25dc
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %r12
movq %r12, %rsi
callq 0x2e74
movq %r12, %rdi
callq 0x264c
leaq 0xc(%rbp), %rdx
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x52c6
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x52ce
movq %rsp, %rdi
callq 0x3e4a
leal 0x1(%r15), %r12d
cmpl %r13d, %r15d
jge 0x52ec
addq $0x30, %rbp
movl %r12d, %r15d
cmpq %rbx, %rbp
jne 0x525e
jmp 0x52ec
xorl %r12d, %r12d
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
setne %dil
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r15
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
je 0x5376
sarq $0x2, %rdx
xorl %ebp, %ebp
movq 0x30(%r14), %rsi
movq 0x38(%r14), %rcx
subq %rsi, %rcx
je 0x535a
testb $0x1, %dil
je 0x53fe
sarq $0x4, %rcx
imulq %r13, %rcx
movl (%rax,%rbp,4), %eax
cmpq $0x1, %rcx
adcq $0x0, %rcx
cmpl %eax, 0xc(%rsi)
je 0x5352
addq $0x30, %rsi
decq %rcx
jne 0x5342
jmp 0x535a
movq %r15, %rdi
callq 0x3150
incq %rbp
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rbp, %rdx
seta %dil
ja 0x5318
cmpl 0x1c(%r14), %r12d
leaq 0x30(%rsp), %r13
leaq 0x88(%rsp), %rbp
jge 0x53ad
movq %rbp, %rdi
callq 0x2630
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2e74
movq %rbp, %rdi
callq 0x264c
incl %r12d
cmpl 0x1c(%r14), %r12d
jl 0x5389
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl 0x28(%rsp), %esi
movq %r13, %rdx
callq *0x10(%rax)
movq %r13, %rdi
callq 0x304c
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x53dc
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
cmpl $0x1, %ebx
movl 0x2c(%rsp), %eax
movl %eax, %r12d
jg 0x5210
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3e7d(%rip), %rdi      # 0x9282
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2210
jmp 0x5451
movq %rax, %rbx
jmp 0x545e
jmp 0x5451
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x5436
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x5448
jmp 0x5451
jmp 0x5451
movq %rax, %rbx
movq %rsp, %rdi
callq 0x304c
jmp 0x5474
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x264c
jmp 0x5454
jmp 0x5451
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x304c
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x5474
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22a0