// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, 0x10(%rsp)
movq (%rdi), %rbp
xorl %r14d, %r14d
pushq $0x50
popq %r13
pushq $0x48
popq %rbx
movq (%rbp), %r12
testq %r12, %r12
je 0x256c
movq %r15, %rdi
movq %r12, %rsi
callq 0x2d99
movq %r13, %rbp
testb %al, %al
jne 0x255f
movq %r15, %rdi
movq %r12, %rsi
callq 0x2dae
movq %rbx, %rbp
testb %al, %al
je 0x25c4
incq 0x40(%r12)
addq %r12, %rbp
movq %r12, %r14
jmp 0x2532
callq 0x2400
movq %rax, %r12
movq %rax, (%rbp)
movq %rax, %rdi
movq %r15, %rsi
callq 0x20e0
leaq 0x20(%r12), %rdi
movq 0x8(%rsp), %rsi
callq 0x20e0
movq $0x1, 0x40(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%r12)
movq %r14, 0x58(%r12)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25f4
leaq 0x20(%r12), %rdi
movq 0x8(%rsp), %rsi
callq 0x20e0
movq 0x58(%r12), %r12
testq %r12, %r12
je 0x25e4
decq 0x40(%r12)
jmp 0x25d3
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop