// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq %rdx, 0xa8(%rsp)
movl %esi, %ebx
movq %rdi, %rbp
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x2710(%rip), %xmm0     # 0x7720
incl %r15d
movss 0x202d(%rip), %xmm1     # 0x7048
ucomiss %xmm0, %xmm1
jae 0x4ffc
movslq 0x62dd(%rip), %rsi      # 0xb304
cmpl %esi, %r15d
movl %esi, %eax
cmovll %r15d, %eax
movl %eax, 0x4(%rsp)
movq %rsi, 0x8(%rsp)
incq %rsi
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x5de0
movslq 0x62b2(%rip), %rsi      # 0xb304
incq %rsi
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x5de0
movq 0x10(%rsp), %rcx
cmpq %rcx, 0x18(%rsp)
je 0x509a
xorl %eax, %eax
xorl %edx, %edx
movq %rax, (%rcx,%rdx,8)
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%rdx,8)
incq %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
cmpq %rsi, %rdx
jb 0x5074
leaq 0x50(%rsp), %r12
movslq 0x4(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x8(%rsp), %rax
cmpl %r15d, %eax
cmovll %eax, %r15d
incl %r15d
movl %ebx, (%rsp)
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
movl %ebx, %esi
leaq 0x10(%rsp), %rdx
leaq 0x28(%rsp), %rcx
callq 0x4f0c
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x50f8
movl %r13d, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movb 0x68(%rax), %cl
testb $0x1, %cl
jne 0x50c3
jmp 0x53c3
movl $0x0, 0x50(%rsp)
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movq %r12, 0x68(%rsp)
movq %rax, 0x70(%rsp)
cmpl $0x0, 0x4(%rsp)
js 0x520f
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %r14
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rbp
movq 0x58(%rsp), %rsi
testq %rsi, %rsi
je 0x5178
movq %r12, %rdi
movq %rdi, %rax
movq %rsi, %rcx
movq 0x20(%rsi), %rdx
xorl %esi, %esi
cmpq %r14, %rdx
setb %sil
movq %rcx, %rdi
cmovbq %rax, %rdi
movq 0x10(%rcx,%rsi,8), %rsi
testq %rsi, %rsi
jne 0x5142
cmpq %r12, %rdi
je 0x5178
cmpq %r14, %rdx
cmovbq %rax, %rcx
cmpq 0x20(%rcx), %r14
jae 0x519f
movq %r14, %rdi
callq 0x4c3e
movq %r14, 0x78(%rsp)
movl $0x1, 0x80(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x5ebe
movb 0x68(%r14), %al
testb $0x1, %al
jne 0x51d5
movb 0x68(%rbp), %al
testb $0x1, %al
jne 0x51d5
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
cmpq %rbp, %rax
jne 0x51d0
leaq 0x1(%rbx), %rcx
cmpq 0xb0(%rsp), %rbx
movq %rcx, %rbx
jl 0x5123
cmpq %rbp, %rax
je 0x520f
movq 0x60(%rsp), %r14
cmpq %r12, %r14
movl (%rsp), %ebx
movq 0x40(%rsp), %rbp
je 0x5200
movq 0x20(%r14), %rdi
callq 0x4c56
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
cmpq %r12, %rax
jne 0x51e7
leaq 0x48(%rsp), %rdi
callq 0x5e6e
jmp 0x50c3
movl $0x70, %edi
callq 0x2210
movq %rax, %r14
leaq 0x98(%rsp), %rbp
movq %rbp, 0x88(%rsp)
movq 0xa8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
movl (%rsp), %ebx
callq 0x48fc
movq %r14, %rdi
movl %ebx, %esi
movq %rbp, %rdx
movl 0x4(%rsp), %ecx
callq 0x4af0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5287
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2220
cmpl $0x0, 0x8(%rsp)
js 0x52cb
xorl %eax, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x28(%r14), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x5290
cmpl $0x0, 0x8(%rsp)
js 0x52cb
xorl %eax, %eax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x28(%rcx), %rcx
movq %r14, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x52b2
movb $0x1, %al
xchgb %al, 0x69(%r14)
movq 0x60(%rsp), %r14
cmpq %r12, %r14
je 0x53cd
movq 0x20(%r14), %rdi
callq 0x4c56
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
cmpq %r12, %rax
jne 0x52df
jmp 0x53cd
movq %rdx, %r13
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x5330
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x5330
movq %rdx, %r13
movq %rax, %rbx
movl $0x70, %esi
movq %r14, %rdi
callq 0x2220
jmp 0x534d
jmp 0x5347
jmp 0x5347
jmp 0x5347
jmp 0x5347
movq %rdx, %r13
movq %rax, %rbx
cmpl $0x1, %r13d
jne 0x542d
movq %rbx, %rdi
callq 0x20b0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x5c81(%rip), %rdi      # 0xaff0
movq %rax, %rsi
callq 0x2200
movb $0xa, 0x78(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x78(%rsp), %rsi
callq 0x2240
movq 0x60(%rsp), %r14
cmpq %r12, %r14
je 0x53b1
movq 0x20(%r14), %rdi
callq 0x4c56
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
cmpq %r12, %rax
jne 0x5398
callq 0x22f0
movl (%rsp), %ebx
movq 0x40(%rsp), %rbp
jmp 0x5200
movb 0x69(%rax), %cl
testb $0x1, %cl
je 0x53c3
jmp 0x53d7
leaq 0x48(%rsp), %rdi
callq 0x5e6e
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x53ee
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5405
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
cmpl $-0x1, %r13d
sete %al
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x542d
jmp 0x5425
movq %rax, %rbx
callq 0x22f0
leaq 0x48(%rsp), %rdi
callq 0x5e6e
jmp 0x544e
movq %rax, %rdi
callq 0x41f1
movq %rax, %rbx
jmp 0x5465
movq %rax, %rbx
jmp 0x547c
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x5465
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x547c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq %rbx, %rdi
callq 0x2320