// File: 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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x158(%rbp)
movq (%rax), %rax
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x1, -0x30(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x122c0
movq -0x150(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0x122e0
jmp 0x12f4f
leaq -0x41(%rbp), %rdi
callq 0x12360
leaq -0x70(%rbp), %rdi
callq 0x53e0
leaq -0x88(%rbp), %rdi
callq 0x53e0
leaq -0xa0(%rbp), %rdi
callq 0xc640
leaq -0xb8(%rbp), %rdi
callq 0xc640
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13490
movq %rax, %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
jae 0x1318d
movslq -0xbc(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x134b0
movq (%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x13179
movq -0x10(%rbp), %rax
testb $0x1, 0x8(%rax)
je 0x13118
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x13116
movq -0x158(%rbp), %rax
movl -0xc0(%rbp), %ecx
movl %ecx, -0x164(%rbp)
movl 0x8(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x164(%rbp), %eax
cmpl %ecx, %eax
jge 0x130ac
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0xc0(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x5400
jmp 0x13051
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0xc0(%rbp), %rcx
movl 0x8(%rax,%rcx,8), %eax
movl %eax, -0xc4(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x10a10
jmp 0x1307f
jmp 0x13100
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x12360
jmp 0x13485
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1344f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0xc0(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
leaq -0x88(%rbp), %rdi
callq 0x5400
jmp 0x130d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0xc0(%rbp), %rcx
movl 0x8(%rax,%rcx,8), %eax
movl %eax, -0xc8(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x10a10
jmp 0x130fe
jmp 0x13100
jmp 0x13102
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x12ff3
jmp 0x13177
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x13175
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0xcc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd8(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x12590
jmp 0x13162
jmp 0x13164
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x13122
jmp 0x13177
jmp 0x13179
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x12f8f
leaq -0x28(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rdi
callq 0x134d0
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rdi
callq 0x13500
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x13530
testb $0x1, %al
jne 0x131d7
jmp 0x13222
leaq -0xe8(%rbp), %rdi
callq 0x13570
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, %rax
je 0x13212
movq -0x170(%rbp), %rdi
movl $0x38, %esi
callq 0x32a0
jmp 0x13214
leaq -0xe8(%rbp), %rdi
callq 0x13590
jmp 0x131be
movl $0x38, %edi
callq 0x3290
movq %rax, -0x178(%rbp)
jmp 0x13235
movq -0x178(%rbp), %rdi
movq -0x158(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x1, %edx
movl %edx, %esi
callq 0x12a50
jmp 0x13254
movq -0x158(%rbp), %rax
movq -0x178(%rbp), %rcx
movq %rcx, (%rax)
movl $0x1, 0xc(%rax)
movl $0x1, 0x10(%rax)
movl $0x0, -0xfc(%rbp)
movslq -0xfc(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5550
movq %rax, %rcx
movq -0x180(%rbp), %rax
cmpq %rcx, %rax
jae 0x13351
movslq -0xfc(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x55b0
movq %rax, %rsi
leaq -0x120(%rbp), %rdi
callq 0x3120
jmp 0x132c8
movslq -0xfc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x11780
movq -0x158(%rbp), %rdi
movl (%rax), %edx
leaq -0x120(%rbp), %rsi
callq 0x11a40
jmp 0x132f2
leaq -0x120(%rbp), %rdi
callq 0x3190
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x1327d
movq -0x178(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x38, %esi
callq 0x32a0
jmp 0x1344f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x3190
jmp 0x1344f
movl $0x0, -0x124(%rbp)
movslq -0x124(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x5550
movq %rax, %rcx
movq -0x188(%rbp), %rax
cmpq %rcx, %rax
jae 0x13410
movslq -0x124(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x55b0
movq %rax, %rsi
leaq -0x148(%rbp), %rdi
callq 0x3120
jmp 0x133ac
movslq -0x124(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x11780
movq -0x158(%rbp), %rdi
movl (%rax), %edx
leaq -0x148(%rbp), %rsi
callq 0x11a40
jmp 0x133d6
leaq -0x148(%rbp), %rdi
callq 0x3190
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x1335b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x3190
jmp 0x1344f
leaq -0xb8(%rbp), %rdi
callq 0xaa50
leaq -0xa0(%rbp), %rdi
callq 0xaa50
leaq -0x88(%rbp), %rdi
callq 0x5470
leaq -0x70(%rbp), %rdi
callq 0x5470
leaq -0x28(%rbp), %rdi
callq 0x125c0
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0xb8(%rbp), %rdi
callq 0xaa50
leaq -0xa0(%rbp), %rdi
callq 0xaa50
leaq -0x88(%rbp), %rdi
callq 0x5470
leaq -0x70(%rbp), %rdi
callq 0x5470
leaq -0x28(%rbp), %rdi
callq 0x125c0
movq -0x50(%rbp), %rdi
callq 0x3410
nop