// File: /workspace/github_cpp_makefile_star2_O3/dvjdjvu[P]bntree/./src/bntree.cpp
// Function: bntree::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void bntree::insert(string key, string val) {
    node_t *search_node, *prev_node, **node;

    node = &this->tree->root;
    search_node = this->tree->root;
    prev_node = NULL;
    uint64_t index_node;

    if (!this->tree->root) {
        index_node = 0;
    } else {
        index_node = this->get_child_weight(search_node->left);
    }

    uint64_t index_del = -1;

    for (;;) {

        if (search_node == NULL) {
            // Добавялем узел
            search_node = *node = this->node_new();

            search_node->data.key = key;
            search_node->data.val = val;
            search_node->weight = 1; // новый узел имеет вес 1

            search_node->left = search_node->right = NULL;
            search_node->parent = prev_node;

            break;
        } else if (key > search_node->data.key) {
            // Идем направо
            prev_node = search_node;
            search_node->weight++; // увеличиваем вес узла
            node = &search_node->right;
            search_node = search_node->right;

            if (search_node)
                index_node += (this->get_child_weight(search_node->left) + 1);

        } else if (key < search_node->data.key) {
            // Идем налево
            prev_node = search_node;
            search_node->weight++; // увеличиваем вес узла
            node = &search_node->left;
            search_node = search_node->left;

            if (search_node)
                index_node -= (this->get_child_weight(search_node->left) + 1);
        } else {
            // Если такой ключ уже существует, то обновляем значение.
            search_node->data.val = val;

            // Идем назад и отменяем изменение веса у верхних узлов
            for (;;) {

                if (search_node->parent) {
                    search_node = search_node->parent;
                } else {
                    return;
                }

                search_node->weight--;
            }
        }
    }

    this->balance(search_node);

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r14
movq (%rdi), %rbx
movq (%rbx), %rax
testq %rax, %rax
je 0x25f3
movq %rax, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x2040
testl %eax, %eax
jle 0x25c6
incq 0x40(%r15)
movq 0x50(%r15), %rax
testq %rax, %rax
jne 0x25a5
jmp 0x25ff
movq %r12, %rdi
movq %r15, %rsi
callq 0x2040
testl %eax, %eax
jns 0x2696
incq 0x40(%r15)
movq 0x48(%r15), %rax
testq %rax, %rax
jne 0x25a5
movq %r14, (%rsp)
movq %r13, %r14
leaq 0x48(%r15), %rbx
jmp 0x260a
movq %r14, (%rsp)
movq %r13, %r14
xorl %r15d, %r15d
jmp 0x260a
movq %r14, (%rsp)
movq %r13, %r14
leaq 0x50(%r15), %rbx
movl $0x60, %edi
callq 0x2170
movq %rax, %r13
addq $0x10, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x40(%r13)
movaps %xmm0, 0x50(%r13)
movq %rax, (%r13)
movq $0x0, 0x8(%r13)
leaq 0x20(%r13), %rbp
leaq 0x30(%r13), %rax
movq %rax, 0x20(%r13)
movq %r13, (%rbx)
movq %r13, %rdi
movq %r12, %rsi
callq 0x20b0
movq %rbp, %rdi
movq %r14, %rsi
callq 0x20b0
movq $0x1, 0x40(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%r13)
movq %r15, 0x58(%r13)
movq (%rsp), %rdi
movq %r13, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x26ce
leaq 0x20(%r15), %rdi
movq %r13, %rsi
callq 0x20b0
movq 0x58(%r15), %rax
testq %rax, %rax
je 0x26b5
decq 0x40(%rax)
movq 0x58(%rax), %rax
jmp 0x26a6
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x26c6
movq %rax, %rdi
callq 0x2f3d