// File: /workspace/github_cpp_makefile_star2_O2/shreyas-gopalakrishna[P]Concurrent-Skip-list/skip_list.cpp
// Function: SkipList::add(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool SkipList::add(int key, string value) {

    // Get the level until which the new node must be available
    int top_level = get_random_level();

    // Initialization of references of the predecessors 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;
    }

    // Keep trying to insert the element into the list. In case predecessors and successors are changed,
    // this loop helps to try the insert again
    while(true){
        
        // Find the predecessors and successors of where the key must be inserted
        int found = find(key, preds, succs);

        // If found and marked, wait and continue insert
        // If found and unmarked, wait until it is fully_linked and return. No insert needed
        // If not found, go ahead with insert
        if(found != -1){
            Node* node_found = succs[found];
            
            if(!node_found->marked){
                while(! node_found->fully_linked){
                }
                return false;
            }
            continue;
        }

        // Store all the Nodes which lock we acquire in a map
        // Map used so that we don't try to acquire lock to a Node we have already acquired
        // This may happen when we have the same predecessor at different levels
        map<Node*, int> locked_nodes;

        // Traverse the skip list and try to acquire the lock of predecessor at every level
        try{
            Node* pred;
            Node* succ;

            // Used to check if the predecessor and successors are same from when we tried to read them before
            bool valid = true;

            for (int level = 0; valid && (level <= top_level); level++){
                pred = preds[level];
                succ = succs[level];

                // If not already acquired lock, then acquire the lock 
                if(!(locked_nodes.count( pred ))){
                    pred->lock();
                    locked_nodes.insert(make_pair(pred, 1));
                }

                // If predecessor marked or if the predecessor and successors change, then abort and try again
                valid = !(pred->marked.load(std::memory_order_seq_cst)) && !(succ->marked.load(std::memory_order_seq_cst)) && pred->next[level]==succ;                
            }

            // Conditons are not met, release locks, abort and try again.
            if(!valid){
                for (auto const& x : locked_nodes){
                    x.first->unlock();
                }
                continue;
            }

            // All conditions satisfied, create the Node and insert it as we have all the required locks
            Node* new_node = new Node(key, value, top_level);

            // Update the predecessor and successors
            for (int level = 0; level <= top_level; level++){
                new_node->next[level] = succs[level];
            }

            for (int level = 0; level <= top_level; level++){
                preds[level]->next[level] = new_node;
            }

            // Mark the node as completely linked.
            new_node->fully_linked = true;
            
            // Release lock of all the nodes held once insert is complete
            for (auto const& x : locked_nodes){
                x.first->unlock();
            }
            
            return true;
        }catch(const std::exception& e){
            // If any exception occurs during the above insert, release locks of the held nodes and try again.
            std::cerr << e.what() << '\n';
            for (auto const& x : locked_nodes){
                    x.first->unlock();
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, 0x88(%rsp)
movl %esi, %r14d
movq %rdi, 0x90(%rsp)
callq 0x4aca
movl %eax, %ebx
movslq 0x57c6(%rip), %rsi      # 0xa2f4
incq %rsi
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x567e
movslq 0x57ad(%rip), %rsi      # 0xa2f4
incq %rsi
leaq 0x28(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x567e
xorl %eax, %eax
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rdx, %rax
jae 0x4b85
andq $0x0, (%rcx,%rax,8)
movq 0x28(%rsp), %rcx
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x4b5b
leaq 0x60(%rsp), %rbp
movl %ebx, 0x1c(%rsp)
movslq %ebx, %r12
leaq 0x58(%rsp), %rbx
leaq 0xa8(%rsp), %r15
movl %r14d, 0xc(%rsp)
movq 0x90(%rsp), %rdi
movl %r14d, %esi
leaq 0x40(%rsp), %rdx
leaq 0x28(%rsp), %rcx
callq 0x4a3a
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x4bde
movl %r13d, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movb 0x68(%rax), %cl
testb $0x1, %cl
jne 0x4ba3
jmp 0x4e20
andl $0x0, 0x60(%rsp)
andq $0x0, 0x68(%rsp)
movq %rbp, 0x70(%rsp)
movq %rbp, 0x78(%rsp)
andq $0x0, 0x80(%rsp)
movb $0x1, %al
xorl %r14d, %r14d
testb $0x1, %al
je 0x4ca5
cmpq %r12, %r14
jg 0x4ca5
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %r15
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x56d0
testq %rax, %rax
jne 0x4c6d
movq 0x10(%rsp), %rdi
callq 0x46ee
movq 0x10(%rsp), %rax
movq %rax, 0x98(%rsp)
movl $0x1, 0xa0(%rsp)
movq %rbx, %rdi
leaq 0x98(%rsp), %rsi
callq 0x57ea
movq 0x10(%rsp), %rax
movb 0x68(%rax), %al
testb $0x1, %al
jne 0x4c81
movb 0x68(%r15), %al
testb $0x1, %al
je 0x4c85
xorl %eax, %eax
jmp 0x4c95
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
cmpq %r15, (%rax,%r14,8)
sete %al
incq %r14
leaq 0xa8(%rsp), %r15
jmp 0x4c01
testb $0x1, %al
je 0x4cfc
pushq $0x70
popq %rdi
callq 0x21f0
movq %rax, %r14
movq %r15, %rdi
movq 0x88(%rsp), %rsi
callq 0x2270
movq %r14, %rdi
movl 0xc(%rsp), %esi
movq %r15, %rdx
movl 0x1c(%rsp), %ecx
callq 0x45e8
movq %r15, %rdi
callq 0x20d0
xorl %eax, %eax
cmpq %r12, %rax
jg 0x4d20
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x28(%r14), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
jmp 0x4ce1
movq 0x70(%rsp), %r14
cmpq %rbp, %r14
je 0x4e0e
movq 0x20(%r14), %rdi
callq 0x46f8
movq %r14, %rdi
callq 0x2150
movq %rax, %r14
jmp 0x4d01
xorl %eax, %eax
cmpq %r12, %rax
jg 0x4d3d
movq 0x40(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x28(%rcx), %rcx
movq %r14, (%rcx,%rax,8)
incq %rax
jmp 0x4d22
movb $0x1, %al
xchgb %al, 0x69(%r14)
movq 0x70(%rsp), %r14
cmpq %rbp, %r14
je 0x4e2a
movq 0x20(%r14), %rdi
callq 0x46f8
movq %r14, %rdi
callq 0x2150
movq %rax, %r14
jmp 0x4d48
movq %rdx, %r13
movq %rax, 0x20(%rsp)
movq %r15, %rdi
callq 0x20d0
jmp 0x4d81
movq %rdx, %r13
movq %rax, 0x20(%rsp)
movq %r14, %rdi
pushq $0x70
popq %rsi
callq 0x2200
leaq 0xa8(%rsp), %r15
movq 0x20(%rsp), %rdi
jmp 0x4db7
jmp 0x4da1
jmp 0x4da9
jmp 0x4da1
movq %rdx, %r13
movq %rax, %rdi
jmp 0x4db7
movq %rdx, %r13
movq %rax, %rdi
leaq 0xa8(%rsp), %r15
cmpl $0x1, %r13d
jne 0x4e63
callq 0x20a0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x521a(%rip), %rdi      # 0x9ff0
movq %rax, %rsi
callq 0x21e0
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x2230
movq 0x70(%rsp), %r14
cmpq %rbp, %r14
je 0x4e09
movq 0x20(%r14), %rdi
callq 0x46f8
movq %r14, %rdi
callq 0x2150
movq %rax, %r14
jmp 0x4dee
callq 0x22e0
movq %rbx, %rdi
callq 0x56ea
movl 0xc(%rsp), %r14d
jmp 0x4ba3
movb 0x69(%rax), %cl
testb $0x1, %cl
je 0x4e20
jmp 0x4e34
leaq 0x58(%rsp), %rdi
callq 0x56ea
cmpl $-0x1, %r13d
sete %bl
leaq 0x28(%rsp), %rdi
callq 0x4778
leaq 0x40(%rsp), %rdi
callq 0x4778
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
jmp 0x4e77
movq %rax, %rbx
jmp 0x4e77
jmp 0x4e6f
movq %rax, %rbx
callq 0x22e0
leaq 0x58(%rsp), %rdi
callq 0x56ea
jmp 0x4e98
movq %rax, %rdi
callq 0x3bfe
movq %rax, %rbx
jmp 0x4ea2
movq %rax, %rbx
jmp 0x4eac
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4778
leaq 0x40(%rsp), %rdi
callq 0x4778
movq %rbx, %rdi
callq 0x2310