// File: /workspace/github_cpp_makefile_star2_O2/dvjdjvu[P]bntree/./src/bntree.cpp
// Function: bntree::erase(unsigned long)
void bntree::erase(uint64_t index) {

    if (index < 0 || index > this->size() - 1) {
        return;
    }

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

    for (;;) {
        if (index == index_node) {
            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 (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);
        }
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x2871
movq 0x40(%r14), %rax
decq %rax
cmpq %r15, %rax
jae 0x2871
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2880
movq 0x40(%rax), %rax
jmp 0x2882
xorl %eax, %eax
pushq $0x1
popq %rcx
pushq $-0x1
popq %rdx
cmpq %r15, %rax
je 0x28cf
decq 0x40(%r14)
cmpq %r15, %rax
jae 0x28ac
movq 0x50(%r14), %r14
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x28c2
movq 0x40(%rsi), %rsi
incq %rsi
jmp 0x28ca
movq 0x48(%r14), %r14
movq 0x50(%r14), %rsi
testq %rsi, %rsi
je 0x28c7
movq 0x40(%rsi), %rsi
notq %rsi
jmp 0x28ca
movq %rcx, %rsi
jmp 0x28ca
movq %rdx, %rsi
addq %rsi, %rax
jmp 0x2888
movq %rbx, %rdi
movq %r14, %rsi
callq 0x278a
testb %al, %al
jne 0x295a
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x295a
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x295a
movq 0x40(%rax), %rdx
xorl %eax, %eax
cmpq 0x40(%rcx), %rdx
adcq $-0x1, %rax
orq $0x1, %rax
addq %r15, %rax
pushq $0x1
popq %rcx
pushq $-0x1
popq %rdx
movq %r14, %r12
cmpq %r15, %rax
je 0x2970
decq 0x40(%r12)
cmpq %r15, %rax
jbe 0x2935
movq 0x50(%r12), %r12
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x294d
movq 0x40(%rsi), %rsi
incq %rsi
jmp 0x2955
movq 0x48(%r12), %r12
movq 0x50(%r12), %rsi
testq %rsi, %rsi
je 0x2952
movq 0x40(%rsi), %rsi
notq %rsi
jmp 0x2955
movq %rcx, %rsi
jmp 0x2955
movq %rdx, %rsi
addq %rsi, %r15
jmp 0x290e
movq %r14, %r12
movq %r12, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x243a
movq %r14, %rdi
movq %r12, %rsi
callq 0x2dc2
movq %rbx, %rdi
movq %r12, %rsi
callq 0x278a
jmp 0x295d