// File: /workspace/github_cpp_makefile_star2_O1/obliviousram[P]oblivious-avl-tree/src/OAVLTreeDeterministicEviction.cpp
// Function: OAVLTreeDeterministicEviction::OAVLTreeDeterministicEviction(UntrustedStorageInterface*, RandForOramInterface*, int, int)
OAVLTreeDeterministicEviction::OAVLTreeDeterministicEviction(UntrustedStorageInterface* storage, RandForOramInterface* rand_gen,
                                           int bucket_size, int num_blocks) {
    this->G = 0;
    this->storage = storage;
    this->rand_gen = rand_gen;
    this->bucket_size = bucket_size;
    this->num_blocks = num_blocks;
    this->num_levels = ceil(log10(num_blocks) / log10(2)) + 1;
    this->num_buckets = pow(2, num_levels)-1;
    if (this->num_buckets*this->bucket_size < this->num_blocks) //deal with precision loss
    {
        throw new runtime_error("Not enough space for the acutal number of blocks.");
    }
    this->num_leaves = pow(2, num_levels-1);
    Bucket::resetState();
    Bucket::setMaxSize(bucket_size);
    this->rand_gen->setBound(num_leaves);
    this->storage->setCapacity(num_buckets);
    this->id_gen = new IdGenerator;
    this->stash = vector<Block>();
    

    for(int i = 0; i < num_buckets; i++){

        Bucket init_bkt = Bucket();
        for(int j = 0; j < bucket_size; j++){
            init_bkt.addBlock(Block());
        }
        storage->writeBucket(i, Bucket(init_bkt));
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %r14d
movq %rdi, %rbx
leaq 0x8c8d(%rip), %rax      # 0xcbd0
movq %rax, (%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x50(%rdi)
movq $-0x1, 0x68(%rdi)
movl $0x0, 0x18(%rdi)
movq %rsi, 0x8(%rsp)
movq %rsi, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
movl %ecx, 0x1c(%rdi)
movl %r8d, 0x28(%rdi)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
callq 0x20d0
divsd 0x529b(%rip), %xmm0     # 0x9228
callq 0x2040
movsd 0x5296(%rip), %xmm1     # 0x9230
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0x20(%rbx)
movapd %xmm1, %xmm0
callq 0x2220
addsd 0x5282(%rip), %xmm0     # 0x9238
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rbx)
imull 0x1c(%rbx), %eax
cmpl 0x28(%rbx), %eax
movq %rbx, (%rsp)
jl 0x412e
movl 0x20(%rbx), %edi
decl %edi
movsd 0x5255(%rip), %xmm0     # 0x9230
callq 0x2220
cvttsd2si %xmm0, %eax
movl %eax, 0x24(%rbx)
callq 0x2fea
movl %r14d, %edi
callq 0x2f5e
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x24(%rax), %esi
movq (%rdi), %rax
callq *0x8(%rax)
movq (%rsp), %rax
movq 0x8(%rax), %rdi
movl 0x2c(%rax), %esi
movq (%rdi), %rax
callq *(%rax)
movl $0x4, %edi
callq 0x2180
movl $0x0, (%rax)
movq (%rsp), %r12
movq %rax, 0x60(%r12)
xorl %r15d, %r15d
leaq 0x10(%rsp), %rbx
movq %r15, 0x10(%rbx)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rbx)
leaq 0x30(%rsp), %rdi
movq %r15, 0x10(%rdi)
movapd %xmm0, (%rdi)
movq 0x30(%r12), %rax
movupd 0x38(%r12), %xmm0
movq (%rbx), %rcx
movq %rcx, 0x30(%r12)
movq 0x8(%rbx), %rcx
movq %rcx, 0x38(%r12)
movq 0x10(%rbx), %rcx
movq %rcx, 0x40(%r12)
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
movq 0x10(%rdi), %rsi
movq %rax, (%rdi)
movupd %xmm0, 0x8(%rdi)
movq %rcx, (%rbx)
movq %rdx, 0x8(%rbx)
movq %rsi, 0x10(%rbx)
callq 0x304c
movq %rbx, %rdi
callq 0x304c
cmpl $0x0, 0x2c(%r12)
jle 0x411f
leaq 0x30(%rsp), %r12
leaq 0x48(%rsp), %r13
leaq 0x10(%rsp), %rbx
movq %r12, %rdi
callq 0x2b38
movl %r14d, %ebp
testl %r14d, %r14d
jle 0x40e6
movq %r13, %rdi
callq 0x2630
movq %r12, %rdi
movq %r13, %rsi
callq 0x2e74
movq %r13, %rdi
callq 0x264c
decl %ebp
jne 0x40c7
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31c2
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl %r15d, %esi
movq %rbx, %rdx
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x304c
movq %r12, %rdi
callq 0x304c
incl %r15d
movq (%rsp), %rax
cmpl 0x2c(%rax), %r15d
jl 0x40b7
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x20c0
movq %rax, %rbx
movl $0x10, %edi
callq 0x2180
movq %rax, %r15
leaq 0x5101(%rip), %rsi      # 0x9250
movq %rax, %rdi
callq 0x2090
movq %r15, (%rbx)
leaq 0x8a3f(%rip), %rsi      # 0xcba0
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2280
movq %rax, %r14
movl $0x10, %esi
movq %r15, %rdi
callq 0x2190
jmp 0x4180
movq %rax, %r14
movq %rbx, %rdi
callq 0x2110
jmp 0x41be
jmp 0x419d
jmp 0x41b1
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x304c
jmp 0x41b4
movq %rax, %r14
jmp 0x41be
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x264c
jmp 0x41b4
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x304c
movq (%rsp), %rdi
leaq 0x30(%rdi), %rbx
addq $0x48, %rdi
callq 0x304c
movq %rbx, %rdi
callq 0x304c
movq %r14, %rdi
callq 0x22a0
nop