// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
callq 0x19412
movq %rax, %r12
movl 0x6c(%rax), %r15d
cmpl $-0x1, %r15d
je 0x1a1b1
movq %r14, 0x10(%rsp)
movq 0x58(%r12), %rax
subq 0x50(%r12), %rax
shrq $0x5, %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%r14), %xmm0
mulsd 0x26b58(%rip), %xmm0    # 0x40870
callq 0x50d0
addsd 0x26b53(%rip), %xmm0    # 0x40878
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1a359
leaq 0x50(%r12), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x19412
movq %rax, %rbx
movq 0x20(%rax), %rax
movq 0x28(%rbx), %rcx
subq %rax, %rcx
movq %r12, 0x38(%rsp)
je 0x19f3a
sarq $0x2, %rcx
movl 0x18(%r12), %edx
xorl %r13d, %r13d
cmpl %edx, (%rax,%r13,4)
je 0x19d7c
incl %r13d
cmpq %r13, %rcx
ja 0x19d6e
testl %r13d, %r13d
je 0x19f3d
decl %r13d
movl (%rax,%r13,4), %esi
movq %r14, %rdi
callq 0x19412
movq %rax, %rbp
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rax), %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%r14), %xmm0
mulsd 0x26ab8(%rip), %xmm0    # 0x40870
callq 0x50d0
addsd 0x26ab3(%rip), %xmm0    # 0x40878
movq %rbx, %rax
addq $0x50, %rax
movq %rax, 0x28(%rsp)
movq %r13, %rax
shlq $0x5, %rax
movq 0x50(%rbx), %rdx
movq %rax, 0x30(%rsp)
addq %rax, %rdx
ucomisd (%rsp), %xmm0
jb 0x1a2a3
leaq 0x50(%rbp), %rdi
movq %rdi, 0x8(%rsp)
movq %rdx, %rsi
callq 0x1c6ac
movl 0x1c(%r12), %eax
testl %eax, %eax
movq %rbp, (%rsp)
jle 0x19f08
movq %rbx, 0x18(%rsp)
leaq 0x20(%rbp), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x50(%r12), %rsi
addq %rbx, %rsi
movq 0x8(%rsp), %rdi
callq 0x1c6ac
movq 0x20(%r12), %rdx
addq %rbp, %rdx
movq (%rsp), %rcx
movq 0x28(%rcx), %rax
cmpq 0x30(%rcx), %rax
je 0x19e5c
movl (%rdx), %esi
movl %esi, (%rax)
addq $0x4, %rax
movq %rax, 0x28(%rcx)
jmp 0x19e71
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x1d42c
movq 0x20(%r12), %rax
movl (%rax,%rbp), %esi
cmpl $-0x1, %esi
je 0x19ee1
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x19412
movq %rax, %r15
movq (%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x6c(%r15)
movq %r12, %rdi
movq %r15, %rsi
callq 0x1984c
leaq 0x50(%r15), %rdi
callq 0x1c79a
movq 0x38(%r15), %rdi
testq %rdi, %rdi
je 0x19eba
movq 0x48(%r15), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0x19ecf
movq 0x30(%r15), %rsi
subq %rdi, %rsi
callq 0x53a0
movl $0x70, %esi
movq %r15, %rdi
callq 0x53a0
movq 0x38(%rsp), %r12
incq %r14
movslq 0x1c(%r12), %rax
addq $0x4, %rbp
addq $0x20, %rbx
cmpq %rax, %r14
jl 0x19e26
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
movq (%rsp), %rbp
movl 0x1c(%rbp), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x1c(%rbp)
movq 0x28(%r12), %rdx
addq $-0x4, %rdx
movq 0x28(%rbp), %rsi
cmpq 0x30(%rbp), %rsi
je 0x1a109
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rbp)
jmp 0x1a115
xorl %r13d, %r13d
movl 0x4(%rax), %esi
movq %r14, %rdi
callq 0x19412
movq %rax, %rbp
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rax), %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%r14), %xmm0
mulsd 0x26904(%rip), %xmm0    # 0x40870
callq 0x50d0
addsd 0x268ff(%rip), %xmm0    # 0x40878
ucomisd (%rsp), %xmm0
movq %rbp, (%rsp)
jb 0x1a1d6
shlq $0x5, %r13
addq 0x50(%rbx), %r13
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x1c6ac
movl 0x1c(%rbp), %eax
testl %eax, %eax
jle 0x1a09c
movq %rbx, 0x18(%rsp)
leaq 0x20(%r12), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x50(%rbp), %rsi
addq %rbx, %rsi
movq 0x8(%rsp), %rdi
callq 0x1c6ac
movq 0x20(%rbp), %rdx
addq %r15, %rdx
movq 0x28(%r12), %rax
cmpq 0x30(%r12), %rax
je 0x19ff2
movl (%rdx), %esi
movl %esi, (%rax)
addq $0x4, %rax
movq %rax, 0x28(%r12)
jmp 0x1a007
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x1d42c
movq 0x20(%rbp), %rax
movl (%rax,%r15), %esi
cmpl $-0x1, %esi
je 0x1a07a
movq %r12, %rbp
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x19412
movq %rax, %r13
movl 0x18(%rbp), %eax
movl %eax, 0x6c(%r13)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1984c
leaq 0x50(%r13), %rdi
callq 0x1c79a
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x1a04f
movq 0x48(%r13), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x20(%r13), %rdi
testq %rdi, %rdi
je 0x1a064
movq 0x30(%r13), %rsi
subq %rdi, %rsi
callq 0x53a0
movl $0x70, %esi
movq %r13, %rdi
callq 0x53a0
movq 0x38(%rsp), %r12
movq (%rsp), %rbp
incq %r14
movslq 0x1c(%rbp), %rax
addq $0x4, %r15
addq $0x20, %rbx
cmpq %rax, %r14
jl 0x19fbf
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
movl 0x1c(%r12), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x1c(%r12)
movq 0x28(%rbp), %rdx
addq $-0x4, %rdx
movq 0x28(%r12), %rsi
cmpq 0x30(%r12), %rsi
je 0x1a0cd
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%r12)
jmp 0x1a0d7
leaq 0x20(%r12), %rdi
callq 0x1d42c
movq %rbx, %rdi
addq $0x50, %rdi
movq 0x20(%rbx), %r15
movq 0x50(%rbx), %rsi
callq 0x1d508
leaq 0x8(%r15), %rsi
movq 0x28(%rbx), %rdx
cmpq %rdx, %rsi
je 0x1a15e
addq $0x4, %r15
subq %rsi, %rdx
movq %r15, %rdi
callq 0x5630
jmp 0x1a15a
addq $0x20, %rbp
movq %rbp, %rdi
callq 0x1d42c
movq %rbx, %rbp
movq 0x20(%rbx), %rbx
movq 0x30(%rsp), %rsi
addq 0x50(%rbp), %rsi
movq 0x28(%rsp), %rdi
callq 0x1d508
leaq (%rbx,%r13,4), %rsi
addq $0x8, %rsi
movq %rbp, %r15
movq 0x28(%rbp), %rdx
cmpq %rdx, %rsi
je 0x1a1a8
leaq (%rbx,%r13,4), %rdi
addq $0x4, %rdi
subq %rsi, %rdx
callq 0x5630
movq %r15, %rbx
movq (%rsp), %rbp
movq 0x28(%rbx), %rdx
addq $-0x4, %rdx
movq %rdx, 0x28(%rbx)
decl 0x1c(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x1984c
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1984c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1984c
movl 0x18(%rbx), %esi
movq %r14, %rdi
callq 0x19412
movq %rax, %r12
movl 0x6c(%rax), %r15d
cmpl $-0x1, %r15d
jne 0x19cea
jmp 0x1a1b1
movq %r15, %rbx
movq (%rsp), %rbp
jmp 0x1a15e
cmpl $0x0, 0x1c(%r12)
jne 0x1a359
movq 0x20(%r12), %rax
movl (%rax), %eax
cmpl $-0x1, %eax
je 0x1a359
movl %eax, 0x64(%r14)
jmp 0x1a359
movq 0x50(%r12), %rsi
movq %rbx, %rbp
movabsq $0x1fffffffe0, %rbx     # imm = 0x1FFFFFFFE0
movq 0x50(%rbp), %rdx
addq %rbx, %rdx
movq 0x8(%rsp), %rdi
callq 0x1c6e4
leaq 0x20(%r12), %rdi
movq 0x20(%r12), %rsi
leaq 0x40(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x1d6ee
incl 0x1c(%r12)
movq (%rsp), %r15
addq $0x50, %r15
movq (%rsp), %rax
movq 0x50(%rax), %rsi
movq %rbp, %r13
movq 0x50(%rbp), %rax
movq (%rsp), %rbp
movl (%rsi), %ecx
movl %ecx, (%rax,%rbx)
movabsq $0x1fffffffe8, %rdi     # imm = 0x1FFFFFFFE8
addq %rax, %rdi
addq $0x8, %rsi
callq 0x1c7e2
decl 0x1c(%rbp)
movq 0x50(%rbp), %rsi
movq %r15, %rdi
callq 0x1d508
movq 0x20(%rbp), %rdi
movq 0x28(%rbp), %rdx
leaq 0x4(%rdi), %rsi
cmpq %rdx, %rsi
je 0x1a27a
subq %rsi, %rdx
callq 0x5630
movq 0x28(%rbp), %rdx
addq $-0x4, %rdx
movq %rdx, 0x28(%rbp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x1984c
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1984c
movq %r14, %rdi
movq %r13, %rsi
jmp 0x1a354
movq 0x50(%r12), %rsi
movq 0x8(%rsp), %rdi
callq 0x1c6e4
leaq 0x20(%r12), %rdi
movq 0x20(%r12), %rsi
leaq 0x44(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x1d6ee
incl 0x1c(%r12)
movq %rbp, %r15
addq $0x50, %r15
movq 0x58(%rbp), %rsi
movq 0x28(%rsp), %rax
movq (%rax), %rax
movl -0x20(%rsi), %ecx
movq 0x30(%rsp), %rdx
leaq (%rax,%rdx), %rdi
addq $0x8, %rdi
movl %ecx, -0x8(%rdi)
addq $-0x18, %rsi
callq 0x1c7e2
decl 0x1c(%rbp)
movq 0x58(%rbp), %rsi
addq $-0x20, %rsi
movq %r15, %rdi
callq 0x1d508
movq 0x28(%rbp), %rsi
movb $0x1, %al
testb %al, %al
jne 0x1a330
leaq -0x4(%rsi), %rdi
movq %rsi, %rdx
subq %rsi, %rdx
callq 0x5630
movq 0x28(%rbp), %rsi
addq $-0x4, %rsi
movq %rsi, 0x28(%rbp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x1984c
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1984c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1984c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq