// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, -0x30(%rbp)
cmpb $0x1, 0x88(%rdi)
jne 0x2693
movl $0x90, %edi
callq 0x21a0
movq %rax, %rbx
movq %rax, %rdi
movl $0x1, %esi
callq 0x412a
movq %rbx, -0x38(%rbp)
movq -0x30(%rbp), %r12
movq %r12, 0x38(%rbx)
movq 0x30(%r12), %rax
testq %rax, %rax
je 0x2576
movq %rax, 0x30(%rbx)
movq %rbx, 0x38(%rax)
movq %rbx, 0x30(%r12)
leaq 0x40(%r12), %rdi
movq 0x40(%r12), %rsi
movq 0x48(%r12), %rdx
subq %rsi, %rdx
movq %rdx, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %r13 # imm = 0xCCCCCCCCCCCCCCCD
imulq %r13, %rcx
movq %rcx, %rax
shrq %rax
movslq %eax, %r14
cmpq %r14, %rcx
jbe 0x2605
movq %rdi, -0x40(%rbp)
movq %rbx, -0x48(%rbp)
movq %rbx, %r15
addq $0x40, %r15
leaq (,%r14,8), %rax
leaq (%rax,%rax,4), %rbx
addq %rbx, %rsi
movq %r15, %rdi
callq 0x41d6
incq %r14
movq 0x40(%r12), %rsi
movq 0x48(%r12), %rdx
subq %rsi, %rdx
movq %rdx, %rax
sarq $0x3, %rax
imulq %r13, %rax
addq $0x28, %rbx
cmpq %r14, %rax
ja 0x25cb
shrq %rax
movq -0x48(%rbp), %rbx
movq -0x40(%rbp), %rdi
movq 0x48(%rbx), %rcx
subq 0x40(%rbx), %rcx
shrq $0x3, %rcx
imull $0xcccccccd, %ecx, %ecx # imm = 0xCCCCCCCD
movl %ecx, 0x20(%rbx)
leaq (%rax,%rax,4), %rax
leaq (%rsi,%rax,8), %rax
addq %rsi, %rdx
movq %rax, %rsi
callq 0x459e
movq 0x28(%r12), %rax
movq 0x48(%r12), %rcx
subq 0x40(%r12), %rcx
shrq $0x3, %rcx
imull $0xcccccccd, %ecx, %ecx # imm = 0xCCCCCCCD
movl %ecx, 0x20(%r12)
testq %rax, %rax
je 0x28d6
movq %rax, 0x28(%rbx)
movq 0x40(%rbx), %rdx
cmpq %rdx, 0x48(%rbx)
je 0x2ce8
movq 0x28(%r12), %rdi
addq $0x20, %rdx
movq 0x60(%rdi), %r14
cmpq 0x68(%rdi), %r14
je 0x295e
movsd (%rdx), %xmm0
movsd %xmm0, (%r14)
addq $0x8, %r14
movq %r14, 0x60(%rdi)
jmp 0x2973
movl $0x90, %edi
callq 0x21a0
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x412a
movq %rbx, -0x38(%rbp)
movq -0x30(%rbp), %r12
movq 0x58(%r12), %rax
movq 0x60(%r12), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq %rdx, %rsi
shrq $0x4, %rsi
movslq %esi, %r15
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x272c
movq %rbx, %r14
addq $0x58, %r14
leaq (,%r15,8), %r13
leaq (%rax,%r13), %rdx
movq 0x60(%rbx), %rsi
cmpq 0x68(%rbx), %rsi
je 0x2704
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x60(%rbx)
jmp 0x2711
movq %r14, %rdi
callq 0x47a8
movq 0x58(%r12), %rax
incq %r15
movq 0x60(%r12), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
addq $0x8, %r13
cmpq %r15, %rdx
ja 0x26e4
andq $-0x2, %rdx
leaq (%rax,%rdx,4), %rax
cmpq %rcx, %rax
je 0x273e
movq %rax, 0x60(%r12)
movq 0x48(%rbx), %rax
subq 0x40(%rbx), %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
movl %eax, 0x20(%rbx)
movq 0x48(%r12), %rax
movq 0x70(%r12), %rcx
subq 0x40(%r12), %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
movl %eax, 0x20(%r12)
movq 0x78(%r12), %rsi
movq %rsi, %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
testb $0x8, %al
jne 0x2805
shrq $0x4, %rax
movslq %eax, %r14
cmpq %r14, %rdx
jbe 0x27f8
movslq %eax, %r15
leaq (%rcx,%r14,8), %rdx
movq 0x78(%rbx), %rsi
cmpq 0x80(%rbx), %rsi
je 0x27b8
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x78(%rbx)
movq -0x30(%rbp), %r12
jmp 0x27c4
addq $0x70, %rbx
movq %rbx, %rdi
callq 0x466e
movq 0x70(%r12), %rcx
movq 0x78(%r12), %rsi
movq %rsi, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x2ccd
movq -0x38(%rbp), %rbx
movq (%rcx,%r14,8), %rax
movq %rbx, 0x28(%rax)
incq %r15
movq %r15, %r14
cmpq %r15, %rdx
ja 0x2796
andq $-0x2, %rdx
leaq (%rcx,%rdx,4), %rax
jmp 0x2888
movq %rdx, %rax
shrq %rax
leal 0x1(%rax), %edi
movslq %edi, %r14
cmpq %r14, %rdx
jbe 0x2880
leal 0x2(%rax), %r15d
leaq (%rcx,%r14,8), %rdx
movq 0x78(%rbx), %rsi
cmpq 0x80(%rbx), %rsi
je 0x283c
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x78(%rbx)
movq -0x30(%rbp), %r12
jmp 0x2848
addq $0x70, %rbx
movq %rbx, %rdi
callq 0x466e
movq 0x70(%r12), %rcx
movq 0x78(%r12), %rsi
movq %rsi, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x2cd9
movq -0x38(%rbp), %rbx
movq (%rcx,%r14,8), %rdx
movq %rbx, 0x28(%rdx)
movslq %r15d, %r14
leal 0x1(%r14), %r15d
cmpq %r14, %rax
ja 0x281a
shrq %rax
leaq (%rcx,%rax,8), %rax
addq $0x8, %rax
cmpq %rsi, %rax
je 0x2892
movq %rax, 0x78(%r12)
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x291a
movq %rax, 0x28(%rbx)
movq 0x58(%rbx), %rdx
cmpq %rdx, 0x60(%rbx)
je 0x2ce8
movq 0x28(%r12), %rdi
movq 0x60(%rdi), %rsi
cmpq 0x68(%rdi), %rsi
je 0x2a59
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x60(%rdi)
jmp 0x2a62
movl $0x90, %edi
callq 0x21a0
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x412a
leaq 0x70(%rbx), %r14
movq 0x78(%rbx), %rsi
cmpq 0x80(%rbx), %rsi
je 0x2b75
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
movq 0x78(%rbx), %rsi
addq $0x8, %rsi
movq %rsi, 0x78(%rbx)
jmp 0x2b85
movl $0x90, %edi
callq 0x21a0
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x412a
leaq 0x70(%rbx), %r14
movq 0x78(%rbx), %rsi
cmpq 0x80(%rbx), %rsi
je 0x2bfc
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
movq 0x78(%rbx), %rsi
addq $0x8, %rsi
movq %rsi, 0x78(%rbx)
jmp 0x2c0c
addq $0x58, %rdi
movq %r14, %rsi
callq 0x47a8
movq 0x28(%r12), %rdi
movq 0x60(%rdi), %r14
movq 0x58(%rdi), %r15
cmpq %r14, %r15
je 0x29ae
movq %r14, %rax
subq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x48e4
movq %r15, %rdi
movq %r14, %rsi
callq 0x49d0
movq 0x28(%r12), %rdi
movq 0x78(%rdi), %rsi
cmpq 0x80(%rdi), %rsi
je 0x29c5
movq %rbx, (%rsi)
addq $0x8, 0x78(%rdi)
jmp 0x29d2
addq $0x70, %rdi
leaq -0x38(%rbp), %rdx
callq 0x466e
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
movq 0x70(%rdi), %rbx
movq 0x78(%rdi), %r14
cmpq %r14, %rbx
je 0x2a29
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x559(%rip), %r15      # 0x24ac
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x4caf
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4dda
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 0x674b(%rip), %rdx      # 0x9190
decq %rdx
cmpq %rdx, %rcx
jbe 0x2cbe
movl %eax, 0x20(%rdi)
jmp 0x2b6b
addq $0x58, %rdi
callq 0x47a8
movq 0x58(%rbx), %rdi
movq 0x60(%rbx), %rdx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0x2a7f
subq %rsi, %rdx
callq 0x2290
movq 0x60(%rbx), %rdx
addq $-0x8, %rdx
movq %rdx, 0x60(%rbx)
movq 0x28(%r12), %rdi
movq 0x58(%rdi), %r14
movq 0x60(%rdi), %r15
cmpq %r15, %r14
je 0x2acb
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x48e4
movq %r14, %rdi
movq %r15, %rsi
callq 0x49d0
movq 0x28(%r12), %rdi
movq 0x78(%rdi), %rsi
cmpq 0x80(%rdi), %rsi
je 0x2ae2
movq %rbx, (%rsi)
addq $0x8, 0x78(%rdi)
jmp 0x2aef
addq $0x70, %rdi
leaq -0x38(%rbp), %rdx
callq 0x466e
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
incl 0x20(%rdi)
movq 0x70(%rdi), %rbx
movq 0x78(%rdi), %r14
cmpq %r14, %rbx
je 0x2b49
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x640(%rip), %r15      # 0x24e5
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x4caf
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4dda
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
movq 0x60(%rdi), %rax
subq 0x58(%rdi), %rax
sarq $0x3, %rax
movslq 0x6634(%rip), %rcx      # 0x9190
decq %rcx
cmpq %rcx, %rax
jbe 0x2cbe
decl 0x20(%rdi)
callq 0x251c
jmp 0x2cbe
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
callq 0x466e
movq 0x78(%rbx), %rsi
cmpq 0x80(%rbx), %rsi
je 0x2b9c
movq -0x38(%rbp), %rcx
movq %rcx, (%rsi)
addq $0x8, 0x78(%rbx)
jmp 0x2bae
leaq -0x38(%rbp), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x466e
movq (%r15), %rcx
movq -0x30(%rbp), %rax
movq %rbx, 0x28(%rax)
movq %rbx, 0x28(%rcx)
movq 0x40(%rcx), %rdx
cmpq %rdx, 0x48(%rcx)
je 0x2ce8
addq $0x20, %rdx
movq 0x60(%rbx), %rsi
cmpq 0x68(%rbx), %rsi
je 0x2beb
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x60(%rbx)
jmp 0x2cb0
movq %rbx, %rdi
addq $0x58, %rdi
callq 0x47a8
jmp 0x2cac
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
callq 0x466e
movq 0x78(%rbx), %rsi
cmpq 0x80(%rbx), %rsi
je 0x2c23
movq -0x38(%rbp), %r14
movq %r14, (%rsi)
addq $0x8, 0x78(%rbx)
jmp 0x2c35
leaq -0x38(%rbp), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x466e
movq (%r15), %r14
movq -0x30(%rbp), %rax
movq %rbx, 0x28(%rax)
movq %rbx, 0x28(%r14)
movq 0x58(%r14), %rdx
cmpq %rdx, 0x60(%r14)
je 0x2ce8
movq 0x60(%rbx), %rsi
cmpq 0x68(%rbx), %rsi
je 0x2c6b
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x60(%rbx)
jmp 0x2c7b
movq %rbx, %rdi
addq $0x58, %rdi
callq 0x47a8
movq -0x38(%rbp), %r14
movq 0x58(%r14), %rdi
movq 0x60(%r14), %rdx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0x2c9e
subq %rsi, %rdx
callq 0x2290
movq 0x60(%r14), %rdx
movq -0x38(%rbp), %rax
jmp 0x2ca1
movq %r14, %rax
addq $-0x8, %rdx
movq %rdx, 0x60(%r14)
decl 0x20(%rax)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
incl 0x20(%rax)
movq %rbx, 0x64f2(%rip)      # 0x91b0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x33a1(%rip), %rdi      # 0x6075
movq %r14, %rsi
jmp 0x2cf3
leaq 0x3395(%rip), %rdi      # 0x6075
movq %r14, %rsi
movq %rax, %rdx
jmp 0x2cf3
leaq 0x3386(%rip), %rdi      # 0x6075
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2250
jmp 0x2d00
jmp 0x2d00
jmp 0x2d00
movq %rax, %r14
movq %rbx, %rdi
callq 0x2180
movq %r14, %rdi
callq 0x22c0