// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r14
movq %rdi, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
movq (%rsi), %rax
movq 0x38(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x40(%rax), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x9944
movq 0x5692(%rip), %rcx      # 0xefc8
cmpb $0x0, (%rcx)
je 0x9940
incl 0x8(%rax)
jmp 0x9944
lock
incl 0x8(%rax)
movq 0x60(%rsp), %rcx
movq (%rcx), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x9974
movq 0x5662(%rip), %rcx      # 0xefc8
cmpb $0x0, (%rcx)
je 0x9970
incl 0x8(%rax)
jmp 0x9974
lock
incl 0x8(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x9c4d
leaq 0x18(%rsp), %r12
leaq 0x20(%rsp), %rbp
leaq 0xf(%rsp), %r13
movq 0x5629(%rip), %r15      # 0xefd0
movq 0x10(%rsp), %rsi
movq 0x38(%rsi), %rax
testq %rax, %rax
je 0x99ba
cmpq %r14, (%rsi)
jbe 0x99c1
cmpq 0x40(%rsp), %rsi
jne 0x99fe
movq %rax, 0x10(%rsp)
addq $0x40, %rsi
movq %r12, %rdi
callq 0x9478
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x99f4
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x99a7
movq %rbp, %rdi
callq 0x9f0a
jmp 0x99a7
cmpq $0x0, 0x28(%rsi)
jne 0x9af4
movq 0x55e0(%rip), %rdi      # 0xeff0
movq 0x60(%rsp), %rsi
callq 0x9d0b
movb $0xa, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32b0
movl $0xd, %edx
movq 0x55b5(%rip), %rdi      # 0xeff0
leaq 0x1988(%rip), %rsi      # 0xb3ca
callq 0x32b0
movq 0x55a2(%rip), %rdi      # 0xeff0
movq %r14, %rsi
callq 0x31a0
movb $0xa, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32b0
movl $0xc, %edx
movq 0x5579(%rip), %rdi      # 0xeff0
leaq 0x195a(%rip), %rsi      # 0xb3d8
callq 0x32b0
movq 0x40(%rsp), %rsi
movq 0x5561(%rip), %rdi      # 0xeff0
callq 0x31d0
movb $0xa, 0xf(%rsp)
movl $0x1, %edx
movq 0x554b(%rip), %rdi      # 0xeff0
movq %r13, %rsi
callq 0x32b0
movq 0x20(%rsp), %rbx
cmpq 0x28(%rsp), %rbx
je 0x9adf
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x31a0
movb $0x9, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32b0
addq $0x8, %rbx
jmp 0x9ab2
movb $0xa, 0xf(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x32b0
movq 0x10(%rsp), %rsi
movq 0x28(%rsi), %rax
movq %rax, 0x10(%rsp)
addq $0x30, %rsi
movq %r12, %rdi
callq 0x9478
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x9b30
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x9b38
movq %rbp, %rdi
callq 0x9f0a
movq 0x10(%rsp), %rcx
movb $0x1, %al
testq %rcx, %rcx
je 0x9b93
cmpq $0x0, 0x28(%rcx)
je 0x9b93
cmpq %r14, (%rcx)
jne 0x9b93
movq 0x18(%rsp), %rax
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x9b7e
movq 0x5462(%rip), %rax      # 0xefc8
cmpb $0x0, (%rax)
je 0x9b75
movq 0x50(%rsp), %rax
incl 0x8(%rax)
jmp 0x9b7e
movq 0x50(%rsp), %rax
lock
incl 0x8(%rax)
cmpq $0x0, 0x28(%rcx)
je 0x9b91
cmpq $0x0, 0x38(%rcx)
setne %bl
xorl %eax, %eax
jmp 0x9b95
xorl %eax, %eax
xorl %ebx, %ebx
cmpq $0x0, 0x50(%rsp)
sete %cl
orb %al, %cl
jne 0x9bac
movq 0x50(%rsp), %rdi
callq 0x9408
testb %bl, %bl
jne 0x9c5a
movq 0x40(%rsp), %rsi
movq 0x58(%rsi), %rcx
movq 0x10(%rsp), %rax
testq %rcx, %rcx
je 0x9c5f
movq 0x58(%rax), %rdx
testq %rdx, %rdx
je 0x9c0a
movq %rdx, 0x10(%rsp)
addq $0x60, %rax
movq %r12, %rdi
movq %rax, %rsi
callq 0x9478
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x9c1d
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x9c25
movq %rcx, 0x10(%rsp)
addq $0x60, %rsi
movq %r12, %rdi
callq 0x9478
jmp 0x9c25
movq %rbp, %rdi
callq 0x9f0a
movq 0x40(%rsp), %rsi
movq 0x58(%rsi), %rax
movq %rax, 0x40(%rsp)
addq $0x60, %rsi
leaq 0x48(%rsp), %rdi
callq 0x9478
cmpq $0x0, 0x10(%rsp)
jne 0x99a7
movq 0x58(%rsp), %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
jmp 0x9c80
movq 0x10(%rsp), %rax
movq 0x58(%rsp), %rbx
movq %rax, (%rbx)
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movq 0x18(%rsp), %rcx
movq %rax, 0x18(%rsp)
movq %rcx, 0x8(%rbx)
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x9c97
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x9ca6
callq 0x9408
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x9cb5
callq 0x9408
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9ccb
jmp 0x9ccb
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x9ce5
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x9cf4
callq 0x9408
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x9d03
callq 0x9408
movq %rbx, %rdi
callq 0x33f0