// File: 6851-2021[P]Cache-Oblivious-Data-Structures/static_search_trees/static_search_trees.cpp
// Function: CA_static_search_tree::get(int, int, int, int, int, int, int)
int CA_static_search_tree::get(int tree_l, int num_inner_nodes, int temp_root, int h, int arr_l, int arr_r, int value) {
    if (arr_l == arr_r)
    {
        // printf("get at index %d, real index %d, value %d\n", arr_l, tree_l + temp_root, this->tree[tree_l+temp_root]);
        return arr_l;
    }
    int mid = (arr_l + arr_r) >> 1;
    if (h > 0 && (h+1) & 3 == 0) {
        h++;
        num_inner_nodes += (1 << (h-4));
        // update tree_l
        int mid = (arr_l + arr_r) >> 1;
        int left_or_right = 1;
        int new_tree_l = (tree_l << 4) + ((temp_root-8) << 1) + 1;
        tree_l = new_tree_l;
        temp_root = 1;
        int last_subtree_size = min((1 << (this->height-((h>>2)<<2))) - 1, 15);
        int real_subtree_root = ((num_inner_nodes<<4)-num_inner_nodes) + (tree_l-num_inner_nodes)*last_subtree_size;
        if (value <= this->tree[real_subtree_root + temp_root]) {
            arr_r = mid;
        } else {
            arr_l = mid + 1;
            tree_l++;
        }
        if (h == this->height - 1) {
            return arr_l;
        }
    }
    mid = (arr_l + arr_r) >> 1;
    int left_child = temp_root << 1;
    int right_child = (temp_root << 1) + 1;
    h++;
    int last_subtree_size = min((1 << (this->height-((h>>2)<<2))) - 1, 15);
    int real_subtree_root = ((num_inner_nodes<<4)-num_inner_nodes) + (tree_l-num_inner_nodes)*last_subtree_size;
    if(value <= this->tree[real_subtree_root + left_child]) {
        return this->get(tree_l, num_inner_nodes, left_child, h, arr_l, mid, value);
    } else {
        return this->get(tree_l, num_inner_nodes, right_child, h, mid + 1, arr_r, value);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x24(%rbp), %eax
cmpl 0x10(%rbp), %eax
jne 0x2881
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2a88
movl -0x24(%rbp), %eax
addl 0x10(%rbp), %eax
sarl %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x20(%rbp)
jle 0x299b
movl -0x20(%rbp), %eax
addl $0x1, %eax
andl $0x0, %eax
cmpl $0x0, %eax
je 0x299b
movq -0x68(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
movl -0x20(%rbp), %ecx
subl $0x4, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
addl -0x18(%rbp), %ecx
movl %ecx, -0x18(%rbp)
movl -0x24(%rbp), %ecx
addl 0x10(%rbp), %ecx
sarl %ecx
movl %ecx, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
movl -0x14(%rbp), %ecx
shll $0x4, %ecx
movl -0x1c(%rbp), %edx
subl $0x8, %edx
shll %edx
addl %edx, %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movl -0x34(%rbp), %ecx
movl %ecx, -0x14(%rbp)
movl $0x1, -0x1c(%rbp)
movl 0x14(%rax), %ecx
movl -0x20(%rbp), %eax
sarl $0x2, %eax
shll $0x2, %eax
subl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl $0xf, -0x40(%rbp)
leaq -0x3c(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3f00
movq -0x68(%rbp), %rcx
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movl -0x18(%rbp), %eax
shll $0x4, %eax
subl -0x18(%rbp), %eax
movl -0x14(%rbp), %edx
subl -0x18(%rbp), %edx
imull -0x38(%rbp), %edx
addl %edx, %eax
movl %eax, -0x44(%rbp)
movl 0x18(%rbp), %eax
movq (%rcx), %rcx
movl -0x44(%rbp), %edx
addl -0x1c(%rbp), %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
jg 0x296b
movl -0x2c(%rbp), %eax
movl %eax, 0x10(%rbp)
jmp 0x297d
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x68(%rbp), %rcx
movl -0x20(%rbp), %eax
movl 0x14(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2999
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2a88
jmp 0x299b
movq -0x68(%rbp), %rax
movl -0x24(%rbp), %ecx
addl 0x10(%rbp), %ecx
sarl %ecx
movl %ecx, -0x28(%rbp)
movl -0x1c(%rbp), %ecx
shll %ecx
movl %ecx, -0x48(%rbp)
movl -0x1c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, -0x4c(%rbp)
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
movl 0x14(%rax), %ecx
movl -0x20(%rbp), %eax
sarl $0x2, %eax
shll $0x2, %eax
subl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x54(%rbp)
movl $0xf, -0x58(%rbp)
leaq -0x54(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3f00
movq -0x68(%rbp), %rcx
movl (%rax), %eax
movl %eax, -0x50(%rbp)
movl -0x18(%rbp), %eax
shll $0x4, %eax
subl -0x18(%rbp), %eax
movl -0x14(%rbp), %edx
subl -0x18(%rbp), %edx
imull -0x50(%rbp), %edx
addl %edx, %eax
movl %eax, -0x5c(%rbp)
movl 0x18(%rbp), %eax
movq (%rcx), %rcx
movl -0x5c(%rbp), %edx
addl -0x48(%rbp), %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
jg 0x2a58
movq -0x68(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x48(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl -0x28(%rbp), %r10d
movl 0x18(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x2840
movl %eax, -0x4(%rbp)
jmp 0x2a88
movq -0x68(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x4c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl -0x28(%rbp), %r9d
addl $0x1, %r9d
movl 0x10(%rbp), %r10d
movl 0x18(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x2840
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)