// File: /workspace/github_cpp_makefile_star2_O3/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 0x2953
movq 0x40(%r14), %rax
decq %rax
cmpq %r15, %rax
jae 0x2953
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2962
movq 0x40(%rax), %rax
jmp 0x29ac
xorl %eax, %eax
jmp 0x29ac
decq 0x40(%r14)
cmpq %r15, %rax
jae 0x2985
movq 0x50(%r14), %r14
movq 0x48(%r14), %rcx
testq %rcx, %rcx
je 0x299b
movq 0x40(%rcx), %rcx
incq %rcx
jmp 0x29a9
movq 0x48(%r14), %r14
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x29a2
movq 0x40(%rcx), %rcx
notq %rcx
jmp 0x29a9
movl $0x1, %ecx
jmp 0x29a9
movq $-0x1, %rcx
addq %rcx, %rax
cmpq %r15, %rax
jne 0x2966
movq %rbx, %rdi
movq %r14, %rsi
callq 0x285c
testb %al, %al
jne 0x2a6f
movq 0x48(%r14), %rax
testq %rax, %rax
je 0x2a6f
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x2a6f
movq 0x40(%rax), %rdx
xorl %eax, %eax
cmpq 0x40(%rcx), %rdx
adcq $-0x1, %rax
orq $0x1, %rax
addq %r15, %rax
movq %r14, %r12
decq 0x40(%r12)
cmpq %r15, %rax
jbe 0x2a18
movq 0x50(%r12), %r12
movq 0x48(%r12), %rcx
testq %rcx, %rcx
je 0x2a30
movq 0x40(%rcx), %rcx
incq %rcx
jmp 0x2a3e
movq 0x48(%r12), %r12
movq 0x50(%r12), %rcx
testq %rcx, %rcx
je 0x2a37
movq 0x40(%rcx), %rcx
notq %rcx
jmp 0x2a3e
movl $0x1, %ecx
jmp 0x2a3e
movq $-0x1, %rcx
addq %rcx, %r15
cmpq %r15, %rax
jne 0x29f6
movq %r14, %rdi
movq %r12, %rsi
callq 0x20b0
addq $0x20, %r14
leaq 0x20(%r12), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x285c
jmp 0x2a72
movq %r14, %r12
movq %r12, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x247e
nop