// File: 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
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x68(%rax)
jl 0x288a
jmp 0x2d75
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x28e6
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x28e1
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x28e1
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, (%rdi)
movq (%rdi), %rax
movq $0x0, 0x70(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x10(%rbp), %rsi
callq 0x22b0
jmp 0x2d75
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x30(%rax,%rcx,8), %rax
cmpq -0x10(%rbp), %rax
je 0x290b
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x28ed
cmpl $0x0, -0x1c(%rbp)
jle 0x29d3
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
cmpl $0x2, 0x68(%rax)
jle 0x29d3
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
xorl %esi, %esi
callq 0x30c0
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x68(%rax), %esi
subl $0x1, %esi
callq 0x31a0
movslq %eax, %rdx
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x6c(%rax), %esi
subl $0x1, %esi
callq 0x3250
movq -0x50(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x3130
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x29ce
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rcx, 0x70(%rax)
jmp 0x2d75
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x68(%rcx), %eax
jge 0x2ab0
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
cmpl $0x2, 0x68(%rax)
jle 0x2ab0
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x68(%rax), %esi
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x30c0
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x31a0
movslq %eax, %rdx
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x3250
movq -0x60(%rbp), %rdi
movl -0x54(%rbp), %esi
movq %rax, %rdx
callq 0x3130
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x6c(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, 0x30(%rax,%rcx,8)
je 0x2aae
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rdx
movl 0x6c(%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq 0x30(%rax,%rdx,8), %rax
movq %rcx, 0x70(%rax)
jmp 0x2ab0
cmpl $0x0, -0x1c(%rbp)
jle 0x2c3d
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movl 0x68(%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
subl $0x1, %esi
callq 0x31a0
movq -0x80(%rbp), %rdi
movl -0x74(%rbp), %esi
movslq %eax, %rdx
callq 0x30c0
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x3250
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
movl 0x6c(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x3250
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %esi
movq %rax, %rdx
callq 0x3130
movq -0x38(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x6c(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, 0x30(%rax,%rcx,8)
je 0x2b6f
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %rax
movq -0x38(%rbp), %rdx
movl 0x6c(%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq 0x30(%rax,%rdx,8), %rax
movq %rcx, 0x70(%rax)
jmp 0x2b71
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0x2c2b
movq -0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rax
movl 0x68(%rax), %eax
movl %eax, -0x94(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x31a0
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
movslq %eax, %rdx
callq 0x30c0
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movl 0x6c(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x3250
movq -0x90(%rbp), %rdi
movl -0x84(%rbp), %esi
movq %rax, %rdx
callq 0x3130
movq -0x38(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x6c(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, 0x30(%rax,%rcx,8)
je 0x2c26
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %rax
movq -0x38(%rbp), %rdx
movl 0x6c(%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq 0x30(%rax,%rdx,8), %rax
movq %rcx, 0x70(%rax)
jmp 0x2b71
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x22b0
jmp 0x2d68
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x31a0
movq -0xb0(%rbp), %rdi
movslq %eax, %rdx
xorl %esi, %esi
callq 0x30c0
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x3250
movq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %esi
subl $0x1, %esi
callq 0x3250
movq -0xa8(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x3130
movq -0x40(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x2cd1
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq $0x0, 0x70(%rax)
jmp 0x2cd3
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0x2d5b
movq -0x40(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x68(%rax), %esi
subl $0x1, %esi
callq 0x31a0
movq -0xc0(%rbp), %rdi
movslq %eax, %rdx
xorl %esi, %esi
callq 0x30c0
movq -0x40(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %esi
subl $0x1, %esi
callq 0x3250
movq -0xb8(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x3130
movq -0x40(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x2d56
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq $0x0, 0x70(%rax)
jmp 0x2cd3
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x22b0
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2860
addq $0xc0, %rsp
popq %rbp
retq
nop