// File: /workspace/github_cpp_makefile_star2_O2/JainamS1996[P]Btree_DataStructure/final.cpp
// Function: splitNode(Node*)
void splitNode(Node *curNode) {
    if (curNode->isleaf) {
        Node *rNode = new Node();
        //curNode->next = rNode;
        rNode->prev = curNode;
        if (curNode->next == NULL) {
            curNode->next = rNode;
        }
        else {
            rNode->next = curNode->next;
            rNode->next->prev = rNode;
            curNode->next = rNode;
        }
        int x = order/2;
        //cout<< x << endl;
        //cout << curNode->record.size() << endl;
        for (int i = curNode->record.size()/2; i<curNode->record.size(); i++)
            rNode->record.push_back(curNode->record.at(i));
        rNode->nNodes = rNode->record.size();
        //cout << rNode->record.size() << endl;
        int z = curNode->record.size() - curNode->record.size()/2;
        curNode->record.erase(curNode->record.begin() + curNode->record.size()/2, curNode->record.end());
        curNode->nNodes = curNode->record.size();
        //cout << curNode->record.size() <<endl;    
        
        // sort(rNode->record.begin(),rNode->record.end());
        // cout << get<0>(rNode->record.at(0));
        if (curNode->parentNode == NULL) {
            Node *pNode = new Node(false);
            pNode->children.push_back(curNode);
            pNode->children.push_back(rNode);
            curNode->parentNode = pNode;
            rNode->parentNode = pNode;
            pNode->keys.push_back(get<0>(rNode->record.at(0)));
            curNode->parentNode->nNodes++;
            //cout<< pNode->keys.at(0)<<endl;
            rootNode = pNode;
            //cout<< rootNode->keys.at(0)<<endl;
            
        }
        else {
            //cout << "hello this is else";
            rNode->parentNode = curNode->parentNode;
           
            curNode->parentNode->keys.push_back(get<0>(rNode->record.at(0)));
            
            sort(curNode->parentNode->keys.begin(),curNode->parentNode->keys.end());
            // for (int i = 0; i< curNode->parentNode->keys.size(); i++)
            //         cout<<curNode->parentNode->keys.at(i)<<endl;
            
            // cout<< "Rnode pkeys :";
            // for (int x = 0; x < rNode->parentNode->keys.size(); x++)
            //         cout<<rNode->parentNode->keys.at(x)<<endl;
                
            curNode->parentNode->children.push_back(rNode);
            //cout<< "ye hua?"<<endl;
            // for (int i = 0; i<curNode->parentNode->children.size();i++)
            //  cout<<get<0>(curNode->parentNode->children.at(i)->record.at(0))<<endl;

            sort(curNode->parentNode->children.begin(),curNode->parentNode->children.end(),childSort);
            curNode->parentNode->nNodes++;
        

                

            if (curNode->parentNode->keys.size() > order-1) {
                
                //cout<<"aa bhi raha hai idhar?     "<<curNode->parentNode->keys.size()<<endl;
                curNode->parentNode->nNodes--;
                // for (int i = 0; i< curNode->parentNode->keys.size(); i++)
                //     cout<<curNode->parentNode->keys.at(i)<<endl;
                splitNode(curNode->parentNode);
             }
            // else {
            //     cout<< "Poora nahi hua" << curNode->parentNode->keys.size() <<endl;
            // }
            
        }
    
    }
    else {                  //INTERNAL NODES 
        Node *rNode = new Node(false);
        //cout << "hello this is the internalnode style"<<endl;
        int x = order/2;
        //cout<< x << endl;
        //cout << curNode->keys.size() << endl;
        for (int i = curNode->keys.size()/2; i<curNode->keys.size(); i++)
            rNode->keys.push_back(curNode->keys.at(i));
        
        //int z = curNode->keys.size() - curNode->keys.size()/2;
        curNode->keys.erase(curNode->keys.begin() + curNode->keys.size()/2, curNode->keys.end());
        // cout << curNode->keys.size() << endl;
        // cout << rNode->keys.size() << endl;
        rNode->nNodes = rNode->record.size();
        curNode->nNodes = curNode->record.size();

        // DISTRIBUTING THE CHILDREN TOO
        if (curNode->children.size()%2) {
            for (int i = curNode->children.size()/2+1; i<curNode->children.size();i++) {
                rNode->children.push_back(curNode->children.at(i));
                curNode->children.at(i)->parentNode = rNode;
            }
                curNode->children.erase(curNode->children.begin() + curNode->children.size()/2 + 1, curNode->children.end());
            // cout<< "BACCHO KA SIZE KE BAAER ME BHI TOH SOCHO" <<endl;
            // cout << curNode->children.size() << endl;
            // cout << rNode->children.size() << endl;
        }
        else {
            for (int i = curNode->children.size()/2; i<curNode->children.size();i++) {
                rNode->children.push_back(curNode->children.at(i));
                curNode->children.at(i)->parentNode = rNode;
            }
            curNode->children.erase(curNode->children.begin() + curNode->children.size()/2, curNode->children.end());
            // cout<< "BACCHO KA PAPA KE BAAER ME BHI TOH SOCHO" <<endl;
            // cout << curNode->children.size() << endl;
            // cout << rNode->children.size() << endl;
        }
        //CHILDREN HAVE BEEN DISTRIBUTED

        if (curNode->parentNode == NULL) {
            Node *pNode = new Node(false);
            pNode->children.push_back(curNode);
            pNode->children.push_back(rNode);
            curNode->parentNode = pNode;
            rNode->parentNode = pNode;
            pNode->keys.push_back(rNode->keys.at(0));
            rNode->keys.erase(rNode->keys.begin());
            rNode->nNodes--;
            curNode->parentNode->nNodes++;
            //cout<< pNode->keys.at(0)<<endl;
            rootNode = pNode;
        }
        else {
            rNode->parentNode = curNode->parentNode;
            curNode->parentNode->keys.push_back(rNode->keys.at(0));
            rNode->keys.erase(rNode->keys.begin());
            sort(curNode->parentNode->keys.begin(),curNode->parentNode->keys.end());
            curNode->parentNode->children.push_back(rNode);
            curNode->parentNode->nNodes++;

            sort(curNode->parentNode->children.begin(),curNode->parentNode->children.end(),keySort);

           
            if (curNode->parentNode->keys.size()>order-1) {
                curNode->parentNode->nNodes--;
                splitNode(curNode->parentNode);
            }
        }
        
    
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, -0x30(%rbp)
cmpb $0x1, 0x88(%rdi)
jne 0x2574
movl $0x90, %edi
callq 0x2190
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x3724
movq %r14, -0x38(%rbp)
movq -0x30(%rbp), %r13
movq %r13, 0x38(%r14)
movq 0x30(%r13), %rax
testq %rax, %rax
je 0x2511
movq %rax, 0x30(%r14)
movq %r14, 0x38(%rax)
movq %r14, 0x30(%r13)
leaq 0x40(%r13), %r15
movq 0x40(%r13), %rsi
movq 0x48(%r13), %rcx
movq %rcx, %rax
subq %rsi, %rax
pushq $0x28
popq %rbx
cqto
idivq %rbx
movq %r14, -0x40(%rbp)
addq $0x40, %r14
shrq %rax
movslq %eax, %r12
movq %rcx, %rax
subq %rsi, %rax
cqto
idivq %rbx
cmpq %r12, %rax
jbe 0x25e6
movq %r15, %rdi
movq %r12, %rsi
callq 0x3830
movq %r14, %rdi
movq %rax, %rsi
callq 0x37f4
incq %r12
movq 0x40(%r13), %rsi
movq 0x48(%r13), %rcx
jmp 0x253d
movl $0x90, %edi
callq 0x2190
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x3724
movq %r13, -0x38(%rbp)
movq -0x30(%rbp), %rbx
leaq 0x58(%rbx), %r15
movq 0x58(%rbx), %rax
movq 0x60(%rbx), %rdx
movq %rdx, %rcx
subq %rax, %rcx
leaq 0x58(%r13), %r14
shrq $0x4, %rcx
movslq %ecx, %r12
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x26cd
movq %r15, %rdi
movq %r12, %rsi
callq 0x38a6
movq %r14, %rdi
movq %rax, %rsi
callq 0x387c
incq %r12
movq 0x58(%rbx), %rax
movq 0x60(%rbx), %rdx
jmp 0x25b0
movq %rax, %rdi
movq -0x40(%rbp), %r12
movq 0x48(%r12), %rax
subq 0x40(%r12), %rax
cqto
idivq %rbx
movl %eax, 0x20(%r12)
shrq %rdi
imulq $0x28, %rdi, %rax
addq %rax, %rsi
movq %r15, %rdi
movq %rcx, %rdx
callq 0x384e
movq 0x28(%r13), %rcx
movq 0x48(%r13), %rax
subq 0x40(%r13), %rax
cqto
idivq %rbx
movl %eax, 0x20(%r13)
testq %rcx, %rcx
je 0x2896
movq %rcx, 0x28(%r12)
movq 0x28(%r13), %rbx
addq $0x58, %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3830
leaq 0x20(%rax), %rsi
movq %rbx, %rdi
callq 0x387c
movq 0x28(%r13), %rax
movq 0x58(%rax), %rdi
movq 0x60(%rax), %rsi
callq 0x4112
movq 0x28(%r13), %rdi
addq $0x70, %rdi
leaq -0x38(%rbp), %rsi
callq 0x3858
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x70(%rax), %rdi
movq 0x78(%rax), %rsi
leaq -0x254(%rip), %rdx      # 0x243c
callq 0x45ca
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
movl 0x20(%rdi), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x20(%rdi)
movq 0x60(%rdi), %rcx
subq 0x58(%rdi), %rcx
sarq $0x3, %rcx
movslq 0x6abf(%rip), %rdx      # 0x9178
decq %rdx
cmpq %rdx, %rcx
jbe 0x2989
movl %eax, 0x20(%rdi)
jmp 0x288c
movq %r14, -0x40(%rbp)
andq $-0x2, %rcx
leaq (%rax,%rcx,4), %rsi
movq %r15, %rdi
callq 0x38c8
movq 0x48(%r13), %rax
subq 0x40(%r13), %rax
pushq $0x28
popq %rsi
cqto
idivq %rsi
movl %eax, 0x20(%r13)
movq 0x48(%rbx), %rax
movq 0x70(%rbx), %rcx
subq 0x40(%rbx), %rax
cqto
idivq %rsi
movl %eax, 0x20(%rbx)
leaq 0x70(%rbx), %r15
movq 0x78(%rbx), %rdx
movq %rdx, %r12
subq %rcx, %r12
testb $0x8, %r12b
jne 0x2773
movq %r13, %r14
addq $0x70, %r13
shrq $0x4, %r12
movslq %r12d, %r12
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r12, %rax
jbe 0x27c5
movq %r15, %rdi
movq %r12, %rsi
callq 0x38d2
movq %r13, %rdi
movq %rax, %rsi
callq 0x3858
movq %r15, %rdi
movq %r12, %rsi
callq 0x38d2
movq (%rax), %rax
movq %r14, 0x28(%rax)
incq %r12
movq 0x70(%rbx), %rcx
movq 0x78(%rbx), %rdx
jmp 0x272b
shrq $0x4, %r12
movq %r13, %r14
addq $0x70, %r13
incl %r12d
movslq %r12d, %r12
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r12, %rax
jbe 0x27cf
movq %r15, %rdi
movq %r12, %rsi
callq 0x38d2
movq %r13, %rdi
movq %rax, %rsi
callq 0x3858
movq %r15, %rdi
movq %r12, %rsi
callq 0x38d2
movq (%rax), %rax
movq %r14, 0x28(%rax)
movq 0x70(%rbx), %rcx
movq 0x78(%rbx), %rdx
jmp 0x277e
andq $-0x2, %rax
leaq (%rcx,%rax,4), %rsi
jmp 0x27db
andq $-0x2, %rax
leaq (%rcx,%rax,4), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0x38f4
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x28fe
movq %rax, 0x28(%r14)
movq 0x28(%rbx), %r15
addq $0x58, %r15
movq -0x40(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x38a6
movq %r15, %rdi
movq %rax, %rsi
callq 0x387c
movq 0x58(%r14), %rsi
movq %r12, %rdi
callq 0x38fe
movq 0x28(%rbx), %rax
movq 0x58(%rax), %rdi
movq 0x60(%rax), %rsi
callq 0x4112
movq 0x28(%rbx), %rdi
addq $0x70, %rdi
leaq -0x38(%rbp), %rsi
callq 0x3858
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
incl 0x20(%rax)
movq 0x70(%rax), %rdi
movq 0x78(%rax), %rsi
leaq -0x3e1(%rip), %rdx      # 0x247c
callq 0x45ca
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
movq 0x60(%rdi), %rax
subq 0x58(%rdi), %rax
sarq $0x3, %rax
movslq 0x68fb(%rip), %rcx      # 0x9178
decq %rcx
cmpq %rcx, %rax
jbe 0x2989
decl 0x20(%rdi)
callq 0x24ba
jmp 0x2989
movl $0x90, %edi
callq 0x2190
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x3724
leaq 0x70(%rbx), %r15
leaq -0x30(%rbp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x3858
leaq -0x38(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3858
movq (%r14), %rax
movq %rbx, 0x28(%rax)
movq (%r12), %rdi
movq %rbx, 0x28(%rdi)
movq %rbx, %r15
addq $0x58, %r15
addq $0x40, %rdi
xorl %esi, %esi
callq 0x3830
leaq 0x20(%rax), %rsi
movq %r15, %rdi
callq 0x387c
jmp 0x2978
movl $0x90, %edi
callq 0x2190
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x3724
leaq 0x70(%rbx), %r12
leaq -0x30(%rbp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x3858
leaq -0x38(%rbp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x3858
movq (%r14), %rax
movq %rbx, 0x28(%rax)
movq (%r15), %rdi
movq %rbx, 0x28(%rdi)
movq %rbx, %r12
addq $0x58, %r12
addq $0x58, %rdi
xorl %esi, %esi
callq 0x38a6
movq %r12, %rdi
movq %rax, %rsi
callq 0x387c
movq (%r15), %rdi
movq 0x58(%rdi), %rsi
addq $0x58, %rdi
callq 0x38fe
movq (%r15), %rax
decl 0x20(%rax)
movq (%r14), %rax
movq 0x28(%rax), %rax
incl 0x20(%rax)
movq %rbx, 0x680f(%rip)      # 0x9198
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x299a
movq %rbx, %rdi
jmp 0x29a7
movq %r13, %rdi
jmp 0x29a7
movq %r14, %rdi
movq %rax, %r14
callq 0x2170
movq %r14, %rdi
callq 0x22a0