// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc(%rsp)
movq %rdi, 0xb0(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x274d(%rip), %xmm0     # 0x7720
incl %r15d
movss 0x206a(%rip), %xmm1     # 0x7048
ucomiss %xmm0, %xmm1
jae 0x4fbf
movslq 0x631a(%rip), %rbx      # 0xb304
cmpl %ebx, %r15d
movl %ebx, %eax
cmovll %r15d, %eax
movl %eax, 0x8(%rsp)
movq %rbx, %rsi
incq %rsi
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x5dcc
movslq 0x62f1(%rip), %rsi      # 0xb304
incq %rsi
leaq 0x28(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x5dcc
movq 0x10(%rsp), %rcx
cmpq %rcx, 0x18(%rsp)
je 0x505b
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 0x5035
leaq 0x58(%rsp), %r14
movslq 0x8(%rsp), %rbp
cmpl %r15d, %ebx
movq %rbx, 0xa0(%rsp)
cmovll %ebx, %r15d
incl %r15d
movl %eax, 0x4(%rsp)
movq 0xb0(%rsp), %rdi
movl 0xc(%rsp), %esi
leaq 0x10(%rsp), %rdx
leaq 0x28(%rsp), %rcx
callq 0x4ec8
cmpl $-0x1, %eax
je 0x50d1
cltq
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movb 0x68(%rax), %cl
movl $0x5, %ebx
testb $0x1, %cl
jne 0x52d7
movb 0x69(%rax), %cl
testb $0x1, %cl
je 0x50b7
movl $0x1, %ebx
movl $0x0, 0x4(%rsp)
jmp 0x52d7
movl $0x0, 0x58(%rsp)
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movq %r14, 0x68(%rsp)
movq %r14, 0x70(%rsp)
movq %rax, 0x78(%rsp)
cmpl $0x0, 0x8(%rsp)
js 0x51ae
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %r12
movq 0x60(%rsp), %rcx
movq %r14, %rax
testq %rcx, %rcx
je 0x512c
movq %r14, %rax
xorl %edx, %edx
cmpq %r12, 0x20(%rcx)
setb %dl
cmovaeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x5115
movq %r14, %rcx
cmpq %r14, %rax
je 0x513f
cmpq 0x20(%rax), %r12
cmovbq %r14, %rax
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %r13
cmpq %r14, %rcx
jne 0x5171
movq %r12, %rdi
callq 0x4bfe
movq %r12, 0x40(%rsp)
movl $0x1, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x5ea6
movb 0x68(%r12), %al
testb $0x1, %al
jne 0x5182
movb 0x68(%r13), %al
testb $0x1, %al
je 0x5186
xorl %eax, %eax
jmp 0x5192
movq 0x28(%r12), %rax
cmpq %r13, (%rax,%rbx,8)
sete %al
testb %al, %al
je 0x51a6
leaq 0x1(%rbx), %rcx
cmpq %rbp, %rbx
movq %rcx, %rbx
jl 0x50fc
testb %al, %al
je 0x52a4
movl $0x70, %edi
callq 0x2210
movq %rax, %r12
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0xa8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq 0xa0(%rsp), %rbx
callq 0x48c4
movq %r12, %rdi
movl 0xc(%rsp), %esi
movq %r13, %rdx
movl 0x8(%rsp), %ecx
callq 0x4ab0
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x522d
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2220
testl %ebx, %ebx
js 0x524d
xorl %eax, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x28(%r12), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x5233
testl %ebx, %ebx
js 0x526c
xorl %eax, %eax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x28(%rcx), %rcx
movq %r12, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x5253
movb $0x1, %al
xchgb %al, 0x69(%r12)
movq 0x68(%rsp), %r12
movl $0x1, %ebx
cmpq %r14, %r12
je 0x529c
movq 0x20(%r12), %rdi
callq 0x4c16
movq %r12, %rdi
callq 0x2160
movq %rax, %r12
cmpq %r14, %rax
jne 0x5282
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x52cd
movq 0x68(%rsp), %r12
movl $0x5, %ebx
cmpq %r14, %r12
je 0x52cd
movq 0x20(%r12), %rdi
callq 0x4c16
movq %r12, %rdi
callq 0x2160
movq %rax, %r12
cmpq %r14, %rax
jne 0x52b3
leaq 0x50(%rsp), %rdi
callq 0x5e56
testl %ebx, %ebx
je 0x507b
cmpl $0x5, %ebx
je 0x507b
jmp 0x53ae
movq %rdx, %r13
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x5320
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x5320
movq %rdx, %r13
movq %rax, %rbx
movl $0x70, %esi
movq %r12, %rdi
callq 0x2220
jmp 0x533d
jmp 0x5337
jmp 0x5337
jmp 0x5337
jmp 0x5337
movq %rdx, %r13
movq %rax, %rbx
cmpl $0x1, %r13d
jne 0x5406
movq %rbx, %rdi
callq 0x20b0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x5c91(%rip), %rdi      # 0xaff0
movq %rax, %rsi
callq 0x2200
movb $0xa, 0x40(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2240
movq 0x68(%rsp), %r12
cmpq %r14, %r12
je 0x53a2
movq 0x20(%r12), %rdi
callq 0x4c16
movq %r12, %rdi
callq 0x2160
movq %rax, %r12
cmpq %r14, %rax
jne 0x5388
xorl %ebx, %ebx
callq 0x22f0
jmp 0x52cd
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x53c5
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movl 0x4(%rsp), %ebx
je 0x53e0
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
andb $0x1, %bl
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5406
jmp 0x53fe
movq %rax, %rbx
callq 0x22f0
leaq 0x50(%rsp), %rdi
callq 0x5e56
jmp 0x5427
movq %rax, %rdi
callq 0x41c3
movq %rax, %rbx
jmp 0x543e
movq %rax, %rbx
jmp 0x5455
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x543e
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5455
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq %rbx, %rdi
callq 0x2320
nop