// File: /workspace/github_cpp_makefile_star2_O2/rocket323[P]fishdb/./btree.cpp
// Function: fishdb::BTree::Maintain(std::shared_ptr<fishdb::MemPage>, std::shared_ptr<fishdb::MemPage>, int)
void BTree::Maintain(std::shared_ptr<MemPage> now, std::shared_ptr<MemPage> parent, int child_idx)
{
    size_t left_sep = (child_idx > 0) ? child_idx - 1 : -1;
    size_t right_sep = (child_idx < (int)parent->children.size() - 1) ? child_idx : -1;
    auto left = (child_idx > 0) ? ReadPage(parent->children[child_idx - 1]) : nil;
    auto right = (child_idx < (int)parent->children.size() - 1) ? ReadPage(parent->children[child_idx + 1]) : nil;

    // 1.
    if (left && (int)left->kvs.size() > m_min_key_num)
    {
        now->kvs.insert(now->kvs.begin(), parent->kvs[left_sep]);
        if (!left->is_leaf)
        {
            now->children.insert(now->children.begin(), left->children.back());
            left->children.pop_back();
        }
        parent->kvs[left_sep] = left->kvs.back();
        left->kvs.pop_back();
        return;
    }
    // 2.
    if (right && (int)right->kvs.size() > m_min_key_num)
    {
        now->kvs.push_back(parent->kvs[right_sep]);
        if (!right->is_leaf)
        {
            now->children.push_back(right->children.front());
            right->children.erase(right->children.begin());
        }
        parent->kvs[right_sep] = right->kvs.front();
        right->kvs.erase(right->kvs.begin());
        return;
    }
    // 3a.
    if (left)
    {
        left->kvs.push_back(parent->kvs[left_sep]);
        left->kvs.insert(left->kvs.end(), now->kvs.begin(), now->kvs.end());
        left->children.insert(left->children.end(), now->children.begin(), now->children.end());

        parent->kvs.erase(parent->kvs.begin() + left_sep);
        parent->children.erase(parent->children.begin() + left_sep + 1);
    }
    // 3b.
    else if (right)
    {
        now->kvs.push_back(parent->kvs[right_sep]);
        now->kvs.insert(now->kvs.end(), right->kvs.begin(), right->kvs.end());
        now->children.insert(now->children.end(), right->children.begin(), right->children.end());

        parent->kvs.erase(parent->kvs.begin() + right_sep);
        parent->children.erase(parent->children.begin() + right_sep + 1);
    }
    else
        assert(false);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r15d
movq %rdx, %rbx
movq %rsi, -0x40(%rbp)
movq %rdi, %r12
movq (%rdx), %rcx
movq 0x48(%rcx), %rax
movq 0x50(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
decl %ecx
cmpl %r15d, %ecx
pushq $-0x1
popq %r13
movl %r13d, %r14d
cmovgl %r15d, %r14d
leal -0x1(%r15), %ecx
testl %r15d, %r15d
cmovgl %ecx, %r13d
jle 0x3eb3
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x3274
jmp 0x3ec3
leaq 0x8336(%rip), %rsi      # 0xc1f0
leaq -0x50(%rbp), %rdi
callq 0x2ee6
movq (%rbx), %rcx
movq 0x48(%rcx), %rax
movq 0x50(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
decl %ecx
cmpl %r15d, %ecx
jle 0x3ef2
movslq %r15d, %rcx
movq 0x8(%rax,%rcx,8), %rdx
leaq -0x38(%rbp), %rdi
movq %r12, %rsi
callq 0x3274
jmp 0x3f02
leaq 0x82f7(%rip), %rsi      # 0xc1f0
leaq -0x38(%rbp), %rdi
callq 0x2ee6
movslq %r13d, %r15
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x3fa1
movq 0x68(%rdi), %rcx
subq 0x60(%rdi), %rcx
shrq $0x6, %rcx
movl 0x48(%r12), %eax
cmpl %ecx, %eax
jge 0x3fb5
movq -0x40(%rbp), %r14
movq (%r14), %rdi
movq 0x60(%rdi), %rsi
addq $0x60, %rdi
movq (%rbx), %rax
shlq $0x6, %r15
movq 0x60(%rax), %rdx
addq %r15, %rdx
callq 0x45dc
movq -0x50(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x3f78
movq (%r14), %rdi
movq 0x48(%rdi), %rsi
addq $0x48, %rdi
movq 0x50(%rax), %rdx
addq $-0x8, %rdx
callq 0x4678
movq -0x50(%rbp), %rax
addq $-0x8, 0x50(%rax)
movq 0x68(%rax), %rsi
addq $-0x40, %rsi
movq (%rbx), %rax
addq 0x60(%rax), %r15
movq %r15, %rdi
callq 0x46ea
movq -0x50(%rbp), %rdi
addq $0x60, %rdi
callq 0x471e
jmp 0x415e
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x417f
movl 0x48(%r12), %eax
jmp 0x3fc2
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x4054
movslq %r14d, %r12
movq 0x68(%rcx), %rdx
subq 0x60(%rcx), %rdx
shrq $0x6, %rdx
cmpl %edx, %eax
jge 0x404b
movq -0x40(%rbp), %r14
movq (%r14), %rdi
addq $0x60, %rdi
movq (%rbx), %rax
shlq $0x6, %r12
movq 0x60(%rax), %rsi
addq %r12, %rsi
callq 0x4738
movq -0x38(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x4022
movq (%r14), %rdi
addq $0x48, %rdi
movq 0x48(%rax), %rsi
callq 0x4774
movq -0x38(%rbp), %rdi
movq 0x48(%rdi), %rsi
addq $0x48, %rdi
callq 0x479c
movq -0x38(%rbp), %rax
movq 0x60(%rax), %rsi
movq (%rbx), %rax
addq 0x60(%rax), %r12
movq %r12, %rdi
callq 0x46ea
movq -0x38(%rbp), %rdi
movq 0x60(%rdi), %rsi
addq $0x60, %rdi
callq 0x4714
jmp 0x415e
testq %rdi, %rdi
je 0x40da
addq $0x60, %rdi
movq (%rbx), %rax
movq %r15, %r12
shlq $0x6, %r12
movq 0x60(%rax), %rsi
addq %r12, %rsi
callq 0x4738
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %rdi
movq 0x68(%rdi), %rsi
addq $0x60, %rdi
movq (%r14), %rax
movq 0x60(%rax), %rdx
movq 0x68(%rax), %rcx
callq 0x47d6
movq -0x50(%rbp), %rdi
movq 0x50(%rdi), %rsi
addq $0x48, %rdi
movq (%r14), %rax
movq 0x48(%rax), %rdx
movq 0x50(%rax), %rcx
callq 0x47f6
movq (%rbx), %rdi
addq 0x60(%rdi), %r12
addq $0x60, %rdi
movq %r12, %rsi
callq 0x4714
movq (%rbx), %rdi
movq 0x48(%rdi), %rax
addq $0x48, %rdi
leaq (%rax,%r15,8), %rsi
addq $0x8, %rsi
callq 0x479c
jmp 0x415e
movq -0x40(%rbp), %r14
movq (%r14), %rdi
addq $0x60, %rdi
movq (%rbx), %rax
movq %r12, %r15
shlq $0x6, %r15
movq 0x60(%rax), %rsi
addq %r15, %rsi
callq 0x4738
movq (%r14), %rdi
movq 0x68(%rdi), %rsi
addq $0x60, %rdi
movq -0x38(%rbp), %rax
movq 0x60(%rax), %rdx
movq 0x68(%rax), %rcx
callq 0x47d6
movq (%r14), %rdi
movq 0x50(%rdi), %rsi
addq $0x48, %rdi
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rdx
movq 0x50(%rax), %rcx
callq 0x47f6
movq (%rbx), %rdi
addq 0x60(%rdi), %r15
addq $0x60, %rdi
movq %r15, %rsi
callq 0x4714
movq (%rbx), %rdi
movq 0x48(%rdi), %rax
addq $0x48, %rdi
leaq (%rax,%r12,8), %rsi
addq $0x8, %rsi
callq 0x479c
leaq -0x30(%rbp), %rdi
callq 0x2688
leaq -0x48(%rbp), %rdi
callq 0x2688
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3106(%rip), %rdi      # 0x728c
leaq 0x2f68(%rip), %rsi      # 0x70f5
leaq 0x30fe(%rip), %rcx      # 0x7292
movl $0x102, %edx            # imm = 0x102
callq 0x2140
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
jmp 0x41bb
movq %rax, %rbx
jmp 0x41c7
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x2688
leaq -0x48(%rbp), %rdi
callq 0x2688
movq %rbx, %rdi
callq 0x2310