// File: 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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0xcf20
movq -0xc8(%rbp), %rdi
leaq 0x104fc(%rip), %rax     # 0x1dc18
movq %rax, (%rdi)
addq $0x30, %rdi
movq %rdi, -0xd8(%rbp)
callq 0x48b0
movq -0xc8(%rbp), %rdi
addq $0x48, %rdi
movq %rdi, -0xd0(%rbp)
callq 0x48b0
movq -0xc8(%rbp), %rdi
addq $0x68, %rdi
callq 0xcf40
movq -0xc8(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0x18(%rax)
movl -0x20(%rbp), %ecx
movl %ecx, 0x24(%rax)
movl -0x20(%rbp), %edi
callq 0xcf60
movsd %xmm0, -0xc0(%rbp)
jmp 0xd78b
movl $0x2, %edi
callq 0xcf60
movsd %xmm0, -0xe0(%rbp)
jmp 0xd79f
movsd -0xe0(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x3040
movq -0xc8(%rbp), %rax
movsd 0x8a61(%rip), %xmm1     # 0x16228
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x1c(%rax)
movl 0x1c(%rax), %esi
movl $0x2, %edi
callq 0x6c00
movsd %xmm0, -0xe8(%rbp)
jmp 0xd7e9
movq -0xc8(%rbp), %rcx
movsd -0xe8(%rbp), %xmm0
movsd 0x8a28(%rip), %xmm1     # 0x16228
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rcx)
movl 0x28(%rcx), %eax
imull 0x18(%rcx), %eax
cmpl 0x24(%rcx), %eax
jge 0xd8c7
movl $0x8, %edi
callq 0x30d0
movq %rax, -0xf8(%rbp)
movl $0x10, %edi
callq 0x3210
movq %rax, -0xf0(%rbp)
jmp 0xd83f
movq -0xf0(%rbp), %rdi
leaq 0x89e3(%rip), %rsi      # 0x16230
callq 0x30a0
jmp 0xd854
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movq %rax, (%rdi)
leaq 0x10284(%rip), %rsi     # 0x1daf0
xorl %eax, %eax
movl %eax, %edx
callq 0x3370
jmp 0xdb1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0xdafb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0xd8b6
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x10, %esi
callq 0x3220
movq -0xf8(%rbp), %rdi
callq 0x3160
jmp 0xdafb
movq -0xc8(%rbp), %rax
movl 0x1c(%rax), %esi
decl %esi
movl $0x2, %edi
callq 0x6c00
movsd %xmm0, -0x100(%rbp)
jmp 0xd8e7
movq -0xc8(%rbp), %rax
movsd -0x100(%rbp), %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x20(%rax)
callq 0x4800
jmp 0xd904
movl -0x1c(%rbp), %edi
callq 0x4730
jmp 0xd90e
movq -0xc8(%rbp), %rax
movq 0x10(%rax), %rdi
movl 0x20(%rax), %esi
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
jmp 0xd927
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x28(%rax), %esi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0xd93f
movl $0x4, %edi
callq 0x3210
movq %rax, -0x108(%rbp)
jmp 0xd952
movq -0x108(%rbp), %rdi
callq 0xcf80
jmp 0xd960
movq -0xc8(%rbp), %rax
movq -0x108(%rbp), %rcx
movq %rcx, 0x60(%rax)
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3100
leaq -0x48(%rbp), %rdi
callq 0x48b0
movq -0xc8(%rbp), %rdi
addq $0x30, %rdi
leaq -0x48(%rbp), %rsi
callq 0x48d0
leaq -0x48(%rbp), %rdi
callq 0x4900
movl $0x0, -0x4c(%rbp)
movq -0xc8(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x28(%rcx), %eax
jge 0xdaf2
leaq -0x68(%rbp), %rdi
callq 0x41c0
jmp 0xd9cd
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xda67
leaq -0xa0(%rbp), %rdi
callq 0x37e0
jmp 0xd9ee
leaq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x45c0
jmp 0xda00
leaq -0xa0(%rbp), %rdi
callq 0x3830
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xd9d4
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x4, %esi
callq 0x3220
jmp 0xdafb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0xdae7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3830
jmp 0xdae7
movq -0x10(%rbp), %rax
movq %rax, -0x118(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x10c(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xcfa0
jmp 0xda8d
movl -0x10c(%rbp), %esi
movq -0x118(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq -0xb8(%rbp), %rdx
callq *%rax
jmp 0xdaac
leaq -0xb8(%rbp), %rdi
callq 0xcfd0
leaq -0x68(%rbp), %rdi
callq 0xcfd0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xd9af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xcfd0
leaq -0x68(%rbp), %rdi
callq 0xcfd0
jmp 0xdafb
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x4900
movq -0xd8(%rbp), %rdi
callq 0x4900
movq -0x28(%rbp), %rdi
callq 0x33a0
nopl (%rax)