// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movl %ecx, %r15d
movq %rdi, %rbx
leaq 0x8906(%rip), %rax      # 0xcbd0
movq %rax, (%rdi)
leaq 0x30(%rdi), %rax
movq %rax, 0x10(%rsp)
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, 0x18(%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 0x4f43(%rip), %xmm0     # 0x9260
callq 0x2040
movsd 0x4f3e(%rip), %xmm1     # 0x9268
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0x20(%rbx)
movapd %xmm1, %xmm0
callq 0x2220
addsd 0x4f2a(%rip), %xmm0     # 0x9270
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rbx)
imull 0x1c(%rbx), %eax
cmpl 0x28(%rbx), %eax
movq %rbx, 0x8(%rsp)
jl 0x4497
movl 0x20(%rbx), %edi
decl %edi
movsd 0x4efc(%rip), %xmm0     # 0x9268
callq 0x2220
cvttsd2si %xmm0, %eax
movl %eax, 0x24(%rbx)
callq 0x2f8e
movl %r15d, %edi
callq 0x2f02
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x24(%rax), %esi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movl 0x2c(%rax), %esi
movq (%rdi), %rax
callq *(%rax)
movl $0x4, %edi
callq 0x2180
movl $0x0, (%rax)
movq 0x8(%rsp), %r14
movq %rax, 0x60(%r14)
movq 0x40(%r14), %rax
movupd 0x30(%r14), %xmm0
xorl %r12d, %r12d
movq 0x10(%rsp), %rcx
movq %r12, 0x10(%rcx)
xorpd %xmm1, %xmm1
movupd %xmm1, (%rcx)
leaq 0x40(%rsp), %rdi
movapd %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
leaq 0x20(%rsp), %rbx
movapd %xmm1, (%rbx)
movq %r12, 0x10(%rbx)
callq 0x2ff0
movq %rbx, %rdi
callq 0x2ff0
cmpl $0x0, 0x2c(%r14)
jle 0x4485
leaq 0x40(%rsp), %r13
leaq 0x58(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq %r13, %rdi
callq 0x2b28
movl %r15d, %ebp
testl %r15d, %r15d
jle 0x444b
movq %rbx, %rdi
callq 0x261c
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2e12
movq %rbx, %rdi
callq 0x2638
decl %ebp
jne 0x442c
movq %r14, %rdi
movq %r13, %rsi
callq 0x3170
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movl %r12d, %esi
movq %r14, %rdx
callq *0x10(%rax)
movq %r14, %rdi
callq 0x2ff0
movq %r13, %rdi
callq 0x2ff0
incl %r12d
movq 0x8(%rsp), %rax
cmpl 0x2c(%rax), %r12d
jl 0x441c
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x20c0
movq %rax, %r14
movl $0x10, %edi
callq 0x2180
movq %rax, %r15
leaq 0x4dd8(%rip), %rsi      # 0x9290
movq %rax, %rdi
callq 0x2090
movq %r15, (%r14)
leaq 0x86d6(%rip), %rsi      # 0xcba0
movq %r14, %rdi
xorl %edx, %edx
callq 0x2280
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x2190
jmp 0x44e9
movq %rax, %rbx
movq %r14, %rdi
callq 0x2110
jmp 0x4527
jmp 0x4506
jmp 0x451a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2ff0
jmp 0x451d
movq %rax, %rbx
jmp 0x4527
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2638
jmp 0x451d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2ff0
movq 0x8(%rsp), %rdi
addq $0x48, %rdi
callq 0x2ff0
movq 0x10(%rsp), %rdi
callq 0x2ff0
movq %rbx, %rdi
callq 0x22a0
nop