// File: rocket323[P]fishdb/./btree.cpp
// Function: fishdb::BTree::Delete(std::shared_ptr<fishdb::MemPage>, std::shared_ptr<fishdb::MemPage>, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int BTree::Delete(std::shared_ptr<MemPage> now, std::shared_ptr<MemPage> parent,
        int child_idx, const std::string &key)
{
    auto iter = LowerBound(now, key);
    size_t p = iter - now->kvs.begin();

    int del_ret = BT_NOT_FOUND;
    if (iter != now->kvs.end() && Equal(iter->key, key))
    {
        if (!now->is_leaf)
        {
            auto left = ReadPage(now->children[p]);
            auto nd = left;
            while (!nd->is_leaf)
                nd = ReadPage(nd->children.back());

            now->kvs[p] = nd->kvs.back();
            Delete(left, now, p, nd->kvs.back().key);
        }
        else
            now->kvs.erase(iter);
        del_ret = BT_OK;
    }
    else if (!now->is_leaf)
        del_ret = Delete(ReadPage(now->children[p]), now, p, key);

    // maintain now
    if (now == m_root)
    {
        if (now->is_leaf || now->kvs.size() > 0) return del_ret;
        assert(now->children.size() == 1);
        m_root = ReadPage(now->children[0]);
    }
    else if ((int)now->kvs.size() < m_min_key_num)
        Maintain(now, parent, child_idx);

    return del_ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rsi, -0x150(%rbp)
movq %rdx, -0x148(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x40f0
movq -0x140(%rbp), %rdi
movq -0x138(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x54c0
movq %rax, -0x130(%rbp)
jmp 0x6637
movq -0x130(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x27e0
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq %rax, -0x68(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x42d0
movq -0x150(%rbp), %rdi
movq %rax, -0x60(%rbp)
movl $0xfffffffe, -0x6c(%rbp) # imm = 0xFFFFFFFE
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4380
movq %rax, -0x78(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4340
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x151(%rbp)
jne 0x66b9
jmp 0x66db
leaq -0x38(%rbp), %rdi
callq 0x7e80
movq -0x140(%rbp), %rdi
movq %rax, %rsi
movq -0x30(%rbp), %rdx
callq 0x5460
movb %al, -0x151(%rbp)
movb -0x151(%rbp), %al
testb $0x1, %al
jne 0x66ea
jmp 0x6961
movq -0x150(%rbp), %rdi
callq 0x4190
testb $0x1, 0x79(%rax)
jne 0x6914
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
movq -0x60(%rbp), %rsi
callq 0x41b0
movq -0x140(%rbp), %rsi
movq (%rax), %rdx
leaq -0x88(%rbp), %rdi
callq 0x5390
leaq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x40f0
leaq -0x98(%rbp), %rdi
callq 0x4190
movb 0x79(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x675c
jmp 0x67d8
leaq -0x98(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x4260
movq -0x140(%rbp), %rsi
movq (%rax), %rdx
leaq -0xa8(%rbp), %rdi
callq 0x5390
jmp 0x678c
leaq -0x98(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x41d0
leaq -0xa8(%rbp), %rdi
callq 0x27e0
jmp 0x6745
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x27e0
jmp 0x6c04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x68f7
leaq -0x98(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x8640
movq -0x150(%rbp), %rdi
movq %rax, -0x160(%rbp)
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
movq -0x60(%rbp), %rsi
callq 0x43f0
movq -0x160(%rbp), %rsi
movq %rax, %rdi
callq 0x8680
jmp 0x6824
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x178(%rbp)
leaq -0x88(%rbp), %rsi
callq 0x40f0
movq -0x150(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x40f0
movl -0x60(%rbp), %eax
movl %eax, -0x164(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x8640
movq -0x140(%rbp), %rdi
movq -0x178(%rbp), %rsi
movq -0x170(%rbp), %rdx
movl -0x164(%rbp), %ecx
movq %rax, %r8
callq 0x65d0
jmp 0x689e
leaq -0xc8(%rbp), %rdi
callq 0x27e0
leaq -0xb8(%rbp), %rdi
callq 0x27e0
leaq -0x98(%rbp), %rdi
callq 0x27e0
leaq -0x88(%rbp), %rdi
callq 0x27e0
jmp 0x6955
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x27e0
leaq -0xb8(%rbp), %rdi
callq 0x27e0
leaq -0x98(%rbp), %rdi
callq 0x27e0
leaq -0x88(%rbp), %rdi
callq 0x27e0
jmp 0x6c04
movq -0x150(%rbp), %rdi
callq 0x4190
addq $0x60, %rax
movq %rax, -0x180(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x7f20
movq -0x180(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x86c0
movq %rax, -0xd8(%rbp)
movl $0x0, -0x6c(%rbp)
jmp 0x6a41
movq -0x150(%rbp), %rdi
callq 0x4190
testb $0x1, 0x79(%rax)
jne 0x6a3f
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
movq -0x60(%rbp), %rsi
callq 0x41b0
movq -0x140(%rbp), %rsi
movq (%rax), %rdx
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x5390
movq -0x150(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x40f0
movq -0x140(%rbp), %rdi
movq -0x198(%rbp), %rsi
movq -0x190(%rbp), %rdx
movl -0x60(%rbp), %ecx
movq -0x30(%rbp), %r8
callq 0x65d0
movl %eax, -0x184(%rbp)
jmp 0x69f3
movl -0x184(%rbp), %eax
movl %eax, -0x6c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x27e0
leaq -0xe8(%rbp), %rdi
callq 0x27e0
jmp 0x6a3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x27e0
leaq -0xe8(%rbp), %rdi
callq 0x27e0
jmp 0x6c04
jmp 0x6a41
movq -0x150(%rbp), %rdi
movq -0x140(%rbp), %rsi
addq $0x70, %rsi
callq 0x8730
testb $0x1, %al
jne 0x6a61
jmp 0x6b35
movq -0x150(%rbp), %rdi
callq 0x4190
testb $0x1, 0x79(%rax)
jne 0x6a91
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4200
cmpq $0x0, %rax
jbe 0x6a9c
movl -0x6c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6bf8
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
callq 0x7ec0
cmpq $0x1, %rax
jne 0x6abc
jmp 0x6adb
leaq 0xb75b(%rip), %rdi      # 0x1221e
leaq 0xb63b(%rip), %rsi      # 0x12105
movl $0xc3, %edx
leaq 0xb762(%rip), %rcx      # 0x12238
callq 0x2150
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x41b0
movq -0x140(%rbp), %rsi
movq (%rax), %rdx
leaq -0x108(%rbp), %rdi
callq 0x5390
movq -0x140(%rbp), %rdi
addq $0x70, %rdi
leaq -0x108(%rbp), %rsi
callq 0x41d0
leaq -0x108(%rbp), %rdi
callq 0x27e0
jmp 0x6bf2
movq -0x150(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4200
movq -0x140(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jge 0x6bf0
movq -0x150(%rbp), %rsi
leaq -0x118(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x40f0
movq -0x148(%rbp), %rsi
leaq -0x128(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x40f0
movq -0x140(%rbp), %rdi
movq -0x1a8(%rbp), %rsi
movq -0x1a0(%rbp), %rdx
movl -0x24(%rbp), %ecx
callq 0x6c10
jmp 0x6bb0
leaq -0x128(%rbp), %rdi
callq 0x27e0
leaq -0x118(%rbp), %rdi
callq 0x27e0
jmp 0x6bf0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x27e0
leaq -0x118(%rbp), %rdi
callq 0x27e0
jmp 0x6c04
jmp 0x6bf2
movl -0x6c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2370
nopl (%rax)