// File: 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
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 0x15324(%rip), %rax     # 0x1db20
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
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0x1c(%rax)
movl -0x20(%rbp), %ecx
movl %ecx, 0x28(%rax)
movl -0x20(%rbp), %edi
callq 0xcf60
movsd %xmm0, -0xc0(%rbp)
jmp 0x8872
movl $0x2, %edi
callq 0xcf60
movsd %xmm0, -0xe0(%rbp)
jmp 0x8886
movsd -0xe0(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x3040
movq -0xc8(%rbp), %rax
movsd 0xd97a(%rip), %xmm1     # 0x16228
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x20(%rax)
movl 0x20(%rax), %esi
movl $0x2, %edi
callq 0x6c00
movsd %xmm0, -0xe8(%rbp)
jmp 0x88d0
movq -0xc8(%rbp), %rcx
movsd -0xe8(%rbp), %xmm0
movsd 0xd941(%rip), %xmm1     # 0x16228
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rcx)
movl 0x2c(%rcx), %eax
imull 0x1c(%rcx), %eax
cmpl 0x28(%rcx), %eax
jge 0x89ae
movl $0x8, %edi
callq 0x30d0
movq %rax, -0xf8(%rbp)
movl $0x10, %edi
callq 0x3210
movq %rax, -0xf0(%rbp)
jmp 0x8926
movq -0xf0(%rbp), %rdi
leaq 0xd8fc(%rip), %rsi      # 0x16230
callq 0x30a0
jmp 0x893b
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movq %rax, (%rdi)
leaq 0x1519d(%rip), %rsi     # 0x1daf0
xorl %eax, %eax
movl %eax, %edx
callq 0x3370
jmp 0x8c03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x8be2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x899d
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 0x8be2
movq -0xc8(%rbp), %rax
movl 0x20(%rax), %esi
decl %esi
movl $0x2, %edi
callq 0x6c00
movsd %xmm0, -0x100(%rbp)
jmp 0x89ce
movq -0xc8(%rbp), %rax
movsd -0x100(%rbp), %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x24(%rax)
callq 0x4800
jmp 0x89eb
movl -0x1c(%rbp), %edi
callq 0x4730
jmp 0x89f5
movq -0xc8(%rbp), %rax
movq 0x10(%rax), %rdi
movl 0x24(%rax), %esi
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
jmp 0x8a0e
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x2c(%rax), %esi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x8a26
movl $0x4, %edi
callq 0x3210
movq %rax, -0x108(%rbp)
jmp 0x8a39
movq -0x108(%rbp), %rdi
callq 0xcf80
jmp 0x8a47
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 0x2c(%rcx), %eax
jge 0x8bd9
leaq -0x68(%rbp), %rdi
callq 0x41c0
jmp 0x8ab4
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x8b4e
leaq -0xa0(%rbp), %rdi
callq 0x37e0
jmp 0x8ad5
leaq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x45c0
jmp 0x8ae7
leaq -0xa0(%rbp), %rdi
callq 0x3830
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x8abb
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x4, %esi
callq 0x3220
jmp 0x8be2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x8bce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3830
jmp 0x8bce
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 0x8b74
movl -0x10c(%rbp), %esi
movq -0x118(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq -0xb8(%rbp), %rdx
callq *%rax
jmp 0x8b93
leaq -0xb8(%rbp), %rdi
callq 0xcfd0
leaq -0x68(%rbp), %rdi
callq 0xcfd0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8a96
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 0x8be2
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
nopw %cs:(%rax,%rax)