// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
leaq 0x72f7(%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
andq $0x0, 0x50(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rdi)
cvtsi2sd %r8d, %xmm0
callq 0x2250
callq 0x2030
movsd 0x37fa(%rip), %xmm1     # 0x71c0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0xc(%r14)
movapd %xmm1, %xmm0
callq 0x21c0
addsd 0x37e5(%rip), %xmm0     # 0x71c8
cvttsd2si %xmm0, %eax
movl %eax, 0x18(%r14)
imull 0x8(%r14), %eax
cmpl 0x14(%r14), %eax
jl 0x3b6b
movl 0xc(%r14), %edi
decl %edi
movsd 0x37b8(%rip), %xmm0     # 0x71c0
callq 0x21c0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%r14)
andq $0x0, 0x20(%r14)
movq 0x75af(%rip), %rdi      # 0xafd0
leaq 0x37ea(%rip), %rsi      # 0x7212
callq 0x2140
movl 0xc(%r14), %esi
movq %rax, %rdi
callq 0x2230
leaq 0x37eb(%rip), %rsi      # 0x722b
movq %rax, %rdi
callq 0x2140
movl 0x14(%r14), %esi
movq %rax, %rdi
callq 0x2230
leaq 0x37da(%rip), %rsi      # 0x7235
movq %rax, %rdi
callq 0x2140
movl 0x18(%r14), %esi
movq %rax, %rdi
callq 0x2230
leaq 0x37c9(%rip), %rsi      # 0x723f
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x20b0
callq 0x27f8
movl %ebp, %edi
callq 0x2792
movq 0x38(%r14), %rdi
movl 0x10(%r14), %esi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x30(%r14), %rdi
movl 0x18(%r14), %esi
movq (%rdi), %rax
callq *(%rax)
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
movapd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4ca8
movq %rbx, %rdi
callq 0x2898
leaq 0x10(%rsp), %rdi
movq %r13, 0x8(%rsp)
callq 0x264c
xorl %ebx, %ebx
testl %ebp, %ebp
cmovgl %ebp, %ebx
leaq 0x68(%rsp), %r15
leaq 0x10(%rsp), %r12
subl $0x1, %ebx
jb 0x3b0c
movq %r15, %rdi
callq 0x24f8
movq %r12, %rdi
movq %r15, %rsi
callq 0x26c8
jmp 0x3af2
xorl %ebp, %ebp
leaq 0xa0(%rsp), %r15
leaq 0x68(%rsp), %r12
leaq 0x10(%rsp), %r13
cmpl 0x18(%r14), %ebp
jge 0x3b4f
movq 0x30(%r14), %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4b2a
movq (%rbx), %rax
movq %rbx, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq *0x10(%rax)
movq %r15, %rdi
callq 0x2898
incl %ebp
jmp 0x3b20
leaq 0x48(%rsp), %rdi
callq 0x2898
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20c0
movq %rax, %r15
leaq 0x3663(%rip), %rsi      # 0x71e0
movq %rax, %rdi
callq 0x2080
movq 0x7464(%rip), %rsi      # 0xaff0
movq 0x7435(%rip), %rdx      # 0xafc8
movq %r15, %rdi
callq 0x2220
movq %rax, %r14
movq %r15, %rdi
callq 0x2100
jmp 0x3bd3
movq %rax, %r14
jmp 0x3bce
movq %rax, %r14
jmp 0x3bd3
movq %rax, %r14
movq %r15, %rdi
callq 0x2898
jmp 0x3bc4
jmp 0x3bc1
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x2898
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x2898
movq %r14, %rdi
callq 0x2240
nop