// File: /workspace/github_cpp_makefile_star2_O3/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), %r15d
movl %r15d, 0x4(%rsp)
leaq 0xd0(%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 0x5b18
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4b24
leaq 0xd0(%rsp), %r15
leaq 0x4(%rsp), %r12
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
leaq 0x10(%rsp), %r13
cmpb $0x0, 0x10(%rsp)
jne 0x33b7
movq 0x100(%rsp), %rax
movq 0x110(%rsp), %rcx
addq $-0x4, %rcx
cmpq %rcx, %rax
je 0x330f
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
addq $0x4, %rax
movq %rax, 0x100(%rsp)
jmp 0x331a
movq %r15, %rdi
movq %r12, %rsi
callq 0x5c8e
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
cmpl %r14d, (%rcx)
jge 0x33a1
movq 0x20(%rsp), %rdx
subq %rax, %rdx
shlq $0x1e, %rdx
leaq (%rdx,%rbp), %rsi
sarq $0x20, %rsi
cmpl %r14d, -0x4(%rcx,%rsi,4)
jl 0x3397
movq 0x38(%rsp), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
movq %rcx, %rdx
testq %rsi, %rsi
jle 0x3383
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 0x3358
subq %rcx, %rdx
shlq $0x1e, %rdx
movabsq $0x100000000, %rcx      # imm = 0x100000000
addq %rcx, %rdx
sarq $0x20, %rdx
movl -0x4(%rax,%rdx,4), %esi
jmp 0x33a3
movl (%rax), %esi
movl %esi, 0x4(%rsp)
movq %rbx, %rdi
movq %r13, %rdx
callq 0x4b24
jmp 0x32d7
leaq 0x10(%rsp), %rdi
movl %r14d, %esi
callq 0x4c50
movl $0x2, %r15d
testl %eax, %eax
jne 0x39e6
movq 0x20(%rsp), %rax
subq 0x18(%rsp), %rax
movzbl 0x10(%rsp), %ecx
xorl $0x1, %ecx
leaq 0xc3(,%rcx,4), %rcx
cmpq %rcx, %rax
jbe 0x37b7
xorps %xmm0, %xmm0
leaq 0x90(%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 0x4e9a
leaq 0x90(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
callq 0x4ca2
leaq 0x10(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movl $0x1a, %edx
callq 0x4eda
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 0x90(%rsp), %rsi
movl $0x1a, %edx
callq 0x5014
leaq 0x58(%rsp), %rsi
movq 0xb0(%rsp), %rax
movl 0x64(%rax), %r14d
movl (%rbx), %ebp
movl 0x4(%rsp), %r15d
movb -0x8(%rsi), %al
leaq 0x2b0(%rsp), %rdi
movb %al, -0x8(%rdi)
callq 0x5862
leaq 0x2c8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x5862
movl 0x88(%rsp), %eax
leaq 0x2a8(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4d02
leaq 0x2a8(%rsp), %rdi
callq 0x4c04
movl 0x4(%rsp), %r12d
movb 0x10(%rsp), %al
leaq 0x270(%rsp), %rdi
movb %al, -0x8(%rdi)
leaq 0x18(%rsp), %rsi
callq 0x5862
leaq 0x288(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5862
movl 0x48(%rsp), %eax
leaq 0x268(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x4d02
leaq 0x268(%rsp), %rdi
callq 0x4c04
movq 0xb0(%rsp), %rax
cmpq %rax, 0xb8(%rsp)
je 0x3566
movq %rax, 0xb8(%rsp)
movq 0x98(%rsp), %rax
cmpq %rax, 0xa0(%rsp)
je 0x3580
movq %rax, 0xa0(%rsp)
movq 0x70(%rsp), %rax
cmpq %rax, 0x78(%rsp)
je 0x3591
movq %rax, 0x78(%rsp)
movq 0x58(%rsp), %rax
cmpq %rax, 0x60(%rsp)
je 0x35a2
movq %rax, 0x60(%rsp)
leaq 0x10(%rsp), %r13
leaq 0x90(%rsp), %r12
movq 0x118(%rsp), %rcx
movq %rcx, %rdx
subq 0xf8(%rsp), %rdx
shrq $0x3, %rdx
cmpq $0x1, %rcx
adcq $-0x1, %rdx
shlq $0x7, %rdx
movq 0xf0(%rsp), %rsi
movq 0x100(%rsp), %rax
movq 0x108(%rsp), %rdi
movq %rax, %r8
subq %rdi, %r8
sarq $0x2, %r8
addq %rdx, %r8
subq 0xe0(%rsp), %rsi
sarq $0x2, %rsi
addq %r8, %rsi
je 0x3830
cmpq %rdi, %rax
je 0x361e
movl -0x4(%rax), %esi
addq $-0x4, %rax
movl %esi, 0x4(%rsp)
jmp 0x3671
movq -0x8(%rcx), %rax
movl 0x1fc(%rax), %eax
movl %eax, 0x4(%rsp)
movl $0x200, %esi            # imm = 0x200
callq 0x21b0
movq 0x118(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x118(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x108(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x110(%rsp)
movl $0x1fc, %ecx            # imm = 0x1FC
addq %rcx, %rax
movl 0x4(%rsp), %esi
movq %rax, 0x100(%rsp)
movq %rbx, %rdi
movq %r13, %rdx
callq 0x4b24
movq 0x20(%rsp), %rax
subq 0x18(%rsp), %rax
movzbl 0x10(%rsp), %ecx
xorl $0x1, %ecx
leaq 0xc3(,%rcx,4), %rcx
cmpq %rcx, %rax
jbe 0x38a7
movq %r12, %rdi
movq %r13, %rsi
callq 0x4e9a
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x4ca2
movq %r13, %rdi
movq %r12, %rsi
movl $0x19, %edx
callq 0x4eda
incl (%rbx)
movb $0x0, 0x50(%rsp)
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
movl $0x19, %edx
callq 0x5014
movl (%rbx), %ebp
movb 0x50(%rsp), %al
movb %al, 0x1a8(%rsp)
leaq 0x1b0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5862
leaq 0x1c8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x5862
movl 0x88(%rsp), %eax
movl %eax, 0x1e0(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x1a8(%rsp), %r14
movq %r14, %rdx
callq 0x4d02
movq %r14, %rdi
callq 0x4c04
movl 0x4(%rsp), %ebp
movb 0x10(%rsp), %al
movb %al, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x5862
leaq 0x188(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5862
movl 0x48(%rsp), %eax
movl %eax, 0x1a0(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x168(%rsp), %r14
movq %r14, %rdx
callq 0x4d02
movq %r14, %rdi
callq 0x4c04
movq 0xb0(%rsp), %rax
movl 0x60(%rax), %r14d
movl (%rbx), %ebp
movl 0x4(%rsp), %r15d
jmp 0x35af
leaq 0x10(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
callq 0x4ca2
movl 0x4(%rsp), %ebp
movb 0x10(%rsp), %al
leaq 0x2f0(%rsp), %rdi
movb %al, -0x8(%rdi)
leaq 0x18(%rsp), %rsi
callq 0x5862
leaq 0x308(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5862
movl 0x48(%rsp), %eax
leaq 0x2e8(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4d02
leaq 0x2e8(%rsp), %rdi
callq 0x4c04
movl $0x1, %r15d
movq %rbx, %rdi
callq 0x4dc6
jmp 0x39e6
xorps %xmm0, %xmm0
leaq 0x130(%rsp), %r12
movups %xmm0, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
movb $0x0, 0x128(%rsp)
movl $0xffffffff, 0x160(%rsp) # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %rdx
movl %r14d, (%rdx)
leaq 0x148(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x565e
movq 0x138(%rsp), %rsi
movq 0x140(%rsp), %rax
movl %r15d, 0x8(%rsp)
cmpq %rax, %rsi
je 0x390f
movl %r15d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x138(%rsp)
jmp 0x392c
leaq 0x10(%rsp), %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x4ca2
movl 0x4(%rsp), %ebp
movb 0x10(%rsp), %al
movb %al, 0x1e8(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x5862
leaq 0x208(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5862
movl 0x48(%rsp), %eax
leaq 0x1e8(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4d02
leaq 0x1e8(%rsp), %rdi
jmp 0x39bc
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x565e
movq 0x138(%rsp), %rsi
movq 0x140(%rsp), %rax
movl %ebp, 0x8(%rsp)
cmpq %rax, %rsi
je 0x3945
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x138(%rsp)
jmp 0x3952
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x565e
movl (%rbx), %ebp
incl %ebp
movl %ebp, (%rbx)
movb 0x128(%rsp), %al
movb %al, 0x228(%rsp)
leaq 0x230(%rsp), %rdi
movq %r12, %rsi
callq 0x5862
leaq 0x248(%rsp), %rdi
movq %r14, %rsi
callq 0x5862
movl 0x160(%rsp), %eax
leaq 0x228(%rsp), %rdx
movl %eax, 0x38(%rdx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4d02
leaq 0x228(%rsp), %rdi
callq 0x4c04
movl (%rbx), %eax
movl %eax, 0x4(%rbx)
leaq 0x128(%rsp), %rdi
callq 0x4c04
movq %rbx, %rdi
callq 0x4dc6
leaq 0x50(%rsp), %rdi
callq 0x4c04
leaq 0x90(%rsp), %rdi
callq 0x4c04
movl $0x1, %r15d
leaq 0xd0(%rsp), %rdi
callq 0x5ac0
leaq 0x10(%rsp), %rdi
callq 0x4c04
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 0x1e8(%rsp), %rdi
jmp 0x3b6c
movq %rax, %rbx
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0x3b98
movq 0x200(%rsp), %rsi
jmp 0x3b8b
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x4c04
jmp 0x3b24
movq %rax, %rbx
movq 0x230(%rsp), %rdi
testq %rdi, %rdi
je 0x3b24
movq 0x240(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x3b24
jmp 0x3b95
movq %rax, %rbx
leaq 0x268(%rsp), %rdi
jmp 0x3b6c
movq %rax, %rbx
movq 0x270(%rsp), %rdi
testq %rdi, %rdi
je 0x3b98
movq 0x280(%rsp), %rsi
jmp 0x3b8b
movq %rax, %rbx
leaq 0x2a8(%rsp), %rdi
jmp 0x3b6c
movq %rax, %rbx
movq 0x2b0(%rsp), %rdi
testq %rdi, %rdi
je 0x3b98
movq 0x2c0(%rsp), %rsi
jmp 0x3b8b
movq %rax, %rbx
leaq 0x2e8(%rsp), %rdi
jmp 0x3baa
movq %rax, %rbx
movq 0x2f0(%rsp), %rdi
testq %rdi, %rdi
je 0x3bb4
movq 0x300(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x3bb4
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x3b6c
movq %rax, %rbx
jmp 0x3bc1
jmp 0x3b95
jmp 0x3bb1
movq %rax, %rbx
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x3b98
movq 0x180(%rsp), %rsi
jmp 0x3b8b
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
jmp 0x3b6c
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x4c04
jmp 0x3b98
movq %rax, %rbx
movq 0x1b0(%rsp), %rdi
testq %rdi, %rdi
je 0x3b98
movq 0x1c0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x3b98
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4c04
leaq 0x90(%rsp), %rdi
callq 0x4c04
jmp 0x3bb4
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x5ac0
leaq 0x10(%rsp), %rdi
callq 0x4c04
movq %rbx, %rdi
callq 0x2340