// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movq %rdi, %rbx
cmpl $-0x1, %esi
je 0x47d3
movl %esi, %r13d
cmpl $0x0, 0xc(%rbx)
jle 0x48bd
leaq 0x40(%rbx), %r14
xorl %r15d, %r15d
leaq 0x78(%rsp), %r12
movl %r13d, 0x4(%rsp)
movq (%rbx), %rax
movq 0x30(%rbx), %rbp
movq %rbx, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq *0x30(%rax)
movq (%rbp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r12, %rdi
callq 0x2918
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x52da
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4559
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %r13
cmpq %r13, %rbp
je 0x45b8
cmpq $-0x1, 0x8(%rbp)
je 0x45aa
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x459f
movq 0x30(%rbp), %rax
movq %rax, 0x30(%rsi)
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups %xmm2, 0x20(%rsi)
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x38, 0x48(%rbx)
jmp 0x45aa
movq %r14, %rdi
movq %rbp, %rdx
callq 0x298e
addq $0x38, %rbp
cmpq %r13, %rbp
jne 0x4568
movq 0x30(%rsp), %rbp
testq %rbp, %rbp
movl 0x4(%rsp), %r13d
je 0x45d2
movq 0x40(%rsp), %rsi
subq %rbp, %rsi
movq %rbp, %rdi
callq 0x2170
incl %r15d
movl 0xc(%rbx), %ebp
cmpl %ebp, %r15d
jl 0x4505
testl %ebp, %ebp
jle 0x48bd
leaq 0x30(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x2754
movq %rbp, 0x70(%rsp)
leal -0x1(%rbp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movl %edx, 0xc(%rsp)
callq *0x30(%rax)
movl %eax, 0x8(%rsp)
movq 0x40(%rbx), %rbp
movq 0x48(%rbx), %r12
cmpq %r12, %rbp
setne %al
movl 0x8(%rbx), %edx
testl %edx, %edx
setg %cl
andb %al, %cl
xorl %r14d, %r14d
cmpb $0x1, %cl
jne 0x46aa
movl (%rbp), %esi
movq (%rbx), %rax
movq %rbx, %rdi
movl 0xc(%rsp), %edx
callq *0x30(%rax)
cmpl %eax, 0x8(%rsp)
jne 0x4699
leaq 0x78(%rsp), %rdi
movq %rbp, %rsi
callq 0x27e2
movl 0x8(%rbp), %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4689
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x4696
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0x5508
incl %r14d
addq $0x38, %rbp
movl 0x8(%rbx), %edx
cmpq %r12, %rbp
je 0x46aa
cmpl %edx, %r14d
jl 0x4646
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
je 0x4746
movq 0x48(%rbx), %rdi
xorl %ebp, %ebp
movabsq $0x6db6db6db6db6db7, %r12 # imm = 0x6DB6DB6DB6DB6DB7
movq 0x40(%rbx), %rsi
movq %rdi, %rdx
subq %rsi, %rdx
je 0x4731
movq %rdx, %r8
sarq $0x3, %r8
imulq %r12, %r8
movslq (%rax,%rbp,4), %r9
cmpq $0x1, %r8
adcq $0x0, %r8
addq $0x38, %rsi
addq $-0x38, %rdx
cmpq %r9, -0x30(%rsi)
je 0x470d
addq $0x38, %rsi
addq $-0x38, %rdx
decq %r8
jne 0x46f8
jmp 0x4731
cmpq %rdi, %rsi
je 0x4729
leaq -0x38(%rsi), %rdi
callq 0x2210
movq 0x48(%rbx), %rdi
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
addq $-0x38, %rdi
movq %rdi, 0x48(%rbx)
incq %rbp
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rbp, %rdx
ja 0x46cd
movl 0x8(%rbx), %edx
cmpl %edx, %r14d
movq 0x70(%rsp), %r12
leaq 0x78(%rsp), %rbp
jge 0x4774
movq %r15, %r13
movq %r15, %rdi
callq 0x25e8
movq %rbp, %rdi
movq %r13, %rsi
callq 0x27e2
incl %r14d
cmpl 0x8(%rbx), %r14d
jl 0x4755
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
movl 0x8(%rsp), %esi
movq %rbp, %rdx
callq *0x10(%rax)
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x47a2
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x47b9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
cmpl $0x1, %r12d
movl 0xc(%rsp), %eax
movl %eax, %ebp
jg 0x45ee
movl 0x4(%rsp), %r13d
jmp 0x48bd
movq 0x38(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl 0xc(%rbx), %eax
testl %eax, %eax
jle 0x4855
xorl %ebp, %ebp
leaq 0x78(%rsp), %r14
leaq 0x30(%rsp), %r15
movq (%rbx), %rax
movq 0x30(%rbx), %r12
movq %rbx, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq *0x30(%rax)
movq (%r12), %rcx
movq %r14, %rdi
movq %r12, %rsi
movl %eax, %edx
callq *0x8(%rcx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x55e4
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x484c
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
incl %ebp
movl 0xc(%rbx), %eax
cmpl %eax, %ebp
jl 0x4803
movl %eax, %r15d
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
testl %r15d, %r15d
jle 0x48b3
leal -0x1(%r15), %edx
movq (%rbx), %rax
movq 0x30(%rbx), %r14
movq %rbx, %rdi
movl %r13d, %esi
callq *0x30(%rax)
decq %r15
movl %r15d, %esi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
subq %rdx, %rcx
sarq $0x4, %rcx
imulq %r12, %rcx
cmpq %rsi, %rcx
jbe 0x48da
leaq (%rsi,%rsi,4), %rcx
shlq $0x4, %rcx
addq %rcx, %rdx
movq (%r14), %rcx
movq %r14, %rdi
movl %eax, %esi
callq *0x10(%rcx)
jmp 0x4862
leaq 0x30(%rsp), %rdi
callq 0x5378
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4b50
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x283c(%rip), %rdi      # 0x711d
movq %rcx, %rdx
xorl %eax, %eax
callq 0x21d0
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x491d
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
jmp 0x491d
jmp 0x495b
movq %rax, %rbx
jmp 0x497b
jmp 0x495b
jmp 0x491a
jmp 0x491a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5378
jmp 0x4992
jmp 0x495b
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4992
movq 0xc0(%rsp), %rsi
jmp 0x498a
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4992
movq 0x40(%rsp), %rsi
jmp 0x498a
jmp 0x495b
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x497b
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4992
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2250