// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
callq 0x1586c
movq %rax, %r15
movq 0x58(%rax), %rax
subq 0x50(%r15), %rax
shrq $0x5, %rax
cmpl %eax, 0x90(%rbx)
jg 0x16749
movl 0x6c(%r15), %esi
cmpl $-0x1, %esi
je 0x16503
movq %rbx, %rdi
callq 0x1586c
movq %rax, %rbp
jmp 0x16529
pushq $0x70
popq %rdi
callq 0x53e0
movq %rax, %rbp
movq %rax, %rdi
callq 0x1803a
movl 0x60(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x60(%rbx)
movl %eax, 0x18(%rbp)
movl %eax, 0x64(%rbx)
movb $0x0, 0x68(%rbp)
pushq $0x70
popq %rdi
callq 0x53e0
movq %rax, %r14
movq %rax, %rdi
callq 0x1803a
movl 0x60(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x60(%rbx)
movq %r14, 0x8(%rsp)
movl %eax, 0x18(%r14)
movq 0x50(%r15), %rax
movq 0x58(%r15), %rsi
subq %rax, %rsi
sarq %rsi
andq $-0x20, %rsi
addq %rax, %rsi
leaq 0x28(%rsp), %rdi
callq 0x18028
leaq 0x20(%rbp), %r14
movq 0x20(%rbp), %r12
movq 0x28(%rbp), %rcx
subq %r12, %rcx
sarq $0x2, %rcx
xorl %eax, %eax
xorl %edx, %edx
movl %edx, %esi
cmpq %rsi, %rcx
jbe 0x1659b
movl (%r12,%rsi,4), %esi
cmpl 0x18(%r15), %esi
je 0x16599
incl %edx
jmp 0x16584
movl %edx, %eax
leaq 0x50(%rbp), %rdi
movl %eax, %r13d
movq %r13, %rsi
shlq $0x5, %rsi
movq %rbp, 0x10(%rsp)
addq 0x50(%rbp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x18180
leaq (%r12,%r13,4), %rsi
leaq 0x24(%rsp), %rdx
orl $-0x1, (%rdx)
movq %r14, %rdi
callq 0x19262
movl 0x18(%r15), %eax
movq 0x10(%rsp), %rsi
movq 0x20(%rsi), %rcx
movl %eax, (%rcx,%r13,4)
movq 0x8(%rsp), %rbp
movl 0x18(%rbp), %eax
leal 0x1(%r13), %edx
movl %eax, (%rcx,%rdx,4)
movl 0x18(%rsi), %eax
movl %eax, 0x6c(%r15)
movl %eax, 0x6c(%rbp)
leaq 0x20(%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rbp), %rax
cmpq %rax, 0x28(%rbp)
je 0x16611
movq %rax, 0x28(%rbp)
addq $0x50, %rbp
movq %rbp, %rdi
callq 0x180ca
movq 0x58(%r15), %rax
subq 0x50(%r15), %rax
movq 0x20(%r15), %rcx
sarq $0x4, %rax
andq $-0x4, %rax
leaq (%rcx,%rax), %rsi
addq $0x4, %rsi
movq 0x18(%rsp), %rdi
callq 0x180fe
movq 0x50(%r15), %rcx
movq 0x58(%r15), %rax
movq %rax, %r12
subq %rcx, %r12
shrq $0x6, %r12
addl $0x2, %r12d
subq %rcx, %rax
leal -0x1(%r12), %r14d
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x166f1
movq %rbx, %r13
movl %r12d, %ebx
leaq (,%rbx,4), %rsi
addq 0x20(%r15), %rsi
movq 0x18(%rsp), %rdi
callq 0x180fe
shlq $0x5, %r14
addq 0x50(%r15), %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x180d2
movq 0x20(%r15), %rax
movl (%rax,%rbx,4), %esi
cmpl $-0x1, %esi
movq %r13, %rbx
je 0x166e1
movq %rbx, %rdi
callq 0x1586c
movq %rax, %r14
movq 0x8(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x6c(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15bec
movq %r14, %rdi
callq 0x1811c
movq %r14, %rdi
pushq $0x70
popq %rsi
callq 0x53f0
movq 0x50(%r15), %rcx
movq 0x58(%r15), %rax
incl %r12d
jmp 0x16659
movq %rax, %rcx
shrq %rcx
movl %ecx, 0x1c(%r15)
notl %ecx
addl %ecx, %eax
movq 0x8(%rsp), %r12
movl %eax, 0x1c(%r12)
movq 0x10(%rsp), %r14
incl 0x1c(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %r12, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15bec
movl 0x18(%r14), %esi
movq %rbx, %rdi
callq 0x164bc
leaq 0x30(%rsp), %rdi
callq 0x9268
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %rbp, %rdi
jmp 0x1676e
jmp 0x1677b
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %r14, %rdi
callq 0x53f0
jmp 0x16788
jmp 0x1677b
jmp 0x1677b
jmp 0x1677b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x9268
movq %rbx, %rdi
callq 0x56f0