// File: /workspace/github_cpp_makefile_star2_O3/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)
cmpl $0x0, 0x20(%rdi)
jle 0x56bc
movq %rdi, %r14
leaq 0x30(%rdi), %rax
movq %rax, 0x30(%rsp)
xorl %ebx, %ebx
leaq 0x40(%rsp), %r12
movq %rsp, %r13
movq (%r14), %rax
movq 0x8(%r14), %rbp
movq %r14, %rdi
movl 0x24(%rsp), %esi
movl %ebx, %edx
callq *0x10(%rax)
movq (%rbp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2ef4
movq %r12, %rdi
callq 0x2ff0
movq (%rsp), %rbp
movq 0x8(%rsp), %r15
cmpq %r15, %rbp
je 0x54dd
cmpl $-0x1, 0xc(%rbp)
je 0x54d7
movq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x30ce
addq $0x30, %rbp
jmp 0x54bf
movq %r13, %rdi
callq 0x2ff0
incl %ebx
movl 0x20(%r14), %eax
cmpl %eax, %ebx
jl 0x5481
testl %eax, %eax
jle 0x56bc
movq %rax, %rbx
leaq 0x40(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, %rdi
callq 0x2b28
movq %r12, %rbp
movq %rbx, 0x38(%rsp)
leal -0x1(%rbx), %edx
movq (%r14), %rax
movq %r14, %rdi
movl 0x24(%rsp), %esi
movl %edx, 0x2c(%rsp)
callq *0x10(%rax)
movl %eax, 0x28(%rsp)
movq 0x30(%r14), %r12
movq 0x38(%r14), %rbx
cmpq %rbx, %r12
setne %al
movl 0x1c(%r14), %edx
testl %edx, %edx
setg %cl
andb %al, %cl
xorl %r15d, %r15d
cmpb $0x1, %cl
jne 0x55d2
movl 0x8(%r12), %esi
movq (%r14), %rax
movq %r14, %rdi
movl 0x2c(%rsp), %edx
callq *0x10(%rax)
cmpl %eax, 0x28(%rsp)
jne 0x55c0
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x25dc
movq %rbp, %rdi
leaq 0x58(%rsp), %r13
movq %r13, %rsi
callq 0x2e12
movq %r13, %rdi
callq 0x2638
leaq 0xc(%r12), %rdx
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x55b5
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x55bd
movq %rsp, %rdi
callq 0x41d0
incl %r15d
addq $0x30, %r12
movl 0x1c(%r14), %edx
cmpq %rbx, %r12
je 0x55d2
cmpl %edx, %r15d
jl 0x5558
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
cmpq %rax, %rcx
movq %rbp, %r12
leaq 0x88(%rsp), %r13
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
je 0x5650
xorl %ebx, %ebx
movq 0x30(%r14), %rsi
movq 0x38(%r14), %rdx
subq %rsi, %rdx
je 0x563a
sarq $0x4, %rdx
imulq %rbp, %rdx
movl (%rax,%rbx,4), %edi
cmpq $0x1, %rdx
adcq $0x0, %rdx
cmpl %edi, 0xc(%rsi)
je 0x5627
addq $0x30, %rsi
decq %rdx
jne 0x5617
jmp 0x563a
movq 0x30(%rsp), %rdi
callq 0x30fa
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
incq %rbx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
ja 0x55f7
movl 0x1c(%r14), %edx
cmpl %edx, %r15d
jge 0x5679
movq %r13, %rdi
callq 0x261c
movq %r12, %rdi
movq %r13, %rsi
callq 0x2e12
movq %r13, %rdi
callq 0x2638
incl %r15d
cmpl 0x1c(%r14), %r15d
jl 0x5655
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl 0x28(%rsp), %esi
movq %r12, %rdx
callq *0x10(%rax)
movq %r12, %rdi
callq 0x2ff0
movq (%rsp), %rdi
testq %rdi, %rdi
movq 0x38(%rsp), %rbx
je 0x56ad
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
cmpl $0x1, %ebx
movl 0x2c(%rsp), %eax
movl %eax, %ebx
jg 0x54ff
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x570e
movq %rax, %rbx
jmp 0x571b
jmp 0x570e
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
jmp 0x5707
jmp 0x570e
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x56fa
jmp 0x570e
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2638
jmp 0x5711
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2ff0
jmp 0x5731
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2ff0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x5731
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22a0
nop