// File: /workspace/github_cpp_makefile_star2_O3/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 0x2fd0
movq %rdi, %rbx
movq 0x10(%rdi), %rax
movq -0x10(%rax), %r14
movq %r14, -0x38(%rbp)
movq -0x8(%rax), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x2db8
movq 0x922a(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x2db0
incl 0x8(%rax)
jmp 0x2db8
lock
incl 0x8(%rax)
movq -0x38(%rbp), %r14
movslq 0x20(%rbx), %rax
movq 0x60(%r14), %rcx
shlq $0x6, %rax
leaq -0x70(%rbp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %rdi
callq 0x31aa
movslq 0x20(%rbx), %rax
cmpb $0x1, 0x79(%r14)
jne 0x2ee5
incl %eax
movq 0x68(%r14), %rcx
subq 0x60(%r14), %rcx
shrq $0x6, %rcx
cmpl %ecx, %eax
jl 0x2f9e
movq 0x91c3(%rip), %rcx      # 0xbfd0
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x2f95
movq -0x10(%rax), %r15
movq -0x8(%rax), %r14
testq %r14, %r14
je 0x2e3e
cmpb $0x0, (%rcx)
je 0x2e4f
incl 0x8(%r14)
movq (%rbx), %rdi
movq %r15, -0x48(%rbp)
movq %r14, -0x40(%rbp)
jmp 0x2e64
movq (%rbx), %rdi
movq %r15, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
jmp 0x2e68
lock
incl 0x8(%r14)
cmpb $0x0, (%rcx)
movq (%rbx), %rdi
movq %r15, -0x48(%rbp)
movq %r14, -0x40(%rbp)
je 0x2ede
incl 0x8(%r14)
leaq -0x48(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x365a
movq %rax, %r13
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2e86
callq 0x277e
movq 0x68(%r15), %r12
cmpq %r12, %r13
je 0x2ea2
movq 0x60(%r15), %rax
movq %r13, %rcx
subq %rax, %rcx
shrq $0x6, %rcx
movl %ecx, 0x20(%rbx)
jmp 0x2ebc
movq 0x10(%rbx), %rax
leaq -0x10(%rax), %rcx
movq %rcx, 0x10(%rbx)
movq -0x8(%rax), %rdi
testq %rdi, %rdi
je 0x2ebc
callq 0x277e
testq %r14, %r14
je 0x2ec9
movq %r14, %rdi
callq 0x277e
cmpq %r12, %r13
movq 0x90fd(%rip), %rcx      # 0xbfd0
je 0x2e0d
jmp 0x2fa1
lock
incl 0x8(%r14)
jmp 0x2e68
movq (%rbx), %rsi
movq 0x48(%r14), %rcx
movq 0x8(%rcx,%rax,8), %rdx
leaq -0x60(%rbp), %rdi
callq 0x353c
leaq 0x8(%rbx), %r14
movaps -0x60(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movups %xmm0, -0x38(%rbp)
testq %rdi, %rdi
je 0x2f29
callq 0x277e
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2f29
callq 0x277e
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0x311a
leaq -0x60(%rbp), %r15
leaq -0x38(%rbp), %r12
movq -0x38(%rbp), %rax
cmpb $0x0, 0x79(%rax)
jne 0x2f91
movq (%rbx), %rsi
movq 0x48(%rax), %rax
movq (%rax), %rdx
movq %r15, %rdi
callq 0x353c
movaps -0x60(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movups %xmm0, -0x38(%rbp)
testq %rdi, %rdi
je 0x2f84
callq 0x277e
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2f84
callq 0x277e
movq %r14, %rdi
movq %r12, %rsi
callq 0x311a
jmp 0x2f3d
xorl %eax, %eax
jmp 0x2f9e
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 0x2fb3
callq 0x21b0
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x2fc1
callq 0x277e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x507f(%rip), %rdi      # 0x8056
leaq 0x5080(%rip), %rsi      # 0x805e
leaq 0x5084(%rip), %rcx      # 0x8069
movl $0x5d, %edx
callq 0x2140
jmp 0x301a
jmp 0x301a
movq %rax, %rbx
jmp 0x302f
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x3009
callq 0x277e
testq %r14, %r14
je 0x301d
movq %r14, %rdi
callq 0x277e
jmp 0x301d
jmp 0x301a
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x302f
callq 0x21b0
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x303d
callq 0x277e
movq %rbx, %rdi
callq 0x2300
nop