// File: ChaohuiYu[P]BPlusTree/BPlusTree.cpp
// Function: BPlusTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::splitNode(BPlusTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::BPlusTreeNode*)
void BPT<T>::splitNode(BPT_NODE* node) {
    // create a new node, it will be the sibling in the right
    // move rightmost (_maxKeyCount - _maxKeyCount / 2) elements to new node

    BPT_NODE* newNode = new BPT_NODE(false, false, _maxKeyCount);
    _nodeCount++;

    int midIdx = _maxKeyCount / 2;
    T midKey = node->_keys[midIdx];

    newNode->_keyCount = _maxKeyCount - midIdx;
    FOR(i, 0, newNode->_keyCount) {
        newNode->_keys[i] = node->_keys[i + midIdx];
        newNode->_pointers[i+1] = node->_pointers[i + 1 + midIdx];
    }
    // newNode->_pointers[ newNode->_keyCount ] = node->_pointers[ node->_keyCount ];
    node->_keyCount = midIdx;

    if (node->_isLeaf) {
        newNode->_isLeaf = true;
        _leafCount++;
    }

    // update child's father
    if (!node->_isLeaf) {
        FOR(i, 0, newNode->_keyCount + 1) {
            if (newNode->_pointers[i] != NULL) {
                ((BPT_NODE *)newNode->_pointers[i])->_father = (void *)newNode;
            }
        }
    }

    if (node->_isRoot) {
        // create a new root
        node -> _isRoot = false ;

        _root = new BPT_NODE(false, false, _maxKeyCount);
        _nodeCount++;

        _root->_isRoot = true;
        _root->_keys[0] = midKey;
        _root->_pointers[0] = node;
        _root->_pointers[1] = newNode;
        _root->_keyCount = 1;

        node->_father = _root;
        newNode->_father = _root;
    } else {
        insertInNode( (BPT_NODE *)node->_father, midKey, (void *)newNode ) ;
        newNode->_father = node->_father;
    }
    // cout << "after split: _root, node, newNode\n";
    // _root->printNode();
    // node->printNode();
    // newNode->printNode();
    // cout << "node == _root->_pointers[0] : " << (node == _root->_pointers[0]) << endl;
    // cout << "newNode == _root->_pointers[1] : " << (newNode == _root->_pointers[1]) << endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movl $0x38, %edi
callq 0x3290
movq %rax, %rdi
movq -0x80(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x78(%rbp)
movl 0x8(%rax), %ecx
xorl %edx, %edx
movl %edx, %esi
callq 0x12a50
jmp 0x12bd1
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, -0x18(%rbp)
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
movl 0x8(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x28(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x3120
movq -0x80(%rbp), %rax
movl 0x8(%rax), %ecx
subl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x12cd3
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x4c(%rbp), %rax
movl %eax, %ecx
movl -0x28(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rsi
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rdi
shlq $0x5, %rax
addq %rax, %rdi
callq 0x3050
jmp 0x12c6b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x12c28
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x38, %esi
callq 0x32a0
jmp 0x12eb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x12eaa
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
testb $0x1, 0x8(%rax)
je 0x12cfc
movq -0x80(%rbp), %rax
movq -0x18(%rbp), %rcx
movb $0x1, 0x8(%rcx)
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0x12d58
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x12d56
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x50(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x12d49
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x50(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x20(%rax)
jmp 0x12d4b
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x12d0d
jmp 0x12d58
movq -0x10(%rbp), %rax
testb $0x1, 0x9(%rax)
je 0x12e2e
movq -0x10(%rbp), %rax
movb $0x0, 0x9(%rax)
movl $0x38, %edi
callq 0x3290
movq %rax, -0x88(%rbp)
jmp 0x12d81
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rax
movl 0x8(%rax), %ecx
xorl %edx, %edx
movl %edx, %esi
callq 0x12a50
jmp 0x12d9a
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, (%rax)
movl 0xc(%rax), %ecx
incl %ecx
movl %ecx, 0xc(%rax)
movq (%rax), %rcx
movb $0x1, 0x9(%rcx)
movq (%rax), %rax
movq 0x10(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3050
jmp 0x12dc9
movq -0x80(%rbp), %rax
movq -0x10(%rbp), %rdx
movq (%rax), %rcx
movq 0x18(%rcx), %rcx
movq %rdx, (%rcx)
movq -0x18(%rbp), %rdx
movq (%rax), %rcx
movq 0x18(%rcx), %rcx
movq %rdx, 0x8(%rcx)
movq (%rax), %rcx
movl $0x1, 0x4(%rcx)
movq (%rax), %rdx
movq -0x10(%rbp), %rcx
movq %rdx, 0x20(%rcx)
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x12e98
movq -0x88(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x38, %esi
callq 0x32a0
jmp 0x12eaa
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x90(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3120
jmp 0x12e4c
movq -0x90(%rbp), %rsi
movq -0x80(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq -0x70(%rbp), %rdx
callq 0x11b90
jmp 0x12e66
leaq -0x70(%rbp), %rdi
callq 0x3190
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x12e98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3190
jmp 0x12eaa
leaq -0x48(%rbp), %rdi
callq 0x3190
addq $0x90, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x3190
movq -0x20(%rbp), %rdi
callq 0x3410
nopl (%rax)