// File: shreyas-gopalakrishna[P]Concurrent-Skip-list/skip_list.cpp
// Function: SkipList::search[abi:cxx11](int)
string SkipList::search(int key){

    // Finds the predecessor and successors 
    vector<Node*> preds(max_level + 1); 
    vector<Node*> succs(max_level + 1);

    for (size_t i = 0; i < preds.size(); i++){
        preds[i] = NULL;
        succs[i] = NULL;
    }

    int found = find(key, preds, succs);

    // If not found return empty.
    if(found == -1){
        return "";
    }

    Node *curr = head; 

    for (int level = max_level; level >= 0; level--){
        while (curr->next[level] != NULL && key > curr->next[level]->get_key()){
            curr = curr->next[level];
        }
    }

    curr = curr->next[0];
    
    // If found, unmarked and fully linked, then return value. Else return empty.
    if ((curr != NULL) && (curr->get_key() == key) && succs[found]->fully_linked && !succs[found]->marked){
        return curr->get_value();
    }else {
        return "";
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xb8(%rbp)
movq %rdi, %rax
movq %rax, -0xb0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl 0xb9bc(%rip), %eax      # 0x17364
incl %eax
cltq
movq %rax, -0xa0(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0xa2d0
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0xcaa0
jmp 0xb9dc
leaq -0x31(%rbp), %rdi
callq 0xa510
movl 0xb979(%rip), %eax      # 0x17364
incl %eax
cltq
movq %rax, -0xc8(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0xa2d0
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0xcaa0
jmp 0xba1f
leaq -0x61(%rbp), %rdi
callq 0xa510
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xa190
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0xbabd
movq -0x70(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xa1b0
movq $0x0, (%rax)
movq -0x70(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xa1b0
movq $0x0, (%rax)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0xba30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0xa510
jmp 0xbd6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x61(%rbp), %rdi
callq 0xa510
jmp 0xbd64
movq -0xa8(%rbp), %rdi
movl -0x14(%rbp), %esi
leaq -0x30(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xaf90
movl %eax, -0xd4(%rbp)
jmp 0xbadc
movl -0xd4(%rbp), %eax
movl %eax, -0x74(%rbp)
cmpl $-0x1, -0x74(%rbp)
jne 0xbb57
leaq -0x75(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x33c0
movq -0xb8(%rbp), %rdi
movq -0xe0(%rbp), %rdx
leaq 0x3605(%rip), %rsi      # 0xf115
callq 0x5c40
jmp 0xbb17
leaq -0x75(%rbp), %rdi
callq 0x32a0
movl $0x1, -0x7c(%rbp)
jmp 0xbd39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xbd5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x75(%rbp), %rdi
callq 0x32a0
jmp 0xbd5b
movq -0xa8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movl 0xb7f6(%rip), %eax      # 0x17364
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
jl 0xbc3a
jmp 0xbb83
movq -0x88(%rbp), %rdi
addq $0x28, %rdi
movslq -0x8c(%rbp), %rsi
callq 0xa1b0
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0xe1(%rbp)
je 0xbbf2
movl -0x14(%rbp), %eax
movl %eax, -0xec(%rbp)
movq -0x88(%rbp), %rdi
addq $0x28, %rdi
movslq -0x8c(%rbp), %rsi
callq 0xa1b0
movq (%rax), %rdi
callq 0x9f00
movl %eax, -0xe8(%rbp)
jmp 0xbbdb
movl -0xec(%rbp), %eax
movl -0xe8(%rbp), %ecx
cmpl %ecx, %eax
setg %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0xbbfe
jmp 0xbc24
movq -0x88(%rbp), %rdi
addq $0x28, %rdi
movslq -0x8c(%rbp), %rsi
callq 0xa1b0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
jmp 0xbb83
jmp 0xbc26
movl -0x8c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xbb74
movq -0x88(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa1b0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0xbcdb
movq -0x88(%rbp), %rdi
callq 0x9f00
movl %eax, -0xf0(%rbp)
jmp 0xbc76
movl -0xf0(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0xbcdb
movslq -0x74(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xa1b0
movq (%rax), %rdi
addq $0x69, %rdi
callq 0xcb10
testb $0x1, %al
jne 0xbca0
jmp 0xbcdb
movslq -0x74(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xa1b0
movq (%rax), %rdi
addq $0x68, %rdi
callq 0xcb10
testb $0x1, %al
jne 0xbcdb
movq -0xb8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x9f20
jmp 0xbcd2
movl $0x1, -0x7c(%rbp)
jmp 0xbd39
leaq -0x8d(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x33c0
movq -0xb8(%rbp), %rdi
movq -0xf8(%rbp), %rdx
leaq 0x3412(%rip), %rsi      # 0xf115
callq 0x5c40
jmp 0xbd0a
leaq -0x8d(%rbp), %rdi
callq 0x32a0
movl $0x1, -0x7c(%rbp)
jmp 0xbd39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x8d(%rbp), %rdi
callq 0x32a0
jmp 0xbd5b
leaq -0x60(%rbp), %rdi
callq 0xa1d0
leaq -0x30(%rbp), %rdi
callq 0xa1d0
movq -0xb0(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0xa1d0
leaq -0x30(%rbp), %rdi
callq 0xa1d0
movq -0x40(%rbp), %rdi
callq 0x33b0
nopw %cs:(%rax,%rax)