// File: /workspace/github_cpp_makefile_star2_O3/CoderUper[P]BTree/BTree.cpp
// Function: BTree::SolveUnderflow(Node*)
void BTree::SolveUnderflow(Node* node)
{
    if(node->key_num>=M-1) return;
    Node *p=node->parent;
    //node是根节点,不受最小数目限制
    if(!p)
    {
        //如果根节点空
        if(node->key_num==0&&node->child[0])
        {
            root=node->child[0];
            root->parent=NULL;
            node->child[0]=NULL;
            release(node);
        }
        return;
    }
    int pos=0;
    while(p->child[pos]!=node) pos++;  
    //node不是根节点，分三种情况讨论
    //（1）node有左兄弟且数目足够
    if(pos>0&&p->child[pos-1]->key_num>M-1)
    {
        Node* lb=p->child[pos-1];
        node->key_insert(0,p->key[pos-1]);
        p->key[pos-1]=lb->key_remove(lb->key_num-1);
        node->child_insert(0,lb->child_remove(lb->child_num-1));
        if(node->child[0]) node->child[0]->parent=node;
        return;
    }
    //（2）node有右兄弟且数目足够
    if(pos<p->key_num&&p->child[pos+1]->key_num>M-1)
    {
        Node* rb=p->child[pos+1];
        node->key_insert(node->key_num,p->key[pos]);
        p->key[pos]=rb->key_remove(0);
        node->child_insert(node->child_num,rb->child_remove(0));
        if(node->child[node->child_num-1])
            node->child[node->child_num-1]->parent=node;
    }
    //（3）向父节点借
    //必有左兄弟
    if(pos>0)
    {
        Node* lb=p->child[pos-1];
        lb->key_insert(lb->key_num,p->key_remove(pos-1));
        p->child_remove(pos);
        lb->child_insert(lb->child_num,node->child_remove(0));
        if(lb->child[lb->child_num-1]) lb->child[lb->child_num-1]->parent=lb;
        while(node->key_num!=0)
        {
            lb->key_insert(lb->key_num,node->key_remove(0));
            lb->child_insert(lb->child_num,node->child_remove(0));
            if(lb->child[lb->child_num-1]) lb->child[lb->child_num-1]->parent=lb;
        }
        release(node);
    }
    //必有右兄弟,数据转入右兄弟
    else
    {
        Node* rb=p->child[pos+1];
        //父节点key转入rb;
        rb->key_insert(0,p->key_remove(pos));
        p->child_remove(pos);
        //node节点的数据从又向左转入rb;
        rb->child_insert(0,node->child_remove(node->child_num-1));
        if(rb->child[0]) rb->child[0]->parent=0;
        while(node->key_num!=0)
        {
            rb->key_insert(0,node->key_remove(node->key_num-1));
            rb->child_insert(0,node->child_remove(node->child_num-1));
            if(rb->child[0]) rb->child[0]->parent=0;
        }
        release(node);
    }
    SolveUnderflow(p);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, -0x40(%rbp)
movl 0x68(%rsi), %eax
cmpl $0x1, %eax
jg 0x2146
movq %rsi, %r14
leaq 0x68(%rsi), %rcx
movq %rcx, -0x30(%rbp)
movq %r14, %rbx
movq 0x70(%r14), %r14
testq %r14, %r14
je 0x2098
xorl %r13d, %r13d
movq %r13, %rcx
incq %r13
cmpq %rbx, 0x30(%r14,%rcx,8)
jne 0x1d3f
cmpq $0x1, %r13
je 0x1d66
leal -0x2(%r13), %r12d
movq 0x30(%r14,%r12,8), %r15
cmpl $0x3, 0x68(%r15)
jge 0x20d4
leaq -0x1(%r13), %rdx
cmpl %edx, 0x68(%r14)
jle 0x1df9
movl %r13d, %ecx
movq 0x30(%r14,%rcx,8), %r15
cmpl $0x3, 0x68(%r15)
jl 0x1df9
movq %rdx, -0x38(%rbp)
movq -0x8(%r14,%r13,8), %rcx
movslq %eax, %rdx
movq %rcx, (%rbx,%rdx,8)
incl %eax
movq -0x30(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2348
cltq
movq %rax, -0x8(%r14,%r13,8)
movslq 0x6c(%rbx), %r12
movq %r15, %rdi
xorl %esi, %esi
callq 0x239a
movslq 0x6c(%rbx), %rcx
cmpl %r12d, %ecx
jle 0x1ddc
movq %rcx, %rdx
movq 0x28(%rbx,%rdx,8), %rsi
movq %rsi, 0x30(%rbx,%rdx,8)
leaq -0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq %r12, %rsi
jg 0x1dc6
movq %rax, 0x30(%rbx,%r12,8)
leal 0x1(%rcx), %eax
movl %eax, 0x6c(%rbx)
movq 0x30(%rbx,%rcx,8), %rax
testq %rax, %rax
movq -0x38(%rbp), %rdx
je 0x1df9
movq %rbx, 0x70(%rax)
cmpq $0x1, %r13
jne 0x1f56
movq 0x38(%r14), %r12
movq %r14, %rdi
xorl %esi, %esi
callq 0x2348
movl %eax, %r15d
movslq 0x68(%r12), %r13
testq %r13, %r13
jle 0x1e33
leaq 0x8(%r12), %rdi
leaq (,%r13,8), %rdx
movq %r12, %rsi
callq 0x1190
movslq %r15d, %rax
movq %rax, (%r12)
incl %r13d
movl %r13d, 0x68(%r12)
movq %r14, %rdi
xorl %esi, %esi
callq 0x239a
movl 0x6c(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x239a
movq %rax, %r15
movslq 0x6c(%r12), %r13
testq %r13, %r13
jle 0x1e7d
leaq 0x30(%r12), %rsi
leaq 0x38(%r12), %rdi
leaq (,%r13,8), %rdx
callq 0x1190
movq %r15, 0x30(%r12)
incl %r13d
movl %r13d, 0x6c(%r12)
testq %r15, %r15
je 0x1e97
movq $0x0, 0x70(%r15)
movq -0x30(%rbp), %rax
movl (%rax), %esi
testl %esi, %esi
je 0x2073
leaq 0x30(%r12), %rax
movq %rax, -0x38(%rbp)
leaq 0x8(%r12), %rax
movq %rax, -0x50(%rbp)
leaq 0x38(%r12), %rax
movq %rax, -0x48(%rbp)
decl %esi
movq %rbx, %rdi
callq 0x2348
movl %eax, %r15d
movslq 0x68(%r12), %r13
testq %r13, %r13
jle 0x1eeb
leaq (,%r13,8), %rdx
movq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x1190
movslq %r15d, %rax
movq %rax, (%r12)
incl %r13d
movl %r13d, 0x68(%r12)
movl 0x6c(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x239a
movq %rax, %r15
movslq 0x6c(%r12), %r13
testq %r13, %r13
jle 0x1f29
leaq (,%r13,8), %rdx
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1190
movq %r15, 0x30(%r12)
incl %r13d
movl %r13d, 0x6c(%r12)
testq %r15, %r15
je 0x1f43
movq $0x0, 0x70(%r15)
movq -0x30(%rbp), %rax
movl (%rax), %esi
testl %esi, %esi
jne 0x1ec0
jmp 0x2073
movq %rdx, -0x38(%rbp)
addl $-0x2, %r13d
movq 0x30(%r14,%r13,8), %r15
movslq 0x68(%r15), %r12
movq %r14, %rdi
movl %r13d, %esi
callq 0x2348
movslq 0x68(%r15), %rcx
cmpl %r12d, %ecx
jle 0x1f93
movq %rcx, %rdx
movq -0x8(%r15,%rdx,8), %rsi
movq %rsi, (%r15,%rdx,8)
leaq -0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq %r12, %rsi
jg 0x1f7e
cltq
movq %rax, (%r15,%r12,8)
incl %ecx
movl %ecx, 0x68(%r15)
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x239a
movslq 0x6c(%r15), %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x239a
movslq 0x6c(%r15), %rcx
cmpl %r12d, %ecx
jle 0x204c
movq %rcx, %rdx
movq 0x28(%r15,%rdx,8), %rsi
movq %rsi, 0x30(%r15,%rdx,8)
leaq -0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq %r12, %rsi
jg 0x1fc9
jmp 0x204c
movslq 0x68(%r15), %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2348
movslq 0x68(%r15), %rcx
cmpl %r12d, %ecx
jle 0x2010
movq %rcx, %rdx
movq -0x8(%r15,%rdx,8), %rsi
movq %rsi, (%r15,%rdx,8)
leaq -0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq %r12, %rsi
jg 0x1ffb
cltq
movq %rax, (%r15,%r12,8)
incl %ecx
movl %ecx, 0x68(%r15)
movslq 0x6c(%r15), %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x239a
movslq 0x6c(%r15), %rcx
cmpl %r12d, %ecx
jle 0x204c
movq %rcx, %rdx
movq 0x28(%r15,%rdx,8), %rsi
movq %rsi, 0x30(%r15,%rdx,8)
leaq -0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq %r12, %rsi
jg 0x2036
movq %rax, 0x30(%r15,%r12,8)
leal 0x1(%rcx), %eax
movl %eax, 0x6c(%r15)
movq 0x30(%r15,%rcx,8), %rax
testq %rax, %rax
je 0x2066
movq %r15, 0x70(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x1fe1
leaq 0x68(%r14), %r15
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x17ca
movq %r15, -0x30(%rbp)
movl (%r15), %eax
cmpl $0x2, %eax
jl 0x1d2c
jmp 0x2146
testl %eax, %eax
jne 0x2146
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x2146
movq -0x40(%rbp), %rdi
movq %rax, (%rdi)
xorl %ecx, %ecx
movq %rcx, 0x70(%rax)
movq %rcx, 0x30(%rbx)
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x17ca
movq (%r14,%r12,8), %rcx
cmpl $0x1, %eax
jne 0x20e4
movq (%rbx), %rdx
movq %rdx, 0x8(%rbx)
movq %rcx, (%rbx)
incl %eax
movq -0x30(%rbp), %rcx
movl %eax, (%rcx)
movl 0x68(%r15), %esi
decl %esi
movq %r15, %rdi
callq 0x2348
cltq
movq %rax, (%r14,%r12,8)
movl 0x6c(%r15), %esi
decl %esi
movq %r15, %rdi
callq 0x239a
movq %rax, %r14
movslq 0x6c(%rbx), %r15
testq %r15, %r15
jle 0x2132
leaq 0x30(%rbx), %rsi
leaq 0x38(%rbx), %rdi
leaq (,%r15,8), %rdx
callq 0x1190
movq %r14, 0x30(%rbx)
incl %r15d
movl %r15d, 0x6c(%rbx)
testq %r14, %r14
je 0x2146
movq %rbx, 0x70(%r14)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop