// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %r14
movq %rdi, -0x38(%rbp)
movslq 0x68(%r14), %rax
cmpq $0x1, %rax
jg 0x20e4
movq %r14, %rbx
movq 0x70(%r14), %r14
testq %r14, %r14
je 0x2039
xorl %r13d, %r13d
movq %r13, %rcx
incq %r13
cmpq %rbx, 0x30(%r14,%rcx,8)
jne 0x1d1b
cmpq $0x1, %r13
je 0x1d42
leal -0x2(%r13), %r12d
movq 0x30(%r14,%r12,8), %r15
cmpl $0x3, 0x68(%r15)
jge 0x2075
leaq -0x1(%r13), %rsi
cmpl %esi, 0x68(%r14)
movq %rsi, -0x30(%rbp)
jle 0x1dcb
movl %r13d, %ecx
movq 0x30(%r14,%rcx,8), %r15
cmpl $0x3, 0x68(%r15)
jl 0x1dcb
movq -0x8(%r14,%r13,8), %rcx
movq %rcx, (%rbx,%rax,8)
incl %eax
movl %eax, 0x68(%rbx)
movq %r15, %rdi
xorl %esi, %esi
callq 0x22e6
cltq
movq %rax, -0x8(%r14,%r13,8)
movslq 0x6c(%rbx), %r12
movq %r15, %rdi
xorl %esi, %esi
callq 0x2338
movslq 0x6c(%rbx), %rcx
cmpl %r12d, %ecx
jle 0x1dae
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 0x1d98
movq %rax, 0x30(%rbx,%r12,8)
leal 0x1(%rcx), %eax
movl %eax, 0x6c(%rbx)
movq 0x30(%rbx,%rcx,8), %rax
testq %rax, %rax
movq -0x30(%rbp), %rsi
je 0x1dcb
movq %rbx, 0x70(%rax)
cmpq $0x1, %r13
jne 0x1f12
movq 0x38(%r14), %r13
movq %r14, %rdi
callq 0x22e6
movl %eax, %r15d
movslq 0x68(%r13), %r12
testq %r12, %r12
jle 0x1e01
leaq 0x8(%r13), %rdi
leaq (,%r12,8), %rdx
movq %r13, %rsi
callq 0x1190
movslq %r15d, %rax
movq %rax, (%r13)
incl %r12d
movl %r12d, 0x68(%r13)
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x2338
movl 0x6c(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x2338
movq %rax, %r15
movslq 0x6c(%r13), %r12
testq %r12, %r12
jle 0x1e49
leaq 0x30(%r13), %rsi
leaq 0x38(%r13), %rdi
leaq (,%r12,8), %rdx
callq 0x1190
movq %r15, 0x30(%r13)
incl %r12d
movl %r12d, 0x6c(%r13)
testq %r15, %r15
je 0x1e61
movq $0x0, 0x70(%r15)
movl 0x68(%rbx), %esi
testl %esi, %esi
je 0x2028
leaq 0x30(%r13), %rax
movq %rax, -0x30(%rbp)
leaq 0x8(%r13), %rax
movq %rax, -0x48(%rbp)
leaq 0x38(%r13), %rax
movq %rax, -0x40(%rbp)
decl %esi
movq %rbx, %rdi
callq 0x22e6
movl %eax, %r15d
movslq 0x68(%r13), %r12
testq %r12, %r12
jle 0x1eae
leaq (,%r12,8), %rdx
movq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x1190
movslq %r15d, %rax
movq %rax, (%r13)
incl %r12d
movl %r12d, 0x68(%r13)
movl 0x6c(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x2338
movq %rax, %r15
movslq 0x6c(%r13), %r12
testq %r12, %r12
jle 0x1eea
leaq (,%r12,8), %rdx
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x1190
movq %r15, 0x30(%r13)
incl %r12d
movl %r12d, 0x6c(%r13)
testq %r15, %r15
je 0x1f02
movq $0x0, 0x70(%r15)
movl 0x68(%rbx), %esi
testl %esi, %esi
jne 0x1e84
jmp 0x2028
addl $-0x2, %r13d
movq 0x30(%r14,%r13,8), %r15
movslq 0x68(%r15), %r12
movq %r14, %rdi
movl %r13d, %esi
callq 0x22e6
movslq 0x68(%r15), %rcx
cmpl %r12d, %ecx
jle 0x1f4b
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 0x1f36
cltq
movq %rax, (%r15,%r12,8)
incl %ecx
movl %ecx, 0x68(%r15)
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x2338
movslq 0x6c(%r15), %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2338
movslq 0x6c(%r15), %rcx
cmpl %r12d, %ecx
jle 0x2004
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 0x1f81
jmp 0x2004
movslq 0x68(%r15), %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x22e6
movslq 0x68(%r15), %rcx
cmpl %r12d, %ecx
jle 0x1fc8
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 0x1fb3
cltq
movq %rax, (%r15,%r12,8)
incl %ecx
movl %ecx, 0x68(%r15)
movslq 0x6c(%r15), %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2338
movslq 0x6c(%r15), %rcx
cmpl %r12d, %ecx
jle 0x2004
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 0x1fee
movq %rax, 0x30(%r15,%r12,8)
leal 0x1(%rcx), %eax
movl %eax, 0x6c(%r15)
movq 0x30(%r15,%rcx,8), %rax
testq %rax, %rax
je 0x201e
movq %r15, 0x70(%rax)
cmpl $0x0, 0x68(%rbx)
jne 0x1f99
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
callq 0x17ca
jmp 0x1cfa
testl %eax, %eax
jne 0x20e4
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x20e4
movq -0x38(%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 0x2085
movq (%rbx), %rdx
movq %rdx, 0x8(%rbx)
movq %rcx, (%rbx)
incl %eax
movl %eax, 0x68(%rbx)
movl 0x68(%r15), %esi
decl %esi
movq %r15, %rdi
callq 0x22e6
cltq
movq %rax, (%r14,%r12,8)
movl 0x6c(%r15), %esi
decl %esi
movq %r15, %rdi
callq 0x2338
movq %rax, %r14
movslq 0x6c(%rbx), %r15
testq %r15, %r15
jle 0x20d0
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 0x20e4
movq %rbx, 0x70(%r14)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop