// File: /workspace/github_cpp_makefile_star2_O3/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 0x19412
movq %rax, %r15
movq 0x58(%rax), %rax
subq 0x50(%r15), %rax
shrq $0x5, %rax
cmpl %eax, 0x90(%rbx)
jg 0x1a67e
movl 0x6c(%r15), %esi
cmpl $-0x1, %esi
je 0x1a3af
movq %rbx, %rdi
callq 0x19412
movq %rax, %rbp
jmp 0x1a3d7
movl $0x70, %edi
callq 0x5390
movq %rax, %rbp
movq %rax, %rdi
callq 0x1c61a
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 0x1c61a
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 0xa26a
movq 0x20(%rbp), %r14
movq 0x28(%rbp), %rcx
subq %r14, %rcx
je 0x1a447
sarq $0x2, %rcx
movl 0x18(%r15), %edx
xorl %eax, %eax
cmpl %edx, (%r14,%rax,4)
je 0x1a449
incl %eax
cmpq %rax, %rcx
ja 0x1a43a
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 0x1c6e4
leaq 0x20(%rbp), %rdi
leaq (%r14,%r12,4), %rsi
leaq 0x14(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x1d6ee
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
movq 0x28(%r13), %rsi
cmpq %rax, %rsi
je 0x1a4b4
movq %rax, 0x28(%r13)
movq %rax, %rsi
movq 0x50(%r13), %r14
movq 0x58(%r13), %r12
cmpq %r14, %r12
movq %rbp, (%rsp)
je 0x1a4e2
movq %r14, %rbp
leaq 0x8(%rbp), %rdi
callq 0xa37e
addq $0x20, %rbp
cmpq %r12, %rbp
jne 0x1a4c8
movq %r14, 0x58(%r13)
movq 0x28(%r13), %rsi
leaq 0x20(%r13), %r14
movq 0x58(%r15), %rcx
movq 0x20(%r15), %rdx
movq 0x50(%r15), %rax
subq %rax, %rcx
movq %rcx, %rdi
sarq $0x4, %rdi
andq $-0x4, %rdi
addq %rdi, %rdx
addq $0x4, %rdx
cmpq 0x30(%r13), %rsi
movq %r14, 0x8(%rsp)
je 0x1a527
movq %rcx, %rdi
sarq $0x5, %rdi
movl (%rdx), %edx
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r13)
jmp 0x1a541
movq %r14, %rdi
callq 0x1d42c
movq 0x50(%r15), %rax
movq 0x58(%r15), %rcx
subq %rax, %rcx
movq %rcx, %rdi
sarq $0x5, %rdi
shrq $0x6, %rcx
leal 0x1(%rcx), %r14d
cmpq %r14, %rdi
jbe 0x1a62d
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 0x1a582
movl (%rdx), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r13)
jmp 0x1a590
movq 0x8(%rsp), %rdi
callq 0x1d42c
movq 0x50(%r15), %rax
shlq $0x5, %r14
addq %rax, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x1c6ac
movq 0x20(%r15), %rax
movl (%rax,%rbp,4), %esi
cmpl $-0x1, %esi
je 0x1a60f
movq %rbx, %rdi
callq 0x19412
movq %rax, %r14
movl 0x18(%r13), %eax
movl %eax, 0x6c(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1984c
movq %r14, %rdi
addq $0x50, %rdi
callq 0x1c79a
movq 0x38(%r14), %rdi
testq %rdi, %rdi
je 0x1a5ed
movq 0x48(%r14), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0x1a602
movq 0x30(%r14), %rsi
subq %rdi, %rsi
callq 0x53a0
movl $0x70, %esi
movq %r14, %rdi
callq 0x53a0
movq 0x50(%r15), %rax
movq 0x58(%r15), %rdi
subq %rax, %rdi
sarq $0x5, %rdi
leal 0x1(%rbp), %ecx
movq %rbp, %r14
cmpq %rbp, %rdi
ja 0x1a55c
movq %rdi, %rax
shrq %rax
movl %eax, 0x1c(%r15)
notl %eax
addl %eax, %edi
movl %edi, 0x1c(%r13)
movq (%rsp), %r14
incl 0x1c(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1984c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1984c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1984c
movl 0x18(%r14), %esi
movq %rbx, %rdi
callq 0x1a368
leaq 0x20(%rsp), %rdi
callq 0xa37e
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 0x1a6a7
jmp 0x1a6b4
movq %rax, %rbx
movl $0x70, %esi
movq %r13, %rdi
callq 0x53a0
jmp 0x1a6c1
jmp 0x1a6b4
jmp 0x1a6b4
jmp 0x1a6b4
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xa37e
movq %rbx, %rdi
callq 0x56e0
nop