// File: 6851-2021[P]Cache-Oblivious-Data-Structures/static_search_trees/static_search_trees.cpp
// Function: co_static_search_tree::update(int, int, int, int, int, int)
int co_static_search_tree::update(int block_index, int height, int range_left, int og_height, int index, int value) {
    if (height == 1)
    {
        this->tree[block_index] = value;
        return value;
    }
    if(height == 2) {
        if(index < range_left + (1 << (og_height - 2))) {
            this->tree[block_index + 1] = value;
        } else {
            this->tree[block_index + 2] = value;
        }
        //merge
        this->tree[block_index] = max(this->tree[block_index + 1], this->tree[block_index + 2]);
        return this->tree[block_index];
    }
    if(height == 3) {
        return this->update_3_base_case(block_index, range_left, og_height, index, value);
    }   
    if(height == 4) {
        return this->update_4_base_case(block_index, range_left, og_height, index, value);
    }
    
    //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_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));

    int child_value = this->update(bottom_tree_block_index, bottom_tree_height, bottom_tree_range_left, bottom_tree_og_height, index, value);


    //get adjacent child
    int adjacent_child_value;
    if(bottom_tree_index % 2 == 0) {
        adjacent_child_value = this->tree[bottom_tree_block_index + bottom_tree_size];
    } else {
        adjacent_child_value = this->tree[bottom_tree_block_index - bottom_tree_size];
    }

    //merge two children value
    int merged_value = max(child_value, adjacent_child_value);

    //update upper tree
    int final_value = this->update(block_index, top_tree_height, range_left, og_height, index, merged_value);

    return final_value;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
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, -0x60(%rbp)
cmpl $0x1, -0x18(%rbp)
jne 0x147a
movq -0x60(%rbp), %rax
movl 0x10(%rbp), %edx
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x10(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1691
cmpl $0x2, -0x18(%rbp)
jne 0x1524
movl -0x24(%rbp), %eax
movl -0x1c(%rbp), %ecx
movl %ecx, -0x64(%rbp)
movl -0x20(%rbp), %ecx
subl $0x2, %ecx
movl $0x1, %edx
shll %cl, %edx
movl -0x64(%rbp), %ecx
addl %edx, %ecx
cmpl %ecx, %eax
jge 0x14bb
movq -0x60(%rbp), %rax
movl 0x10(%rbp), %edx
movq (%rax), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x14d1
movq -0x60(%rbp), %rax
movl 0x10(%rbp), %edx
movq (%rax), %rax
movl -0x14(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movl -0x14(%rbp), %eax
addl $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rsi
callq 0x31c0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movl (%rcx), %esi
movq (%rax), %rcx
movslq -0x14(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4(%rbp)
jmp 0x1691
cmpl $0x3, -0x18(%rbp)
jne 0x154c
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
movl 0x10(%rbp), %r9d
callq 0x3200
movl %eax, -0x4(%rbp)
jmp 0x1691
cmpl $0x4, -0x18(%rbp)
jne 0x1574
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
movl 0x10(%rbp), %r9d
callq 0x3500
movl %eax, -0x4(%rbp)
jmp 0x1691
movq -0x60(%rbp), %rdi
movslq -0x18(%rbp), %rcx
leaq 0x5a8d(%rip), %rax      # 0x7010
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movl -0x18(%rbp), %eax
subl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x20(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x24(%rbp), %eax
subl -0x1c(%rbp), %eax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
sarl %cl, %eax
movl %eax, -0x38(%rbp)
movl -0x28(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x14(%rbp), %eax
addl -0x30(%rbp), %eax
movl -0x38(%rbp), %edx
movl -0x28(%rbp), %ecx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
subl -0x38(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
shll %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x44(%rbp), %ecx
movl -0x34(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl 0x10(%rbp), %eax
movl %eax, (%rsp)
callq 0x1430
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1640
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
addl -0x3c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
jmp 0x1656
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
subl -0x3c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x31c0
movq -0x60(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x50(%rbp)
movl -0x14(%rbp), %esi
movl -0x2c(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl -0x50(%rbp), %eax
movl %eax, (%rsp)
callq 0x1430
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)