// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movl %esi, 0x4(%rsp)
movq %rdi, %r14
leaq 0x30(%rdi), %rax
movq %rax, 0x28(%rsp)
xorl %ebx, %ebx
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %r15
movl 0x20(%r14), %ebp
cmpl %ebp, %ebx
jge 0x4b80
movq (%r14), %rax
movq 0x8(%r14), %rbp
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
callq *0x10(%rax)
movq (%rbp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2c1a
movq %r12, %rdi
callq 0x2d0c
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r13
cmpq %r13, %rbp
je 0x4b74
cmpl $-0x1, 0xc(%rbp)
je 0x4b6e
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x2d80
addq $0x30, %rbp
jmp 0x4b56
movq %r15, %rdi
callq 0x2d0c
incl %ebx
jmp 0x4b0f
leaq 0x30(%rsp), %rbx
testl %ebp, %ebp
jle 0x4d05
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %rbx, %rdi
callq 0x28e4
decl %ebp
movq (%r14), %rax
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
callq *0x10(%rax)
movl %eax, 0x8(%rsp)
movq 0x30(%r14), %rbp
movq 0x38(%r14), %r13
addq $0xc, %rbp
movl $0x0, (%rsp)
leaq -0xc(%rbp), %r12
cmpq %r13, %r12
je 0x4c32
movl (%rsp), %eax
cmpl 0x1c(%r14), %eax
jge 0x4c32
movl -0x4(%rbp), %esi
movq (%r14), %rax
movq %r14, %rdi
movl 0xc(%rsp), %edx
callq *0x10(%rax)
cmpl %eax, 0x8(%rsp)
jne 0x4c2c
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x253c
movq %rbx, %rdi
leaq 0x48(%rsp), %r15
movq %r15, %rsi
callq 0x2b68
movq %r15, %rdi
callq 0x2594
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x36d4
incl (%rsp)
addq $0x30, %rbp
jmp 0x4bcf
xorl %ebp, %ebp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x2, %rax
cmpq %rbp, %rax
jbe 0x4ca6
pushq $-0x30
popq %r12
xorl %r13d, %r13d
movq 0x38(%r14), %rax
subq 0x30(%r14), %rax
cqto
pushq $0x30
popq %rcx
idivq %rcx
cmpq %r13, %rax
jbe 0x4ca1
movq %rbx, %r15
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x4f0a
movl 0xc(%rax), %ebx
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x4f28
incq %r13
addq $0x30, %r12
cmpl (%rax), %ebx
movq %r15, %rbx
jne 0x4c4e
movq 0x28(%rsp), %rdi
addq (%rdi), %r12
movq %r12, %rsi
callq 0x2dac
incq %rbp
jmp 0x4c34
movl 0xc(%rsp), %ebp
leaq 0x78(%rsp), %r15
movl (%rsp), %eax
cmpl 0x1c(%r14), %eax
jge 0x4cdd
movl %eax, %r12d
movq %r15, %rdi
callq 0x257a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2b68
movq %r15, %rdi
callq 0x2594
movl %r12d, %eax
incl %eax
jmp 0x4cb2
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl 0x8(%rsp), %esi
movq %rbx, %rdx
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x2d0c
leaq 0x10(%rsp), %rdi
callq 0x3750
jmp 0x4b85
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4d58
jmp 0x4d58
movq %rax, %rbx
jmp 0x4d65
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x4d3e
jmp 0x4d58
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x4d4d
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2d0c
jmp 0x4d6f
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x2594
jmp 0x4d5b
jmp 0x4d58
jmp 0x4d58
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2d0c
leaq 0x10(%rsp), %rdi
callq 0x3750
movq %rbx, %rdi
callq 0x22b0
nop