// File: /workspace/github_cpp_makefile_star2_O2/Konano[P]UselessDatabase/Index.cpp
// Function: Index::overflow_downstream(int)
void Index::overflow_downstream(int now_index) {
    //cout << "down" << now->index << " "  << now->record_cnt << endl;

    BtreeNode* now = Index::convert_buf_to_BtreeNode(now_index);

    if (now->fa_index == -1) {
        if (now->record_cnt != 0) 
            return;
        else {
            if (now->child[0] == -1) return;
            root_page = now->child[0];
            return;
        }
    }
    else if ((int)now->record.size() >= ceil(btree_max_per_node / 2.0) - 1) {
        return;
    }
    else {
        BtreeNode* fa = Index::convert_buf_to_BtreeNode(now->fa_index);
        uint i;
        for (i = 0; i < fa->child.size(); i++) {
            if (fa->child[i] == now->index) {
                break;
            }
        }
        if (i != 0) {
            BtreeNode* left = Index::convert_buf_to_BtreeNode(fa->child[i - 1]);
            if (left->record_cnt <= ceil(btree_max_per_node / 2.0) - 1) {
                left->record.push_back(fa->record[i - 1]);
                for (int j = 0; j < now->record_cnt; j++) {
                    left->record.push_back(now->record[j]);
                    left->child.push_back(now->child[j]);
                    if(now->child[j] != -1){
                        BtreeNode* temp = Index::convert_buf_to_BtreeNode(now->child[j]);
                        temp->fa_index = left->index;
                        Index::convert_BtreeNode_to_buf(temp);
                        delete temp;
                    }
                }
                left->record_cnt += 1 + now->record_cnt;
                left->child.push_back(now->child[now->child.size() - 1]);
                vector<BtreeRecord>::iterator it = fa->record.begin();
                vector<int>::iterator itx = fa->child.begin();
                itx++;
                uint temp = i;
                while (--temp) {it ++; itx++; }
                fa->record.erase(it);
                fa->child.erase(itx);
                fa->record_cnt--;
                Index::convert_BtreeNode_to_buf(now);
                Index::convert_BtreeNode_to_buf(left);       
                Index::convert_BtreeNode_to_buf(fa);
                overflow_downstream(fa->index); 
            }
            else {
                now->record.insert(now->record.begin(), fa->record[i - 1]);
                now->child.insert(now->child.begin(), -1);
                now->record_cnt ++;

                fa->record[i - 1] = left->record[left->record.size() - 1];

                left->record_cnt --;
                left->record.erase(left->record.end() - 1);
                left->child.erase(left->child.end() - 1);
                Index::convert_BtreeNode_to_buf(now);
                Index::convert_BtreeNode_to_buf(left);
                Index::convert_BtreeNode_to_buf(fa);
            }
        }
        else {
            BtreeNode* right = Index::convert_buf_to_BtreeNode(fa->child[i + 1]);
            if (right->record_cnt <= ceil(btree_max_per_node / 2.0) - 1) {
                now->record.push_back(fa->record[i]);
                for (int j = 0; j < right->record_cnt; j++) {
                    now->record.push_back(right->record[j]);
                    now->child.push_back(right->child[j]);
                    if(right->child[j] != -1){
                        BtreeNode* temp = Index::convert_buf_to_BtreeNode(right->child[j]);
                        temp->fa_index = now->index;
                        Index::convert_BtreeNode_to_buf(temp);
                        delete temp;
                    }
                }
                now->record_cnt += 1 + right->record_cnt;
                now->child.push_back(right->child[right->child.size() - 1]);
                vector<BtreeRecord>::iterator it = fa->record.begin();
                vector<int>::iterator itx = fa->child.begin();
                uint temp = i;
                itx ++;
                while (temp--) {it ++; itx++; }
                fa->record.erase(it);
                fa->child.erase(itx);
                fa->record_cnt--;
                Index::convert_BtreeNode_to_buf(now);
                Index::convert_BtreeNode_to_buf(right);        
                Index::convert_BtreeNode_to_buf(fa);
                overflow_downstream(fa->index);
            }
            else {
                now->record.insert(now->record.begin(), fa->record[i - 1]);
                now->child.insert(now->child.begin(), -1);
                now->record_cnt ++;

                fa->record[i - 1] = right->record[0];

                right->record_cnt --;
                right->record.erase(right->record.begin());
                right->child.erase(right->child.begin());
                Index::convert_BtreeNode_to_buf(now);
                Index::convert_BtreeNode_to_buf(right);
                Index::convert_BtreeNode_to_buf(fa);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq %rdi, 0x18(%rsp)
movq %rbx, %rdi
callq 0x1586c
movq %rax, %r15
movl 0x6c(%rax), %ebp
cmpl $-0x1, %ebp
je 0x1636e
movq 0x58(%r15), %rax
subq 0x50(%r15), %rax
shrq $0x5, %rax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
cvtsi2sdl 0x90(%rbx), %xmm0
mulsd 0x21bb9(%rip), %xmm0    # 0x37bb0
callq 0x50c0
addsd 0x21bb4(%rip), %xmm0    # 0x37bb8
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x164ac
leaq 0x50(%r15), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1586c
movq %rax, %r14
leaq 0x20(%rax), %r12
movq 0x20(%rax), %rax
movq 0x28(%r14), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
xorl %r13d, %r13d
movq %r15, %rbp
movl %r13d, %r13d
cmpq %r13, %rcx
jbe 0x16059
movl (%rax,%r13,4), %edx
cmpl 0x18(%rbp), %edx
je 0x16059
incl %r13d
jmp 0x16043
testq %r13, %r13
movq %rbp, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movq %r12, 0x28(%rsp)
je 0x161fc
leal -0x1(%r13), %r12d
movl (%rax,%r12,4), %esi
movq %rbx, %rdi
callq 0x1586c
movq %rax, %r15
cvtsi2sdl 0x1c(%rax), %xmm0
movsd %xmm0, (%rsp)
cvtsi2sdl 0x90(%rbx), %xmm0
mulsd 0x21b12(%rip), %xmm0    # 0x37bb0
callq 0x50c0
addsd 0x21b0d(%rip), %xmm0    # 0x37bb8
shlq $0x5, %r12
movq 0x50(%r14), %rdx
addq %r12, %rdx
ucomisd (%rsp), %xmm0
jb 0x16390
decl %r13d
addq $0x50, %r14
movq %r14, 0x38(%rsp)
leaq 0x50(%r15), %rdi
movq %rdi, 0x10(%rsp)
movq %rdx, %rsi
callq 0x180d2
movq %r15, %rax
addq $0x20, %rax
movq %rax, (%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
movslq 0x1c(%rbp), %rax
cmpq %rax, %r12
jge 0x1618b
movq 0x50(%rbp), %rsi
addq %r14, %rsi
movq 0x10(%rsp), %rdi
callq 0x180d2
movq 0x20(%rbp), %rsi
addq %rbx, %rsi
movq (%rsp), %rdi
callq 0x180fe
movq 0x20(%rbp), %rax
movl (%rax,%rbx), %esi
cmpl $-0x1, %esi
je 0x1617b
movq %r12, 0x8(%rsp)
movq %r14, %r12
movq %r15, %r14
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x1586c
movq %rax, %rbp
movl 0x18(%r14), %eax
movl %eax, 0x6c(%rbp)
movq %r15, %rdi
movq %r14, %r15
movq %r12, %r14
movq 0x8(%rsp), %r12
movq %rbp, %rsi
callq 0x15bec
movq %rbp, %rdi
callq 0x1811c
movq %rbp, %rdi
movq 0x20(%rsp), %rbp
pushq $0x70
popq %rsi
callq 0x53f0
incq %r12
addq $0x20, %r14
addq $0x4, %rbx
jmp 0x160f1
movl 0x1c(%r15), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x1c(%r15)
movq 0x28(%rbp), %rsi
addq $-0x4, %rsi
movq (%rsp), %rdi
callq 0x180fe
movq 0x30(%rsp), %rbx
movq 0x20(%rbx), %rax
movl %r13d, %esi
leaq (%rax,%rsi,4), %r14
addq $0x4, %r14
shlq $0x5, %rsi
addq 0x50(%rbx), %rsi
movq 0x38(%rsp), %rdi
callq 0x18140
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %r14
callq 0x18146
decl 0x1c(%rbx)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x16355
movl 0x4(%rax), %esi
movq %rbx, %rdi
callq 0x1586c
movq %rbp, %r15
movq %rax, %rbp
cvtsi2sdl 0x1c(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cvtsi2sdl 0x90(%rbx), %xmm0
mulsd 0x21988(%rip), %xmm0    # 0x37bb0
callq 0x50c0
addsd 0x21983(%rip), %xmm0    # 0x37bb8
addq $0x20, %r15
movq %r15, (%rsp)
ucomisd 0x10(%rsp), %xmm0
jb 0x16415
movq %r14, %rax
addq $0x50, %rax
movq %rax, 0x10(%rsp)
shlq $0x5, %r13
addq 0x50(%r14), %r13
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x180d2
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
movslq 0x1c(%rbp), %rax
cmpq %rax, %r12
jge 0x162ef
movq 0x50(%rbp), %rsi
addq %r14, %rsi
movq 0x8(%rsp), %rdi
callq 0x180d2
movq 0x20(%rbp), %rsi
addq %rbx, %rsi
movq (%rsp), %rdi
callq 0x180fe
movq 0x20(%rbp), %rax
movl (%rax,%rbx), %esi
cmpl $-0x1, %esi
je 0x162e2
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x1586c
movq %rax, %r13
movq 0x20(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x6c(%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0x15bec
movq %r13, %rdi
callq 0x1811c
movq %r13, %rdi
pushq $0x70
popq %rsi
callq 0x53f0
incq %r12
addq $0x20, %r14
addq $0x4, %rbx
jmp 0x16272
movq 0x20(%rsp), %r15
movl 0x1c(%r15), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x1c(%r15)
movq 0x28(%rbp), %rsi
addq $-0x4, %rsi
movq (%rsp), %rdi
callq 0x180fe
movq 0x30(%rsp), %rbx
movq 0x20(%rbx), %r14
movq 0x50(%rbx), %rsi
addq $0x4, %r14
movq 0x10(%rsp), %rdi
callq 0x18140
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %r14
callq 0x18146
decl 0x1c(%rbx)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15bec
movl 0x18(%r14), %esi
jmp 0x15fba
cmpl $0x0, 0x1c(%r15)
jne 0x164ac
movq 0x20(%r15), %rax
movl (%rax), %eax
cmpl $-0x1, %eax
je 0x164ac
movl %eax, 0x64(%rbx)
jmp 0x164ac
movq 0x50(%rbp), %rsi
movq 0x8(%rsp), %rdi
callq 0x18180
movq %rbp, %rdi
addq $0x20, %rdi
movq 0x20(%rbp), %rsi
leaq 0x44(%rsp), %rdx
orl $-0x1, (%rdx)
callq 0x19262
incl 0x1c(%rbp)
movq %r15, %r13
movq %r14, %r15
leaq 0x50(%r13), %r14
movq 0x58(%r13), %rsi
addq $-0x20, %rsi
addq 0x50(%r15), %r12
movq %r12, %rdi
callq 0x1821c
decl 0x1c(%r13)
movq 0x58(%r13), %rsi
addq $-0x20, %rsi
movq %r14, %rdi
callq 0x18140
movq %r13, %rdi
addq $0x20, %rdi
movq 0x28(%r13), %rsi
addq $-0x4, %rsi
callq 0x18146
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x1649c
movq 0x20(%rsp), %r12
movq 0x50(%r12), %rsi
movabsq $0x1fffffffe0, %r13     # imm = 0x1FFFFFFFE0
movq 0x50(%r14), %rdx
addq %r13, %rdx
movq 0x8(%rsp), %rdi
callq 0x18180
movq 0x20(%r12), %rsi
leaq 0x40(%rsp), %rdx
orl $-0x1, (%rdx)
movq (%rsp), %rdi
callq 0x19262
incl 0x1c(%r12)
movq %r14, %r15
leaq 0x50(%rbp), %r14
movq 0x50(%rbp), %rsi
addq 0x50(%r15), %r13
movq %r13, %rdi
callq 0x1821c
decl 0x1c(%rbp)
movq 0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x18140
movq %rbp, %rdi
addq $0x20, %rdi
movq 0x20(%rbp), %rsi
callq 0x18146
movq %rbx, %rdi
movq %r12, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x15bec
movq %rbx, %rdi
movq %r15, %rsi
callq 0x15bec
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop