// File: /workspace/github_cpp_makefile_star2_O1/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 $0x328, %rsp            # imm = 0x328
movl %edx, 0xc(%rsp)
movl %esi, %r14d
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rsp)
movups %xmm0, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movb $0x1, 0x10(%rsp)
movl $0xffffffff, 0x48(%rsp) # imm = 0xFFFFFFFF
movl 0x4(%rdi), %eax
movl %eax, 0x4(%rsp)
leaq 0x110(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x5b52
movl 0x4(%rsp), %esi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x4b7e
leaq 0x110(%rsp), %r15
leaq 0x4(%rsp), %r12
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
leaq 0x10(%rsp), %r13
cmpb $0x0, 0x10(%rsp)
jne 0x3402
movq 0x140(%rsp), %rax
movq 0x150(%rsp), %rcx
addq $-0x4, %rcx
cmpq %rcx, %rax
je 0x3344
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
addq $0x4, %rax
movq %rax, 0x140(%rsp)
jmp 0x334f
movq %r15, %rdi
movq %r12, %rsi
callq 0x5cc8
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
cmpl %r14d, (%rcx)
jge 0x3388
movq 0x20(%rsp), %rdx
subq %rax, %rdx
movq %rdx, %rsi
shlq $0x1e, %rsi
addq %rbp, %rsi
sarq $0x20, %rsi
cmpl %r14d, -0x4(%rcx,%rsi,4)
jge 0x338c
shrq $0x2, %rdx
movslq %edx, %rcx
movl -0x4(%rax,%rcx,4), %eax
jmp 0x33ea
movl (%rax), %eax
jmp 0x33ea
movq 0x38(%rsp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
movq %rcx, %rdx
testq %rsi, %rsi
jle 0x33ce
movq %rcx, %rdx
movq %rsi, %rdi
shrq %rdi
movq %rdi, %r8
notq %r8
addq %rsi, %r8
leaq (%rdx,%rdi,4), %rsi
addq $0x4, %rsi
cmpl %r14d, -0x4(%rsi)
cmovlq %rsi, %rdx
cmovgeq %rdi, %r8
movq %r8, %rsi
testq %r8, %r8
jg 0x33a3
subq %rcx, %rdx
shlq $0x1e, %rdx
movabsq $0x100000000, %rcx      # imm = 0x100000000
addq %rcx, %rdx
sarq $0x20, %rdx
movl -0x4(%rax,%rdx,4), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %esi
movq %rbx, %rdi
movq %r13, %rdx
callq 0x4b7e
jmp 0x330c
leaq 0x10(%rsp), %rdi
movl %r14d, %esi
callq 0x4ca8
movl $0x2, %r15d
testl %eax, %eax
jne 0x3a75
movq 0x20(%rsp), %rax
subq 0x18(%rsp), %rax
movzbl 0x10(%rsp), %ecx
xorl $0x1, %ecx
leaq 0xc3(,%rcx,4), %rcx
cmpq %rcx, %rax
jbe 0x3a01
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %rdi
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x8(%rdi)
movb $0x1, %al
movb %al, (%rdi)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x38(%rdi)
movups %xmm0, 0x78(%rsp)
movups %xmm0, 0x68(%rsp)
movups %xmm0, 0x58(%rsp)
movb %al, 0x50(%rsp)
movl %ecx, 0x88(%rsp)
leaq 0x10(%rsp), %rsi
callq 0x4ef2
leaq 0xd0(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
callq 0x4cfa
leaq 0x10(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movl $0x1a, %edx
callq 0x4f32
movl (%rbx), %eax
incl %eax
movl %eax, (%rbx)
movl 0x48(%rsp), %ecx
leaq 0x50(%rsp), %rdi
movl %ecx, 0x38(%rdi)
movl %eax, 0x48(%rsp)
leaq 0xd0(%rsp), %rsi
movl $0x1a, %edx
callq 0x5066
leaq 0x58(%rsp), %rsi
movq 0xf0(%rsp), %rax
movl 0x64(%rax), %ebp
movl (%rbx), %r12d
movl 0x4(%rsp), %r14d
movb -0x8(%rsi), %al
leaq 0x2b0(%rsp), %rdi
movb %al, -0x8(%rdi)
callq 0x58ae
leaq 0x2c8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x58ae
movl 0x88(%rsp), %eax
leaq 0x2a8(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4d5a
leaq 0x2a8(%rsp), %rdi
callq 0x4c5e
movl 0x4(%rsp), %r15d
movb 0x10(%rsp), %al
leaq 0x270(%rsp), %rdi
movb %al, -0x8(%rdi)
leaq 0x18(%rsp), %rsi
callq 0x58ae
leaq 0x288(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x58ae
movl 0x48(%rsp), %eax
leaq 0x268(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4d5a
leaq 0x268(%rsp), %rdi
callq 0x4c5e
movq 0xf0(%rsp), %rax
cmpq %rax, 0xf8(%rsp)
je 0x35b2
movq %rax, 0xf8(%rsp)
movq 0xd8(%rsp), %rax
cmpq %rax, 0xe0(%rsp)
je 0x35cc
movq %rax, 0xe0(%rsp)
movq 0x70(%rsp), %rax
cmpq %rax, 0x78(%rsp)
je 0x35dd
movq %rax, 0x78(%rsp)
movq 0x58(%rsp), %rax
cmpq %rax, 0x60(%rsp)
je 0x35ee
movq %rax, 0x60(%rsp)
leaq 0x98(%rsp), %r15
xorl %r13d, %r13d
movq 0x158(%rsp), %rcx
movq %rcx, %rdx
subq 0x138(%rsp), %rdx
shrq $0x3, %rdx
cmpq $0x1, %rcx
adcq $-0x1, %rdx
shlq $0x7, %rdx
movq 0x130(%rsp), %rsi
movq 0x140(%rsp), %rax
movq 0x148(%rsp), %rdi
movq %rax, %r8
subq %rdi, %r8
sarq $0x2, %r8
addq %rdx, %r8
subq 0x120(%rsp), %rsi
sarq $0x2, %rsi
addq %r8, %rsi
jne 0x369c
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, (%r15)
movb $0x0, 0x90(%rsp)
movl $0xffffffff, 0xc8(%rsp) # imm = 0xFFFFFFFF
movl %ebp, 0x8(%rsp)
movq 0xb8(%rsp), %rsi
cmpq 0xc0(%rsp), %rsi
je 0x36b1
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xb8(%rsp)
jmp 0x36c3
cmpq %rdi, %rax
je 0x36f2
movl -0x4(%rax), %ecx
addq $-0x4, %rax
movl %ecx, 0x4(%rsp)
jmp 0x3741
leaq 0xb0(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x56aa
movl %r14d, 0x8(%rsp)
movq 0xa0(%rsp), %rsi
cmpq 0xa8(%rsp), %rsi
je 0x3911
movl %r14d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xa0(%rsp)
jmp 0x391e
movq -0x8(%rcx), %rax
movl 0x1fc(%rax), %eax
movl %eax, 0x4(%rsp)
movl $0x200, %esi            # imm = 0x200
callq 0x21b0
movq 0x158(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x158(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x148(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x150(%rsp)
movl $0x1fc, %ecx            # imm = 0x1FC
addq %rcx, %rax
movq %rax, 0x140(%rsp)
movl 0x4(%rsp), %esi
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x4b7e
movzbl 0x10(%rsp), %eax
xorl $0x1, %eax
leaq 0xc3(,%rax,4), %rax
movq 0x20(%rsp), %rcx
subq 0x18(%rsp), %rcx
cmpq %rax, %rcx
jbe 0x38a5
leaq 0xd0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4ef2
leaq 0xd0(%rsp), %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x4cfa
leaq 0x10(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movl $0x19, %edx
callq 0x4f32
incl (%rbx)
movb $0x0, 0x50(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movl $0x19, %edx
callq 0x5066
movl (%rbx), %ebp
movb 0x50(%rsp), %al
movb %al, 0x1a8(%rsp)
leaq 0x1b0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x58ae
leaq 0x1c8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x58ae
movl 0x88(%rsp), %eax
movl %eax, 0x1e0(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x1a8(%rsp), %r14
movq %r14, %rdx
callq 0x4d5a
movq %r14, %rdi
callq 0x4c5e
movl 0x4(%rsp), %ebp
movb 0x10(%rsp), %al
movb %al, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x58ae
leaq 0x188(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x58ae
movl 0x48(%rsp), %eax
movl %eax, 0x1a0(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x168(%rsp), %r14
movq %r14, %rdx
callq 0x4d5a
movq %r14, %rdi
callq 0x4c5e
movq 0xf0(%rsp), %rax
movl 0x60(%rax), %ebp
movl (%rbx), %r12d
movl 0x4(%rsp), %r14d
jmp 0x39d0
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x4cfa
movl 0x4(%rsp), %r13d
movb 0x10(%rsp), %al
movb %al, 0x1e8(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x58ae
leaq 0x208(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x58ae
movl 0x48(%rsp), %eax
movl %eax, 0x220(%rsp)
movq %rbx, %rdi
movl %r13d, %esi
leaq 0x1e8(%rsp), %r13
movq %r13, %rdx
callq 0x4d5a
movq %r13, %rdi
jmp 0x39c8
movq %r15, %rdi
leaq 0x8(%rsp), %rdx
callq 0x56aa
movl %r12d, 0x8(%rsp)
movq 0xa0(%rsp), %rsi
cmpq 0xa8(%rsp), %rsi
je 0x3946
movl %r12d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xa0(%rsp)
jmp 0x3953
movq %r15, %rdi
leaq 0x8(%rsp), %rdx
callq 0x56aa
movl (%rbx), %r13d
incl %r13d
movl %r13d, (%rbx)
movb 0x90(%rsp), %al
movb %al, 0x228(%rsp)
leaq 0x230(%rsp), %rdi
movq %r15, %rsi
callq 0x58ae
leaq 0x248(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x58ae
movl 0xc8(%rsp), %eax
movl %eax, 0x260(%rsp)
movq %rbx, %rdi
movl %r13d, %esi
leaq 0x228(%rsp), %r13
movq %r13, %rdx
callq 0x4d5a
movq %r13, %rdi
callq 0x4c5e
movl (%rbx), %eax
movl %eax, 0x4(%rbx)
leaq 0x90(%rsp), %rdi
callq 0x4c5e
movb $0x1, %r13b
testb $0x1, %r13b
je 0x35f9
movq %rbx, %rdi
callq 0x4e1e
leaq 0x50(%rsp), %rdi
callq 0x4c5e
leaq 0xd0(%rsp), %rdi
callq 0x4c5e
movl $0x1, %r15d
jmp 0x3a75
leaq 0x10(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
callq 0x4cfa
movl 0x4(%rsp), %ebp
movb 0x10(%rsp), %al
leaq 0x2f0(%rsp), %rdi
movb %al, -0x8(%rdi)
leaq 0x18(%rsp), %rsi
callq 0x58ae
leaq 0x308(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x58ae
movl 0x48(%rsp), %eax
leaq 0x2e8(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4d5a
leaq 0x2e8(%rsp), %rdi
callq 0x4c5e
movl $0x1, %r15d
movq %rbx, %rdi
callq 0x4e1e
leaq 0x110(%rsp), %rdi
callq 0x5b02
leaq 0x10(%rsp), %rdi
callq 0x4c5e
movl %r15d, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x268(%rsp), %rdi
jmp 0x3c15
movq %rax, %rbx
movq 0x270(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1f
movq 0x280(%rsp), %rsi
jmp 0x3bca
movq %rax, %rbx
leaq 0x2a8(%rsp), %rdi
jmp 0x3c15
movq %rax, %rbx
movq 0x2b0(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1f
movq 0x2c0(%rsp), %rsi
jmp 0x3bca
movq %rax, %rbx
leaq 0x2e8(%rsp), %rdi
jmp 0x3c31
movq %rax, %rbx
movq 0x2f0(%rsp), %rdi
testq %rdi, %rdi
je 0x3c3b
movq 0x300(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x3c3b
movq %rax, %rbx
jmp 0x3c48
jmp 0x3c1c
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
jmp 0x3c15
movq %rax, %rbx
movq 0x1b0(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1f
movq 0x1c0(%rsp), %rsi
jmp 0x3bca
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
jmp 0x3c15
movq %rax, %rbx
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1f
movq 0x200(%rsp), %rsi
jmp 0x3bca
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0x3c15
movq %rax, %rbx
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1f
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x3c1f
jmp 0x3c38
movq %rax, %rbx
movq 0x230(%rsp), %rdi
testq %rdi, %rdi
je 0x3c0d
movq 0x240(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x3c0d
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x4c5e
jmp 0x3c0d
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x4c5e
jmp 0x3c1f
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4c5e
leaq 0xd0(%rsp), %rdi
callq 0x4c5e
jmp 0x3c3b
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x5b02
leaq 0x10(%rsp), %rdi
callq 0x4c5e
movq %rbx, %rdi
callq 0x2340