// File: /workspace/github_cpp_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, %rbx
movq %rdi, -0x38(%rbp)
movl 0x68(%rbx), %esi
cmpl $0x1, %esi
jg 0x1d04
movq 0x70(%rbx), %r15
testq %r15, %r15
je 0x1c86
xorl %r12d, %r12d
movq %r12, %rax
incq %r12
cmpq %rbx, 0x30(%r15,%rax,8)
jne 0x1ade
cmpq $0x1, %r12
je 0x1b06
leal -0x2(%r12), %r13d
movq 0x30(%r15,%r13,8), %r14
cmpl $0x3, 0x68(%r14)
jge 0x1cba
leaq -0x1(%r12), %r14
cmpl %r14d, 0x68(%r15)
jle 0x1b6e
movl %r12d, %eax
movq 0x30(%r15,%rax,8), %r13
cmpl $0x3, 0x68(%r13)
jl 0x1b6e
movq -0x8(%r15,%r12,8), %rdx
movq %rbx, %rdi
callq 0x1eca
movq %r13, %rdi
xorl %esi, %esi
callq 0x1f24
cltq
movq %rax, -0x8(%r15,%r12,8)
movl 0x6c(%rbx), %eax
movl %eax, -0x30(%rbp)
movq %r13, %rdi
xorl %esi, %esi
callq 0x1f72
movq %rbx, %rdi
movl -0x30(%rbp), %esi
movq %rax, %rdx
callq 0x1ef6
movslq 0x6c(%rbx), %rax
movq 0x28(%rbx,%rax,8), %rax
testq %rax, %rax
je 0x1b6e
movq %rbx, 0x70(%rax)
cmpq $0x1, %r12
jne 0x1be8
movq 0x38(%r15), %r12
movq %r15, %rdi
xorl %esi, %esi
callq 0x1f24
movslq %eax, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1eca
movq %r15, %rdi
xorl %esi, %esi
callq 0x1f72
movl 0x6c(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x1f72
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x1ef6
movq 0x30(%r12), %rax
testq %rax, %rax
jne 0x1be1
movl 0x68(%rbx), %esi
testl %esi, %esi
je 0x1c72
decl %esi
movq %rbx, %rdi
callq 0x1f24
movslq %eax, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1eca
jmp 0x1b99
andq $0x0, 0x70(%rax)
jmp 0x1bbd
addl $-0x2, %r12d
movq 0x30(%r15,%r12,8), %r13
movq %r14, -0x30(%rbp)
movl 0x68(%r13), %r14d
movq %r15, %rdi
movl %r12d, %esi
callq 0x1f24
movslq %eax, %rdx
movq %r13, %rdi
movl %r14d, %esi
callq 0x1eca
movq %r15, %rdi
movq -0x30(%rbp), %rsi
callq 0x1f72
movl 0x6c(%r13), %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1f72
movq %r13, %rdi
movl %r14d, %esi
movq %rax, %rdx
callq 0x1ef6
movslq 0x6c(%r13), %rax
movq 0x28(%r13,%rax,8), %rax
testq %rax, %rax
jne 0x1c6c
cmpl $0x0, 0x68(%rbx)
je 0x1c72
movl 0x68(%r13), %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1f24
movslq %eax, %rdx
movq %r13, %rdi
movl %r14d, %esi
callq 0x1eca
jmp 0x1c1e
movq %r13, 0x70(%rax)
jmp 0x1c48
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
callq 0x1756
movq %r15, %rbx
jmp 0x1ac2
testl %esi, %esi
jne 0x1d04
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x1d04
movq -0x38(%rbp), %rdi
movq %rax, (%rdi)
andq $0x0, 0x70(%rax)
andq $0x0, 0x30(%rbx)
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1756
movq (%r15,%r13,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1eca
movl 0x68(%r14), %esi
decl %esi
movq %r14, %rdi
callq 0x1f24
cltq
movq %rax, (%r15,%r13,8)
movl 0x6c(%r14), %esi
decl %esi
movq %r14, %rdi
callq 0x1f72
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x1ef6
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x1d04
movq %rbx, 0x70(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop