// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, -0x50(%rbp)
movq %rdi, %r13
leal -0x1(%r15), %eax
movq %rdx, %rbx
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 0x4565
movl %eax, %eax
movq (%rcx,%rax,8), %rdx
leaq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x353c
movq (%rbx), %rcx
movq 0x48(%rcx), %rax
movq 0x50(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
decl %ecx
cmpl %r15d, %ecx
jle 0x4539
movslq %r15d, %rcx
movq 0x8(%rax,%rcx,8), %rdx
leaq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x353c
jmp 0x459c
movq 0x7ca0(%rip), %rax      # 0xc1e0
movq %rax, -0x38(%rbp)
movq 0x7c9d(%rip), %rax      # 0xc1e8
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x459c
movq 0x7a75(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x4598
incl 0x8(%rax)
jmp 0x459c
movq 0x7c74(%rip), %rax      # 0xc1e0
movq %rax, -0x48(%rbp)
movq 0x7c71(%rip), %rax      # 0xc1e8
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x450a
movq 0x7a49(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x482a
incl 0x8(%rax)
jmp 0x450a
lock
incl 0x8(%rax)
movslq %r14d, %r15
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x465c
movq 0x68(%rdi), %rax
subq 0x60(%rdi), %rax
movslq %r12d, %r12
shrq $0x6, %rax
movl 0x48(%r13), %ecx
cmpl %eax, %ecx
jge 0x4684
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 0x4e38
movq -0x48(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x4614
movq (%r14), %rdi
movq 0x48(%rdi), %rsi
addq $0x48, %rdi
movq 0x50(%rax), %rdx
addq $-0x8, %rdx
callq 0x4ee0
movq -0x48(%rbp), %rax
addq $-0x8, 0x50(%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 0x536e
jmp 0x48ab
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x48d6
movq 0x68(%rax), %rcx
subq 0x60(%rax), %rcx
shrq $0x6, %rcx
movq (%rbx), %rax
cmpl %ecx, 0x48(%r13)
jge 0x46fc
movq -0x50(%rbp), %r13
jmp 0x46ac
movq -0x38(%rbp), %rax
testq %rax, %rax
movq -0x50(%rbp), %r13
je 0x478a
movq 0x68(%rax), %rdx
subq 0x60(%rax), %rdx
shrq $0x6, %rdx
movq (%rbx), %rax
cmpl %edx, %ecx
jge 0x478d
movq (%r13), %rdi
addq $0x60, %rdi
shlq $0x6, %r15
movq 0x60(%rax), %rsi
addq %r15, %rsi
callq 0x4f62
movq -0x38(%rbp), %r14
cmpb $0x0, 0x79(%r14)
jne 0x4871
movq (%r13), %rdi
movq 0x48(%r14), %rdx
movq 0x50(%rdi), %rsi
cmpq 0x58(%rdi), %rsi
je 0x4833
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x50(%rdi)
jmp 0x4840
movq -0x50(%rbp), %r14
movq (%r14), %rdi
addq $0x60, %rdi
movq %r15, %r12
shlq $0x6, %r12
movq 0x60(%rax), %rsi
addq %r12, %rsi
callq 0x4f62
movq (%r14), %rdi
movq 0x68(%rdi), %rsi
addq $0x60, %rdi
movq -0x38(%rbp), %rax
movq 0x60(%rax), %rdx
movq 0x68(%rax), %rcx
callq 0x5be2
movq (%r14), %rdi
movq 0x50(%rdi), %rsi
addq $0x48, %rdi
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rdx
movq 0x50(%rax), %rcx
callq 0x5e86
movq (%rbx), %rdi
addq 0x60(%rdi), %r12
addq $0x60, %rdi
movq %r12, %rsi
callq 0x5b4a
movq (%rbx), %rbx
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rdx
leaq (%rax,%r15,8), %rsi
addq $0x10, %rsi
cmpq %rdx, %rsi
je 0x481d
leaq (%rax,%r15,8), %rdi
jmp 0x480d
movq (%rbx), %rax
addq $0x60, %rdi
movq %r12, %r15
shlq $0x6, %r15
movq 0x60(%rax), %rsi
addq %r15, %rsi
callq 0x4f62
movq -0x48(%rbp), %rdi
movq 0x68(%rdi), %rsi
addq $0x60, %rdi
movq (%r13), %rax
movq 0x60(%rax), %rdx
movq 0x68(%rax), %rcx
callq 0x5be2
movq -0x48(%rbp), %rdi
movq 0x50(%rdi), %rsi
addq $0x48, %rdi
movq (%r13), %rax
movq 0x48(%rax), %rdx
movq 0x50(%rax), %rcx
callq 0x5e86
movq (%rbx), %rdi
addq 0x60(%rdi), %r15
addq $0x60, %rdi
movq %r15, %rsi
callq 0x5b4a
movq (%rbx), %rbx
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rdx
leaq (%rax,%r12,8), %rsi
addq $0x10, %rsi
cmpq %rdx, %rsi
je 0x481d
leaq (%rax,%r12,8), %rdi
addq $0x8, %rdi
subq %rsi, %rdx
callq 0x22e0
movq 0x50(%rbx), %rdx
addq $-0x8, %rdx
movq %rdx, 0x50(%rbx)
jmp 0x48ab
lock
incl 0x8(%rax)
jmp 0x450a
addq $0x48, %rdi
callq 0x5a74
movq -0x38(%rbp), %r14
movq 0x48(%r14), %rdi
movq 0x50(%r14), %rdx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0x4863
subq %rsi, %rdx
callq 0x22e0
movq 0x50(%r14), %rdx
movq -0x38(%rbp), %rax
jmp 0x4866
movq %r14, %rax
addq $-0x8, %rdx
movq %rdx, 0x50(%r14)
movq %rax, %r14
movq 0x60(%r14), %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 0x5b4a
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x48b9
callq 0x277e
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x48c7
callq 0x277e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39ec(%rip), %rdi      # 0x82c9
leaq 0x384e(%rip), %rsi      # 0x8132
leaq 0x39e4(%rip), %rcx      # 0x82cf
movl $0x102, %edx            # imm = 0x102
callq 0x2140
jmp 0x490c
jmp 0x490c
jmp 0x490c
jmp 0x490c
jmp 0x490c
jmp 0x490c
jmp 0x490c
jmp 0x490c
jmp 0x490c
movq %rax, %rbx
jmp 0x491d
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x491d
callq 0x277e
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x492b
callq 0x277e
movq %rbx, %rdi
callq 0x2300
nop