// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, 0x28(%rsp)
pushq $0x68
popq %rdi
callq 0x3280
movq %rax, %r15
leaq 0x78(%rsp), %rdi
movq %rbx, 0x40(%rsp)
movq %rbx, %rsi
callq 0x3340
movb $0x1, %bl
leaq 0x78(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x7d64
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x7ad2
leaq 0x78(%rsp), %rdi
callq 0x3438
leaq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x7dba
movq 0x8(%rsp), %rax
cmpq %r14, (%rax)
jne 0x6dd0
leaq 0x68(%rsp), %rbx
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x78e2
movq (%rbx), %rax
testq %rax, %rax
je 0x6dc6
cmpq $0x0, 0x28(%rax)
je 0x6dc6
movq 0x38(%rax), %rbx
leaq 0x70(%rsp), %rdi
callq 0x77e6
testq %rbx, %rbx
je 0x6dd0
leaq 0x8(%rsp), %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x7094
addq $0x8, %rdi
movq 0x40(%rsp), %rsi
callq 0x3180
movq 0x8(%rsp), %rsi
addq $0x58, %rsi
movq %rbx, %rdi
callq 0x787c
jmp 0x6d97
leaq 0x70(%rsp), %rdi
callq 0x77e6
leaq 0x8(%rsp), %rbx
movq 0x8(%rsp), %rsi
cmpq $0x0, 0x58(%rsi)
je 0x6def
addq $0x58, %rsi
movq %rbx, %rdi
callq 0x787c
jmp 0x6dd5
movq %r14, 0x60(%rsp)
leaq 0x48(%rsp), %r12
movq (%r12), %rdi
addq $0x28, %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0x787c
movq (%r13), %rsi
addq $0x38, %rsi
movq (%r12), %rdi
addq $0x38, %rdi
callq 0x787c
movq (%r13), %rax
movq 0x38(%rax), %rdi
addq $0x28, %rdi
movq %r12, %rsi
callq 0x787c
movq (%r13), %rdi
addq $0x38, %rdi
movq %r12, %rsi
callq 0x787c
movq 0x28(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x58(%rsp)
leaq 0x18(%rsp), %r15
leaq 0x30(%rsp), %r14
leaq 0x98(%rsp), %rbp
callq 0x30e0
testb $0x1, %al
je 0x7094
movq 0x8(%rsp), %rsi
cmpq $0x0, 0x28(%rsi)
movq 0x48(%rsi), %rax
je 0x6e9b
testq %rax, %rax
jne 0x6fb8
addq $0x28, %rsi
movq %r13, %rdi
callq 0x787c
jmp 0x6e74
testq %rax, %rax
jne 0x6fb8
pushq $0x68
popq %rdi
callq 0x3280
movq %rax, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%rax)
andq $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 %r15, %rdi
movq %rax, %rsi
callq 0x7ad2
pushq $0x68
popq %rdi
callq 0x3280
movq %rax, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%rax)
andq $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 %r14, %rdi
movq %rax, %rsi
callq 0x7ad2
movq 0x18(%rsp), %rdi
addq $0x58, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x787c
movq 0x18(%rsp), %rdi
addq $0x38, %rdi
movq %r14, %rsi
callq 0x787c
movq (%rbx), %rdi
addq $0x48, %rdi
movq %r15, %rsi
callq 0x787c
movq 0x30(%rsp), %rdi
addq $0x28, %rdi
movq %r15, %rsi
callq 0x787c
movq 0x30(%rsp), %rdi
addq $0x58, %rdi
movq 0x58(%rsp), %r14
movq %r14, %rsi
callq 0x787c
movq 0x10(%rbx), %rdi
addq $0x48, %rdi
leaq 0x30(%rsp), %rsi
callq 0x787c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x787c
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x787c
leaq 0x38(%rsp), %rdi
callq 0x77e6
leaq 0x20(%rsp), %rdi
callq 0x77e6
movq 0x8(%rsp), %rsi
addq $0x48, %rsi
movq %r13, %rdi
callq 0x787c
pushq $0x68
popq %rdi
callq 0x3280
movq %rax, %rbx
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
callq 0x3340
movb $0x1, %r14b
movq %rbx, %rdi
movq 0x60(%rsp), %rsi
movq %rbp, %rdx
callq 0x7d64
xorl %r14d, %r14d
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7ad2
movq %rbp, %rdi
callq 0x3438
movq 0x18(%rsp), %rdi
addq $0x28, %rdi
movq %r13, %rsi
callq 0x787c
movq 0x8(%rsp), %rsi
addq $0x38, %rsi
movq 0x18(%rsp), %rdi
addq $0x38, %rdi
callq 0x787c
movq 0x18(%rsp), %rdi
addq $0x58, %rdi
movq %r12, %rsi
callq 0x787c
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x28, %rdi
movq %r15, %rsi
callq 0x787c
movq 0x8(%rsp), %rdi
addq $0x38, %rdi
movq %r15, %rsi
callq 0x787c
movq 0x48(%rsp), %rdi
addq $0x48, %rdi
movq %r15, %rsi
callq 0x787c
movq %r12, %rdi
movq %r15, %rsi
callq 0x787c
leaq 0x20(%rsp), %rdi
callq 0x77e6
leaq 0x30(%rsp), %r14
jmp 0x6e67
leaq 0x10(%rsp), %rdi
callq 0x77e6
leaq 0x50(%rsp), %rdi
callq 0x77e6
movb $0x1, %al
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
jmp 0x712b
movq %rax, %r12
jmp 0x70d7
movq %rax, %r12
leaq 0x78(%rsp), %rdi
callq 0x3438
testb %bl, %bl
je 0x7135
pushq $0x68
popq %rsi
movq %r15, %rdi
callq 0x3290
jmp 0x7135
jmp 0x70f7
movq %rax, %r12
leaq 0x20(%rsp), %rdi
callq 0x77e6
jmp 0x7121
jmp 0x70f7
movq %rax, %r12
jmp 0x7121
movq %rax, %r12
jmp 0x7116
movq %rax, %r12
leaq 0x98(%rsp), %rdi
callq 0x3438
testb %r14b, %r14b
je 0x7121
pushq $0x68
popq %rsi
movq %rbx, %rdi
callq 0x3290
leaq 0x10(%rsp), %rdi
callq 0x77e6
leaq 0x50(%rsp), %rdi
callq 0x77e6
movq %r12, %rdi
callq 0x33e0
nop