// File: /workspace/github_cpp_makefile_star2_O1/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 0x25f5
movq %rax, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x2040
movl $0x50, %ebx
testl %eax, %eax
jg 0x25dc
movq %r12, %rdi
movq %r15, %rsi
callq 0x2040
movl $0x48, %ebx
testl %eax, %eax
jns 0x2693
incq 0x40(%r15)
movq (%r15,%rbx), %rax
testq %rax, %rax
jne 0x25ad
movq %r14, (%rsp)
movq %r13, %r14
addq %r15, %rbx
jmp 0x25ff
movq %r14, (%rsp)
movq %r13, %r14
xorl %r15d, %r15d
movl $0x60, %edi
callq 0x2170
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
leaq 0x20(%rax), %rbp
addq $0x30, %rax
leaq 0x10(%r13), %rcx
movq %rcx, (%r13)
xorl %ecx, %ecx
movq %rcx, 0x8(%r13)
movb %cl, 0x10(%r13)
movq %rax, 0x20(%r13)
movq %rcx, 0x28(%r13)
movb %cl, 0x30(%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 0x26cc
leaq 0x20(%r15), %rdi
movq %r13, %rsi
callq 0x20b0
movq 0x58(%r15), %rax
testq %rax, %rax
je 0x26b2
decq 0x40(%rax)
movq 0x58(%rax), %rax
jmp 0x26a3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x26c3
movq %rax, %rdi
callq 0x2f59
nop