// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, 0x20(%rsp)
movq %r15, %rdi
callq 0x19bd2
movq %rax, %r13
movl 0x6c(%rax), %ebp
cmpl $-0x1, %ebp
je 0x1a995
movq 0x58(%r13), %rax
subq 0x50(%r13), %rax
shrq $0x5, %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%r15), %xmm0
mulsd 0x2738c(%rip), %xmm0    # 0x41880
callq 0x50d0
addsd 0x27387(%rip), %xmm0    # 0x41888
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1ab2e
movq %r15, %rdi
movl %ebp, %esi
callq 0x19bd2
movq 0x20(%rax), %rsi
movq %rax, 0x8(%rsp)
movq 0x28(%rax), %rcx
subq %rsi, %rcx
je 0x1a54c
sarq $0x2, %rcx
movl 0x18(%r13), %edx
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpl %edx, (%rsi,%r14,4)
je 0x1a551
incl %ebx
movq %rbx, %r14
cmpq %rbx, %rcx
ja 0x1a53a
jmp 0x1a551
xorl %ebx, %ebx
xorl %r14d, %r14d
leaq 0x50(%r13), %rax
movq %rax, 0x10(%rsp)
testl %ebx, %ebx
movq %r13, 0x28(%rsp)
je 0x1a71e
decl %ebx
movl (%rsi,%rbx,4), %esi
movq %r15, %rdi
callq 0x19bd2
movq %rax, %r12
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%r15), %xmm0
mulsd 0x272e7(%rip), %xmm0    # 0x41880
callq 0x50d0
addsd 0x272e2(%rip), %xmm0    # 0x41888
movq 0x8(%rsp), %rax
movq %rax, %rbp
addq $0x50, %rbp
movq %rbx, %r15
shlq $0x5, %r15
movq 0x50(%rax), %rdx
addq %r15, %rdx
ucomisd 0x18(%rsp), %xmm0
jb 0x1a9b8
leaq 0x50(%r12), %rdi
movq %rdi, 0x18(%rsp)
movq %rdx, %rsi
callq 0x1cdce
movl 0x1c(%r13), %eax
testl %eax, %eax
movq %r12, 0x10(%rsp)
jle 0x1a6e6
movq %rbp, 0x30(%rsp)
leaq 0x20(%r12), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x50(%r13), %rsi
addq %rbp, %rsi
movq 0x18(%rsp), %rdi
callq 0x1cdce
movq 0x20(%r13), %rdx
addq %r14, %rdx
movq 0x28(%r12), %rsi
cmpq 0x30(%r12), %rsi
je 0x1a639
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r12)
jmp 0x1a643
movq 0x38(%rsp), %rdi
callq 0x1db1c
movq 0x20(%r13), %rax
movl (%rax,%r14), %esi
cmpl $-0x1, %esi
je 0x1a6c4
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x19bd2
movq %rax, %r13
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x6c(%r13)
movq %r12, %rdi
movq 0x10(%rsp), %r12
movq %r13, %rsi
callq 0x1a022
testq %r13, %r13
je 0x1a6c4
movq %r13, %rdi
addq $0x50, %rdi
callq 0x1cebc
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x1a6a2
movq 0x48(%r13), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x20(%r13), %rdi
testq %rdi, %rdi
je 0x1a6b7
movq 0x30(%r13), %rsi
subq %rdi, %rsi
callq 0x53a0
movl $0x70, %esi
movq %r13, %rdi
callq 0x53a0
incq %r15
movq 0x28(%rsp), %r13
movslq 0x1c(%r13), %rax
addq $0x20, %rbp
addq $0x4, %r14
cmpq %rax, %r15
jl 0x1a606
movq 0x30(%rsp), %rbp
movl 0x1c(%r12), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x1c(%r12)
movq 0x28(%r13), %rdx
addq $-0x4, %rdx
movq 0x28(%r12), %rsi
cmpq 0x30(%r12), %rsi
je 0x1a8a6
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r12)
jmp 0x1a8b2
movl 0x4(%rsi), %esi
movq %r15, %rdi
callq 0x19bd2
movq %rax, %r12
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%r15), %xmm0
mulsd 0x27132(%rip), %xmm0    # 0x41880
callq 0x50d0
addsd 0x2712d(%rip), %xmm0    # 0x41888
ucomisd 0x18(%rsp), %xmm0
jb 0x1aa65
shlq $0x5, %r14
movq 0x8(%rsp), %rax
addq 0x50(%rax), %r14
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1cdce
movl 0x1c(%r12), %eax
testl %eax, %eax
jle 0x1a875
leaq 0x20(%r13), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x50(%r12), %rsi
addq %r14, %rsi
movq 0x10(%rsp), %rdi
callq 0x1cdce
movq 0x20(%r12), %rdx
addq %rbx, %rdx
movq 0x28(%r13), %rsi
cmpq 0x30(%r13), %rsi
je 0x1a7d1
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r13)
jmp 0x1a7db
movq 0x18(%rsp), %rdi
callq 0x1db1c
movq 0x20(%r12), %rax
movl (%rax,%rbx), %esi
cmpl $-0x1, %esi
je 0x1a857
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
callq 0x19bd2
movq %rax, %r13
movq 0x28(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x6c(%r13)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1a022
testq %r13, %r13
je 0x1a857
movq %r13, %rdi
addq $0x50, %rdi
callq 0x1cebc
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x1a835
movq 0x48(%r13), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x20(%r13), %rdi
testq %rdi, %rdi
je 0x1a84a
movq 0x30(%r13), %rsi
subq %rdi, %rsi
callq 0x53a0
movl $0x70, %esi
movq %r13, %rdi
callq 0x53a0
incq %r15
movslq 0x1c(%r12), %rax
addq $0x20, %r14
addq $0x4, %rbx
cmpq %rax, %r15
movq 0x28(%rsp), %r13
jl 0x1a79f
movl 0x1c(%r13), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x1c(%r13)
movq 0x28(%r12), %rdx
addq $-0x4, %rdx
movq 0x28(%r13), %rsi
cmpq 0x30(%r13), %rsi
je 0x1a919
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r13)
jmp 0x1a925
addq $0x20, %r12
movq %r12, %rdi
callq 0x1db1c
movq 0x20(%rsp), %r15
movq %rbx, %rsi
shlq $0x5, %rsi
movq 0x8(%rsp), %r12
movq 0x20(%r12), %r14
addq 0x50(%r12), %rsi
movq %rbp, %rdi
callq 0x1dbf8
leaq (%r14,%rbx,4), %rsi
addq $0x8, %rsi
movq 0x28(%r12), %rdx
cmpq %rdx, %rsi
je 0x1a8f7
leaq (%r14,%rbx,4), %rdi
addq $0x4, %rdi
subq %rsi, %rdx
callq 0x5630
movq 0x8(%rsp), %rbx
addq $-0x4, 0x28(%rbx)
decl 0x1c(%rbx)
movq %r15, %rdi
movq %r13, %rsi
callq 0x1a022
movq %r15, %rdi
movq 0x10(%rsp), %rsi
jmp 0x1a97d
movq %r13, %rdi
addq $0x20, %rdi
callq 0x1db1c
movq 0x20(%rsp), %r15
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
addq $0x50, %rdi
movq 0x20(%rbx), %r14
movq 0x50(%rbx), %rsi
callq 0x1dbf8
leaq 0x8(%r14), %rsi
movq 0x28(%rbx), %rdx
cmpq %rdx, %rsi
je 0x1a95f
addq $0x4, %r14
subq %rsi, %rdx
movq %r14, %rdi
callq 0x5630
movq 0x8(%rsp), %rbx
addq $-0x4, 0x28(%rbx)
decl 0x1c(%rbx)
movq %r15, %rdi
movq %r13, %rsi
callq 0x1a022
movq %r15, %rdi
movq %r12, %rsi
callq 0x1a022
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1a022
movl 0x18(%rbx), %esi
jmp 0x1a4b0
cmpl $0x0, 0x1c(%r13)
jne 0x1ab2e
movq 0x20(%r13), %rax
movl (%rax), %eax
cmpl $-0x1, %eax
je 0x1ab2e
movl %eax, 0x64(%r15)
jmp 0x1ab2e
movq 0x50(%r13), %rsi
movq 0x10(%rsp), %rdi
callq 0x1ce06
movq %r13, %rdi
addq $0x20, %rdi
movq 0x20(%r13), %rsi
leaq 0x44(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x1dd72
incl 0x1c(%r13)
movq %r12, %r14
addq $0x50, %r14
movq 0x58(%r12), %rsi
movq (%rbp), %rax
movl -0x20(%rsi), %ecx
leaq (%rax,%r15), %rdi
addq $0x8, %rdi
movl %ecx, -0x8(%rdi)
addq $-0x18, %rsi
callq 0x1cefe
decl 0x1c(%r12)
movq 0x58(%r12), %rsi
addq $-0x20, %rsi
movq %r14, %rdi
callq 0x1dbf8
movb $0x1, %al
testb %al, %al
jne 0x1aa3c
movq 0x28(%r12), %rsi
leaq -0x4(%rsi), %rdi
movq %rsi, %rdx
subq %rsi, %rdx
callq 0x5630
addq $-0x4, 0x28(%r12)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a022
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1a022
movq %rbx, %rdi
jmp 0x1ab24
movq 0x50(%r13), %rsi
movabsq $0x1fffffffe0, %rbx     # imm = 0x1FFFFFFFE0
movq %r13, %r14
movq 0x8(%rsp), %r13
movq 0x50(%r13), %rdx
addq %rbx, %rdx
movq 0x10(%rsp), %rdi
callq 0x1ce06
movq %r14, %rdi
addq $0x20, %rdi
movq 0x20(%r14), %rsi
leaq 0x40(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x1dd72
incl 0x1c(%r14)
movq %r12, %r14
addq $0x50, %r14
movq 0x50(%r12), %rsi
movq 0x50(%r13), %rax
movl (%rsi), %ecx
movl %ecx, (%rax,%rbx)
movabsq $0x1fffffffe8, %rdi     # imm = 0x1FFFFFFFE8
addq %rax, %rdi
addq $0x8, %rsi
callq 0x1cefe
decl 0x1c(%r12)
movq 0x50(%r12), %rsi
movq %r14, %rdi
callq 0x1dbf8
movq 0x20(%r12), %rdi
movq 0x28(%r12), %rdx
leaq 0x4(%rdi), %rsi
cmpq %rdx, %rsi
je 0x1ab03
subq %rsi, %rdx
callq 0x5630
addq $-0x4, 0x28(%r12)
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x1a022
movq %r15, %rdi
movq %r12, %rsi
callq 0x1a022
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x1a022
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop