// File: /workspace/github_cpp_makefile_star2_O3/6851-2021[P]Cache-Oblivious-Data-Structures/static_search_trees/static_search_trees.cpp
// Function: co_static_search_tree::get(int, int, int, int, int)
int co_static_search_tree::get(int block_index, int height, int range_left, int og_height, int value) {
    if (height == 1)
    {
        return range_left;
    }
    if(height == 2) {
        if(value <= this->tree[block_index + 1])
            return range_left;
        return range_left + (1 << (og_height - 2));
    }
    if(height == 3){
        return this->get_3_base_case(block_index, range_left, og_height, value);
    }
    if(height == 4) {
        return this->get_4_base_case(block_index, range_left, og_height, value);
    }

    
    int bottom_tree_height = bottom_tree_height_memo[height];
    int top_tree_height = height - bottom_tree_height;

    int index = this->get(block_index, top_tree_height, range_left, og_height, value);
    
    int top_tree_size = (1 << top_tree_height) - 1;

    int bottom_tree_og_height = og_height - top_tree_height;
    int bottom_tree_index = (index - range_left) >> (bottom_tree_og_height - 1);
    int bottom_tree_size = (1 << bottom_tree_height) - 1;

    int bottom_tree_block_index = block_index + top_tree_size + (bottom_tree_index << bottom_tree_height) - bottom_tree_index;
    int bottom_tree_range_left = range_left + (bottom_tree_index << (bottom_tree_og_height - 1));

    if (value <= tree[bottom_tree_block_index]){
        return this->get(bottom_tree_block_index, bottom_tree_height, bottom_tree_range_left, bottom_tree_og_height, value);
    }
    else{
        bottom_tree_index += 1;
        bottom_tree_block_index += bottom_tree_size;
        bottom_tree_range_left += (1 << (bottom_tree_og_height - 1));
        return this->get(bottom_tree_block_index, bottom_tree_height, bottom_tree_range_left, bottom_tree_og_height, value);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %ebx
movl %r8d, %r15d
movl %ecx, %r14d
movl %edx, %r12d
movq %rdi, 0x8(%rsp)
movq (%rdi), %rbp
leal -0x1(%r12), %eax
cmpl $0x3, %eax
jbe 0x1693
movslq %r12d, %rax
leaq 0x2a2e(%rip), %rcx      # 0x4040
movl (%rcx,%rax,4), %r13d
subl %r13d, %r12d
movq 0x8(%rsp), %rdi
movl %r12d, %edx
movl %r14d, %ecx
movl %r15d, %r8d
movl %ebx, %r9d
movq %rsi, 0x10(%rsp)
callq 0x15d8
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %r12d, %ecx
shll %cl, %esi
subl %r12d, %r15d
subl %r14d, %eax
leal -0x1(%r15), %edx
movl %edx, %ecx
sarl %cl, %eax
notl %esi
addl 0x10(%rsp), %esi
movl %eax, %edi
movl %r13d, %ecx
shll %cl, %edi
subl %eax, %esi
addl %edi, %esi
movslq %esi, %rsi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
shll %cl, %r11d
xorl %edi, %edi
cmpl %ebx, (%rbp,%rsi,4)
setl %dil
notl %r11d
movl $0x0, %ecx
cmovgel %ecx, %r11d
addl %eax, %edi
movl %edx, %ecx
shll %cl, %edi
addl %r11d, %esi
addl %edi, %r14d
movl %r13d, %r12d
jmp 0x15fa
leaq 0x2996(%rip), %rcx      # 0x4030
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movslq %esi, %rax
cmpl %ebx, 0x4(%rbp,%rax,4)
jge 0x178a
addb $-0x2, %r15b
movl $0x1, %eax
movl %r15d, %ecx
shll %cl, %eax
addl %eax, %r14d
jmp 0x178a
addl $-0x3, %r15d
movl $0x1, %eax
movl %r15d, %ecx
shll %cl, %eax
movslq %esi, %rcx
cmpl %ebx, 0x4(%rbp,%rcx,4)
jge 0x172d
cmpl %ebx, 0x14(%rbp,%rcx,4)
jge 0x1757
movl $0x2, %edx
movl %r15d, %ecx
shll %cl, %edx
addl %r14d, %edx
addl %eax, %edx
movl %edx, %r14d
jmp 0x178a
addl $-0x4, %r15d
movl $0x1, %eax
movl %r15d, %ecx
shll %cl, %eax
movslq %esi, %rdx
cmpl %ebx, 0x4(%rbp,%rdx,4)
jge 0x173b
cmpl %ebx, 0x24(%rbp,%rdx,4)
jge 0x1761
movl $0x6, %esi
movl %r15d, %ecx
shll %cl, %esi
addl %r14d, %esi
xorl %r14d, %r14d
cmpl %ebx, 0x34(%rbp,%rdx,4)
jmp 0x1775
xorl %edx, %edx
cmpl %ebx, 0x8(%rbp,%rcx,4)
cmovll %eax, %edx
addl %edx, %r14d
jmp 0x178a
cmpl %ebx, 0xc(%rbp,%rdx,4)
jge 0x177e
movl $0x2, %esi
movl %r15d, %ecx
shll %cl, %esi
addl %r14d, %esi
xorl %r14d, %r14d
cmpl %ebx, 0x1c(%rbp,%rdx,4)
jmp 0x1775
addl %r14d, %eax
addl %eax, %eax
movl %eax, %r14d
jmp 0x178a
movl $0x4, %esi
movl %r15d, %ecx
shll %cl, %esi
addl %r14d, %esi
xorl %r14d, %r14d
cmpl %ebx, 0x28(%rbp,%rdx,4)
cmovll %eax, %r14d
addl %esi, %r14d
jmp 0x178a
xorl %ecx, %ecx
cmpl %ebx, 0x10(%rbp,%rdx,4)
cmovll %eax, %ecx
addl %ecx, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq