// File: /workspace/github_cpp_makefile_star2_O2/xx01cyx[P]LSM-Tree/MemTable.cc
// Function: MemTable::put(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void MemTable::put(LsmKey k, const LsmValue& v) {

    stack<Node*> path = stack<Node*>();
    Node* p = head;

    while (p) {
        while (p->next && p->next->key < k)
            p = p->next;
        if (p->next && k == p->next->key) {    // substitute
            p = p->next;        // p points to the topmost existing node
            while (p) {
                p->value = v;
                p = p->down;
            }
            return;
        }
        path.push(p);
        p = p->down;
    }

    Node* prevAddedNode = nullptr;
    do {                                // 50% add a new layer
        Node* prev;
        if (!path.empty()) {
            prev = path.top();
            path.pop();
        } else {
            prev = head = new Node(head);
        }
        Node* newNode = new Node(k, v, prev->next, prevAddedNode);
        prev->next = newNode;
        prevAddedNode = newNode;
    } while (rand() & 1);

    keyNumber++;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x6842
movq %rsp, %r12
movq %rbx, 0x8(%rsp)
movq (%rbx), %rdi
movq %rdi, (%rsp)
testq %rdi, %rdi
je 0x6030
movq 0x28(%rdi), %rdi
testq %rdi, %rdi
je 0x5ff7
cmpq %r15, (%rdi)
jae 0x5ff5
movq %rdi, (%rsp)
jmp 0x5fe1
je 0x6022
movq %r14, %rdi
movq %r12, %rsi
callq 0x6b8c
movq (%rsp), %rbx
addq $0x30, %rbx
jmp 0x5fd5
addq $0x8, %rdi
movq 0x10(%rsp), %rsi
callq 0x3140
movq (%rsp), %rax
movq 0x30(%rax), %rdi
movq %rdi, (%rsp)
testq %rdi, %rdi
jne 0x600c
jmp 0x613c
leaq 0x28(%rsp), %r12
xorl %ebx, %ebx
leaq 0x18(%rsp), %r13
movq 0x68(%rsp), %rax
cmpq 0x48(%rsp), %rax
je 0x6073
cmpq 0x70(%rsp), %rax
jne 0x6063
movq 0x80(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %rbp
leaq 0x38(%rsp), %rdi
callq 0x6dc0
jmp 0x60ab
pushq $0x38
popq %rdi
callq 0x3250
movq %rax, %rbp
movq 0x8(%rsp), %rdx
movq (%rdx), %rax
andq $0x0, (%rbp)
movq %rbp, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%rbp)
andq $0x0, 0x10(%rbp)
movb $0x0, 0x18(%rbp)
andq $0x0, 0x28(%rbp)
movq %rax, 0x30(%rbp)
movq %rbp, (%rdx)
pushq $0x38
popq %rdi
callq 0x3250
movq %rax, %r14
movq %r13, %rdi
movq 0x10(%rsp), %rsi
callq 0x32f0
movq 0x28(%rbp), %rax
movq %r15, (%r14)
movq %r14, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%r14)
movq 0x18(%rsp), %rdx
cmpq %r12, %rdx
je 0x60ee
movq %rdx, 0x8(%r14)
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%r14)
jmp 0x60f6
movups (%r12), %xmm0
movups %xmm0, (%rcx)
movq 0x20(%rsp), %rcx
movq %r12, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
movq %rcx, 0x10(%r14)
movb $0x0, 0x28(%rsp)
movq %rax, 0x28(%r14)
movq %rbx, 0x30(%r14)
movq %r13, %rdi
callq 0x34a8
movq %r14, 0x28(%rbp)
callq 0x3080
movq %r14, %rbx
testb $0x1, %al
jne 0x603c
movq 0x8(%rsp), %rax
incq 0x8(%rax)
leaq 0x38(%rsp), %rdi
callq 0x68da
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x615e
jmp 0x615e
jmp 0x615e
movq %rax, %rbx
jmp 0x6171
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r14, %rdi
callq 0x3260
leaq 0x38(%rsp), %rdi
callq 0x68da
movq %rbx, %rdi
callq 0x3440
nop