// 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>>>::redistributeNode(BPlusTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::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 $0xe8, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
leaq 0x70(%rsp), %r14
movq %rax, (%r14)
leaq 0x90(%rsp), %r15
leaq 0x50(%rsp), %r12
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x9b3a
xorl %eax, %eax
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movq %rax, 0x10(%r12)
movaps %xmm0, (%r12)
movq %rax, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq (%r15), %r14
movq 0x8(%r15), %r15
cmpq %r14, %r15
je 0xa3f0
xorl %ebp, %ebp
leaq 0x8(%rsp), %r12
movq (%r14,%rbp,8), %r14
testq %r14, %r14
je 0xa3b7
movl 0x4(%r14), %eax
cmpb $0x0, 0x8(%r14)
je 0xa382
testl %eax, %eax
jle 0xa3b7
xorl %r13d, %r13d
xorl %r15d, %r15d
movl 0x8(%rbx), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
movq 0x10(%r14), %rsi
addq %r13, %rsi
cmpq %rax, %r15
jge 0xa320
leaq 0x70(%rsp), %rdi
callq 0x41f8
movq 0x18(%r14), %rax
movl 0x8(%rax,%r15,8), %eax
movl %eax, 0x8(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xa350
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xa36c
leaq 0x50(%rsp), %rdi
callq 0x41f8
movq 0x18(%r14), %rax
movl 0x8(%rax,%r15,8), %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xa35f
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0xa36c
leaq 0x30(%rsp), %rdi
movq %r12, %rdx
callq 0xa8ee
jmp 0xa36c
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0xa8ee
incq %r15
movslq 0x4(%r14), %rax
addq $0x20, %r13
cmpq %rax, %r15
jl 0xa2d5
jmp 0xa3b7
testl %eax, %eax
js 0xa3b7
movq $-0x1, %r15
movq 0x18(%r14), %rax
movq 0x8(%rax,%r15,8), %rax
movq %rax, 0x8(%rsp)
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0xaa64
movslq 0x4(%r14), %rax
incq %r15
cmpq %rax, %r15
jl 0xa38d
incq %rbp
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %r15
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
ja 0xa2ab
jmp 0xa3f0
movq (%r14), %rdi
movl $0x38, %esi
callq 0x21e0
addq $0x8, %r14
cmpq %r15, %r14
jne 0xa3df
movl $0x38, %edi
callq 0x21d0
movq %rax, %r14
movl 0x8(%rbx), %ecx
movq %rax, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x9f32
movq %r14, (%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xc(%rbx)
movq 0x70(%rsp), %rax
cmpq %rax, 0x78(%rsp)
je 0xa4bb
movl $0x8, %r12d
leaq 0xd8(%rsp), %r15
xorl %r13d, %r13d
leaq 0xc8(%rsp), %r14
movq %r15, 0xc8(%rsp)
movq -0x8(%rax,%r12), %rsi
movq (%rax,%r12), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x42a0
movq 0x30(%rsp), %rax
movl (%rax,%r13,4), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x93f2
movq 0xc8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa49e
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21e0
incq %r13
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
addq $0x20, %r12
cmpq %r13, %rcx
ja 0xa451
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0xa54e
movl $0x8, %r12d
leaq 0xb8(%rsp), %r15
xorl %r13d, %r13d
leaq 0xa8(%rsp), %r14
movq %r15, 0xa8(%rsp)
movq -0x8(%rax,%r12), %rsi
movq (%rax,%r12), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x42a0
movq 0x10(%rsp), %rax
movl (%rax,%r13,4), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x93f2
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa531
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21e0
incq %r13
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
addq $0x20, %r12
cmpq %r13, %rcx
ja 0xa4e4
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa565
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xa57c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
leaq 0x50(%rsp), %rdi
callq 0x4236
leaq 0x70(%rsp), %rdi
callq 0x4236
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0xa5ad
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x38, %esi
movq %r14, %rdi
jmp 0xa607
jmp 0xa614
jmp 0xa614
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa617
movq 0xb8(%rsp), %rsi
jmp 0xa604
jmp 0xa614
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa617
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0xa617
jmp 0xa614
jmp 0xa614
jmp 0xa614
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa62e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xa645
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
leaq 0x50(%rsp), %rdi
callq 0x4236
leaq 0x70(%rsp), %rdi
callq 0x4236
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0xa676
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x22e0