// File: Konano[P]UselessDatabase/Index.cpp
// Function: Index::overflow_upstream(int)
void Index::overflow_upstream(int now_index) {

    BtreeNode* now = Index::convert_buf_to_BtreeNode(now_index);

    //cout << "up" << now->index << " "  << now->record_cnt << endl;
    if ((int)now->record.size() <= btree_max_per_node - 1) {
        return;
    }
    else {

        BtreeNode* fa;

        if (now->fa_index == -1) {
            fa = new BtreeNode();
            fa->index = page_num++;
            root_page = page_num - 1;
            fa->is_leaf = false;
        }
        else{
            fa = Index::convert_buf_to_BtreeNode(now->fa_index);
        }
        BtreeNode* right = new BtreeNode();
        right->index = page_num++;

        BtreeRecord mid = now->record[now->record.size() >> 1];

        uint loc = 0;
        for(uint i = 0;i < fa->child.size();i ++){
            if(fa->child[i] == now->index){
                loc = i;
                break;
            }
        }

        vector<BtreeRecord>::iterator it = fa->record.begin() + loc;
        vector<int>::iterator itx = fa->child.begin() + loc;

        fa->record.insert(it, mid);
        fa->child.insert(itx, -1);
        fa->child[loc] = now->index;
        fa->child[loc + 1] = right->index;
        now->fa_index = fa->index;
        right->fa_index = fa->index;
        right->child.clear();
        right->record.clear();
        right->child.push_back(now->child[(now->record.size() >> 1) + 1]);
        for (uint i = (now->record.size() >> 1) + 1; i < now->record.size(); i++) {
            right->child.push_back(now->child[i + 1]);
            right->record.push_back(now->record[i]);
            if(now->child[i + 1] != -1){
                BtreeNode* temp = Index::convert_buf_to_BtreeNode(now->child[i + 1]);
                temp->fa_index = right->index;
                Index::convert_BtreeNode_to_buf(temp);
                delete temp;
            }
        }

        int left_size = (now->record.size() >> 1);
        int right_size = now->record.size() - left_size - 1;

        now->record_cnt = left_size;
        right->record_cnt = right_size;
        fa->record_cnt ++;

        Index::convert_BtreeNode_to_buf(now);
        Index::convert_BtreeNode_to_buf(right);
        Index::convert_BtreeNode_to_buf(fa);
        overflow_upstream(fa->index);     
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
movl -0xc(%rbp), %esi
callq 0x36fd0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d880
movq -0xc8(%rbp), %rcx
movl 0x90(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0x38b1f
jmp 0x390c9
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x6c(%rax)
jne 0x38ba0
movl $0x70, %edi
callq 0x64a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
callq 0x3d5a0
jmp 0x38b47
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq %rcx, -0x20(%rbp)
movl 0x60(%rax), %edx
movl %edx, %ecx
addl $0x1, %ecx
movl %ecx, 0x60(%rax)
movq -0x20(%rbp), %rcx
movl %edx, 0x18(%rcx)
movl 0x60(%rax), %ecx
subl $0x1, %ecx
movl %ecx, 0x64(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x68(%rax)
jmp 0x38bb7
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x70, %esi
callq 0x64b0
jmp 0x390d2
movq -0xc8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x6c(%rax), %esi
callq 0x36fd0
movq %rax, -0x20(%rbp)
movl $0x70, %edi
callq 0x64a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd8(%rbp)
callq 0x3d5a0
jmp 0x38bd5
movq -0xc8(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movl 0x60(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x60(%rax)
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
addq $0x50, %rax
movq %rax, -0xe0(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d880
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
shrq %rsi
callq 0x3d7a0
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x3d520
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq %rax, -0xe8(%rbp)
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0x3d860
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jae 0x38cbb
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
movl -0x60(%rbp), %eax
movl %eax, %esi
callq 0x3d6f0
movl (%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x38cae
movl -0x60(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x38cbb
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x70, %esi
callq 0x64b0
jmp 0x390d2
jmp 0x38cb0
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x38c41
movq -0x20(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d460
movq %rax, -0x70(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x3ddd0
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0xd0f0
movq %rax, -0x80(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, %esi
leaq -0x80(%rbp), %rdi
callq 0x3de10
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
addq $0x50, %rax
movq %rax, -0xf8(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3d9d0
movq -0xf8(%rbp), %rdi
movq -0x88(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x3dac0
movq %rax, -0xf0(%rbp)
jmp 0x38d40
movq -0xf0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x108(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x3da80
movq -0x108(%rbp), %rdi
movl $0xffffffff, -0x9c(%rbp) # imm = 0xFFFFFFFF
movq -0x98(%rbp), %rsi
leaq -0x9c(%rbp), %rdx
callq 0x3dcd0
movq %rax, -0x100(%rbp)
jmp 0x38d9a
movq -0x100(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x120(%rbp)
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
callq 0x3d6f0
movl -0x120(%rbp), %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x11c(%rbp)
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
movl -0x5c(%rbp), %eax
incl %eax
movl %eax, %esi
callq 0x3d6f0
movl -0x11c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x38(%rbp), %rdi
addq $0x20, %rdi
callq 0x3d660
movq -0x38(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d6a0
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x110(%rbp)
movq -0x18(%rbp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, -0x118(%rbp)
addq $0x50, %rdi
callq 0x3d880
movq -0x118(%rbp), %rdi
movq %rax, %rsi
shrq %rsi
incq %rsi
callq 0x3d6f0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x3d7f0
jmp 0x38e7e
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d880
shrq %rax
addq $0x1, %rax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movq %rax, -0x128(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d880
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x39019
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x130(%rbp)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movl -0xac(%rbp), %eax
incl %eax
movl %eax, %esi
callq 0x3d6f0
movq -0x130(%rbp), %rdi
movq %rax, %rsi
callq 0x3d7f0
jmp 0x38efc
movq -0x38(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x138(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
movl -0xac(%rbp), %eax
movl %eax, %esi
callq 0x3d7a0
movq -0x138(%rbp), %rdi
movq %rax, %rsi
callq 0x3d730
jmp 0x38f31
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3d6f0
cmpl $-0x1, (%rax)
je 0x39003
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movl -0xac(%rbp), %eax
incl %eax
movl %eax, %esi
callq 0x3d6f0
movq -0xc8(%rbp), %rdi
movl (%rax), %esi
callq 0x36fd0
movq %rax, -0x140(%rbp)
jmp 0x38f82
movq -0xc8(%rbp), %rdi
movq -0x140(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0xb8(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0xb8(%rbp), %rsi
callq 0x376a0
jmp 0x38fb6
movq -0xb8(%rbp), %rax
movq %rax, -0x148(%rbp)
cmpq $0x0, %rax
je 0x38fe7
movq -0x148(%rbp), %rdi
callq 0x3d8a0
movq -0x148(%rbp), %rdi
movl $0x70, %esi
callq 0x64b0
jmp 0x39003
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3d560
jmp 0x390d2
jmp 0x39005
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x38e98
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d880
shrq %rax
movl %eax, -0xbc(%rbp)
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d880
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
movl -0xbc(%rbp), %eax
notl %eax
addl %ecx, %eax
movl %eax, -0xc0(%rbp)
movl -0xbc(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl -0xc0(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %ecx
incl %ecx
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rsi
callq 0x376a0
jmp 0x39087
movq -0xc8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x376a0
jmp 0x39099
movq -0xc8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x376a0
jmp 0x390ab
movq -0xc8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x18(%rax), %esi
callq 0x38ad0
jmp 0x390c0
leaq -0x58(%rbp), %rdi
callq 0x3d560
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x6890
nopl (%rax,%rax)