// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %r13
pushq $0x38
popq %rdi
callq 0x21f0
movq %rax, %rbx
movl 0x8(%r13), %ecx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x808c
incl 0xc(%r13)
movl 0x8(%r13), %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %eax, 0xc(%rsp)
movslq %eax, %r15
movq %r15, %r12
shlq $0x5, %r12
movq 0x10(%r14), %rsi
addq %r12, %rsi
leaq 0x18(%rsp), %rdi
callq 0x2250
movq %r13, 0x10(%rsp)
movl 0x8(%r13), %eax
subl %r15d, %eax
movl %eax, 0x4(%rbx)
leaq 0x8(,%r15,8), %r13
xorl %ebp, %ebp
xorl %r15d, %r15d
cltq
cmpq %rax, %r15
jge 0x81d9
movq 0x10(%r14), %rsi
addq %r12, %rsi
addq %rbp, %rsi
movq 0x10(%rbx), %rdi
addq %rbp, %rdi
callq 0x2130
movq 0x18(%r14), %rax
addq %r13, %rax
movq (%rax,%r15,8), %rax
movq 0x18(%rbx), %rcx
movq %rax, 0x8(%rcx,%r15,8)
incq %r15
movl 0x4(%rbx), %eax
addq $0x20, %rbp
jmp 0x819c
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%r14)
cmpb $0x0, 0x8(%r14)
je 0x8261
movb $0x1, 0x8(%rbx)
movq 0x10(%rsp), %r15
incl 0x10(%r15)
cmpb $0x1, 0x9(%r14)
jne 0x8283
movb $0x0, 0x9(%r14)
pushq $0x38
popq %rdi
callq 0x21f0
movq %rax, %r12
movl 0x8(%r15), %ecx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x808c
movq %r12, (%r15)
incl 0xc(%r15)
movb $0x1, 0x9(%r12)
movq 0x10(%r12), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2130
movq (%r15), %rax
movq 0x18(%rax), %rax
movq %r14, (%rax)
movq (%r15), %rax
movq 0x18(%rax), %rax
movq %rbx, 0x8(%rax)
movq (%r15), %rax
movl $0x1, 0x4(%rax)
movq %rax, 0x20(%r14)
jmp 0x82b7
xorl %ecx, %ecx
movq 0x10(%rsp), %r15
cmpq %rax, %rcx
jg 0x81f5
movq 0x18(%rbx), %rdx
movq (%rdx,%rcx,8), %rdx
testq %rdx, %rdx
je 0x827e
movq %rbx, 0x20(%rdx)
incq %rcx
jmp 0x8268
movq 0x20(%r14), %r12
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2250
leaq 0x38(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x792a
leaq 0x38(%rsp), %rdi
callq 0x2100
movq 0x20(%r14), %rax
movq %rax, 0x20(%rbx)
leaq 0x18(%rsp), %rdi
callq 0x2100
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x2100
jmp 0x8308
movq %rax, %r14
pushq $0x38
popq %rsi
movq %r12, %rdi
callq 0x2200
jmp 0x8308
movq %rax, %r14
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0x2200
jmp 0x8312
jmp 0x8305
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x2100
movq %r14, %rdi
callq 0x22e0