// File: 6851-2021[P]Cache-Oblivious-Data-Structures/static_search_trees/static_search_trees.cpp
// Function: co_static_search_tree::range_update(int, int, int, int, int, int, int*)
void co_static_search_tree::range_update(int block_index, int height, int range_left,  int og_height, int query_range_left, int query_range_right, int* items) {
    if (height == 1)
    {

        this->tree[block_index] = items[range_left];
        return;
    }

    //update bottom tree
    int bottom_tree_height = bottom_tree_height_memo[height];

    int top_tree_height = height - bottom_tree_height;
    
    int top_tree_size = (1 << top_tree_height) - 1;

    int bottom_tree_og_height = og_height - top_tree_height;
    int bottom_tree_size = (1 << bottom_tree_height) - 1;

    int bottom_tree_index_range_left = (query_range_left - range_left) >> (bottom_tree_og_height - 1);
    int bottom_tree_index_range_right = (query_range_right - range_left) >> (bottom_tree_og_height - 1);

    for (int bottom_tree_index = bottom_tree_index_range_left; bottom_tree_index <= bottom_tree_index_range_right; ++bottom_tree_index) {
        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));
        this->range_update(bottom_tree_block_index, bottom_tree_height, bottom_tree_range_left, bottom_tree_og_height, max(query_range_left, bottom_tree_range_left), min(query_range_right, bottom_tree_range_left + (1 << (bottom_tree_og_height - 1)) - 1), items);
    }

    bottom_tree_index_range_left = bottom_tree_index_range_left & (~1);
    int range_lenght = (1 << (bottom_tree_og_height - 1));
    for (int bottom_tree_index = bottom_tree_index_range_left; bottom_tree_index <= bottom_tree_index_range_right; bottom_tree_index += 2)
    {
        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));
        items[bottom_tree_range_left] = max(this->tree[bottom_tree_block_index], this->tree[bottom_tree_block_index + bottom_tree_size]);
    }

    //update upper tree
    this->range_update(block_index, top_tree_height, range_left, og_height, query_range_left, query_range_right, items);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movl %r9d, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x1, -0x10(%rbp)
jne 0x1973
movq -0x60(%rbp), %rax
movq 0x18(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movl (%rcx,%rdx,4), %edx
movq (%rax), %rax
movslq -0xc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1b68
movslq -0x10(%rbp), %rcx
leaq 0x5692(%rip), %rax      # 0x7010
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movl -0x10(%rbp), %eax
subl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x18(%rbp), %eax
subl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x20(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
subl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
sarl %cl, %eax
movl %eax, -0x34(%rbp)
movl 0x10(%rbp), %eax
subl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
sarl %cl, %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jg 0x1aa0
movl -0xc(%rbp), %eax
addl -0x28(%rbp), %eax
movl -0x3c(%rbp), %edx
movl -0x20(%rbp), %ecx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x14(%rbp), %eax
movl -0x3c(%rbp), %edx
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x68(%rbp)
leaq -0x1c(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x31c0
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movl -0x44(%rbp), %eax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
leaq 0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3f00
movq -0x60(%rbp), %rdi
movl -0x74(%rbp), %esi
movl -0x70(%rbp), %edx
movl -0x6c(%rbp), %ecx
movl -0x68(%rbp), %r8d
movl -0x64(%rbp), %r9d
movl (%rax), %r10d
movq 0x18(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1920
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x19de
movl -0x34(%rbp), %eax
andl $-0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x4c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x38(%rbp), %eax
jg 0x1b3d
movq -0x60(%rbp), %rax
movl -0xc(%rbp), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, -0x7c(%rbp)
movl -0x50(%rbp), %edx
movl -0x20(%rbp), %ecx
shll %cl, %edx
movl -0x7c(%rbp), %ecx
addl %edx, %ecx
subl -0x50(%rbp), %ecx
movl %ecx, -0x54(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, -0x78(%rbp)
movl -0x50(%rbp), %edx
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
shll %cl, %edx
movl -0x78(%rbp), %ecx
addl %edx, %ecx
movl %ecx, -0x58(%rbp)
movq (%rax), %rdi
movslq -0x54(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movl -0x54(%rbp), %eax
addl -0x30(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rsi
callq 0x31c0
movl (%rax), %edx
movq 0x18(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x50(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x50(%rbp)
jmp 0x1abf
movq -0x60(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x24(%rbp), %edx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x1c(%rbp), %r9d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1920
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)