// File: /workspace/github_cpp_makefile_star2_O2/rocket323[P]fishdb/./btree.cpp
// Function: fishdb::BTree::Insert(std::shared_ptr<fishdb::MemPage>, std::shared_ptr<fishdb::MemPage>, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void BTree::Insert(std::shared_ptr<MemPage> now, std::shared_ptr<MemPage> parent,
        int upper_idx, const std::string &key, const std::string &data)
{
    auto iter = LowerBound(now, key);
    size_t p = iter - now->kvs.begin();
    if (iter != now->kvs.end() && Equal(iter->key, key))
        iter->value = data;
    else if (!now->is_leaf)
    {
        assert(now);
        assert(now->children.size() > p);
        Insert(ReadPage(now->children[p]), now, p, key, data);
    }
    else
        now->kvs.insert(iter, KV(key, data));
    if ((int)now->kvs.size() <= 2 * m_min_key_num) return;

    //split full
    size_t mid = now->kvs.size() / 2;
    auto left = m_pager.NewPage();
    auto right = m_pager.NewPage();

    left->is_leaf = right->is_leaf = now->is_leaf;
    left->kvs.assign(now->kvs.begin(), now->kvs.begin() + mid);
    right->kvs.assign(now->kvs.begin() + mid + 1, now->kvs.end());
    if (!now->is_leaf)
    {
        left->children.assign(now->children.begin(), now->children.begin() + mid + 1);
        right->children.assign(now->children.begin() + mid + 1, now->children.end());
    }

    if (!parent)
    {
        m_root = m_pager.NewPage();
        m_root->is_leaf = false;
        parent = m_root;
        m_pager.SetRoot(m_root->header.page_no);
        assert(upper_idx == 0);
        parent->children.resize(upper_idx + 1);
    }
    assert(upper_idx < (int)parent->children.size());
    parent->kvs.insert(parent->kvs.begin() + upper_idx, now->kvs[mid]);
    parent->children[upper_idx] = left->header.page_no;
    parent->children.insert(parent->children.begin() + upper_idx + 1, right->header.page_no);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, -0x40(%rbp)
movq %r8, %r15
movl %ecx, -0x34(%rbp)
movq %rdx, -0x58(%rbp)
movq %rsi, %r14
movq %rdi, %r12
leaq -0x98(%rbp), %rbx
movq %rbx, %rdi
callq 0x2ee6
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3308
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x2688
movq %r14, -0x30(%rbp)
movq (%r14), %r14
movq %rbx, %r13
subq 0x60(%r14), %r13
cmpq 0x68(%r14), %rbx
je 0x3794
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x32c6
testb %al, %al
je 0x378d
addq $0x20, %rbx
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
callq 0x2110
jmp 0x383d
movq -0x30(%rbp), %rax
movq (%rax), %r14
cmpb $0x0, 0x79(%r14)
je 0x37d1
addq $0x60, %r14
leaq -0xd8(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x454e
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x48d4
leaq -0xd8(%rbp), %rdi
callq 0x4584
jmp 0x383d
sarq $0x6, %r13
movq 0x48(%r14), %rax
movq 0x50(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x3a32
movq (%rax,%r13,8), %rdx
leaq -0x88(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3274
leaq -0x78(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x2ee6
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl %r13d, %ecx
movq %r15, %r8
movq -0x40(%rbp), %r9
callq 0x36fe
leaq -0x70(%rbp), %rdi
callq 0x2688
leaq -0x80(%rbp), %rdi
callq 0x2688
movq -0x30(%rbp), %r15
movq (%r15), %rax
movq 0x68(%rax), %r14
subq 0x60(%rax), %r14
sarq $0x6, %r14
movl 0x48(%r12), %eax
addl %eax, %eax
cmpl %r14d, %eax
jge 0x3a01
leaq -0xd8(%rbp), %rdi
pushq $0x2
popq %rbx
movq %r12, %rsi
movl %ebx, %edx
callq 0x5ad2
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x5ad2
shrq %r14
movq (%r15), %rax
movb 0x79(%rax), %cl
movq -0x50(%rbp), %rdx
movb %cl, 0x79(%rdx)
movq -0xd8(%rbp), %rdi
movb %cl, 0x79(%rdi)
addq $0x60, %rdi
movq 0x60(%rax), %rsi
movq %r14, %r13
shlq $0x6, %r13
leaq (%rsi,%r13), %rdx
callq 0x4bb6
movq -0x50(%rbp), %rdi
addq $0x60, %rdi
movq (%r15), %rax
movq 0x60(%rax), %rcx
movq 0x68(%rax), %rdx
leaq (%rcx,%r13), %rsi
addq $0x40, %rsi
callq 0x4bb6
movq (%r15), %rax
cmpb $0x0, 0x79(%rax)
jne 0x3919
movq -0xd8(%rbp), %rdi
addq $0x48, %rdi
movq 0x48(%rax), %rsi
leaq (%rsi,%r14,8), %rdx
addq $0x8, %rdx
callq 0x4e5a
movq -0x50(%rbp), %rdi
addq $0x48, %rdi
movq (%r15), %rax
movq 0x48(%rax), %rcx
movq 0x50(%rax), %rdx
leaq (%rcx,%r14,8), %rsi
addq $0x8, %rsi
callq 0x4e5a
movq -0x58(%rbp), %r14
movq (%r14), %rdi
testq %rdi, %rdi
jne 0x398e
leaq -0x68(%rbp), %rdi
pushq $0x2
popq %rdx
movq %r12, %rsi
callq 0x5ad2
leaq 0x70(%r12), %rbx
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0x30d2
leaq -0x60(%rbp), %rdi
callq 0x2688
movq 0x70(%r12), %rax
movb $0x0, 0x79(%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4816
movq 0x70(%r12), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x5bee
cmpl $0x0, -0x34(%rbp)
jne 0x3a4f
movq (%r14), %rdi
addq $0x48, %rdi
pushq $0x1
popq %rsi
callq 0x45a4
movq (%r14), %rdi
movq 0x50(%rdi), %rax
subq 0x48(%rdi), %rax
shrq $0x3, %rax
movl -0x34(%rbp), %ecx
cmpl %ecx, %eax
jle 0x3a13
movslq %ecx, %rbx
movq %rbx, %rsi
shlq $0x6, %rsi
addq 0x60(%rdi), %rsi
addq $0x60, %rdi
movq (%r15), %rax
addq 0x60(%rax), %r13
movq %r13, %rdx
callq 0x45dc
movq -0xd8(%rbp), %rax
movq (%rax), %rax
movq (%r14), %rdi
movq 0x48(%rdi), %rcx
addq $0x48, %rdi
leaq (%rcx,%rbx,8), %rsi
addq $0x8, %rsi
movq %rax, -0x8(%rsi)
movq -0x50(%rbp), %rdx
callq 0x4678
leaq -0x48(%rbp), %rdi
callq 0x2688
leaq -0xd0(%rbp), %rdi
callq 0x2688
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37c7(%rip), %rdi      # 0x71e1
leaq 0x36d4(%rip), %rsi      # 0x70f5
leaq 0x3713(%rip), %rcx      # 0x713b
movl $0x9a, %edx
callq 0x2140
leaq 0x3780(%rip), %rdi      # 0x71b9
leaq 0x36b5(%rip), %rsi      # 0x70f5
leaq 0x36f4(%rip), %rcx      # 0x713b
pushq $0x7c
popq %rdx
callq 0x2140
leaq 0x377c(%rip), %rdi      # 0x71d2
leaq 0x3698(%rip), %rsi      # 0x70f5
leaq 0x36d7(%rip), %rcx      # 0x713b
movl $0x97, %edx
callq 0x2140
jmp 0x3a9f
jmp 0x3a9f
jmp 0x3a9f
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x2688
leaq -0x80(%rbp), %rdi
jmp 0x3ac3
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x4584
jmp 0x3ac8
jmp 0x3a9f
jmp 0x3a9f
jmp 0x3a9f
jmp 0x3a9f
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x2688
leaq -0xd0(%rbp), %rdi
jmp 0x3ac3
movq %rax, %rbx
jmp 0x3aab
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x2688
movq %rbx, %rdi
callq 0x2310