// File: 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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x3f70
testb $0x1, %al
jne 0x3b75
jmp 0x3b77
jmp 0x3b96
leaq 0xe4d5(%rip), %rdi      # 0x12053
leaq 0xe4d6(%rip), %rsi      # 0x1205b
movl $0x5d, %edx
leaq 0xe4d5(%rip), %rcx      # 0x12066
callq 0x2150
movq -0xb0(%rbp), %rdi
addq $0x8, %rdi
callq 0x43b0
movq %rax, %rsi
leaq -0x18(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x40f0
movq -0xb8(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
movq -0xb0(%rbp), %rax
addq $0x60, %rdi
movslq 0x20(%rax), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x20e0
jmp 0x3bea
leaq -0x18(%rbp), %rdi
callq 0x4190
testb $0x1, 0x79(%rax)
je 0x3dbc
movq -0xb0(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4200
movq %rax, %rcx
movl -0xbc(%rbp), %eax
cmpl %ecx, %eax
jge 0x3c5f
movq -0xb0(%rbp), %rax
movl 0x20(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rax)
movl $0x1, -0x48(%rbp)
jmp 0x3f30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3f54
jmp 0x3c61
movq -0xb0(%rbp), %rdi
addq $0x8, %rdi
callq 0x4240
cmpq $0x0, %rax
jbe 0x3db7
movq -0xb0(%rbp), %rdi
addq $0x8, %rdi
callq 0x43b0
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x40f0
movq -0xb0(%rbp), %rax
movq -0xe0(%rbp), %rsi
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
leaq -0x70(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x40f0
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x5590
movq %rax, -0xc8(%rbp)
jmp 0x3ce6
movq -0xc8(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x27e0
leaq -0x58(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4380
movq %rax, -0x78(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4340
testb $0x1, %al
jne 0x3d26
jmp 0x3d85
leaq -0x58(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x60, %rdi
callq 0x4310
movq %rax, -0x80(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x42d0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x1, -0x48(%rbp)
jmp 0x3d9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x27e0
leaq -0x58(%rbp), %rdi
callq 0x27e0
jmp 0x3f4b
movq -0xb0(%rbp), %rdi
addq $0x8, %rdi
callq 0x4410
movl $0x0, -0x48(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x27e0
movl -0x48(%rbp), %eax
testl %eax, %eax
jne 0x3f30
jmp 0x3db2
jmp 0x3c61
jmp 0x3f17
movq -0xb0(%rbp), %rax
movl 0x20(%rax), %ecx
incl %ecx
movl %ecx, -0x84(%rbp)
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
movslq -0x84(%rbp), %rsi
callq 0x41b0
movq -0xe8(%rbp), %rsi
movq (%rax), %rdx
leaq -0x98(%rbp), %rdi
callq 0x5390
jmp 0x3e0c
leaq -0x18(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
leaq -0x98(%rbp), %rsi
movq %rsi, -0xf8(%rbp)
callq 0x41d0
movq -0xf8(%rbp), %rdi
callq 0x27e0
movq -0xb0(%rbp), %rdi
movq -0xf0(%rbp), %rsi
addq $0x8, %rdi
callq 0x4120
jmp 0x3e4f
jmp 0x3e51
leaq -0x18(%rbp), %rdi
callq 0x4190
movb 0x79(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3e68
jmp 0x3f00
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x100(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
addq $0x48, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x41b0
movq -0x100(%rbp), %rsi
movq (%rax), %rdx
leaq -0xa8(%rbp), %rdi
callq 0x5390
jmp 0x3eaa
leaq -0x18(%rbp), %rdi
movq %rdi, -0x108(%rbp)
leaq -0xa8(%rbp), %rsi
movq %rsi, -0x110(%rbp)
callq 0x41d0
movq -0x110(%rbp), %rdi
callq 0x27e0
movq -0xb0(%rbp), %rdi
movq -0x108(%rbp), %rsi
addq $0x8, %rdi
callq 0x4120
jmp 0x3eed
jmp 0x3e51
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3f4b
movq -0xb0(%rbp), %rax
movl $0x0, 0x20(%rax)
movl $0x1, -0x48(%rbp)
jmp 0x3f30
movq -0xb0(%rbp), %rax
movb $0x0, 0x24(%rax)
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movl $0x1, -0x48(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2140
leaq -0x18(%rbp), %rdi
callq 0x27e0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x2140
leaq -0x18(%rbp), %rdi
callq 0x27e0
movq -0x40(%rbp), %rdi
callq 0x2370
nopw %cs:(%rax,%rax)