// File: /workspace/github_cpp_makefile_star2_O1/rocket323[P]fishdb/./iter.cpp
// Function: fishdb::Iterator::Next()
void Iterator::Next()
{
    assert(Valid());

    auto now = m_stack.back();
    std::string cur_key = now->kvs[m_kv_idx].key;

    // 1. we are leaf
    if (now->is_leaf)
    {
        // 1.a leaf have more kv
        if (m_kv_idx + 1 < (int)now->kvs.size())
        {
            m_kv_idx++;
            return;
        }

        // 1.b no more kv, find first parent which has more kv
        while (m_stack.size() > 0)
        {
            auto nd = m_stack.back();
            auto iter = m_btree->UpperBound(nd, cur_key);
            if (iter != nd->kvs.end())
            {
                m_kv_idx = iter - nd->kvs.begin();
                return;
            }
            else
                m_stack.pop_back();
        }
    }
    // 2. we are inner node
    else
    {
        int p = m_kv_idx + 1;
        now = m_btree->ReadPage(now->children[p]);
        m_stack.push_back(now);
        while (!now->is_leaf)
        {
            now = m_btree->ReadPage(now->children[0]);
            m_stack.push_back(now);
        }
        m_kv_idx = 0;
        return;
    }

    // invalid if came here
    m_valid = false;
    m_kv_idx = -1;
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpb $0x0, 0x24(%rdi)
je 0x2fac
movq %rdi, %rbx
movq 0x10(%rdi), %rax
movq -0x10(%rax), %rcx
movq %rcx, -0x38(%rbp)
movq -0x8(%rax), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x2da2
movq 0x923c(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x2d9e
incl 0x8(%rax)
jmp 0x2da2
lock
incl 0x8(%rax)
movq -0x38(%rbp), %rax
movslq 0x20(%rbx), %rcx
movq 0x60(%rax), %rax
shlq $0x6, %rcx
leaq -0x70(%rbp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %rdi
callq 0x317e
movq -0x38(%rbp), %rcx
movslq 0x20(%rbx), %rax
cmpb $0x1, 0x79(%rcx)
jne 0x2ec1
incl %eax
movq 0x68(%rcx), %rdx
subq 0x60(%rcx), %rdx
shrq $0x6, %rdx
cmpl %edx, %eax
jl 0x2f7a
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x2f71
movq -0x10(%rax), %r12
movq -0x8(%rax), %r14
testq %r14, %r14
je 0x2e29
movq 0x91b7(%rip), %rax      # 0xbfd0
cmpb $0x0, (%rax)
je 0x2e24
incl 0x8(%r14)
jmp 0x2e29
lock
incl 0x8(%r14)
movq (%rbx), %rdi
movq %r12, -0x60(%rbp)
movq %r14, -0x58(%rbp)
testq %r14, %r14
je 0x2e50
movq 0x9190(%rip), %rax      # 0xbfd0
cmpb $0x0, (%rax)
je 0x2e4b
incl 0x8(%r14)
jmp 0x2e50
lock
incl 0x8(%r14)
leaq -0x60(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x3638
movq %rax, %r13
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2e6e
callq 0x277e
movq 0x68(%r12), %r15
cmpq %r15, %r13
je 0x2e8c
movq 0x60(%r12), %rax
movq %r13, %rcx
subq %rax, %rcx
shrq $0x6, %rcx
movl %ecx, 0x20(%rbx)
jmp 0x2ea6
movq 0x10(%rbx), %rax
leaq -0x10(%rax), %rcx
movq %rcx, 0x10(%rbx)
movq -0x8(%rax), %rdi
testq %rdi, %rdi
je 0x2ea6
callq 0x277e
testq %r14, %r14
je 0x2eb3
movq %r14, %rdi
callq 0x277e
cmpq %r15, %r13
je 0x2df7
jmp 0x2f7d
movq (%rbx), %rsi
movq 0x48(%rcx), %rcx
movq 0x8(%rcx,%rax,8), %rdx
leaq -0x50(%rbp), %rdi
callq 0x351a
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movups %xmm0, -0x38(%rbp)
testq %rdi, %rdi
je 0x2ef3
callq 0x277e
leaq 0x8(%rbx), %r14
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2f05
callq 0x277e
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0x30f6
leaq -0x50(%rbp), %r15
leaq -0x38(%rbp), %r12
movq -0x38(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x2f6d
movq (%rbx), %rsi
movq 0x48(%rax), %rax
movq (%rax), %rdx
movq %r15, %rdi
callq 0x351a
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movups %xmm0, -0x38(%rbp)
testq %rdi, %rdi
je 0x2f52
callq 0x277e
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2f60
callq 0x277e
movq %r14, %rdi
movq %r12, %rsi
callq 0x30f6
jmp 0x2f19
xorl %eax, %eax
jmp 0x2f7a
movb $0x0, 0x24(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x20(%rbx)
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x2f8f
callq 0x21b0
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x2f9d
callq 0x277e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50a3(%rip), %rdi      # 0x8056
leaq 0x50a4(%rip), %rsi      # 0x805e
leaq 0x50a8(%rip), %rcx      # 0x8069
movl $0x5d, %edx
callq 0x2140
jmp 0x2ff6
jmp 0x2ff6
movq %rax, %rbx
jmp 0x300b
movq %rax, %rbx
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2fe5
callq 0x277e
testq %r14, %r14
je 0x2ff9
movq %r14, %rdi
callq 0x277e
jmp 0x2ff9
jmp 0x2ff6
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x300b
callq 0x21b0
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x3019
callq 0x277e
movq %rbx, %rdi
callq 0x2300
nop