// File: /workspace/github_cpp_makefile_star2_O2/obliviousram[P]oblivious-avl-tree/src/OAVLTreePathEviction.cpp
// Function: OAVLTreePathEviction::OAVLTreePathEviction(UntrustedStorageInterface*, RandForOramInterface*, int, int)
OAVLTreePathEviction::OAVLTreePathEviction(UntrustedStorageInterface* storage, RandForOramInterface* rand_gen,
                                           int bucket_size, int num_blocks) {
    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 0x7b59(%rip), %rax      # 0xcc68
movq %rax, (%rdi)
leaq 0x30(%rdi), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rdi)
movupd %xmm0, 0x40(%rdi)
movupd %xmm0, 0x50(%rdi)
orq $-0x1, 0x68(%rdi)
movq %rsi, 0x18(%rsp)
movq %rsi, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
movl %ecx, %r15d
movl %ecx, 0x18(%rdi)
movl %r8d, 0x24(%rdi)
movl %r8d, %edi
callq 0x4f00
divsd 0x30ce(%rip), %xmm0     # 0x8228
callq 0x2030
movsd 0x30c9(%rip), %xmm1     # 0x8230
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0x1c(%rbx)
movapd %xmm1, %xmm0
callq 0x2220
addsd 0x30b5(%rip), %xmm0     # 0x8238
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rbx)
imull 0x18(%rbx), %eax
cmpl 0x24(%rbx), %eax
movq %rbx, 0x8(%rsp)
jl 0x52b2
movl 0x1c(%rbx), %edi
decl %edi
movsd 0x3087(%rip), %xmm0     # 0x8230
callq 0x2220
cvttsd2si %xmm0, %eax
movl %eax, 0x20(%rbx)
callq 0x2cae
movl %r15d, %edi
callq 0x2c28
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x20(%rax), %esi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movl 0x28(%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), %rbx
leaq 0x58(%rsp), %r14
movq 0x8(%rsp), %rax
cmpl 0x28(%rax), %r13d
jge 0x52a0
movq %r12, %rdi
callq 0x28e4
movl %r15d, %ebp
subl $0x1, %ebp
jb 0x526f
movq %r14, %rdi
callq 0x257a
movq %r12, %rdi
movq %r14, %rsi
callq 0x2b68
movq %r14, %rdi
callq 0x2594
jmp 0x524d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2db2
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movl %r13d, %esi
movq %rbx, %rdx
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x2d0c
movq %r12, %rdi
callq 0x2d0c
incl %r13d
jmp 0x5237
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x20d0
movq %rax, %r14
pushq $0x10
popq %rdi
callq 0x2190
movq %rax, %r15
leaq 0x2f81(%rip), %rsi      # 0x8250
movq %rax, %rdi
callq 0x2080
movq %r15, (%r14)
leaq 0x78bf(%rip), %rsi      # 0xcba0
movq %r14, %rdi
xorl %edx, %edx
callq 0x2290
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x21a0
jmp 0x52fe
movq %rax, %rbx
movq %r14, %rdi
callq 0x2110
jmp 0x533c
jmp 0x531b
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2d0c
jmp 0x5332
jmp 0x5320
movq %rax, %rbx
jmp 0x533c
movq %rax, %rbx
jmp 0x5332
movq %rax, %rbx
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 %rbx, %rdi
callq 0x22b0