// File: /workspace/github_cpp_makefile_star2_O3/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
movl %ecx, %r15d
movq %rdi, %rbx
leaq 0x7166(%rip), %rax      # 0xcc68
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)
movq %rsi, 0x18(%rsp)
movq %rsi, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
movl %ecx, 0x18(%rdi)
movl %r8d, 0x24(%rdi)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
callq 0x20d0
divsd 0x3712(%rip), %xmm0     # 0x9260
callq 0x2040
movsd 0x370d(%rip), %xmm1     # 0x9268
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0x1c(%rbx)
movapd %xmm1, %xmm0
callq 0x2220
addsd 0x36f9(%rip), %xmm0     # 0x9270
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rbx)
imull 0x18(%rbx), %eax
cmpl 0x24(%rbx), %eax
movq %rbx, 0x8(%rsp)
jl 0x5cc8
movl 0x1c(%rbx), %edi
decl %edi
movsd 0x36cb(%rip), %xmm0     # 0x9268
callq 0x2220
cvttsd2si %xmm0, %eax
movl %eax, 0x20(%rbx)
callq 0x2f8e
movl %r15d, %edi
callq 0x2f02
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)
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, 0x28(%r14)
jle 0x5cb6
leaq 0x40(%rsp), %r13
leaq 0x58(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq %r13, %rdi
callq 0x2b28
movl %r15d, %ebp
testl %r15d, %r15d
jle 0x5c7c
movq %rbx, %rdi
callq 0x261c
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2e12
movq %rbx, %rdi
callq 0x2638
decl %ebp
jne 0x5c5d
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 0x28(%rax), %r12d
jl 0x5c4d
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 0x35a7(%rip), %rsi      # 0x9290
movq %rax, %rdi
callq 0x2090
movq %r15, (%r14)
leaq 0x6ea5(%rip), %rsi      # 0xcba0
movq %r14, %rdi
xorl %edx, %edx
callq 0x2280
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x2190
jmp 0x5d1a
movq %rax, %rbx
movq %r14, %rdi
callq 0x2110
jmp 0x5d58
jmp 0x5d37
jmp 0x5d4b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2ff0
jmp 0x5d4e
movq %rax, %rbx
jmp 0x5d58
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2638
jmp 0x5d4e
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