// File: /workspace/github_c_makefile_star2_O3/qq-smart[P]qq_sh_server/core/qq_rbtree.c
// Function: qq_rbtree_delete
void
qq_rbtree_delete(qq_rbtree_t *tree, qq_rbtree_node_t *node)
{
    qq_uint_t           red;
    qq_rbtree_node_t  **root, *sentinel, *subst, *temp, *w;

    root = &tree->root;
    sentinel = tree->sentinel;

    if (node->left == sentinel) {
        temp = node->right;
        subst = node;
    } else if (node->right == sentinel) {
        temp = node->left;
        subst = node;
    } else {
        subst = qq_rbtree_min(node->right, sentinel);

        if (subst->left != sentinel) {
            temp = subst->left;
        } else {
            temp = subst->right;
        }
    }

    if (subst == *root) {
        *root = temp;
        qq_rbt_black(temp);

        /* DEBUG stuff */
        node->left = NULL;
        node->right = NULL;
        node->parent = NULL;
        node->key = 0;

        return;
    }

    red = qq_rbt_is_red(subst);

    if (subst == subst->parent->left) {
        subst->parent->left = temp;
    } else {
        subst->parent->right = temp;
    }

    if (subst == node) {
        temp->parent = subst->parent;
    } else {
        if (subst->parent == node) {
            temp->parent = subst;
        } else {
            temp->parent = subst->parent;
        }

        subst->left = node->left;
        subst->right = node->right;
        subst->parent = node->parent;
        qq_rbt_copy_color(subst, node);

        if (node == *root) {
            *root = subst;
        } else {
            if (node == node->parent->left) {
                node->parent->left = subst;
            } else {
                node->parent->right = subst;
            }
        }

        if (subst->left != sentinel) {
            subst->left->parent = subst;
        }

        if (subst->right != sentinel) {
            subst->right->parent = subst;
        }
    }

    /* DEBUG stuff */
    node->left = NULL;
    node->right = NULL;
    node->parent = NULL;
    node->key = 0;

    if (red) {
        return;
    }

    /* a delete fixup */

    while (temp != *root && qq_rbt_is_black(temp)) {
        if (temp == temp->parent->left) {
            w = temp->parent->right;

            if (qq_rbt_is_red(w)) {
                qq_rbt_black(w);
                qq_rbt_red(temp->parent);
                qq_rbtree_left_rotate(root, sentinel, temp->parent);
                w = temp->parent->right;
            }

            if (qq_rbt_is_black(w->left) && qq_rbt_is_black(w->right)) {
                qq_rbt_red(w);
                temp = temp->parent;
            } else {
                if (qq_rbt_is_black(w->right)) {
                    qq_rbt_black(w->left);
                    qq_rbt_red(w);
                    qq_rbtree_right_rotate(root, sentinel, w);
                    w = temp->parent->right;
                }

                qq_rbt_copy_color(w, temp->parent);
                qq_rbt_black(temp->parent);
                qq_rbt_black(w->right);
                qq_rbtree_left_rotate(root, sentinel, temp->parent);
                temp = *root;
            }
        } else {
            w = temp->parent->left;

            if (qq_rbt_is_red(w)) {
                qq_rbt_black(w);
                qq_rbt_red(temp->parent);
                qq_rbtree_right_rotate(root, sentinel, temp->parent);
                w = temp->parent->left;
            }

            if (qq_rbt_is_black(w->left) && qq_rbt_is_black(w->right)) {
                qq_rbt_red(w);
                temp = temp->parent;
            } else {
                if (qq_rbt_is_black(w->left)) {
                    qq_rbt_black(w->right);
                    qq_rbt_red(w);
                    qq_rbtree_left_rotate(root, sentinel, w);
                    w = temp->parent->left;
                }

                qq_rbt_copy_color(w, temp->parent);
                qq_rbt_black(temp->parent);
                qq_rbt_black(w->left);
                qq_rbtree_right_rotate(root, sentinel, temp->parent);
                temp = *root;
            }
        }
    }

    qq_rbt_black(temp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq 0x8(%rdi), %rax
movq 0x8(%rsi), %r9
movq 0x10(%rsi), %rdx
movq %rsi, %rcx
cmpq %rax, %r9
je 0x81f3
cmpq %rax, %rdx
je 0x81f6
movq %rdx, %rcx
movq 0x8(%rdx), %rdx
cmpq %rax, %rdx
jne 0x81e1
movq 0x10(%rcx), %r9
jmp 0x81f6
movq %rdx, %r9
cmpq (%rdi), %rcx
je 0x8275
movq 0x18(%rcx), %r8
xorl %r10d, %r10d
cmpq 0x8(%r8), %rcx
movb 0x20(%rcx), %dl
setne %r10b
movq %r9, 0x8(%r8,%r10,8)
cmpq %rsi, %rcx
je 0x828c
cmpq %rsi, %r8
cmoveq %rcx, %r8
movq %r8, 0x18(%r9)
movq 0x8(%rsi), %r8
movq %r8, 0x8(%rcx)
movq 0x10(%rsi), %r8
movq %r8, 0x10(%rcx)
movq 0x18(%rsi), %r8
movq %r8, 0x18(%rcx)
movb 0x20(%rsi), %r10b
movb %r10b, 0x20(%rcx)
movq %rdi, %r10
cmpq %rsi, (%rdi)
je 0x825a
leaq 0x8(%r8), %r11
leaq 0x10(%r8), %r10
cmpq %rsi, 0x8(%r8)
cmoveq %r11, %r10
movq %rcx, (%r10)
movq 0x8(%rcx), %r8
cmpq %rax, %r8
je 0x826a
movq %rcx, 0x18(%r8)
movq 0x10(%rcx), %r10
cmpq %rax, %r10
jne 0x8292
jmp 0x8296
movq %r9, (%rdi)
movb $0x0, 0x20(%r9)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
jmp 0x850a
movq %r9, %r10
movq %r8, %rcx
movq %rcx, 0x18(%r10)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
testb %dl, %dl
jne 0x850a
cmpq (%rdi), %r9
je 0x8505
xorl %ecx, %ecx
cmpb $0x0, 0x20(%r9)
jne 0x8505
leaq 0x18(%r9), %rsi
movq 0x18(%r9), %rdx
movq 0x8(%rdx), %r8
cmpq %r8, %r9
je 0x83db
cmpb $0x0, 0x20(%r8)
je 0x8326
movb $0x0, 0x20(%r8)
movb $0x1, 0x20(%rdx)
movq 0x10(%r8), %r9
movq %r9, 0x8(%rdx)
cmpq %rax, %r9
je 0x82f4
movq %rdx, 0x18(%r9)
movq 0x18(%rdx), %r10
movq %r10, 0x18(%r8)
movq %rdi, %r9
cmpq %rdx, (%rdi)
je 0x8314
leaq 0x10(%r10), %r11
leaq 0x8(%r10), %r9
cmpq %rdx, 0x10(%r10)
cmoveq %r11, %r9
movq %r8, (%r9)
movq %rdx, 0x10(%r8)
movq %r8, 0x18(%rdx)
movq (%rsi), %rdx
movq 0x8(%rdx), %r8
movq 0x8(%r8), %r9
cmpb $0x0, 0x20(%r9)
jne 0x838f
movq 0x10(%r8), %rdx
cmpb $0x0, 0x20(%rdx)
je 0x84fe
movb $0x0, 0x20(%rdx)
movb $0x1, 0x20(%r8)
movq 0x8(%rdx), %r9
movq %r9, 0x10(%r8)
cmpq %rax, %r9
je 0x8359
movq %r8, 0x18(%r9)
movq 0x18(%r8), %r10
movq %r10, 0x18(%rdx)
movq %rdi, %r9
cmpq %r8, (%rdi)
je 0x8379
leaq 0x8(%r10), %r11
leaq 0x10(%r10), %r9
cmpq %r8, 0x8(%r10)
cmoveq %r11, %r9
movq %rdx, (%r9)
movq %r8, 0x8(%rdx)
movq %rdx, 0x18(%r8)
movq (%rsi), %rdx
movq 0x8(%rdx), %r8
movq 0x8(%r8), %r9
movb 0x20(%rdx), %sil
movb %sil, 0x20(%r8)
movb %cl, 0x20(%rdx)
movb %cl, 0x20(%r9)
movq 0x10(%r8), %rsi
movq %rsi, 0x8(%rdx)
cmpq %rax, %rsi
je 0x83af
movq %rdx, 0x18(%rsi)
movq 0x18(%rdx), %r9
movq %r9, 0x18(%r8)
movq %rdi, %rsi
cmpq %rdx, (%rdi)
je 0x83cf
leaq 0x10(%r9), %r10
leaq 0x8(%r9), %rsi
cmpq %rdx, 0x10(%r9)
cmoveq %r10, %rsi
movq %r8, (%rsi)
movq %rdx, 0x10(%r8)
jmp 0x84e9
movq 0x10(%rdx), %r8
cmpb $0x0, 0x20(%r8)
je 0x8432
movb $0x0, 0x20(%r8)
movb $0x1, 0x20(%rdx)
movq 0x8(%r8), %r9
movq %r9, 0x10(%rdx)
cmpq %rax, %r9
je 0x8400
movq %rdx, 0x18(%r9)
movq 0x18(%rdx), %r10
movq %r10, 0x18(%r8)
movq %rdi, %r9
cmpq %rdx, (%rdi)
je 0x8420
leaq 0x8(%r10), %r11
leaq 0x10(%r10), %r9
cmpq %rdx, 0x8(%r10)
cmoveq %r11, %r9
movq %r8, (%r9)
movq %rdx, 0x8(%r8)
movq %r8, 0x18(%rdx)
movq (%rsi), %rdx
movq 0x10(%rdx), %r8
movq 0x8(%r8), %r9
movq 0x10(%r8), %r10
cmpb $0x0, 0x20(%r9)
movb 0x20(%r10), %r11b
je 0x84a1
testb %r11b, %r11b
jne 0x84a6
movb $0x0, 0x20(%r9)
movb $0x1, 0x20(%r8)
movq 0x10(%r9), %rdx
movq %rdx, 0x8(%r8)
cmpq %rax, %rdx
je 0x8465
movq %r8, 0x18(%rdx)
movq 0x18(%r8), %r10
movq %r10, 0x18(%r9)
movq %rdi, %rdx
cmpq %r8, (%rdi)
je 0x8485
leaq 0x10(%r10), %r11
leaq 0x8(%r10), %rdx
cmpq %r8, 0x10(%r10)
cmoveq %r11, %rdx
movq %r9, (%rdx)
movq %r8, 0x10(%r9)
movq %r9, 0x18(%r8)
movq (%rsi), %rdx
movq 0x10(%rdx), %r8
movq 0x8(%r8), %r9
movq 0x10(%r8), %r10
jmp 0x84a6
testb %r11b, %r11b
je 0x84fe
movb 0x20(%rdx), %sil
movb %sil, 0x20(%r8)
movb %cl, 0x20(%rdx)
movb %cl, 0x20(%r10)
movq %r9, 0x10(%rdx)
cmpq %rax, %r9
je 0x84c2
movq %rdx, 0x18(%r9)
movq 0x18(%rdx), %r9
movq %r9, 0x18(%r8)
movq %rdi, %rsi
cmpq %rdx, (%rdi)
je 0x84e2
leaq 0x8(%r9), %r10
leaq 0x10(%r9), %rsi
cmpq %rdx, 0x8(%r9)
cmoveq %r10, %rsi
movq %r8, (%rsi)
movq %rdx, 0x8(%r8)
movq %r8, 0x18(%rdx)
movq %rdi, %rsi
movq (%rsi), %r9
cmpq (%rdi), %r9
jne 0x82b3
jmp 0x8505
movb $0x1, 0x20(%r8)
jmp 0x84f0
movb $0x0, 0x20(%r9)
popq %rbp
retq