// File: /workspace/github_cpp_makefile_star2_O3/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 0x707b(%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), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rdi)
movq $0x0, 0x50(%rdi)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
callq 0x2260
callq 0x2040
movsd 0x3578(%rip), %xmm1     # 0x71c0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0xc(%rbx)
movapd %xmm1, %xmm0
callq 0x21e0
addsd 0x3564(%rip), %xmm0     # 0x71c8
cvttsd2si %xmm0, %eax
movl %eax, 0x18(%rbx)
imull 0x8(%rbx), %eax
cmpl 0x14(%rbx), %eax
movq %r12, 0x8(%rsp)
jl 0x3e7e
movl 0xc(%rbx), %edi
decl %edi
movsd 0x3536(%rip), %xmm0     # 0x71c0
callq 0x21e0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rbx)
movq $0x0, 0x20(%rbx)
movq 0x732b(%rip), %rdi      # 0xafd0
leaq 0x3566(%rip), %rsi      # 0x7212
movl $0x18, %edx
callq 0x2190
movl 0xc(%rbx), %esi
movq 0x7310(%rip), %rdi      # 0xafd0
callq 0x2240
movq %rax, %r14
leaq 0x355c(%rip), %rsi      # 0x722b
movl $0x9, %edx
movq %rax, %rdi
callq 0x2190
movl 0x14(%rbx), %esi
movq %r14, %rdi
callq 0x2240
movq %rax, %r14
leaq 0x3544(%rip), %rsi      # 0x7235
movl $0x9, %edx
movq %rax, %rdi
callq 0x2190
movl 0x18(%rbx), %esi
movq %r14, %rdi
callq 0x2240
movq %rax, %r14
leaq 0x352c(%rip), %rsi      # 0x723f
movl $0x8, %edx
movq %rax, %rdi
callq 0x2190
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
callq 0x2986
movl %ebp, %edi
callq 0x291e
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)
movq 0x40(%rbx), %rdi
movq 0x50(%rbx), %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%r12)
movq $0x0, 0x10(%r12)
testq %rdi, %rdi
je 0x3d94
subq %rdi, %rsi
callq 0x2170
leaq 0x68(%rsp), %rdi
callq 0x2754
testl %ebp, %ebp
jle 0x3dc3
leaq 0x10(%rsp), %r14
leaq 0x68(%rsp), %r15
movq %r14, %rdi
callq 0x25e8
movq %r15, %rdi
movq %r14, %rsi
callq 0x27e2
decl %ebp
jne 0x3dac
cmpl $0x0, 0x18(%rbx)
jle 0x3e4f
leaq 0x48(%rsp), %r14
xorl %ebp, %ebp
leaq 0xa0(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x30(%rbx), %r13
movq 0x98(%rsp), %rax
movq %rax, 0x40(%rsp)
movupd 0x68(%rsp), %xmm0
movupd 0x78(%rsp), %xmm1
movups 0x88(%rsp), %xmm2
movaps %xmm2, 0x30(%rsp)
movapd %xmm1, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x52da
movq (%r13), %rax
movq %r13, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq *0x10(%rax)
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x3e48
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
incl %ebp
cmpl 0x18(%rbx), %ebp
jl 0x3de1
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3e6c
movq 0xb0(%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, %r15
leaq 0x334e(%rip), %rsi      # 0x71e0
movq %rax, %rdi
callq 0x2090
movq 0x714f(%rip), %rsi      # 0xaff0
movq 0x7120(%rip), %rdx      # 0xafc8
movq %r15, %rdi
callq 0x2230
movq %rax, %r14
movq %r15, %rdi
callq 0x2110
jmp 0x3f02
jmp 0x3ebf
movq %rax, %r14
jmp 0x3f02
jmp 0x3ee2
movq %rax, %r14
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x3ee5
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
jmp 0x3ee5
movq %rax, %r14
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3f02
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3f1b
movq 0x50(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq %r14, %rdi
callq 0x2250
nop