// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %r13
movl %ecx, %r15d
movq %rdx, -0x70(%rbp)
movq %rsi, %r12
movq %rdi, %rbx
leaq -0xe0(%rbp), %r14
movq %r14, %rdi
callq 0x2ee6
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, -0x48(%rbp)
movq %r13, %rdx
callq 0x3308
movq %rax, %r13
leaq -0xd8(%rbp), %rdi
callq 0x2688
movq (%r12), %rdi
movq %r13, %r14
subq 0x60(%rdi), %r14
movq %r14, %rax
sarq $0x6, %rax
movq %rax, -0x40(%rbp)
cmpq 0x68(%rdi), %r13
movq %r12, %rsi
movl %r15d, %r12d
movq %rsi, -0x38(%rbp)
je 0x3b7f
movq %rbx, %rdi
movq %r13, %rsi
movq -0x48(%rbp), %rdx
callq 0x32c6
movq -0x38(%rbp), %rsi
movq (%rsi), %rdi
testb %al, %al
je 0x3b7f
cmpb $0x0, 0x79(%rdi)
je 0x3bfe
addq $0x60, %rdi
movq %r13, %rsi
callq 0x4714
jmp 0x3cf9
cmpb $0x0, 0x79(%rdi)
je 0x3b8e
pushq $-0x2
popq %r13
jmp 0x3d00
movl %r12d, -0x2c(%rbp)
movq 0x48(%rdi), %rax
movq -0x40(%rbp), %r14
movq (%rax,%r14,8), %rdx
leaq -0xb0(%rbp), %r15
movq %r15, %rdi
movq %rsi, %r13
movq %rbx, %rsi
callq 0x3274
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x2ee6
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %r14d, %ecx
movq -0x48(%rbp), %r8
callq 0x3ad0
movl %eax, %r13d
leaq -0x98(%rbp), %rdi
callq 0x2688
leaq -0xa8(%rbp), %rdi
callq 0x2688
movl -0x2c(%rbp), %r12d
jmp 0x3cfc
movl %r12d, -0x2c(%rbp)
movq 0x48(%rdi), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq -0x58(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3274
leaq -0x68(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x2ee6
leaq -0xe8(%rbp), %r13
leaq -0xf0(%rbp), %r12
movq -0x68(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x3c6c
movq 0x50(%rax), %rax
movq -0x8(%rax), %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3274
movq %r15, %rdi
movq %r12, %rsi
callq 0x30d2
movq %r13, %rdi
callq 0x2688
jmp 0x3c3a
movq 0x68(%rax), %rsi
addq $-0x40, %rsi
movq -0x38(%rbp), %r12
movq (%r12), %rax
addq 0x60(%rax), %r14
movq %r14, %rdi
callq 0x46ea
leaq -0xd0(%rbp), %r14
leaq -0x58(%rbp), %rsi
movq %r14, %rdi
callq 0x2ee6
leaq -0xc0(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x2ee6
movq -0x68(%rbp), %rax
movq 0x68(%rax), %r8
addq $-0x40, %r8
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq -0x40(%rbp), %rcx
callq 0x3ad0
movl -0x2c(%rbp), %r12d
leaq -0xb8(%rbp), %rdi
callq 0x2688
leaq -0xc8(%rbp), %rdi
callq 0x2688
leaq -0x60(%rbp), %rdi
callq 0x2688
leaq -0x50(%rbp), %rdi
callq 0x2688
xorl %r13d, %r13d
movq -0x38(%rbp), %rsi
movq (%rsi), %rax
cmpq 0x70(%rbx), %rax
je 0x3d61
movq 0x68(%rax), %rcx
subq 0x60(%rax), %rcx
shrq $0x6, %rcx
cmpl %ecx, 0x48(%rbx)
jle 0x3d71
leaq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x2ee6
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movq -0x70(%rbp), %rsi
callq 0x2ee6
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x3e52
leaq -0x78(%rbp), %rdi
callq 0x2688
leaq -0x88(%rbp), %rdi
callq 0x2688
jmp 0x3d71
cmpb $0x0, 0x79(%rax)
jne 0x3d71
movq 0x68(%rax), %rcx
cmpq 0x60(%rax), %rcx
je 0x3d86
movl %r13d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rax), %rcx
movq 0x50(%rax), %rax
subq %rcx, %rax
cmpq $0x8, %rax
jne 0x3dbe
leaq 0x70(%rbx), %r14
movq (%rcx), %rdx
leaq -0x58(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3274
movq %r14, %rdi
movq %r15, %rsi
callq 0x30d2
leaq -0x50(%rbp), %rdi
jmp 0x3d5a
leaq 0x3445(%rip), %rdi      # 0x720a
leaq 0x3329(%rip), %rsi      # 0x70f5
leaq 0x3451(%rip), %rcx      # 0x7224
movl $0xc3, %edx
callq 0x2140
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x2688
leaq -0xc8(%rbp), %rdi
callq 0x2688
jmp 0x3e38
jmp 0x3e35
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x2688
leaq -0x88(%rbp), %rdi
jmp 0x3e45
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x2688
leaq -0xa8(%rbp), %rdi
jmp 0x3e45
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
jmp 0x3e45
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2688
leaq -0x50(%rbp), %rdi
callq 0x2688
movq %rbx, %rdi
callq 0x2310