// File: /workspace/github_cpp_makefile_star2_O1/dvjdjvu[P]bntree/./src/bntree.cpp
// Function: bntree::erase(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void bntree::erase(string key) {

    node_t *search_node = this->tree->root;
    uint64_t index_node = this->get_child_weight(search_node->left);

    for (;;) {
        if (key == search_node->data.key) {
            if (this->erase_simple(search_node)) {
                // pass
            } else if (search_node->left && search_node->right) {
                // Самый сложный случай, удаляемый узел имеет 2-х детей.
                
                node_t *del_node = search_node;
                uint64_t _index;
                // Маленькая балансировка, если правый вес больше левого то удаляем через право
                // иначе через лево
                if (search_node->right->weight > search_node->left->weight) {
                    _index = index_node + 1;
                } else {
                    _index = index_node - 1;
                }

                // Ищем узел со следующим индексом.
                for (;;) {
                    if (_index == index_node) {
                        // Узел найден
                        break;
                    } else if (_index > index_node) {
                        search_node->weight--;
                        search_node = search_node->right;
                        index_node += (this->get_child_weight(search_node->left) + 1);
                    } else {
                        search_node->weight--;

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

                del_node->data = search_node->data;
                this->erase_simple(search_node);
            }

            this->node_free(search_node);

            return;
        } else if (key > search_node->data.key) {
            search_node->weight--;
            search_node = search_node->right;
            index_node += (this->get_child_weight(search_node->left) + 1);
        } else {
            search_node->weight--;
            search_node = search_node->left;
            index_node -= (this->get_child_weight(search_node->right) + 1);
        }

    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rax
movq (%rax), %r14
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2ad1
movq 0x40(%rax), %r12
jmp 0x2ad4
xorl %r12d, %r12d
movq 0x8(%r15), %rdx
cmpq 0x8(%r14), %rdx
jne 0x2af2
testq %rdx, %rdx
je 0x2b44
movq (%r14), %rsi
movq (%r15), %rdi
callq 0x2180
testl %eax, %eax
je 0x2b44
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
decq 0x40(%r14)
testl %eax, %eax
jle 0x2b1b
movq 0x50(%r14), %r14
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2b31
movq 0x40(%rax), %rax
incq %rax
jmp 0x2b3f
movq 0x48(%r14), %r14
movq 0x50(%r14), %rax
testq %rax, %rax
je 0x2b38
movq 0x40(%rax), %rax
notq %rax
jmp 0x2b3f
movl $0x1, %eax
jmp 0x2b3f
movq $-0x1, %rax
addq %rax, %r12
jmp 0x2ad4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2862
testb %al, %al
jne 0x2bfc
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2bfc
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x2bfc
movq 0x40(%rax), %rdx
xorl %eax, %eax
cmpq 0x40(%rcx), %rdx
adcq $-0x1, %rax
orq $0x1, %rax
addq %r12, %rax
movq %r14, %r15
decq 0x40(%r15)
cmpq %r12, %rax
jbe 0x2ba8
movq 0x50(%r15), %r15
movq 0x48(%r15), %rcx
testq %rcx, %rcx
je 0x2bbe
movq 0x40(%rcx), %rcx
incq %rcx
jmp 0x2bcc
movq 0x48(%r15), %r15
movq 0x50(%r15), %rcx
testq %rcx, %rcx
je 0x2bc5
movq 0x40(%rcx), %rcx
notq %rcx
jmp 0x2bcc
movl $0x1, %ecx
jmp 0x2bcc
movq $-0x1, %rcx
addq %rcx, %r12
cmpq %r12, %rax
jne 0x2b89
movq %r14, %rdi
movq %r15, %rsi
callq 0x20b0
addq $0x20, %r14
leaq 0x20(%r15), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2862
jmp 0x2bff
movq %r14, %r15
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x2486
movq %rax, %rdi
callq 0x2f59