// File: /workspace/github_c_makefile_star2_O1/qq-smart[P]qq_sh_server/core/qq_rbtree.c
// Function: qq_rbtree_insert
void
qq_rbtree_insert(qq_rbtree_t *tree, qq_rbtree_node_t *node)
{
    qq_rbtree_node_t  **root, *temp, *sentinel;

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

    if (*root == sentinel) {
        node->parent = NULL;
        node->left = sentinel;
        node->right = sentinel;
        qq_rbt_black(node);
        *root = node;

        return;
    }

    tree->insert(*root, node, sentinel);

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

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

                qq_rbt_black(node->parent);
                qq_rbt_red(node->parent->parent);
                qq_rbtree_right_rotate(root, sentinel, node->parent->parent);
            }
        } else {
            temp = node->parent->parent->left;

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

                qq_rbt_black(node->parent);
                qq_rbt_red(node->parent->parent);
                qq_rbtree_left_rotate(root, sentinel, node->parent->parent);
            }
        }
    }

    qq_rbt_black(*root);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x8(%rbx), %r14
cmpq %r14, %rdi
je 0x8278
movq %r15, %rsi
movq %r14, %rdx
callq *0x10(%rbx)
movq (%rbx), %rsi
cmpq %r15, %rsi
je 0x8272
movq 0x18(%r15), %rcx
cmpb $0x0, 0x20(%rcx)
je 0x8272
leaq 0x20(%rcx), %rdx
xorl %eax, %eax
movq 0x18(%rcx), %rsi
movq 0x8(%rsi), %rdi
cmpq %rdi, %rcx
je 0x819d
cmpb $0x0, 0x20(%rdi)
jne 0x81a7
movq 0x8(%rcx), %rdx
cmpq %rdx, %r15
jne 0x8148
movq 0x10(%rdx), %rsi
movq %rsi, 0x8(%rcx)
cmpq %r14, %rsi
je 0x8116
movq %rcx, 0x18(%rsi)
movq 0x18(%rcx), %rsi
movq %rsi, 0x18(%rdx)
movq %rbx, %rsi
cmpq %rcx, (%rbx)
je 0x813a
movq 0x18(%rcx), %rdi
leaq 0x10(%rdi), %r8
leaq 0x8(%rdi), %rsi
cmpq %rcx, 0x10(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x10(%rdx)
movq %rdx, 0x18(%rcx)
movq %rcx, %r15
movq 0x18(%r15), %rcx
movb $0x0, 0x20(%rcx)
movq 0x18(%rcx), %rcx
movb $0x1, 0x20(%rcx)
movq 0x10(%rcx), %rdx
movq 0x8(%rdx), %rsi
movq %rsi, 0x10(%rcx)
cmpq %r14, %rsi
je 0x816d
movq %rcx, 0x18(%rsi)
movq 0x18(%rcx), %rsi
movq %rsi, 0x18(%rdx)
movq %rbx, %rsi
cmpq %rcx, (%rbx)
je 0x8191
movq 0x18(%rcx), %rdi
leaq 0x8(%rdi), %r8
leaq 0x10(%rdi), %rsi
cmpq %rcx, 0x8(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x8(%rdx)
jmp 0x8254
movq 0x10(%rsi), %rdi
cmpb $0x0, 0x20(%rdi)
je 0x81b8
movb %al, (%rdx)
movb %al, 0x20(%rdi)
movb $0x1, 0x20(%rsi)
movq %rsi, %r15
jmp 0x8258
movq 0x10(%rcx), %rdx
cmpq %rdx, %r15
jne 0x8204
movq 0x8(%rdx), %rsi
movq %rsi, 0x10(%rcx)
cmpq %r14, %rsi
je 0x81d2
movq %rcx, 0x18(%rsi)
movq 0x18(%rcx), %rsi
movq %rsi, 0x18(%rdx)
movq %rbx, %rsi
cmpq %rcx, (%rbx)
je 0x81f6
movq 0x18(%rcx), %rdi
leaq 0x8(%rdi), %r8
leaq 0x10(%rdi), %rsi
cmpq %rcx, 0x8(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x8(%rdx)
movq %rdx, 0x18(%rcx)
movq %rcx, %r15
movq 0x18(%r15), %rcx
movb $0x0, 0x20(%rcx)
movq 0x18(%rcx), %rcx
movb $0x1, 0x20(%rcx)
movq 0x8(%rcx), %rdx
movq 0x10(%rdx), %rsi
movq %rsi, 0x8(%rcx)
cmpq %r14, %rsi
je 0x8229
movq %rcx, 0x18(%rsi)
movq 0x18(%rcx), %rsi
movq %rsi, 0x18(%rdx)
movq %rbx, %rsi
cmpq %rcx, (%rbx)
je 0x824d
movq 0x18(%rcx), %rdi
leaq 0x10(%rdi), %r8
leaq 0x8(%rdi), %rsi
cmpq %rcx, 0x10(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x10(%rdx)
movq %rdx, 0x18(%rcx)
movq (%rbx), %rsi
cmpq %rsi, %r15
je 0x8272
movq 0x18(%r15), %rcx
leaq 0x20(%rcx), %rdx
cmpb $0x0, 0x20(%rcx)
jne 0x80e1
movb $0x0, 0x20(%rsi)
jmp 0x8290
movq $0x0, 0x18(%r15)
movq %r14, 0x8(%r15)
movq %r14, 0x10(%r15)
movb $0x0, 0x20(%r15)
movq %r15, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq