// File: ChaohuiYu[P]BPlusTree/BPlusTree.cpp
// Function: BPlusTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::insertInNode(BPlusTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::BPlusTreeNode*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, void*)
void BPT<T>::insertInNode(BPT_NODE* node, T key, void* value) {
    // cout << "node: " << node << ", key: " << key << ", value: " << value << endl;
    // find position to insert
    int insertIdx = 0;
    while (insertIdx < node->_keyCount && node->_keys[insertIdx] < key) insertIdx++;

    // move the key and values after the position right
    for (int i = node->_keyCount; i > insertIdx; i--)
        node->_keys[i] = node->_keys[i-1];
    for (int i = node->_keyCount + 1; i > insertIdx + 1; i--)
        node->_pointers[i] = node->_pointers[i-1];

    // insert key and value
    node->_keys[insertIdx] = key;
    node->_pointers[insertIdx + 1] = value;
    node->_keyCount++;

    // check if this node need to split
    if (node->_keyCount == node->_maxKeyCount) {
        splitNode(node);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdx, -0x40(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpl 0x4(%rdx), %ecx
movb %al, -0x41(%rbp)
jge 0x11beb
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x8720
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x11bf4
jmp 0x11bff
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x11bbb
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x11c4b
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x28(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x28(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x3050
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x11c09
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x11c95
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x11c58
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x3050
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x11cf3
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x12b90
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)