// File: /workspace/github_cpp_makefile_star2_O2/obliviousram[P]PathOHeap/src/OHeapReadPathEviction.cpp
// Function: OHeapReadPathEviction::evictAndUpdateMin(int)
void OHeapReadPathEviction::evictAndUpdateMin(int leaf) {
  if (leaf != -1) {
    // pull all blocks on path into stash
    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.t != -1) {
          this->stash.push_back(b);
        }
      }
    }

    // push as many stash blocks as possible into the leaf bucket
    // then push as many stash blocks as possible into the leaf's parent
    // etc.
    for (int l = num_levels-1; l >= 0; l--) {
      auto timestamps_evicted = 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);
          timestamps_evicted.push_back(be_evicted.t);
          counter++;
        }
      }

      // removing from the stash; careful not to modify the stash while iterating over it
      for (int i = 0; i < timestamps_evicted.size(); i++) {
        for (int j = 0; j < this->stash.size(); j++) {
          if (this->stash.at(j).t == timestamps_evicted.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);
    }
  } else {
    // dummy ops
    leaf = this->rand_gen->getRandomLeaf();
    auto buckets = std::vector<Bucket>();

    for (int l = 0; l < num_levels; l++) {
      buckets.push_back(this->storage->readBucket(P(leaf, l)));
    }

    for (int l = num_levels-1; l >= 0; l--) {
      this->storage->writeBucket(this->P(leaf, l), buckets.at(l));
    }
  }

  this->updateMin(leaf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
movl %esi, (%rsp)
cmpl $-0x1, %esi
je 0x42eb
leaq 0x40(%rbx), %r14
xorl %ebp, %ebp
leaq 0x30(%rsp), %r15
movl 0xc(%rbx), %eax
cmpl %eax, %ebp
jge 0x4181
movq (%rbx), %rax
movq 0x30(%rbx), %r12
movq %rbx, %rdi
movl (%rsp), %esi
movl %ebp, %edx
callq *0x30(%rax)
movq (%r12), %rcx
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r13, %rdi
callq 0x278c
movq %r15, %rdi
movq %rax, %rsi
callq 0x4b56
leaq 0xa0(%rsp), %rdi
callq 0x2898
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %r13
cmpq %r13, %r12
je 0x4172
cmpq $-0x1, 0x8(%r12)
je 0x416c
movq %r14, %rdi
movq %r12, %rsi
callq 0x2800
addq $0x38, %r12
jmp 0x4154
movq %r15, %rdi
callq 0x2898
incl %ebp
jmp 0x40fd
movl %eax, %ebp
testl %ebp, %ebp
jle 0x43a0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x264c
decl %ebp
movq (%rbx), %rax
movq %rbx, %rdi
movl (%rsp), %esi
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
callq *0x30(%rax)
movl %eax, 0x8(%rsp)
movq 0x40(%rbx), %rbp
movq 0x48(%rbx), %r12
movl $0x0, 0x4(%rsp)
cmpq %r12, %rbp
je 0x421c
movl 0x4(%rsp), %eax
cmpl 0x8(%rbx), %eax
jge 0x421c
movl (%rbp), %esi
movq (%rbx), %rax
movq %rbx, %rdi
movl 0xc(%rsp), %edx
callq *0x30(%rax)
cmpl %eax, 0x8(%rsp)
jne 0x4216
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x26c8
movl 0x8(%rbp), %eax
movl %eax, 0x30(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4e84
incl 0x4(%rsp)
addq $0x38, %rbp
jmp 0x41cb
xorl %ebp, %ebp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x2, %rax
cmpq %rbp, %rax
jbe 0x428b
pushq $-0x38
popq %r12
xorl %r13d, %r13d
movq 0x48(%rbx), %rax
subq 0x40(%rbx), %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
cmpq %r13, %rax
jbe 0x4286
movq %r14, %rdi
movq %r13, %rsi
callq 0x2836
movq 0x8(%rax), %r15
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x2f0c
movslq (%rax), %rax
incq %r13
addq $0x38, %r12
cmpq %rax, %r15
jne 0x4238
addq (%r14), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x2854
incq %rbp
jmp 0x421e
movl 0xc(%rsp), %ebp
leaq 0x30(%rsp), %r15
movl 0x4(%rsp), %eax
cmpl 0x8(%rbx), %eax
jge 0x42bc
movl %eax, %r12d
movq %r15, %rdi
callq 0x24f8
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x26c8
movl %r12d, %eax
incl %eax
jmp 0x4298
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
movl 0x8(%rsp), %esi
leaq 0x68(%rsp), %rdx
callq *0x10(%rax)
leaq 0xa0(%rsp), %rdi
callq 0x2898
leaq 0x10(%rsp), %rdi
callq 0x2f2e
jmp 0x4183
movq 0x38(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, (%rsp)
leaq 0xa0(%rsp), %r14
leaq 0x30(%rsp), %r15
andq $0x0, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
xorl %ebp, %ebp
leaq 0x68(%rsp), %r12
movl 0xc(%rbx), %r13d
cmpl %r13d, %ebp
jge 0x4358
movq (%rbx), %rax
movq 0x30(%rbx), %r13
movq %rbx, %rdi
movl (%rsp), %esi
movl %ebp, %edx
callq *0x30(%rax)
movq (%r13), %rcx
movq %r12, %rdi
movq %r13, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4fdc
movq %r14, %rdi
callq 0x2898
incl %ebp
jmp 0x4317
leaq 0x30(%rsp), %r14
testl %r13d, %r13d
jle 0x4396
leal -0x1(%r13), %edx
movq (%rbx), %rax
movq 0x30(%rbx), %r15
movq %rbx, %rdi
movl (%rsp), %esi
callq *0x30(%rax)
movl %eax, %ebp
decq %r13
movl %r13d, %esi
movq %r14, %rdi
callq 0x4bc6
movq (%r15), %rcx
movq %r15, %rdi
movl %ebp, %esi
movq %rax, %rdx
callq *0x10(%rcx)
jmp 0x435d
leaq 0x30(%rsp), %rdi
callq 0x4be4
movq %rbx, %rdi
movl (%rsp), %esi
callq 0x45c8
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r14, %rdi
callq 0x2898
jmp 0x43d8
jmp 0x4408
jmp 0x4408
movq %rax, %rbx
jmp 0x4418
jmp 0x43d5
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4be4
jmp 0x4422
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x43fd
jmp 0x4408
jmp 0x4408
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2898
jmp 0x4422
jmp 0x4408
jmp 0x4408
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x2898
leaq 0x10(%rsp), %rdi
callq 0x2f2e
movq %rbx, %rdi
callq 0x2240