// File: pawan231[P]MiniDataBase/include/BPtree.h
// Function: BPtree::insert_record(int, int)
int BPtree::insert_record(int primary_key, int record_num)
{
	//printf("pri %d\n record_num %d",primary_key,record_num);
    Btreenode n(true);
    int q, j, prop_n, prop_k, prop_new, curr_node = root_num;
    bool finish = false;
    std::stack < int >S;

    read_node(curr_node, n);

    //Traverse the tree till we get the leaf node;
    while (!n.isleaf())
    {
        S.push(curr_node);      //Storing address in case of split
        q = n.num_pointers();
        if (primary_key <= n.get_key(1))
        {
            curr_node = n.get_pointer(1);
        }
        else if (primary_key > n.get_key(q - 1))
        {
            curr_node = n.get_pointer(q);
        }
        else
        {
            curr_node = n.get_pointer(n.get_next_key(primary_key) + 1);
        }
        read_node(curr_node, n);
    }

    //Here n is Leaf Node
    //if key exist exist then return;
    if (n.search_key(primary_key))  //key exist
    {
        return BPTREE_INSERT_ERROR_EXIST;
    }

    if (!n.full())
    {
        //leaf node empty insert here and exit
        n.insert_key(primary_key, record_num);
        write_node(curr_node, n);
        update_meta_data();
        return BPTREE_INSERT_SUCCESS;
    }

    //if node n is full, then split;
    Btreenode temp(true), new_node(true);

    temp = n;
    temp.insert_key(primary_key, record_num);
    j = ceil((BPTREE_MAX_KEYS_PER_NODE + 1.0) / 2.0);
    n.copy_first(temp, j);
    files_till_now++;
    new_node.set_next(n.get_next());
    n.set_next(files_till_now);
    new_node.copy_last(temp, j);

    prop_k = temp.get_key(j);
    prop_new = files_till_now;
    prop_n = curr_node;
    write_node(files_till_now, new_node);
    write_node(curr_node, n);
    temp.clear_data();
    new_node.clear_data();

    /* Keep repeating until we reach root
       or find an empty internal node */
    while (!finish)
    {
        if (S.size() == 0)
        {
            /*Last element splitted was root
               so create new root and assign meta_data */
            Btreenode nn(false);
            nn.push_key(prop_k);
            nn.push_pointer(prop_n);
            nn.push_pointer(prop_new);
            files_till_now++;
            write_node(files_till_now, nn);
            root_num = files_till_now;
            finish = true;
        }
        else
        {
            curr_node = S.top();
            S.pop();
            read_node(curr_node, n);
            if (!n.full())
            {
                n.insert_key(prop_k, prop_new);
                write_node(curr_node, n);
                finish = true;
            }
            else
            {
                /* Split is propogating towards top */
                temp = n;
                temp.insert_key(prop_k, prop_new);
                j = floor((BPTREE_MAX_KEYS_PER_NODE + 1.0) / 2.0);
                n.copy_first(temp, j);
                files_till_now++;
                new_node.set_leaf(false);
                new_node.copy_last(temp, j);
                write_node(files_till_now, new_node);
                write_node(curr_node, n);
                prop_k = temp.get_key(j);
                prop_new = files_till_now;
                prop_n = curr_node;
            }
        }
    }

    update_meta_data();
    return BPTREE_INSERT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x360(%rbp)
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x5320
movq -0x360(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x70(%rbp)
movb $0x0, -0x71(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x55e0
jmp 0x282d
movq -0x360(%rbp), %rdi
movl -0x70(%rbp), %esi
leaq -0x58(%rbp), %rdx
callq 0x5370
jmp 0x2842
jmp 0x2844
leaq -0x58(%rbp), %rdi
callq 0x5460
movb %al, -0x361(%rbp)
jmp 0x2855
movb -0x361(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2866
jmp 0x29b3
leaq -0xc8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5610
jmp 0x2878
leaq -0x58(%rbp), %rdi
callq 0x5480
movl %eax, -0x368(%rbp)
jmp 0x2889
movl -0x368(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x370(%rbp)
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x54a0
movl %eax, -0x36c(%rbp)
jmp 0x28b1
movl -0x370(%rbp), %eax
movl -0x36c(%rbp), %ecx
cmpl %ecx, %eax
jg 0x2913
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x54d0
movl %eax, -0x374(%rbp)
jmp 0x28d7
movl -0x374(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x2999
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x30c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x30b6
movl -0x14(%rbp), %eax
movl %eax, -0x37c(%rbp)
movl -0x5c(%rbp), %esi
decl %esi
leaq -0x58(%rbp), %rdi
callq 0x54a0
movl %eax, -0x378(%rbp)
jmp 0x2932
movl -0x37c(%rbp), %eax
movl -0x378(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2961
movl -0x5c(%rbp), %esi
leaq -0x58(%rbp), %rdi
callq 0x54d0
movl %eax, -0x380(%rbp)
jmp 0x2956
movl -0x380(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x2997
movl -0x14(%rbp), %esi
leaq -0x58(%rbp), %rdi
callq 0x5500
movl %eax, -0x384(%rbp)
jmp 0x2975
movl -0x384(%rbp), %esi
incl %esi
leaq -0x58(%rbp), %rdi
callq 0x54d0
movl %eax, -0x388(%rbp)
jmp 0x298e
movl -0x388(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x2999
movq -0x360(%rbp), %rdi
movl -0x70(%rbp), %esi
leaq -0x58(%rbp), %rdx
callq 0x5370
jmp 0x29ae
jmp 0x2844
movl -0x14(%rbp), %esi
leaq -0x58(%rbp), %rdi
callq 0x5640
movl %eax, -0x38c(%rbp)
jmp 0x29c7
movl -0x38c(%rbp), %eax
cmpl $0x0, %eax
je 0x29e8
movl $0x2, -0x4(%rbp)
movl $0x1, -0xd8(%rbp)
jmp 0x3095
leaq -0x58(%rbp), %rdi
callq 0x56a0
movb %al, -0x38d(%rbp)
jmp 0x29f9
movb -0x38d(%rbp), %al
testb $0x1, %al
jne 0x2aa3
jmp 0x2a09
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x58(%rbp), %rdi
callq 0x5700
jmp 0x2a1a
movl -0x70(%rbp), %eax
movl %eax, -0x394(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5970
jmp 0x2a35
movl -0x394(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x118(%rbp), %rdx
callq 0x5860
jmp 0x2a50
leaq -0x118(%rbp), %rdi
callq 0x5570
movq -0x360(%rbp), %rdi
callq 0x59f0
jmp 0x2a6a
movl $0x1, -0x4(%rbp)
movl $0x1, -0xd8(%rbp)
jmp 0x3095
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x5570
jmp 0x30b6
leaq -0x158(%rbp), %rdi
movl $0x1, %esi
callq 0x5320
jmp 0x2ab6
leaq -0x198(%rbp), %rdi
movl $0x1, %esi
callq 0x5320
jmp 0x2ac9
leaq -0x158(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5b20
jmp 0x2adb
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x158(%rbp), %rdi
callq 0x5700
jmp 0x2aef
movl $0x1a, -0x60(%rbp)
movl -0x60(%rbp), %edx
leaq -0x58(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x5be0
jmp 0x2b0b
movq -0x360(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq -0x58(%rbp), %rdi
callq 0x5d00
movl %eax, -0x398(%rbp)
jmp 0x2b29
movl -0x398(%rbp), %esi
leaq -0x198(%rbp), %rdi
callq 0x5ce0
jmp 0x2b3d
movq -0x360(%rbp), %rax
movl (%rax), %esi
leaq -0x58(%rbp), %rdi
callq 0x5ce0
jmp 0x2b51
movl -0x60(%rbp), %edx
leaq -0x198(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x5d20
jmp 0x2b69
movl -0x60(%rbp), %esi
leaq -0x158(%rbp), %rdi
callq 0x54a0
movl %eax, -0x39c(%rbp)
jmp 0x2b80
movq -0x360(%rbp), %rax
movl -0x39c(%rbp), %ecx
movl %ecx, -0x68(%rbp)
movl (%rax), %ecx
movl %ecx, -0x6c(%rbp)
movl -0x70(%rbp), %ecx
movl %ecx, -0x64(%rbp)
movl (%rax), %eax
movl %eax, -0x3a0(%rbp)
leaq -0x1d8(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x5970
jmp 0x2bb8
movl -0x3a0(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x1d8(%rbp), %rdx
callq 0x5860
jmp 0x2bd3
leaq -0x1d8(%rbp), %rdi
callq 0x5570
movl -0x70(%rbp), %eax
movl %eax, -0x3a4(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5970
jmp 0x2bfa
movl -0x3a4(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x218(%rbp), %rdx
callq 0x5860
jmp 0x2c15
leaq -0x218(%rbp), %rdi
callq 0x5570
leaq -0x158(%rbp), %rdi
callq 0x5e00
jmp 0x2c2f
leaq -0x198(%rbp), %rdi
callq 0x5e00
jmp 0x2c3d
jmp 0x2c3f
movb -0x71(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2c4d
jmp 0x3042
leaq -0xc8(%rbp), %rdi
callq 0x5e30
movq %rax, -0x3b0(%rbp)
jmp 0x2c62
movq -0x3b0(%rbp), %rax
cmpq $0x0, %rax
jne 0x2ddf
leaq -0x258(%rbp), %rdi
xorl %esi, %esi
callq 0x5320
jmp 0x2c83
movl -0x68(%rbp), %esi
leaq -0x258(%rbp), %rdi
callq 0x5e50
jmp 0x2c94
movl -0x64(%rbp), %esi
leaq -0x258(%rbp), %rdi
callq 0x5e80
jmp 0x2ca5
movl -0x6c(%rbp), %esi
leaq -0x258(%rbp), %rdi
callq 0x5e80
jmp 0x2cb6
movq -0x360(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movl (%rax), %eax
movl %eax, -0x3b4(%rbp)
leaq -0x298(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x5970
jmp 0x2ce0
movl -0x3b4(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x298(%rbp), %rdx
callq 0x5860
jmp 0x2cfb
leaq -0x298(%rbp), %rdi
callq 0x5570
movq -0x360(%rbp), %rax
movl (%rax), %ecx
movl %ecx, 0x4(%rax)
movb $0x1, -0x71(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x5570
jmp 0x303d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x3087
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x307b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x5570
jmp 0x307b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x5570
jmp 0x307b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x2dce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x298(%rbp), %rdi
callq 0x5570
leaq -0x258(%rbp), %rdi
callq 0x5570
jmp 0x307b
leaq -0xc8(%rbp), %rdi
callq 0x5eb0
movq %rax, -0x3c0(%rbp)
jmp 0x2df4
movq -0x3c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x70(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5ed0
jmp 0x2e0e
movq -0x360(%rbp), %rdi
movl -0x70(%rbp), %esi
leaq -0x58(%rbp), %rdx
callq 0x5370
jmp 0x2e23
leaq -0x58(%rbp), %rdi
callq 0x56a0
movb %al, -0x3c1(%rbp)
jmp 0x2e34
movb -0x3c1(%rbp), %al
testb $0x1, %al
jne 0x2ec3
jmp 0x2e44
movl -0x68(%rbp), %esi
movl -0x6c(%rbp), %edx
leaq -0x58(%rbp), %rdi
callq 0x5700
jmp 0x2e55
movl -0x70(%rbp), %eax
movl %eax, -0x3c8(%rbp)
leaq -0x2d8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5970
jmp 0x2e70
movl -0x3c8(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x2d8(%rbp), %rdx
callq 0x5860
jmp 0x2e8b
leaq -0x2d8(%rbp), %rdi
callq 0x5570
movb $0x1, -0x71(%rbp)
jmp 0x303b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0x5570
jmp 0x307b
leaq -0x158(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5b20
jmp 0x2ed5
movl -0x68(%rbp), %esi
movl -0x6c(%rbp), %edx
leaq -0x158(%rbp), %rdi
callq 0x5700
jmp 0x2ee9
movl $0x19, -0x60(%rbp)
movl -0x60(%rbp), %edx
leaq -0x58(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x5be0
jmp 0x2f05
movq -0x360(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
leaq -0x198(%rbp), %rdi
xorl %esi, %esi
callq 0x5ef0
jmp 0x2f22
movl -0x60(%rbp), %edx
leaq -0x198(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x5d20
jmp 0x2f3a
movq -0x360(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3cc(%rbp)
leaq -0x318(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x5970
jmp 0x2f5e
movl -0x3cc(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x318(%rbp), %rdx
callq 0x5860
jmp 0x2f79
leaq -0x318(%rbp), %rdi
callq 0x5570
movl -0x70(%rbp), %eax
movl %eax, -0x3d0(%rbp)
leaq -0x358(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5970
jmp 0x2fa0
movl -0x3d0(%rbp), %esi
movq -0x360(%rbp), %rdi
leaq -0x358(%rbp), %rdx
callq 0x5860
jmp 0x2fbb
leaq -0x358(%rbp), %rdi
callq 0x5570
movl -0x60(%rbp), %esi
leaq -0x158(%rbp), %rdi
callq 0x54a0
movl %eax, -0x3d4(%rbp)
jmp 0x2fde
movq -0x360(%rbp), %rax
movl -0x3d4(%rbp), %ecx
movl %ecx, -0x68(%rbp)
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x303b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x318(%rbp), %rdi
callq 0x5570
jmp 0x307b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x5570
jmp 0x307b
jmp 0x303d
jmp 0x2c3f
movq -0x360(%rbp), %rdi
callq 0x59f0
jmp 0x3050
movl $0x1, -0x4(%rbp)
movl $0x1, -0xd8(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x5570
leaq -0x158(%rbp), %rdi
callq 0x5570
jmp 0x3095
leaq -0x198(%rbp), %rdi
callq 0x5570
leaq -0x158(%rbp), %rdi
callq 0x5570
jmp 0x30b6
leaq -0xc8(%rbp), %rdi
callq 0x5f10
leaq -0x58(%rbp), %rdi
callq 0x5570
movl -0x4(%rbp), %eax
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
leaq -0xc8(%rbp), %rdi
callq 0x5f10
leaq -0x58(%rbp), %rdi
callq 0x5570
movq -0xd0(%rbp), %rdi
callq 0x2340
nopw (%rax,%rax)