// File: /workspace/github_cpp_makefile_star2_O1/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, -0x50(%rbp)
movq %rdi, %r13
leal -0x1(%r15), %eax
movq (%rdx), %rdx
movq 0x48(%rdx), %rcx
movq 0x50(%rdx), %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
decl %edx
cmpl %r15d, %edx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmovgl %r15d, %r14d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %r15d, %r15d
cmovgl %eax, %r12d
jle 0x4551
cltq
movq (%rcx,%rax,8), %rdx
leaq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x351a
movq (%rbx), %rcx
movq 0x48(%rcx), %rax
movq 0x50(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
decl %ecx
cmpl %r15d, %ecx
jle 0x4525
movslq %r15d, %rcx
movq 0x8(%rax,%rcx,8), %rdx
leaq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x351a
jmp 0x4588
movq 0x7cb4(%rip), %rax      # 0xc1e0
movq %rax, -0x38(%rbp)
movq 0x7cb1(%rip), %rax      # 0xc1e8
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x4588
movq 0x7a89(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x4584
incl 0x8(%rax)
jmp 0x4588
movq 0x7c88(%rip), %rax      # 0xc1e0
movq %rax, -0x48(%rbp)
movq 0x7c85(%rip), %rax      # 0xc1e8
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x44f6
movq 0x7a5d(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x47f0
incl 0x8(%rax)
jmp 0x44f6
lock
incl 0x8(%rax)
movslq %r12d, %r12
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x4647
movq 0x68(%rdi), %rax
subq 0x60(%rdi), %rax
shrq $0x6, %rax
cmpl %eax, 0x48(%r13)
jge 0x4647
movq -0x50(%rbp), %r14
movq (%r14), %rdi
movq 0x60(%rdi), %rsi
addq $0x60, %rdi
movq (%rbx), %rax
shlq $0x6, %r12
movq 0x60(%rax), %rdx
addq %r12, %rdx
callq 0x4dee
movq -0x48(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x45fb
movq (%r14), %rdi
movq 0x48(%rdi), %rsi
addq $0x48, %rdi
movq 0x50(%rax), %rdx
addq $-0x8, %rdx
callq 0x4e96
movq -0x48(%rbp), %rax
addq $-0x8, 0x50(%rax)
movq -0x48(%rbp), %rax
movq 0x68(%rax), %r14
leaq -0x40(%r14), %rsi
movq (%rbx), %rax
addq 0x60(%rax), %r12
movq %r12, %rdi
callq 0x2110
addq $0x20, %r12
addq $-0x20, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x2110
movq -0x48(%rbp), %rax
leaq 0x60(%rax), %rdi
movq 0x68(%rax), %rsi
addq $-0x40, %rsi
movq %rsi, 0x68(%rax)
callq 0x5320
jmp 0x4862
movslq %r14d, %r15
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x46b9
movq 0x68(%rax), %rcx
subq 0x60(%rax), %rcx
shrq $0x6, %rcx
cmpl %ecx, 0x48(%r13)
jge 0x46b9
movq -0x50(%rbp), %r14
movq (%r14), %rdi
addq $0x60, %rdi
movq (%rbx), %rax
shlq $0x6, %r15
movq 0x60(%rax), %rsi
addq %r15, %rsi
callq 0x4f18
movq -0x38(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x4824
movq (%r14), %rdi
movq 0x48(%rax), %rdx
movq 0x50(%rdi), %rsi
cmpq 0x58(%rdi), %rsi
je 0x47f9
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x50(%rdi)
jmp 0x4802
testq %rdi, %rdi
movq -0x50(%rbp), %r14
je 0x4750
addq $0x60, %rdi
movq (%rbx), %rax
movq %r12, %r15
shlq $0x6, %r15
movq 0x60(%rax), %rsi
addq %r15, %rsi
callq 0x4f18
movq -0x48(%rbp), %rdi
movq 0x68(%rdi), %rsi
addq $0x60, %rdi
movq (%r14), %rax
movq 0x60(%rax), %rdx
movq 0x68(%rax), %rcx
callq 0x5b80
movq -0x48(%rbp), %rdi
movq 0x50(%rdi), %rsi
addq $0x48, %rdi
movq (%r14), %rax
movq 0x48(%rax), %rdx
movq 0x50(%rax), %rcx
callq 0x5e24
movq (%rbx), %rdi
addq 0x60(%rdi), %r15
addq $0x60, %rdi
movq %r15, %rsi
callq 0x5ae8
movq (%rbx), %rbx
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rdx
leaq (%rax,%r12,8), %rsi
addq $0x10, %rsi
cmpq %rdx, %rsi
je 0x47e9
leaq (%rax,%r12,8), %rdi
jmp 0x47dd
testq %rax, %rax
je 0x488d
movq (%r14), %rdi
addq $0x60, %rdi
movq (%rbx), %rax
movq %r15, %r12
shlq $0x6, %r12
movq 0x60(%rax), %rsi
addq %r12, %rsi
callq 0x4f18
movq (%r14), %rdi
movq 0x68(%rdi), %rsi
addq $0x60, %rdi
movq -0x38(%rbp), %rax
movq 0x60(%rax), %rdx
movq 0x68(%rax), %rcx
callq 0x5b80
movq (%r14), %rdi
movq 0x50(%rdi), %rsi
addq $0x48, %rdi
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rdx
movq 0x50(%rax), %rcx
callq 0x5e24
movq (%rbx), %rdi
addq 0x60(%rdi), %r12
addq $0x60, %rdi
movq %r12, %rsi
callq 0x5ae8
movq (%rbx), %rbx
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rdx
leaq (%rax,%r15,8), %rsi
addq $0x10, %rsi
cmpq %rdx, %rsi
je 0x47e9
leaq (%rax,%r15,8), %rdi
addq $0x8, %rdi
subq %rsi, %rdx
callq 0x22e0
addq $-0x8, 0x50(%rbx)
jmp 0x4862
lock
incl 0x8(%rax)
jmp 0x44f6
addq $0x48, %rdi
callq 0x5a12
movq -0x38(%rbp), %r14
movq 0x48(%r14), %rdi
movq 0x50(%r14), %rdx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0x481f
subq %rsi, %rdx
callq 0x22e0
addq $-0x8, 0x50(%r14)
movq -0x38(%rbp), %rax
movq 0x60(%rax), %r14
movq (%rbx), %rax
addq 0x60(%rax), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2110
addq $0x20, %r15
addq $0x20, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x2110
movq -0x38(%rbp), %rdi
movq 0x60(%rdi), %rsi
addq $0x60, %rdi
callq 0x5ae8
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4870
callq 0x277e
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x487e
callq 0x277e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3a39(%rip), %rdi      # 0x82cd
leaq 0x3897(%rip), %rsi      # 0x8132
leaq 0x3a31(%rip), %rcx      # 0x82d3
movl $0x102, %edx            # imm = 0x102
callq 0x2140
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
jmp 0x48c3
movq %rax, %rbx
jmp 0x48d4
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x48d4
callq 0x277e
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x48e2
callq 0x277e
movq %rbx, %rdi
callq 0x2300