// File: SeanChao[P]KV-Store/./skiplist.h
// Function: SkipList<unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::skipSearch(unsigned long) const
std::shared_ptr<typename SkipList<Key, Value>::Node>
SkipList<Key, Value>::skipSearch(Key key) const {
    std::shared_ptr<Node> temp = head->succ;
    std::shared_ptr<Node> levelHead = head;  // marks the head of current level
    std::vector<Key> path;
    while (temp) {
        while ((temp->succ && temp->key <= key) || temp == levelHead) {
            temp = temp->succ;
            path.push_back(temp->key);
        }
        if (!temp->pred) {
            std::cerr << *this << '\n';
            std::cerr << "ERR with key " << key << '\n';
            std::cerr << "level head @" << levelHead << '\n';
            for (auto n = path.begin(); n != path.end(); n++) {
                std::cout << *n << '\t';
            }
            std::cout << '\n';
        }
        temp = temp->pred;

        path.push_back(temp->key);
        // find a match
        // temp->pred is to prevent visiting head's
        if (temp && temp->pred && temp->key == key && valid(temp)) {
            return temp;
        }
        // return if no lower level exists
        if (!levelHead->below) return temp;
        // go to lower level or the head of lower level
        if (temp->below) {
            temp = temp->below;
            path.push_back(temp->key);
        } else
            temp = levelHead->below;
        levelHead = levelHead->below;
        // if (!temp->below) return temp;
        // temp = temp->below;
    }
    return nullptr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0xa8(%rbp)
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0xc7b0
movq %rax, %rsi
addq $0x38, %rsi
leaq -0x28(%rbp), %rdi
callq 0xc8a0
movq -0x98(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xc8a0
leaq -0x50(%rbp), %rdi
callq 0x10680
leaq -0x28(%rbp), %rdi
callq 0xc800
testb $0x1, %al
jne 0x101d3
jmp 0x10608
jmp 0x101d5
leaq -0x28(%rbp), %rdi
callq 0xc7b0
movq %rax, %rdi
addq $0x38, %rdi
callq 0xc800
testb $0x1, %al
jne 0x101f0
jmp 0x1020a
leaq -0x28(%rbp), %rdi
callq 0xc7b0
movq (%rax), %rcx
movb $0x1, %al
cmpq -0x18(%rbp), %rcx
movb %al, -0xa9(%rbp)
jbe 0x1021d
leaq -0x28(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x106a0
movb %al, -0xa9(%rbp)
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0x10229
jmp 0x1027c
leaq -0x28(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0xc7b0
movq -0xb8(%rbp), %rdi
movq %rax, %rsi
addq $0x38, %rsi
callq 0xc7d0
movq -0xb8(%rbp), %rdi
callq 0xc7b0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x106e0
jmp 0x10266
jmp 0x101d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x1064d
leaq -0x28(%rbp), %rdi
callq 0xc7b0
movq %rax, %rdi
addq $0x28, %rdi
callq 0xc800
testb $0x1, %al
jne 0x103e1
movq -0x98(%rbp), %rsi
movq 0xed49(%rip), %rdi      # 0x1eff0
callq 0x10750
movq %rax, -0xc0(%rbp)
jmp 0x102b5
movq -0xc0(%rbp), %rdi
movl $0xa, %esi
callq 0x43a0
jmp 0x102c8
movq 0xed21(%rip), %rdi      # 0x1eff0
leaq 0x612a(%rip), %rsi      # 0x16400
callq 0x42f0
movq %rax, -0xc8(%rbp)
jmp 0x102e4
movq -0xc8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x4090
movq %rax, -0xd0(%rbp)
jmp 0x102fd
movq -0xd0(%rbp), %rdi
movl $0xa, %esi
callq 0x43a0
jmp 0x10310
movq 0xecd9(%rip), %rdi      # 0x1eff0
leaq 0x60f0(%rip), %rsi      # 0x1640e
callq 0x42f0
movq %rax, -0xd8(%rbp)
jmp 0x1032c
movq -0xd8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x10950
movq %rax, -0xe0(%rbp)
jmp 0x10345
movq -0xe0(%rbp), %rdi
movl $0xa, %esi
callq 0x43a0
jmp 0x10358
leaq -0x50(%rbp), %rdi
callq 0x10990
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x10a00
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x109c0
testb $0x1, %al
jne 0x10385
jmp 0x103cc
leaq -0x68(%rbp), %rdi
callq 0x10a30
movq (%rax), %rsi
movq 0xec38(%rip), %rdi      # 0x1efd0
callq 0x4090
movq %rax, -0xe8(%rbp)
jmp 0x103a6
movq -0xe8(%rbp), %rdi
movl $0x9, %esi
callq 0x43a0
jmp 0x103b9
jmp 0x103bb
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
callq 0x10a50
movq %rax, -0x78(%rbp)
jmp 0x10365
movq 0xebfd(%rip), %rdi      # 0x1efd0
movl $0xa, %esi
callq 0x43a0
jmp 0x103df
jmp 0x103e1
leaq -0x28(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0xc7b0
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
addq $0x28, %rsi
callq 0xc7d0
movq -0xf0(%rbp), %rdi
callq 0xc7b0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x106e0
jmp 0x1041e
leaq -0x28(%rbp), %rdi
callq 0xc800
movb %al, %cl
movb $0x0, -0x89(%rbp)
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0xf1(%rbp)
jne 0x10442
jmp 0x104c9
leaq -0x28(%rbp), %rdi
callq 0xc7b0
movq %rax, %rdi
addq $0x28, %rdi
callq 0xc800
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0xf1(%rbp)
jne 0x10468
jmp 0x104c9
leaq -0x28(%rbp), %rdi
callq 0xc7b0
movq (%rax), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0xf1(%rbp)
jne 0x104c9
leaq -0x88(%rbp), %rdi
movq %rdi, -0x100(%rbp)
leaq -0x28(%rbp), %rsi
callq 0xc8a0
movq -0x98(%rbp), %rdi
movq -0x100(%rbp), %rsi
movb $0x1, -0x89(%rbp)
callq 0xc820
movb %al, -0xf2(%rbp)
jmp 0x104bb
movb -0xf2(%rbp), %al
movb %al, -0xf1(%rbp)
jmp 0x104c9
movb -0xf1(%rbp), %al
movb %al, -0x101(%rbp)
testb $0x1, -0x89(%rbp)
jne 0x104e0
jmp 0x104ec
leaq -0x88(%rbp), %rdi
callq 0xc8d0
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0x104f8
jmp 0x1053f
movq -0xa8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x10a90
movl $0x1, -0x90(%rbp)
jmp 0x10622
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
testb $0x1, -0x89(%rbp)
jne 0x1052e
jmp 0x1053a
leaq -0x88(%rbp), %rdi
callq 0xc8d0
jmp 0x1064d
leaq -0x38(%rbp), %rdi
callq 0xc7b0
movq %rax, %rdi
addq $0x58, %rdi
callq 0xc800
testb $0x1, %al
jne 0x10577
movq -0xa8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x10a90
movl $0x1, -0x90(%rbp)
jmp 0x10622
leaq -0x28(%rbp), %rdi
callq 0xc7b0
movq %rax, %rdi
addq $0x58, %rdi
callq 0xc800
testb $0x1, %al
jne 0x10592
jmp 0x105d1
leaq -0x28(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0xc7b0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
addq $0x58, %rsi
callq 0xc7d0
movq -0x110(%rbp), %rdi
callq 0xc7b0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x106e0
jmp 0x105cf
jmp 0x105ea
leaq -0x38(%rbp), %rdi
callq 0xc7b0
movq %rax, %rsi
addq $0x58, %rsi
leaq -0x28(%rbp), %rdi
callq 0xc7d0
leaq -0x38(%rbp), %rdi
callq 0xc7b0
movq %rax, %rsi
addq $0x58, %rsi
leaq -0x38(%rbp), %rdi
callq 0xc7d0
jmp 0x101c1
movq -0xa8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf2a0
movl $0x1, -0x90(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x10ac0
leaq -0x38(%rbp), %rdi
callq 0xc8d0
leaq -0x28(%rbp), %rdi
callq 0xc8d0
movq -0xa0(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0x10ac0
leaq -0x38(%rbp), %rdi
callq 0xc8d0
leaq -0x28(%rbp), %rdi
callq 0xc8d0
movq -0x58(%rbp), %rdi
callq 0x4530
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)