// File: 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
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rsi, -0x200(%rbp)
movq %rdx, -0x1f8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1f0(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x40f0
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x54c0
movq %rax, -0x1e0(%rbp)
jmp 0x5c5b
movq -0x1e0(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x27e0
movq -0x200(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq %rax, -0x68(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x42d0
movq -0x200(%rbp), %rdi
movq %rax, -0x60(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4380
movq %rax, -0x70(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4340
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x201(%rbp)
jne 0x5cd6
jmp 0x5cf8
leaq -0x38(%rbp), %rdi
callq 0x7e80
movq -0x1f0(%rbp), %rdi
movq %rax, %rsi
movq -0x28(%rbp), %rdx
callq 0x5460
movb %al, -0x201(%rbp)
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x5d04
jmp 0x5d4a
movq -0x30(%rbp), %rax
movq %rax, -0x210(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7e80
movq -0x210(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rdi
callq 0x2040
jmp 0x5f29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x27e0
jmp 0x65b9
movq -0x200(%rbp), %rdi
callq 0x4190
testb $0x1, 0x79(%rax)
jne 0x5e89
movq -0x200(%rbp), %rdi
callq 0x50f0
testb $0x1, %al
jne 0x5d72
jmp 0x5d74
jmp 0x5d93
leaq 0xc3d0(%rip), %rdi      # 0x1214b
leaq 0xc383(%rip), %rsi      # 0x12105
movl $0x7b, %edx
leaq 0xc3c1(%rip), %rcx      # 0x1214f
callq 0x2150
movq -0x200(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x7ec0
cmpq -0x60(%rbp), %rax
jbe 0x5db3
jmp 0x5dd2
leaq 0xc413(%rip), %rdi      # 0x121cd
leaq 0xc344(%rip), %rsi      # 0x12105
movl $0x7c, %edx
leaq 0xc382(%rip), %rcx      # 0x1214f
callq 0x2150
movq -0x200(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
movq -0x60(%rbp), %rsi
callq 0x41b0
movq -0x1f0(%rbp), %rsi
movq (%rax), %rdx
leaq -0x80(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x5390
movq -0x200(%rbp), %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x40f0
movq -0x1f0(%rbp), %rdi
movq -0x220(%rbp), %rsi
movq -0x218(%rbp), %rdx
movl -0x60(%rbp), %ecx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
callq 0x5bf0
jmp 0x5e49
leaq -0x90(%rbp), %rdi
callq 0x27e0
leaq -0x80(%rbp), %rdi
callq 0x27e0
jmp 0x5f27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x27e0
leaq -0x80(%rbp), %rdi
callq 0x27e0
jmp 0x65b9
movq -0x200(%rbp), %rdi
callq 0x4190
addq $0x60, %rax
movq %rax, -0x238(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x7f20
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xd8(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x7f60
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rdx
movq -0x98(%rbp), %rsi
callq 0x7ee0
movq %rax, -0x228(%rbp)
jmp 0x5eee
movq -0x228(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x7fc0
jmp 0x5f27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x7fc0
jmp 0x65b9
jmp 0x5f29
movq -0x200(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4200
movq -0x1f0(%rbp), %rcx
movl 0x48(%rcx), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0x5f56
jmp 0x65a4
movq -0x200(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4200
movq -0x1f0(%rbp), %rsi
shrq %rax
movq %rax, -0xe8(%rbp)
leaq -0xf8(%rbp), %rdi
movl $0x2, %edx
movl %edx, -0x23c(%rbp)
callq 0xe300
movq -0x1f0(%rbp), %rsi
movl -0x23c(%rbp), %edx
leaq -0x108(%rbp), %rdi
callq 0xe300
jmp 0x5fb1
movq -0x200(%rbp), %rdi
callq 0x4190
movb 0x79(%rax), %al
movb %al, -0x252(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x4190
movb -0x252(%rbp), %cl
andb $0x1, %cl
movb %cl, -0x251(%rbp)
movb %cl, 0x79(%rax)
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x250(%rbp)
callq 0x4190
movb -0x251(%rbp), %cl
movq -0x250(%rbp), %rdi
movb %cl, 0x79(%rax)
callq 0x4190
movq -0x200(%rbp), %rdi
addq $0x60, %rax
movq %rax, -0x248(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq -0x200(%rbp), %rdi
movq %rax, -0x110(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq %rax, -0x120(%rbp)
movq -0xe8(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x8030
movq -0x248(%rbp), %rdi
movq %rax, -0x118(%rbp)
movq -0x110(%rbp), %rsi
movq -0x118(%rbp), %rdx
callq 0x7ff0
jmp 0x608b
leaq -0x108(%rbp), %rdi
callq 0x4190
movq -0x200(%rbp), %rdi
addq $0x60, %rax
movq %rax, -0x260(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq %rax, -0x138(%rbp)
movq -0xe8(%rbp), %rsi
leaq -0x138(%rbp), %rdi
callq 0x8030
movq %rax, -0x130(%rbp)
leaq -0x130(%rbp), %rdi
movl $0x1, %esi
callq 0x8030
movq -0x200(%rbp), %rdi
movq %rax, -0x128(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4380
movq -0x260(%rbp), %rdi
movq %rax, -0x140(%rbp)
movq -0x128(%rbp), %rsi
movq -0x140(%rbp), %rdx
callq 0x7ff0
jmp 0x612e
movq -0x200(%rbp), %rdi
callq 0x4190
testb $0x1, 0x79(%rax)
jne 0x62ba
leaq -0xf8(%rbp), %rdi
callq 0x4190
movq -0x200(%rbp), %rdi
addq $0x48, %rax
movq %rax, -0x268(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x80b0
movq -0x200(%rbp), %rdi
movq %rax, -0x148(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x80b0
movq %rax, -0x160(%rbp)
movq -0xe8(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0x80e0
movq %rax, -0x158(%rbp)
leaq -0x158(%rbp), %rdi
movl $0x1, %esi
callq 0x80e0
movq -0x268(%rbp), %rdi
movq %rax, -0x150(%rbp)
movq -0x148(%rbp), %rsi
movq -0x150(%rbp), %rdx
callq 0x8070
jmp 0x61e7
leaq -0x108(%rbp), %rdi
callq 0x4190
movq -0x200(%rbp), %rdi
addq $0x48, %rax
movq %rax, -0x270(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x80b0
movq %rax, -0x178(%rbp)
movq -0xe8(%rbp), %rsi
leaq -0x178(%rbp), %rdi
callq 0x80e0
movq %rax, -0x170(%rbp)
leaq -0x170(%rbp), %rdi
movl $0x1, %esi
callq 0x80e0
movq -0x200(%rbp), %rdi
movq %rax, -0x168(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x5040
movq -0x270(%rbp), %rdi
movq %rax, -0x180(%rbp)
movq -0x168(%rbp), %rsi
movq -0x180(%rbp), %rdx
callq 0x8070
jmp 0x628a
jmp 0x62ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x65ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x27e0
jmp 0x65ad
movq -0x1f8(%rbp), %rdi
callq 0x50f0
testb $0x1, %al
jne 0x63a4
movq -0x1f0(%rbp), %rsi
leaq -0x190(%rbp), %rdi
movl $0x2, %edx
callq 0xe300
jmp 0x62e8
movq -0x1f0(%rbp), %rdi
addq $0x70, %rdi
movq %rdi, -0x278(%rbp)
leaq -0x190(%rbp), %rsi
movq %rsi, -0x280(%rbp)
callq 0x41d0
movq -0x280(%rbp), %rdi
callq 0x27e0
movq -0x278(%rbp), %rdi
callq 0x4190
movq -0x1f8(%rbp), %rdi
movq -0x278(%rbp), %rsi
movb $0x0, 0x79(%rax)
callq 0x8120
movq -0x278(%rbp), %rdi
callq 0x4190
movq -0x1f0(%rbp), %rdi
movq (%rax), %rsi
callq 0xe540
jmp 0x6359
cmpl $0x0, -0x1c(%rbp)
jne 0x6361
jmp 0x6380
leaq 0xbe7e(%rip), %rdi      # 0x121e6
leaq 0xbd96(%rip), %rsi      # 0x12105
movl $0x97, %edx
leaq 0xbdd4(%rip), %rcx      # 0x1214f
callq 0x2150
movq -0x1f8(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
movl -0x1c(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x8150
jmp 0x63a2
jmp 0x63a4
movq -0x1f8(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, -0x284(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x7ec0
movq %rax, %rcx
movl -0x284(%rbp), %eax
cmpl %ecx, %eax
jge 0x63d4
jmp 0x63f3
leaq 0xbe1a(%rip), %rdi      # 0x121f5
leaq 0xbd23(%rip), %rsi      # 0x12105
movl $0x9a, %edx
leaq 0xbd61(%rip), %rcx      # 0x1214f
callq 0x2150
movq -0x1f8(%rbp), %rdi
callq 0x4190
movq -0x1f8(%rbp), %rdi
addq $0x60, %rax
movq %rax, -0x298(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq %rax, -0x1a8(%rbp)
movslq -0x1c(%rbp), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x8030
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x7f20
movq -0x200(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
movq -0xe8(%rbp), %rsi
callq 0x43f0
movq -0x298(%rbp), %rdi
movq %rax, %rdx
movq -0x198(%rbp), %rsi
callq 0x81e0
movq %rax, -0x290(%rbp)
jmp 0x6491
movq -0x290(%rbp), %rax
movq %rax, -0x1b0(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x4190
movq -0x1f8(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x2b0(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x41b0
movq -0x2b0(%rbp), %rcx
movq -0x1f8(%rbp), %rdi
movq %rcx, (%rax)
callq 0x4190
movq -0x1f8(%rbp), %rdi
addq $0x48, %rax
movq %rax, -0x2a8(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x80b0
movq %rax, -0x1d0(%rbp)
movslq -0x1c(%rbp), %rsi
leaq -0x1d0(%rbp), %rdi
callq 0x80e0
movq %rax, -0x1c8(%rbp)
leaq -0x1c8(%rbp), %rdi
movl $0x1, %esi
callq 0x80e0
movq %rax, -0x1c0(%rbp)
leaq -0x1b8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x8600
leaq -0x108(%rbp), %rdi
callq 0x4190
movq -0x2a8(%rbp), %rdi
movq %rax, %rdx
movq -0x1b8(%rbp), %rsi
callq 0x8400
movq %rax, -0x2a0(%rbp)
jmp 0x657e
movq -0x2a0(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x27e0
leaq -0xf8(%rbp), %rdi
callq 0x27e0
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
leaq -0xf8(%rbp), %rdi
callq 0x27e0
movq -0x50(%rbp), %rdi
callq 0x2370
nopw %cs:(%rax,%rax)