// 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>>>::put(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool SkipList<Key, Value>::put(Key key, Value value) {
    std::shared_ptr<Node> newNode(new Node(key, value));
    std::shared_ptr<Node> ptr = skipSearch(key);  // nearest element's top level
    // if the key exists, then update its value
    if (ptr->key == key && valid(ptr)) {
        while (ptr) {
            ptr->val = value;
            ptr = ptr->below;
        }
        return true;
    }

    while (ptr->below) ptr = ptr->below;
    newNode->pred = ptr;
    newNode->succ = ptr->succ;
    ptr->succ->pred = newNode;
    ptr->succ = newNode;

    // randomly grow higher
    while (rand() & 1) {
        // find the nearest node with higher level
        while (ptr->pred && !ptr->above) ptr = ptr->pred;
        // generate the head for new higher level
        if (!ptr->above) {
            std::shared_ptr<Node> newHead(new Node());  // new head
            std::shared_ptr<Node> newTail(new Node());  // new trailing node
            newHead->below = head;
            newHead->succ = newTail;
            head->above = newHead;
            newTail->pred = newHead;
            newTail->below = tail;
            tail->above = newTail;
            head = newHead;
            tail = newTail;
        }
        ptr = ptr->above;
        // build the new level
        std::shared_ptr<Node> growNode(new Node(key, value));
        growNode->pred = ptr;
        growNode->succ = ptr->succ;
        growNode->below = newNode;
        ptr->succ->pred = growNode;
        ptr->succ = growNode;
        newNode->above = growNode;
        newNode = growNode;
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, 0x40(%rsp)
movl $0x68, %edi
callq 0x3270
movq %rax, %r12
leaq 0x98(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x9582
movb $0x1, %bpl
leaq 0x88(%rsp), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x98a2
leaq 0x28(%rsp), %rdi
movq %r12, -0x8(%rdi)
xorl %ebp, %ebp
movq %r12, %rsi
callq 0x9612
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x8702
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rsp, %rdi
movq 0x40(%rsp), %rsi
movq %r13, %rdx
callq 0x98fa
movq (%rsp), %rcx
movq (%rcx), %rax
cmpq %r13, %rax
je 0x8722
xorl %ebp, %ebp
jmp 0x8755
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x8741
movq 0x6895(%rip), %rdx      # 0xefc8
cmpb $0x0, (%rdx)
je 0x873d
incl 0x8(%rdi)
jmp 0x8741
lock
incl 0x8(%rdi)
cmpq $0x0, 0x28(%rcx)
je 0x8753
cmpq $0x0, 0x38(%rcx)
setne %bpl
jmp 0x8755
xorl %ebp, %ebp
cmpq %r13, %rax
setne %al
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x876a
callq 0x9408
movq (%rsp), %rsi
testb %bpl, %bpl
je 0x87b6
testq %rsi, %rsi
je 0x8bb2
leaq 0x8(%rsp), %r14
addq $0x8, %rsi
movq %rsi, %rdi
movq %rbx, %rsi
callq 0x3170
movq (%rsp), %rsi
movq 0x58(%rsi), %rax
movq %rax, (%rsp)
addq $0x60, %rsi
movq %r14, %rdi
callq 0x9478
movq (%rsp), %rsi
testq %rsi, %rsi
jne 0x8781
jmp 0x8bb2
movq 0x58(%rsi), %rax
testq %rax, %rax
movq %r13, 0x60(%rsp)
je 0x87e6
leaq 0x8(%rsp), %r14
movq %rax, (%rsp)
addq $0x60, %rsi
movq %r14, %rdi
callq 0x9478
movq (%rsp), %rsi
movq 0x58(%rsi), %rax
testq %rax, %rax
jne 0x87c9
movq 0x20(%rsp), %rdi
movq %rsi, 0x28(%rdi)
addq $0x30, %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x9478
movq -0x8(%r13), %rsi
movq 0x20(%rsp), %rdi
movq 0x38(%rsi), %rax
movq %rax, 0x38(%rdi)
addq $0x40, %rdi
addq $0x40, %rsi
callq 0x9478
movq -0x8(%r13), %rax
movq 0x38(%rax), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rdi)
addq $0x30, %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x9478
movq -0x8(%r13), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rdi)
addq $0x40, %rdi
movq %r14, %rsi
callq 0x9478
callq 0x30e0
testb $0x1, %al
je 0x8bb2
leaq 0x38(%rsp), %r15
movq 0x40(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x58(%rsp)
addq $0x18, %rax
movq %rax, 0x48(%rsp)
leaq 0x78(%rsp), %r12
movq %rbx, 0x50(%rsp)
movq (%rsp), %rsi
movq 0x28(%rsi), %rax
testq %rax, %rax
je 0x88b2
cmpq $0x0, 0x48(%rsi)
jne 0x88b2
movq %rax, (%rsp)
addq $0x30, %rsi
movq %r13, %rdi
callq 0x9478
jmp 0x888c
cmpq $0x0, 0x48(%rsi)
jne 0x8a3f
movl $0x68, %edi
callq 0x3270
movq %rax, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x10(%rax)
movb $0x0, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movups %xmm0, 0x48(%rax)
movups %xmm0, 0x58(%rax)
movq %rax, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x9612
movl $0x68, %edi
callq 0x3270
movq %rax, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x10(%rax)
movb $0x0, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movups %xmm0, 0x48(%rax)
movups %xmm0, 0x58(%rax)
movq %rax, 0x30(%rsp)
movq %r15, %rdi
movq %rax, %rsi
callq 0x9612
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %r14
movq (%r14), %rax
movq %rax, 0x58(%rdi)
addq $0x60, %rdi
movq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0x9478
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rdi)
addq $0x40, %rdi
movq %r15, %rsi
callq 0x9478
movq (%r14), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rdi)
addq $0x50, %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x9478
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rdi)
addq $0x30, %rdi
movq %r15, %rsi
callq 0x9478
movq 0x30(%rsp), %rdi
movq 0x10(%r14), %rax
movq %rax, 0x58(%rdi)
addq $0x60, %rdi
movq 0x48(%rsp), %rbp
movq %rbp, %rsi
callq 0x9478
movq 0x10(%r14), %rdi
movq 0x30(%rsp), %rax
movq %rax, 0x48(%rdi)
addq $0x50, %rdi
leaq 0x38(%rsp), %rsi
callq 0x9478
movq 0x10(%rsp), %rax
movq %rax, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9478
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r14)
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
callq 0x9478
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x8a2b
callq 0x9408
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movq 0x50(%rsp), %rbx
je 0x8a3f
callq 0x9408
movq (%rsp), %rsi
movq 0x48(%rsi), %rax
movq %rax, (%rsp)
addq $0x50, %rsi
movq %r13, %rbp
movq %r13, %rdi
callq 0x9478
movl $0x68, %edi
callq 0x3270
movq %rax, %r14
movq %r12, %r15
movq %r12, 0x68(%rsp)
movq (%rbx), %rsi
movq %rbx, %r13
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x9582
movb $0x1, %r12b
movq %r14, %rdi
movq 0x60(%rsp), %rsi
movq %rbx, %rdx
callq 0x98a2
movq %r14, 0x10(%rsp)
xorl %r12d, %r12d
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x9612
movq 0x68(%rsp), %rdi
movq %r15, %r12
cmpq %r15, %rdi
movq %r13, %rbx
je 0x8ace
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
movq (%rsp), %rax
movq %rax, 0x28(%rdi)
addq $0x30, %rdi
movq %rbp, %r13
movq %rbp, %rsi
callq 0x9478
movq (%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x38(%rsi), %rax
movq %rax, 0x38(%rdi)
addq $0x40, %rdi
addq $0x40, %rsi
callq 0x9478
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x58(%rdi)
addq $0x60, %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x9478
movq (%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rdi)
addq $0x30, %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x9478
movq (%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x38(%rdi)
addq $0x40, %rdi
movq %r15, %rsi
callq 0x9478
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rdi)
addq $0x50, %rdi
movq %r15, %rsi
callq 0x9478
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x9478
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
leaq 0x38(%rsp), %r15
je 0x8ba5
callq 0x9408
callq 0x30e0
testb $0x1, %al
jne 0x888c
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x8bc1
callq 0x9408
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x8bd0
callq 0x9408
movb $0x1, %al
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x8c88
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x8c14
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x8c14
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3280
testb %bpl, %bpl
je 0x8c97
movl $0x68, %esi
movq %r12, %rdi
callq 0x3280
jmp 0x8c97
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x8c79
callq 0x9408
jmp 0x8c79
jmp 0x8c48
jmp 0x8c48
movq %rax, %rbx
movb $0x1, %r12b
jmp 0x8c67
movq %rax, %rbx
jmp 0x8c79
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x8c67
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
testb %r12b, %r12b
je 0x8c79
movl $0x68, %esi
movq %r14, %rdi
callq 0x3280
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x8c88
callq 0x9408
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x8c97
callq 0x9408
movq %rbx, %rdi
callq 0x33f0
nop