// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdi, %rbx
callq 0x19bd2
movq %rax, %r15
movq 0x58(%rax), %rax
subq 0x50(%r15), %rax
shrq $0x5, %rax
cmpl %eax, 0x90(%rbx)
jg 0x1ae3b
movl 0x6c(%r15), %esi
cmpl $-0x1, %esi
je 0x1ab85
movq %rbx, %rdi
callq 0x19bd2
movq %rax, %rbp
jmp 0x1abad
movl $0x70, %edi
callq 0x5390
movq %rax, %rbp
movq %rax, %rdi
callq 0x1cce0
movl 0x60(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x60(%rbx)
movl %eax, 0x18(%rbp)
movl %eax, 0x64(%rbx)
movb $0x0, 0x68(%rbp)
movl $0x70, %edi
callq 0x5390
movq %rax, %r13
movq %rax, %rdi
callq 0x1cce0
movl 0x60(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x60(%rbx)
movl %eax, 0x18(%r13)
movq 0x50(%r15), %rax
movq 0x58(%r15), %rcx
subq %rax, %rcx
sarq %rcx
andq $-0x20, %rcx
leaq (%rax,%rcx), %rsi
addq $0x8, %rsi
movl -0x8(%rsi), %eax
leaq 0x20(%rsp), %rdi
movl %eax, -0x8(%rdi)
callq 0xa450
movq 0x20(%rbp), %r14
movq 0x28(%rbp), %rcx
subq %r14, %rcx
je 0x1ac1d
sarq $0x2, %rcx
movl 0x18(%r15), %edx
xorl %eax, %eax
cmpl %edx, (%r14,%rax,4)
je 0x1ac1f
incl %eax
cmpq %rax, %rcx
ja 0x1ac10
xorl %eax, %eax
leaq 0x50(%rbp), %rdi
movl %eax, %r12d
movq %r12, %rsi
shlq $0x5, %rsi
addq 0x50(%rbp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x1ce06
leaq 0x20(%rbp), %rdi
leaq (%r14,%r12,4), %rsi
leaq 0x14(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x1dd72
movl 0x18(%r15), %eax
movq 0x20(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
movl 0x18(%r13), %eax
leal 0x1(%r12), %edx
movl %eax, (%rcx,%rdx,4)
movl 0x18(%rbp), %eax
movl %eax, 0x6c(%r15)
movl %eax, 0x6c(%r13)
movq 0x20(%r13), %rax
cmpq %rax, 0x28(%r13)
je 0x1ac84
movq %rax, 0x28(%r13)
movq 0x50(%r13), %r14
movq 0x58(%r13), %r12
cmpq %r14, %r12
movq %rbp, (%rsp)
je 0x1acae
movq %r14, %rbp
leaq 0x8(%rbp), %rdi
callq 0xa564
addq $0x20, %rbp
cmpq %r12, %rbp
jne 0x1ac98
movq %r14, 0x58(%r13)
leaq 0x20(%r13), %rdi
movq 0x20(%r15), %rax
movq 0x58(%r15), %rcx
subq 0x50(%r15), %rcx
sarq $0x4, %rcx
andq $-0x4, %rcx
leaq (%rax,%rcx), %rdx
addq $0x4, %rdx
movq 0x28(%r13), %rsi
cmpq 0x30(%r13), %rsi
movq %rdi, 0x8(%rsp)
je 0x1aceb
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r13)
jmp 0x1acf0
callq 0x1db1c
movq 0x58(%r15), %rax
subq 0x50(%r15), %rax
movq %rax, %rcx
shrq $0x6, %rcx
leal 0x1(%rcx), %r14d
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x1adea
movq %r13, %r12
addq $0x50, %r12
addl $0x2, %ecx
movl %ecx, %ebp
leaq (,%rbp,4), %rdx
addq 0x20(%r15), %rdx
movq 0x28(%r13), %rsi
cmpq 0x30(%r13), %rsi
je 0x1ad40
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r13)
jmp 0x1ad4a
movq 0x8(%rsp), %rdi
callq 0x1db1c
shlq $0x5, %r14
addq 0x50(%r15), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x1cdce
movq 0x20(%r15), %rax
movl (%rax,%rbp,4), %esi
cmpl $-0x1, %esi
je 0x1adcf
movq %rbx, %rdi
callq 0x19bd2
movq %rax, %r14
movl 0x18(%r13), %eax
movl %eax, 0x6c(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a022
testq %r14, %r14
je 0x1adcf
movq %r14, %rdi
addq $0x50, %rdi
callq 0x1cebc
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x1adad
movq 0x48(%r14), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0x1adc2
movq 0x30(%r14), %rsi
subq %rdi, %rsi
callq 0x53a0
movl $0x70, %esi
movq %r14, %rdi
callq 0x53a0
movq 0x58(%r15), %rax
subq 0x50(%r15), %rax
sarq $0x5, %rax
leal 0x1(%rbp), %ecx
movq %rbp, %r14
cmpq %rbp, %rax
ja 0x1ad1a
movq %rax, %rcx
shrq %rcx
movl %ecx, 0x1c(%r15)
notl %ecx
addl %ecx, %eax
movl %eax, 0x1c(%r13)
movq (%rsp), %r14
incl 0x1c(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a022
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a022
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a022
movl 0x18(%r14), %esi
movq %rbx, %rdi
callq 0x1ab3e
leaq 0x20(%rsp), %rdi
callq 0xa564
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x70, %esi
movq %rbp, %rdi
jmp 0x1ae64
jmp 0x1ae71
movq %rax, %rbx
movl $0x70, %esi
movq %r13, %rdi
callq 0x53a0
jmp 0x1ae7e
jmp 0x1ae71
jmp 0x1ae71
jmp 0x1ae71
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xa564
movq %rbx, %rdi
callq 0x56e0