// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movl $0x38, %edi
callq 0x21d0
movq %rax, %rbx
movl 0x8(%rbp), %ecx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x9f32
incl 0xc(%rbp)
movl 0x8(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movq 0x10(%r14), %rax
movl %ecx, 0xc(%rsp)
movslq %ecx, %r15
movq %r15, %r12
shlq $0x5, %r12
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax,%r12), %rsi
movq 0x8(%rax,%r12), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x42a0
movl 0x8(%rbp), %eax
subl %r15d, %eax
movl %eax, 0x4(%rbx)
jle 0xa0a9
movq %rbp, 0x30(%rsp)
leaq 0x8(,%r15,8), %r13
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x10(%r14), %rsi
addq %r12, %rsi
addq %r15, %rsi
movq 0x10(%rbx), %rdi
addq %r15, %rdi
callq 0x2110
movq 0x18(%r14), %rax
addq %r13, %rax
movq (%rax,%rbp,8), %rax
movq 0x18(%rbx), %rcx
movq %rax, 0x8(%rcx,%rbp,8)
leaq 0x1(%rbp), %rcx
movslq 0x4(%rbx), %rax
addq $0x20, %r15
movq %rcx, %rbp
cmpq %rax, %rcx
jl 0xa061
leaq 0x20(%rsp), %r13
movq 0x30(%rsp), %rbp
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%r14)
cmpb $0x0, 0x8(%r14)
je 0xa0c1
movb $0x1, 0x8(%rbx)
incl 0x10(%rbp)
jmp 0xa0e2
testl %eax, %eax
js 0xa0e2
movq 0x18(%rbx), %rcx
incl %eax
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
testq %rsi, %rsi
je 0xa0da
movq %rbx, 0x20(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0xa0cd
cmpb $0x1, 0x9(%r14)
jne 0xa14e
movb $0x0, 0x9(%r14)
movl $0x38, %edi
callq 0x21d0
movq %rax, %r12
movl 0x8(%rbp), %ecx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x9f32
movq %r12, (%rbp)
incl 0xc(%rbp)
movb $0x1, 0x9(%r12)
movq 0x10(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
movq (%rbp), %rax
movq 0x18(%rax), %rax
movq %r14, (%rax)
movq (%rbp), %rax
movq 0x18(%rax), %rax
movq %rbx, 0x8(%rax)
movq (%rbp), %rax
movl $0x1, 0x4(%rax)
movq %rax, 0x20(%r14)
jmp 0xa1a0
movq 0x20(%r14), %r12
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x42a0
leaq 0x38(%rsp), %rdx
movq %rbp, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x955a
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0xa19c
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x20(%r14), %rax
movq %rax, 0x20(%rbx)
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0xa1bb
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21e0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0xa205
movq 0x48(%rsp), %rsi
incq %rsi
jmp 0xa1ec
movq %rax, %r14
movl $0x38, %esi
movq %r12, %rdi
callq 0x21e0
jmp 0xa205
movq %rax, %r14
movl $0x38, %esi
movq %rbx, %rdi
jmp 0xa21c
jmp 0xa202
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xa221
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r14, %rdi
callq 0x22e0
nop