// File: /workspace/github_c_makefile_star2_O3/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 0x8129
movq %r15, %rsi
movq %r14, %rdx
callq *0x10(%rbx)
movq (%rbx), %rdx
cmpq %r15, %rdx
je 0x8123
movq 0x18(%r15), %rcx
cmpb $0x0, 0x20(%rcx)
je 0x8123
leaq 0x20(%rcx), %rdi
xorl %eax, %eax
movq 0x18(%rcx), %rdx
movq 0x8(%rdx), %rsi
cmpq %rsi, %rcx
je 0x7faf
cmpb $0x0, 0x20(%rsi)
je 0x7fcc
movb %al, (%rdi)
movb %al, 0x20(%rsi)
jmp 0x7fc0
movq 0x10(%rdx), %r8
cmpb $0x0, 0x20(%r8)
je 0x7fdd
movb %al, (%rdi)
movb %al, 0x20(%r8)
movb $0x1, 0x20(%rdx)
movq %rdx, %r15
jmp 0x8109
movq 0x8(%rcx), %rsi
cmpq %rsi, %r15
je 0x7ff5
movq %rcx, %rsi
movq %r15, %rcx
jmp 0x8035
movq 0x10(%rcx), %rdi
cmpq %rdi, %r15
je 0x807e
movq %rcx, %rdi
movq %r15, %rcx
jmp 0x80c2
movq 0x10(%rsi), %rdi
movq %rdi, 0x8(%rcx)
cmpq %r14, %rdi
je 0x800a
movq %rcx, 0x18(%rdi)
movq 0x18(%rcx), %rdx
movq %rdx, 0x18(%rsi)
movq %rbx, %rdi
cmpq %rcx, (%rbx)
je 0x8026
leaq 0x10(%rdx), %r8
leaq 0x8(%rdx), %rdi
cmpq %rcx, 0x10(%rdx)
cmoveq %r8, %rdi
movq %rsi, (%rdi)
movq %rcx, 0x10(%rsi)
movq %rsi, 0x18(%rcx)
movq 0x18(%rsi), %rdx
movb $0x0, 0x20(%rsi)
movb $0x1, 0x20(%rdx)
movq 0x10(%rdx), %rsi
movq 0x8(%rsi), %rdi
movq %rdi, 0x10(%rdx)
cmpq %r14, %rdi
je 0x8052
movq %rdx, 0x18(%rdi)
movq 0x18(%rdx), %r8
movq %r8, 0x18(%rsi)
movq %rbx, %rdi
cmpq %rdx, (%rbx)
je 0x8072
leaq 0x8(%r8), %r9
leaq 0x10(%r8), %rdi
cmpq %rdx, 0x8(%r8)
cmoveq %r9, %rdi
movq %rsi, (%rdi)
movq %rdx, 0x8(%rsi)
jmp 0x8102
movq 0x8(%rdi), %rsi
movq %rsi, 0x10(%rcx)
cmpq %r14, %rsi
je 0x8093
movq %rcx, 0x18(%rsi)
movq 0x18(%rcx), %rdx
movq %rdx, 0x18(%rdi)
movq %rbx, %rsi
cmpq %rcx, (%rbx)
je 0x80af
leaq 0x8(%rdx), %r8
leaq 0x10(%rdx), %rsi
cmpq %rcx, 0x8(%rdx)
cmoveq %r8, %rsi
movq %rdi, (%rsi)
movq %rcx, 0x8(%rdi)
movq %rdi, 0x18(%rcx)
movq 0x18(%rdi), %rdx
movq 0x8(%rdx), %rsi
movb $0x0, 0x20(%rdi)
movb $0x1, 0x20(%rdx)
movq 0x10(%rsi), %rdi
movq %rdi, 0x8(%rdx)
cmpq %r14, %rdi
je 0x80db
movq %rdx, 0x18(%rdi)
movq 0x18(%rdx), %r8
movq %r8, 0x18(%rsi)
movq %rbx, %rdi
cmpq %rdx, (%rbx)
je 0x80fb
leaq 0x10(%r8), %r9
leaq 0x8(%r8), %rdi
cmpq %rdx, 0x10(%r8)
cmoveq %r9, %rdi
movq %rsi, (%rdi)
movq %rdx, 0x10(%rsi)
movq %rsi, 0x18(%rdx)
movq %rcx, %r15
movq (%rbx), %rdx
cmpq %rdx, %r15
je 0x8123
movq 0x18(%r15), %rcx
leaq 0x20(%rcx), %rdi
cmpb $0x0, 0x20(%rcx)
jne 0x7f95
movb $0x0, 0x20(%rdx)
jmp 0x8141
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