// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x8(%rbx), %r14
cmpq %r14, %rdi
je 0x79b6
movq %r15, %rsi
movq %r14, %rdx
callq *0x10(%rbx)
xorl %r13d, %r13d
movq (%rbx), %rax
cmpq %rax, %r15
je 0x79b0
movq 0x18(%r15), %r12
cmpb $0x0, 0x20(%r12)
je 0x79b0
movq 0x18(%r12), %rdx
movq 0x8(%rdx), %rax
cmpq %rax, %r12
je 0x7924
cmpb $0x0, 0x20(%rax)
jne 0x792e
cmpq 0x8(%r12), %r15
je 0x794c
movq %r12, %rax
jmp 0x7966
movq 0x10(%rdx), %rax
cmpb $0x0, 0x20(%rax)
je 0x7940
movb %r13b, 0x20(%r12)
movb %r13b, 0x20(%rax)
movb $0x1, 0x20(%rdx)
movq %rdx, %r15
jmp 0x78e8
cmpq 0x10(%r12), %r15
je 0x797e
movq %r12, %rax
jmp 0x7998
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x7a1d
movq 0x18(%r12), %rax
movq 0x18(%rax), %rdx
movq %r12, %r15
movb $0x0, 0x20(%rax)
movb $0x1, 0x20(%rdx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x79da
jmp 0x78e8
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x79da
movq 0x18(%r12), %rax
movq 0x18(%rax), %rdx
movq %r12, %r15
movb $0x0, 0x20(%rax)
movb $0x1, 0x20(%rdx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7a1d
jmp 0x78e8
movb $0x0, 0x20(%rax)
jmp 0x79cb
andq $0x0, 0x18(%r15)
movq %r14, 0x8(%r15)
movq %r14, 0x10(%r15)
movb $0x0, 0x20(%r15)
movq %r15, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq