// File: /workspace/github_cpp_makefile_star2_O2/ChaohuiYu[P]BPlusTree/BPlusTree.cpp
// Function: BPlusTree<int>::redistributeNode(BPlusTree<int>::BPlusTreeNode*)
void BPT<T>::redistributeNode(BPT_NODE* node) {
    // if the number of elements in a node < _maxKeyCount/2
    // redistribute the keys and values in siblings

    // BPT_NODE* node;
    vector<BPT_NODE*> nodes {_root};
    vector<T> keys;
    vector<T> keys2;
    vector<int> values;
    vector<int> values2;
    int i = 0;

    while (i < nodes.size()) {
        node = nodes[i];
        if (node != NULL) {
            if (node->_isLeaf) {
                FOR(i, 0, node->_keyCount) {
                    if (i < _maxKeyCount / 2) {
                        keys.push_back(node->_keys[i]);
                        values.push_back((int)(long long)node->_pointers[i+1]);
                    }
                    else {
                        keys2.push_back(node->_keys[i]);
                        values2.push_back((int)(long long)node->_pointers[i+1]);
                    }
                }
            }
            else {
                FOR(i, 0, node->_keyCount + 1) {
                    nodes.push_back((BPT_NODE*)node->_pointers[i]);
                }
            }
        }

        i++;
    }

    // for (int i = 0; i < keys.size(); i++) {
    //     cout << "key: " << keys[i] << ", value: " << values[i] << endl;
    // }

    for (auto& node : nodes) {
        delete node;
    }

    _root = new BPT_NODE(true, true, _maxKeyCount);
    _nodeCount = 1;
    _leafCount = 1;

    for (int i = 0; i < keys.size(); i++) {
        insertValue(keys[i], values[i]);
    }
    for (int i = 0; i < keys2.size(); i++) {
        insertValue(keys2[i], values2[i]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
leaq 0x30(%rsp), %r14
movq %rax, (%r14)
leaq 0x58(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x10(%rsp), %r15
movq %r14, %rsi
movq %r15, %rcx
callq 0x6fc4
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0x90(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x70(%rsp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
xorl %ecx, %ecx
pushq $0x2
popq %r13
leaq 0x8(%rsp), %r14
movq 0x58(%rsp), %r15
movq 0x60(%rsp), %r12
movq %r12, %rax
subq %r15, %rax
sarq $0x3, %rax
cmpq %rcx, %rax
jbe 0x75c7
movq %rcx, 0x50(%rsp)
movq (%r15,%rcx,8), %rbp
testq %rbp, %rbp
je 0x75ba
cmpb $0x0, 0x8(%rbp)
je 0x758f
xorl %r12d, %r12d
xorl %r15d, %r15d
movslq 0x4(%rbp), %rax
cmpq %rax, %r15
jge 0x75ba
movl 0x8(%rbx), %eax
cltd
idivl %r13d
cltq
movq 0x10(%rbp), %rsi
addq %r12, %rsi
cmpq %rax, %r15
jge 0x7562
leaq 0x30(%rsp), %rdi
callq 0x61a2
movq 0x18(%rbp), %rax
movl 0x8(%rax,%r12,2), %eax
movl %eax, 0x8(%rsp)
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x88d2
jmp 0x7586
leaq 0x10(%rsp), %rdi
callq 0x61a2
movq 0x18(%rbp), %rax
movl 0x8(%rax,%r12,2), %eax
movl %eax, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x88d2
incq %r15
addq $0x4, %r12
jmp 0x7517
xorl %r15d, %r15d
movslq 0x4(%rbp), %rax
cmpq %rax, %r15
jg 0x75ba
movq 0x18(%rbp), %rax
movq (%rax,%r15,8), %rax
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
callq 0x8792
incq %r15
jmp 0x7592
movq 0x50(%rsp), %rcx
incq %rcx
jmp 0x74dc
pushq $0x38
popq %r14
cmpq %r12, %r15
je 0x75e1
movq (%r15), %rdi
movq %r14, %rsi
callq 0x2200
addq $0x8, %r15
jmp 0x75cb
pushq $0x38
popq %rdi
callq 0x21f0
movq %rax, %r14
movl 0x8(%rbx), %ecx
pushq $0x1
popq %rdx
movq %rax, %rdi
movl %edx, %esi
callq 0x72ae
movq %r14, (%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xc(%rbx)
xorl %r14d, %r14d
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
jbe 0x7643
movl (%rax,%r14,4), %esi
movq 0x90(%rsp), %rax
movl (%rax,%r14,4), %edx
movq %rbx, %rdi
callq 0x6c9e
incq %r14
jmp 0x7610
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
jbe 0x7676
movl (%rax,%r14,4), %esi
movq 0x70(%rsp), %rax
movl (%rax,%r14,4), %edx
movq %rbx, %rdi
callq 0x6c9e
incq %r14
jmp 0x7646
leaq 0x70(%rsp), %rdi
callq 0x5b74
leaq 0x90(%rsp), %rdi
callq 0x5b74
leaq 0x10(%rsp), %rdi
callq 0x5b74
leaq 0x30(%rsp), %rdi
callq 0x5b74
leaq 0x58(%rsp), %rdi
callq 0x8692
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r14, %rdi
callq 0x2200
jmp 0x76dc
jmp 0x76d9
jmp 0x76d9
jmp 0x76d9
jmp 0x76d9
jmp 0x76d9
jmp 0x76d9
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5b74
leaq 0x90(%rsp), %rdi
callq 0x5b74
leaq 0x10(%rsp), %rdi
callq 0x5b74
leaq 0x30(%rsp), %rdi
callq 0x5b74
leaq 0x58(%rsp), %rdi
callq 0x8692
movq %rbx, %rdi
callq 0x22e0
nop