// File: /workspace/github_c_makefile_star2_O1/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), %r8
movq 0x10(%rsi), %rdx
movq %rsi, %rcx
cmpq %rax, %r8
je 0x8342
cmpq %rax, %rdx
je 0x8345
movq %rdx, %rcx
movq 0x8(%rdx), %rdx
cmpq %rax, %rdx
jne 0x8330
movq 0x10(%rcx), %r8
jmp 0x8345
movq %rdx, %r8
cmpq (%rdi), %rcx
je 0x83c8
movq 0x18(%rcx), %r9
xorl %r10d, %r10d
cmpq 0x8(%r9), %rcx
movb 0x20(%rcx), %dl
setne %r10b
movq %r8, 0x8(%r9,%r10,8)
cmpq %rsi, %rcx
je 0x83df
cmpq %rsi, %r9
cmoveq %rcx, %r9
movq %r9, 0x18(%r8)
movq 0x8(%rsi), %r9
movq %r9, 0x8(%rcx)
movq 0x10(%rsi), %r9
movq %r9, 0x10(%rcx)
movq 0x18(%rsi), %r9
movq %r9, 0x18(%rcx)
movb 0x20(%rsi), %r9b
movb %r9b, 0x20(%rcx)
movq %rdi, %r9
cmpq %rsi, (%rdi)
je 0x83ad
movq 0x18(%rsi), %r10
leaq 0x8(%r10), %r11
leaq 0x10(%r10), %r9
cmpq %rsi, 0x8(%r10)
cmoveq %r11, %r9
movq %rcx, (%r9)
movq 0x8(%rcx), %r9
cmpq %rax, %r9
je 0x83bd
movq %rcx, 0x18(%r9)
movq 0x10(%rcx), %r10
cmpq %rax, %r10
jne 0x83e5
jmp 0x83e9
movq %r8, (%rdi)
movb $0x0, 0x20(%r8)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
jmp 0x869b
movq %r8, %r10
movq %r9, %rcx
movq %rcx, 0x18(%r10)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
testb %dl, %dl
jne 0x869b
cmpq (%rdi), %r8
je 0x8696
xorl %ecx, %ecx
cmpb $0x0, 0x20(%r8)
jne 0x8696
leaq 0x18(%r8), %rdx
movq 0x18(%r8), %r9
movq 0x8(%r9), %rsi
cmpq %rsi, %r8
je 0x84e7
cmpb $0x0, 0x20(%rsi)
je 0x847c
movb $0x0, 0x20(%rsi)
movb $0x1, 0x20(%r9)
movq 0x10(%rsi), %r8
movq %r8, 0x8(%r9)
cmpq %rax, %r8
je 0x8446
movq %r9, 0x18(%r8)
movq 0x18(%r9), %r8
movq %r8, 0x18(%rsi)
movq %rdi, %r8
cmpq %r9, (%rdi)
je 0x846a
movq 0x18(%r9), %r10
leaq 0x10(%r10), %r11
leaq 0x8(%r10), %r8
cmpq %r9, 0x10(%r10)
cmoveq %r11, %r8
movq %rsi, (%r8)
movq %r9, 0x10(%rsi)
movq %rsi, 0x18(%r9)
movq (%rdx), %rsi
movq 0x8(%rsi), %rsi
movq 0x8(%rsi), %r8
movb 0x20(%r8), %r8b
testb %r8b, %r8b
je 0x8620
movq (%rdx), %rdx
movb 0x20(%rdx), %r8b
movb %r8b, 0x20(%rsi)
movb %cl, 0x20(%rdx)
movq 0x8(%rsi), %rsi
movb %cl, 0x20(%rsi)
movq 0x8(%rdx), %rsi
movq 0x10(%rsi), %r8
movq %r8, 0x8(%rdx)
cmpq %rax, %r8
je 0x84b7
movq %rdx, 0x18(%r8)
movq 0x18(%rdx), %r8
movq %r8, 0x18(%rsi)
movq %rdi, %r8
cmpq %rdx, (%rdi)
je 0x84db
movq 0x18(%rdx), %r9
leaq 0x10(%r9), %r10
leaq 0x8(%r9), %r8
cmpq %rdx, 0x10(%r9)
cmoveq %r10, %r8
movq %rsi, (%r8)
movq %rdx, 0x10(%rsi)
jmp 0x860b
movq 0x10(%r9), %rsi
cmpb $0x0, 0x20(%rsi)
je 0x8541
movb $0x0, 0x20(%rsi)
movb $0x1, 0x20(%r9)
movq 0x8(%rsi), %r8
movq %r8, 0x10(%r9)
cmpq %rax, %r8
je 0x850b
movq %r9, 0x18(%r8)
movq 0x18(%r9), %r8
movq %r8, 0x18(%rsi)
movq %rdi, %r8
cmpq %r9, (%rdi)
je 0x852f
movq 0x18(%r9), %r10
leaq 0x8(%r10), %r11
leaq 0x10(%r10), %r8
cmpq %r9, 0x8(%r10)
cmoveq %r11, %r8
movq %rsi, (%r8)
movq %r9, 0x8(%rsi)
movq %rsi, 0x18(%r9)
movq (%rdx), %rsi
movq 0x10(%rsi), %rsi
movq 0x8(%rsi), %r8
cmpb $0x0, 0x20(%r8)
jne 0x855b
movq 0x10(%rsi), %r9
cmpb $0x0, 0x20(%r9)
je 0x868d
movq 0x10(%rsi), %r9
cmpb $0x0, 0x20(%r9)
jne 0x85b6
movb $0x0, 0x20(%r8)
movb $0x1, 0x20(%rsi)
movq 0x10(%r8), %r9
movq %r9, 0x8(%rsi)
cmpq %rax, %r9
je 0x8580
movq %rsi, 0x18(%r9)
movq 0x18(%rsi), %r9
movq %r9, 0x18(%r8)
movq %rdi, %r9
cmpq %rsi, (%rdi)
je 0x85a4
movq 0x18(%rsi), %r10
leaq 0x10(%r10), %r11
leaq 0x8(%r10), %r9
cmpq %rsi, 0x10(%r10)
cmoveq %r11, %r9
movq %r8, (%r9)
movq %rsi, 0x10(%r8)
movq %r8, 0x18(%rsi)
movq (%rdx), %rsi
movq 0x10(%rsi), %rsi
movq (%rdx), %rdx
movb 0x20(%rdx), %r8b
movb %r8b, 0x20(%rsi)
movb %cl, 0x20(%rdx)
movq 0x10(%rsi), %rsi
movb %cl, 0x20(%rsi)
movq 0x10(%rdx), %rsi
movq 0x8(%rsi), %r8
movq %r8, 0x10(%rdx)
cmpq %rax, %r8
je 0x85e0
movq %rdx, 0x18(%r8)
movq 0x18(%rdx), %r8
movq %r8, 0x18(%rsi)
movq %rdi, %r8
cmpq %rdx, (%rdi)
je 0x8604
movq 0x18(%rdx), %r9
leaq 0x8(%r9), %r10
leaq 0x10(%r9), %r8
cmpq %rdx, 0x8(%r9)
cmoveq %r10, %r8
movq %rsi, (%r8)
movq %rdx, 0x8(%rsi)
movq %rsi, 0x18(%rdx)
movq %rdi, %rdx
movq (%rdx), %r8
cmpq (%rdi), %r8
jne 0x8406
jmp 0x8696
movq 0x10(%rsi), %r9
cmpb $0x0, 0x20(%r9)
je 0x868d
testb %r8b, %r8b
jne 0x848d
movq 0x10(%rsi), %r8
movb $0x0, 0x20(%r8)
movb $0x1, 0x20(%rsi)
movq 0x8(%r8), %r9
movq %r9, 0x10(%rsi)
cmpq %rax, %r9
je 0x8652
movq %rsi, 0x18(%r9)
movq 0x18(%rsi), %r9
movq %r9, 0x18(%r8)
movq %rdi, %r9
cmpq %rsi, (%rdi)
je 0x8676
movq 0x18(%rsi), %r10
leaq 0x8(%r10), %r11
leaq 0x10(%r10), %r9
cmpq %rsi, 0x8(%r10)
cmoveq %r11, %r9
movq %r8, (%r9)
movq %rsi, 0x8(%r8)
movq %r8, 0x18(%rsi)
movq (%rdx), %rsi
movq 0x8(%rsi), %rsi
jmp 0x848d
movb $0x1, 0x20(%rsi)
jmp 0x8612
movb $0x0, 0x20(%r8)
popq %rbp
retq