// File: /workspace/github_cpp_makefile_star2_O1/obliviousram[P]PathOHeap/src/OHeapReadPathEviction.cpp
// Function: OHeapReadPathEviction::OHeapReadPathEviction(UntrustedStorageInterface*, RandForOHeapInterface*, int, int, bool)
OHeapReadPathEviction::OHeapReadPathEviction(UntrustedStorageInterface* storage,
    RandForOHeapInterface* rand_gen, int bucket_size, int num_blocks, bool type_hiding_security)
    : storage(storage), rand_gen(rand_gen), bucket_size(bucket_size), num_blocks(num_blocks),
      type_hiding_security(type_hiding_security) {
  this->num_levels = ceil(log2(num_blocks)) + 1;
  this->num_buckets = pow(2, num_levels) - 1;
  if (this->num_buckets*this->bucket_size < this->num_blocks) {  // potential precision loss
    throw std::runtime_error("Not enough space for the acutal number of blocks.");
  }
  this->num_leaves = pow(2, num_levels - 1);
  this->t = 0;
  
  std::cout << "Initializing OHeap with " << this->num_levels << " levels, " <<
      this->num_blocks << " blocks, " << this->num_buckets << " buckets" << std::endl;

  Bucket::resetState();
  Bucket::setMaxSize(bucket_size);
  this->rand_gen->setBound(num_leaves);
  this->storage->setCapacity(num_buckets);
  
  this->stash = std::vector<Block>();

  Bucket init_bkt = Bucket();
  for (int i = 0; i < bucket_size; i++) {
    init_bkt.addBlock(Block());
  }

  for (int i = 0; i < num_buckets; i++) {
    this->storage->writeBucket(i, Bucket(init_bkt));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %ecx, %ebp
movq %rdi, %rbx
leaq 0x6fcb(%rip), %rax      # 0xac80
movq %rax, (%rdi)
movl %ecx, 0x8(%rdi)
movl %r8d, 0x14(%rdi)
movb %r9b, 0x28(%rdi)
movq %rsi, 0x30(%rdi)
movq %rdx, 0x38(%rdi)
leaq 0x40(%rdi), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rdi)
movq $0x0, 0x50(%rdi)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
callq 0x2260
callq 0x2040
movsd 0x34c8(%rip), %xmm1     # 0x71c0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0xc(%rbx)
movapd %xmm1, %xmm0
callq 0x21e0
addsd 0x34b4(%rip), %xmm0     # 0x71c8
cvttsd2si %xmm0, %eax
movl %eax, 0x18(%rbx)
imull 0x8(%rbx), %eax
cmpl 0x14(%rbx), %eax
movq %r13, 0x8(%rsp)
jl 0x3f3b
movl 0xc(%rbx), %edi
decl %edi
movsd 0x3486(%rip), %xmm0     # 0x71c0
callq 0x21e0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rbx)
movq $0x0, 0x20(%rbx)
movq 0x727b(%rip), %rdi      # 0xafd0
leaq 0x34b6(%rip), %rsi      # 0x7212
movl $0x18, %edx
callq 0x2190
movl 0xc(%rbx), %esi
movq 0x7260(%rip), %rdi      # 0xafd0
callq 0x2240
movq %rax, %r15
leaq 0x34ac(%rip), %rsi      # 0x722b
movl $0x9, %edx
movq %rax, %rdi
callq 0x2190
movl 0x14(%rbx), %esi
movq %r15, %rdi
callq 0x2240
movq %rax, %r15
leaq 0x3494(%rip), %rsi      # 0x7235
movl $0x9, %edx
movq %rax, %rdi
callq 0x2190
movl 0x18(%rbx), %esi
movq %r15, %rdi
callq 0x2240
movq %rax, %r15
leaq 0x347c(%rip), %rsi      # 0x723f
movl $0x8, %edx
movq %rax, %rdi
callq 0x2190
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
callq 0x29b4
movl %ebp, %edi
callq 0x294c
movq 0x38(%rbx), %rdi
movl 0x10(%rbx), %esi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x30(%rbx), %rdi
movl 0x18(%rbx), %esi
movq (%rdi), %rax
callq *(%rax)
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movapd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
movq %r13, %rdi
movq %r14, %rsi
callq 0x54f8
movq (%r14), %rdi
testq %rdi, %rdi
je 0x3e52
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
leaq 0x10(%rsp), %rdi
callq 0x275c
testl %ebp, %ebp
jle 0x3e81
leaq 0x60(%rsp), %r15
leaq 0x10(%rsp), %r12
movq %r15, %rdi
callq 0x25e8
movq %r12, %rdi
movq %r15, %rsi
callq 0x27ea
decl %ebp
jne 0x3e6a
cmpl $0x0, 0x18(%rbx)
jle 0x3f12
leaq 0x98(%rsp), %r15
xorl %ebp, %ebp
leaq 0x48(%rsp), %r12
leaq 0x60(%rsp), %r13
movq 0x30(%rbx), %r14
movq 0x40(%rsp), %rax
movq %rax, 0x90(%rsp)
movupd 0x10(%rsp), %xmm0
movupd 0x20(%rsp), %xmm1
movups 0x30(%rsp), %xmm2
movaps %xmm2, 0x80(%rsp)
movapd %xmm1, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x540c
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
movq %r13, %rdx
callq *0x10(%rax)
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3f0b
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
incl %ebp
cmpl 0x18(%rbx), %ebp
jl 0x3e9f
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x3f29
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20c0
movq %rax, %r12
leaq 0x3291(%rip), %rsi      # 0x71e0
movq %rax, %rdi
callq 0x2090
movq 0x7092(%rip), %rsi      # 0xaff0
movq 0x7063(%rip), %rdx      # 0xafc8
movq %r12, %rdi
callq 0x2230
movq %rax, %r15
movq %r12, %rdi
callq 0x2110
jmp 0x3fbf
jmp 0x3f7c
movq %rax, %r15
jmp 0x3fbf
jmp 0x3fa5
movq %rax, %r15
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3fa8
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
jmp 0x3fa8
movq %rax, %r15
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x3fbf
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3fd8
movq 0x50(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq %r15, %rdi
callq 0x2250