// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movl %edx, 0x4(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movb $0x1, 0x8(%rsp)
orl $-0x1, 0x40(%rsp)
movl %esi, %r14d
movq %rdi, %rbx
movl 0x4(%rdi), %r15d
movl %r15d, (%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x4162
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4086
leaq 0xc8(%rsp), %r15
movq %rsp, %r12
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
leaq 0x8(%rsp), %r13
cmpb $0x0, 0x8(%rsp)
jne 0x26f9
movq %r15, %rdi
movq %r12, %rsi
callq 0x5220
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rdx
cmpl %r14d, (%rdx)
jge 0x26d0
movq 0x18(%rsp), %rcx
subq %rax, %rcx
shlq $0x1e, %rcx
leaq (%rcx,%rbp), %rsi
sarq $0x20, %rsi
cmpl %r14d, -0x4(%rdx,%rsi,4)
jge 0x26d4
sarq $0x20, %rcx
movl -0x4(%rax,%rcx,4), %esi
jmp 0x26e9
movl (%rax), %esi
jmp 0x26e9
movq %r13, %rdi
movl %r14d, %esi
callq 0x4118
movq 0x10(%rsp), %rcx
cltq
movl (%rcx,%rax,4), %esi
movl %esi, (%rsp)
movq %rbx, %rdi
movq %r13, %rdx
callq 0x4086
jmp 0x268a
leaq 0x8(%rsp), %rdi
movl %r14d, %esi
callq 0x417e
testl %eax, %eax
je 0x2711
pushq $0x2
jmp 0x2b21
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
movzbl 0x8(%rsp), %ecx
xorl $0x1, %ecx
leaq 0xc3(,%rcx,4), %rcx
cmpq %rcx, %rax
jbe 0x29d6
xorps %xmm0, %xmm0
leaq 0x88(%rsp), %rdi
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x8(%rdi)
movb $0x1, %al
movb %al, (%rdi)
orl $-0x1, 0x38(%rdi)
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movb %al, 0x48(%rsp)
orl $-0x1, 0x80(%rsp)
leaq 0x8(%rsp), %rsi
callq 0x4382
leaq 0x88(%rsp), %rdi
movl %r14d, %esi
movl 0x4(%rsp), %edx
callq 0x419c
leaq 0x8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
pushq $0x1a
popq %rdx
callq 0x43c2
movl (%rbx), %eax
incl %eax
movl %eax, (%rbx)
movl 0x40(%rsp), %ecx
leaq 0x48(%rsp), %rdi
movl %ecx, 0x38(%rdi)
movl %eax, 0x40(%rsp)
leaq 0x88(%rsp), %rsi
pushq $0x1a
popq %rdx
callq 0x448a
movq 0xa8(%rsp), %rax
movl 0x64(%rax), %r14d
movl (%rbx), %ebp
movl (%rsp), %r12d
leaq 0x218(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4296
leaq 0x218(%rsp), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x41fc
leaq 0x218(%rsp), %rdi
callq 0x4142
movl (%rsp), %r15d
leaq 0x1d8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4296
leaq 0x1d8(%rsp), %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x41fc
leaq 0x1d8(%rsp), %rdi
callq 0x4142
movq 0xa8(%rsp), %rax
cmpq %rax, 0xb0(%rsp)
je 0x285a
movq %rax, 0xb0(%rsp)
movq 0x90(%rsp), %rax
cmpq %rax, 0x98(%rsp)
je 0x2874
movq %rax, 0x98(%rsp)
movq 0x68(%rsp), %rax
cmpq %rax, 0x70(%rsp)
je 0x2885
movq %rax, 0x70(%rsp)
movl %r12d, %r15d
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0x2899
movq %rax, 0x58(%rsp)
leaq 0x8(%rsp), %r12
leaq 0x88(%rsp), %r13
leaq 0xc8(%rsp), %rdi
callq 0x52bc
testq %rax, %rax
je 0x2a28
movq 0xf8(%rsp), %rax
cmpq 0x100(%rsp), %rax
jne 0x28e2
movq 0x110(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movl -0x4(%rax), %eax
movl %eax, (%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x5454
movl (%rsp), %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x4086
leaq 0x2d8(%rsp), %r15
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
movzbl 0x8(%rsp), %ecx
xorl $0x1, %ecx
leaq 0xc3(,%rcx,4), %rcx
cmpq %rcx, %rax
jbe 0x2abd
movq %r13, %rdi
movq %r12, %rsi
callq 0x4382
movq %r13, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x419c
movq %r12, %rdi
movq %r13, %rsi
pushq $0x19
popq %rdx
callq 0x43c2
incl (%rbx)
movb $0x0, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
pushq $0x19
popq %rdx
callq 0x448a
movl (%rbx), %ebp
leaq 0x298(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4296
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x298(%rsp), %r14
movq %r14, %rdx
callq 0x41fc
movq %r14, %rdi
callq 0x4142
movl (%rsp), %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x4296
movq %rbx, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x41fc
movq %r15, %rdi
callq 0x4142
movq 0xa8(%rsp), %rax
movl 0x60(%rax), %r14d
movl (%rbx), %ebp
movl (%rsp), %r15d
jmp 0x28a6
leaq 0x8(%rsp), %rdi
movl %r14d, %esi
movl 0x4(%rsp), %edx
callq 0x419c
movl (%rsp), %ebp
leaq 0x258(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4296
leaq 0x258(%rsp), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x41fc
leaq 0x258(%rsp), %rdi
callq 0x4142
movq %rbx, %rdi
callq 0x42e2
jmp 0x2b1f
xorps %xmm0, %xmm0
leaq 0x120(%rsp), %rax
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
leaq 0x118(%rsp), %rdi
movb $0x0, (%rdi)
orl $-0x1, 0x38(%rdi)
movl %r14d, %esi
callq 0x456e
leaq 0x118(%rsp), %rdi
movl %r15d, %esi
callq 0x4584
leaq 0x118(%rsp), %rdi
movl %ebp, %esi
callq 0x4584
movl (%rbx), %ebp
incl %ebp
movl %ebp, (%rbx)
leaq 0x198(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x4296
leaq 0x198(%rsp), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x41fc
leaq 0x198(%rsp), %rdi
callq 0x4142
movl (%rbx), %eax
movl %eax, 0x4(%rbx)
leaq 0x118(%rsp), %rdi
jmp 0x2afb
leaq 0x8(%rsp), %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x419c
movl (%rsp), %ebp
leaq 0x158(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4296
leaq 0x158(%rsp), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x41fc
leaq 0x158(%rsp), %rdi
callq 0x4142
movq %rbx, %rdi
callq 0x42e2
leaq 0x48(%rsp), %rdi
callq 0x4142
leaq 0x88(%rsp), %rdi
callq 0x4142
pushq $0x1
popq %rbx
leaq 0xc8(%rsp), %rdi
callq 0x5018
leaq 0x8(%rsp), %rdi
callq 0x4142
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
jmp 0x2bc3
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x4142
jmp 0x2b98
jmp 0x2bcc
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
jmp 0x2bc3
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
jmp 0x2bc3
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
jmp 0x2be1
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
jmp 0x2bc3
movq %rax, %rbx
jmp 0x2bf8
jmp 0x2bcc
jmp 0x2be8
movq %rax, %rbx
leaq 0x2d8(%rsp), %rdi
jmp 0x2bc3
movq %rax, %rbx
leaq 0x298(%rsp), %rdi
callq 0x4142
jmp 0x2bcf
jmp 0x2bcc
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4142
leaq 0x88(%rsp), %rdi
callq 0x4142
jmp 0x2beb
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x5018
leaq 0x8(%rsp), %rdi
callq 0x4142
movq %rbx, %rdi
callq 0x2320