// File: /workspace/github_cpp_makefile_star2_O1/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
movl %esi, 0x4(%rsp)
cmpl $-0x1, %esi
je 0x48b7
movl 0xc(%rbx), %r13d
testl %r13d, %r13d
jle 0x46af
leaq 0x40(%rbx), %r14
xorl %r15d, %r15d
leaq 0x78(%rsp), %r12
movq (%rbx), %rax
movq 0x30(%rbx), %rbp
movq %rbx, %rdi
movl 0x4(%rsp), %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 0x2946
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x540c
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4631
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %r13
cmpq %r13, %rbp
je 0x4688
cmpq $-0x1, 0x8(%rbp)
je 0x4682
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x4677
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 0x4682
movq %r14, %rdi
movq %rbp, %rdx
callq 0x29bc
addq $0x38, %rbp
jmp 0x463b
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x469f
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
incl %r15d
movl 0xc(%rbx), %r13d
cmpl %r13d, %r15d
jl 0x45dc
testl %r13d, %r13d
jle 0x49a4
leaq 0x78(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %r12, %rdi
callq 0x275c
movq %r13, 0x70(%rsp)
leal -0x1(%r13), %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), %r13
cmpq %r13, %rbp
je 0x477c
xorl %r15d, %r15d
xorl %r14d, %r14d
movl 0x8(%rbx), %r12d
cmpl %r12d, %r15d
jge 0x4769
movl (%rbp), %esi
movq (%rbx), %rax
movq %rbx, %rdi
movl 0xc(%rsp), %edx
callq *0x30(%rax)
movl %r15d, %r14d
cmpl %eax, 0x8(%rsp)
jne 0x4769
leaq 0x78(%rsp), %rdi
movq %rbp, %rsi
callq 0x27ea
movl 0x8(%rbp), %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4756
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x4765
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x5674
leal 0x1(%r15), %r14d
cmpl %r12d, %r15d
jge 0x477f
addq $0x38, %rbp
movl %r14d, %r15d
cmpq %r13, %rbp
jne 0x4707
jmp 0x477f
xorl %r14d, %r14d
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
subq %rcx, %rax
setne %r9b
movabsq $0x6db6db6db6db6db7, %r15 # imm = 0x6DB6DB6DB6DB6DB7
je 0x482b
sarq $0x2, %rax
xorl %ebp, %ebp
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rdi
movq %rdi, %r8
subq %rsi, %r8
movq %rdi, %rdx
subq %rsi, %rdx
je 0x480a
testb $0x1, %r9b
je 0x49c2
sarq $0x3, %r8
imulq %r15, %r8
movslq (%rcx,%rbp,4), %rax
cmpq $0x1, %r8
adcq $0x0, %r8
addq $0x38, %rsi
addq $-0x38, %rdx
cmpq %rax, -0x30(%rsi)
je 0x47f7
addq $0x38, %rsi
addq $-0x38, %rdx
decq %r8
jne 0x47e2
jmp 0x480a
cmpq %rdi, %rsi
je 0x4805
leaq -0x38(%rsi), %rdi
callq 0x2210
addq $-0x38, 0x48(%rbx)
incq %rbp
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
subq %rcx, %rax
sarq $0x2, %rax
cmpq %rbp, %rax
seta %r9b
ja 0x47a6
cmpl 0x8(%rbx), %r14d
movq 0x70(%rsp), %r15
leaq 0x78(%rsp), %r12
leaq 0x30(%rsp), %r13
jge 0x485c
movq %r13, %rdi
callq 0x25e8
movq %r12, %rdi
movq %r13, %rsi
callq 0x27ea
incl %r14d
cmpl 0x8(%rbx), %r14d
jl 0x4840
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
movl 0x8(%rsp), %esi
movq %r12, %rdx
callq *0x10(%rax)
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x488a
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x48a1
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
cmpl $0x1, %r15d
movl 0xc(%rsp), %eax
movl %eax, %r13d
jg 0x46bd
jmp 0x49a4
movq 0x38(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, 0x4(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl 0xc(%rbx), %eax
testl %eax, %eax
jle 0x493b
xorl %ebp, %ebp
leaq 0x78(%rsp), %r14
leaq 0x30(%rsp), %r15
movq (%rbx), %rax
movq 0x30(%rbx), %r12
movq %rbx, %rdi
movl 0x4(%rsp), %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 0x5750
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4932
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
incl %ebp
movl 0xc(%rbx), %eax
cmpl %eax, %ebp
jl 0x48e8
movl %eax, %r15d
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
testl %r15d, %r15d
jle 0x499a
leal -0x1(%r15), %edx
movq (%rbx), %rax
movq 0x30(%rbx), %r14
movq %rbx, %rdi
movl 0x4(%rsp), %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 0x49d6
leaq (%rsi,%rsi,4), %rcx
shlq $0x4, %rcx
addq %rcx, %rdx
movq (%r14), %rcx
movq %r14, %rdi
movl %eax, %esi
callq *0x10(%rcx)
jmp 0x4948
leaq 0x30(%rsp), %rdi
callq 0x54aa
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x4c4e
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26d0(%rip), %rdi      # 0x7099
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21d0
leaq 0x26bc(%rip), %rdi      # 0x7099
movq %rcx, %rdx
xorl %eax, %eax
callq 0x21d0
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4a19
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
jmp 0x4a19
jmp 0x4a16
jmp 0x4a59
movq %rax, %rbx
jmp 0x4a79
jmp 0x4a59
jmp 0x4a16
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x54aa
jmp 0x4a90
jmp 0x4a59
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4a90
movq 0xc0(%rsp), %rsi
jmp 0x4a88
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4a90
movq 0x40(%rsp), %rsi
jmp 0x4a88
jmp 0x4a59
jmp 0x4a59
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4a79
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4a90
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2250