// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rsi, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq (%rax), %rcx
movq %rcx, -0x30(%rbp)
movq (%rax), %rcx
movq (%rcx), %rcx
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq (%rax), %rax
cmpq $0x0, (%rax)
jne 0x2780
movq $0x0, -0x38(%rbp)
jmp 0x2795
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2700
movq %rax, -0x38(%rbp)
movq $-0x1, -0x40(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x280b
movq -0x48(%rbp), %rdi
callq 0x2560
movq -0x58(%rbp), %rsi
movq -0x30(%rbp), %rcx
movq %rax, (%rcx)
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2040
movq -0x50(%rbp), %rsi
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0x2040
movq -0x20(%rbp), %rax
movq $0x1, 0x40(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x292d
movq -0x58(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x38d0
testb $0x1, %al
jne 0x281e
jmp 0x2877
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2872
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2700
addq $0x1, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x2926
movq -0x58(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3910
testb $0x1, %al
jne 0x288a
jmp 0x28e6
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x28e4
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2700
movq %rax, %rcx
addq $0x1, %rcx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x2924
movq -0x50(%rbp), %rsi
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
callq 0x2040
movq -0x20(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x2910
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x2912
jmp 0x293a
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x40(%rax)
jmp 0x28f7
jmp 0x2926
jmp 0x2928
jmp 0x279d
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x2940
addq $0x60, %rsp
popq %rbp
retq