// File: /workspace/github_cpp_makefile_star2_O1/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 0x9b8c
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 0xa44c
xorl %ebp, %ebp
leaq 0x8(%rsp), %r12
movq (%r14,%rbp,8), %r14
testq %r14, %r14
je 0xa40e
movl 0x4(%r14), %eax
cmpb $0x0, 0x8(%r14)
je 0xa3d9
testl %eax, %eax
jle 0xa40e
xorl %r13d, %r13d
xorl %r15d, %r15d
movl 0x8(%rbx), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
cmpq %rax, %r15
jge 0xa370
movq 0x10(%r14), %rsi
addq %r13, %rsi
leaq 0x70(%rsp), %rdi
callq 0x420a
movq 0x18(%r14), %rax
movl 0x8(%rax,%r15,8), %eax
movl %eax, 0x8(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xa3a7
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xa3c3
movq 0x10(%r14), %rsi
addq %r13, %rsi
leaq 0x50(%rsp), %rdi
callq 0x420a
movq 0x18(%r14), %rax
movl 0x8(%rax,%r15,8), %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xa3b6
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0xa3c3
leaq 0x30(%rsp), %rdi
movq %r12, %rdx
callq 0xa95a
jmp 0xa3c3
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0xa95a
incq %r15
movslq 0x4(%r14), %rax
addq $0x20, %r13
cmpq %rax, %r15
jl 0xa325
jmp 0xa40e
testl %eax, %eax
js 0xa40e
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 0xaadc
movslq 0x4(%r14), %rax
incq %r15
cmpq %rax, %r15
jl 0xa3e4
incq %rbp
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %r15
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
ja 0xa2fb
jmp 0xa44c
movq (%r14), %rdi
testq %rdi, %rdi
je 0xa448
movl $0x38, %esi
callq 0x21e0
addq $0x8, %r14
cmpq %r15, %r14
jne 0xa436
movl $0x38, %edi
callq 0x21d0
movq %rax, %r14
movl 0x8(%rbx), %ecx
movq %rax, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x9f84
movq %r14, (%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xc(%rbx)
movq 0x70(%rsp), %rax
cmpq %rax, 0x78(%rsp)
je 0xa517
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 0x42ae
movq 0x30(%rsp), %rax
movl (%rax,%r13,4), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9474
movq 0xc8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa4fa
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 0xa4ad
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0xa5aa
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 0x42ae
movq 0x10(%rsp), %rax
movl (%rax,%r13,4), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9474
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa58d
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 0xa540
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa5c1
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xa5d8
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
leaq 0x50(%rsp), %rdi
callq 0x4248
leaq 0x70(%rsp), %rdi
callq 0x4248
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0xa609
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 0xa663
jmp 0xa670
jmp 0xa670
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa673
movq 0xb8(%rsp), %rsi
jmp 0xa660
jmp 0xa670
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r15, %rdi
je 0xa673
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0xa673
jmp 0xa670
jmp 0xa670
jmp 0xa670
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa68a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xa6a1
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
leaq 0x50(%rsp), %rdi
callq 0x4248
leaq 0x70(%rsp), %rdi
callq 0x4248
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0xa6d2
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x22e0