// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %rbx
leaq 0x90f9(%rip), %rax      # 0xcbd0
movq %rax, (%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rdi)
movupd %xmm0, 0x40(%rdi)
movupd %xmm0, 0x50(%rdi)
orq $-0x1, 0x68(%rdi)
leaq 0x30(%rdi), %rax
movq %rax, 0x10(%rsp)
andl $0x0, 0x18(%rdi)
movq %rsi, 0x18(%rsp)
movq %rsi, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
movl %ecx, %r15d
movl %ecx, 0x1c(%rdi)
movl %r8d, 0x28(%rdi)
movl %r8d, %edi
callq 0x4f00
divsd 0x4702(%rip), %xmm0     # 0x8228
callq 0x2030
movsd 0x46fd(%rip), %xmm1     # 0x8230
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0x20(%rbx)
movapd %xmm1, %xmm0
callq 0x2220
addsd 0x46e9(%rip), %xmm0     # 0x8238
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rbx)
imull 0x1c(%rbx), %eax
cmpl 0x28(%rbx), %eax
movq %rbx, 0x8(%rsp)
jl 0x3c7e
movl 0x20(%rbx), %edi
decl %edi
movsd 0x46bb(%rip), %xmm0     # 0x8230
callq 0x2220
cvttsd2si %xmm0, %eax
movl %eax, 0x24(%rbx)
callq 0x2cae
movl %r15d, %edi
callq 0x2c28
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)
pushq $0x4
popq %rdi
callq 0x2190
andl $0x0, (%rax)
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %r12
movapd %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x2e7a
movq %r12, %rdi
callq 0x2d0c
xorl %r13d, %r13d
testl %r15d, %r15d
cmovlel %r13d, %r15d
leaq 0x40(%rsp), %r14
leaq 0x58(%rsp), %rbx
movq 0x8(%rsp), %rax
cmpl 0x2c(%rax), %r13d
jge 0x3c6c
movq %r12, %rdi
callq 0x28e4
movl %r15d, %ebp
subl $0x1, %ebp
jb 0x3c3b
movq %rbx, %rdi
callq 0x257a
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2b68
movq %rbx, %rdi
callq 0x2594
jmp 0x3c19
movq %r14, %rdi
movq %r12, %rsi
callq 0x2db2
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movl %r13d, %esi
movq %r14, %rdx
callq *0x10(%rax)
movq %r14, %rdi
callq 0x2d0c
movq %r12, %rdi
callq 0x2d0c
incl %r13d
jmp 0x3c03
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x20d0
movq %rax, %rbx
pushq $0x10
popq %rdi
callq 0x2190
movq %rax, %r15
leaq 0x45b5(%rip), %rsi      # 0x8250
movq %rax, %rdi
callq 0x2080
movq %r15, (%rbx)
leaq 0x8ef3(%rip), %rsi      # 0xcba0
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2290
movq %rax, %r14
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x21a0
jmp 0x3cca
movq %rax, %r14
movq %rbx, %rdi
callq 0x2110
jmp 0x3d08
jmp 0x3ce7
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x2d0c
jmp 0x3cfe
jmp 0x3cec
movq %rax, %r14
jmp 0x3d08
movq %rax, %r14
jmp 0x3cfe
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x2594
leaq 0x20(%rsp), %rdi
callq 0x2d0c
movq 0x8(%rsp), %rdi
addq $0x48, %rdi
callq 0x2d0c
movq 0x10(%rsp), %rdi
callq 0x2d0c
movq %r14, %rdi
callq 0x22b0