// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/common/tree.c
// Function: tt_rbtree_insert
static int tt_rbtree_insert(struct tt_bintree *tree, const void *key,
		const void *v)
{
	struct tt_bintree_node *z = calloc(1, sizeof(struct tt_bintree_node));
	if (!z)
		return TT_ENOMEM;
	z->key = (void *)key;
	z->data = (void *)v;
	z->ext.color = TT_BINTREE_RED;

	/* Insert as normal binary search tree */
	struct tt_bintree_node *y = NULL, *x = tree->_root;
	while (x) {
		y = x;
		if (tree->knum.cmp(&tree->knum, z->key, x->key) < 0)
			x = x->left;
		else
			x = x->right;
	}
	z->parent = y;
	if (!y)
		tree->_root = z;	/* Empty tree */
	else if (tree->knum.cmp(&tree->knum, z->key, y->key) < 0)
		y->left = z;
	else
		y->right = z;

	/* Rotate to maintain red-black property */
	while (z->parent && z->parent->ext.color == TT_BINTREE_RED) {
		if (z->parent == z->parent->parent->left) {
			y = z->parent->parent->right;
			if (y && y->ext.color == TT_BINTREE_RED) {
				z->parent->ext.color = TT_BINTREE_BLACK;
				y->ext.color = TT_BINTREE_BLACK;
				z->parent->parent->ext.color = TT_BINTREE_RED;
				z = z->parent->parent;
			} else if (z == z->parent->right) {
				z = z->parent;
				tt_rbtree_rotate_left(tree, z);
			} else {
				z->parent->ext.color = TT_BINTREE_BLACK;
				z->parent->parent->ext.color = TT_BINTREE_RED;
				tt_rbtree_rotate_right(tree, z->parent->parent);
			}
		} else {
			y = z->parent->parent->left;
			if (y && y->ext.color == TT_BINTREE_RED) {
				z->parent->ext.color = TT_BINTREE_BLACK;
				y->ext.color = TT_BINTREE_BLACK;
				z->parent->parent->ext.color = TT_BINTREE_RED;
				z = z->parent->parent;
			} else if (z == z->parent->left) {
				z = z->parent;
				tt_rbtree_rotate_right(tree, z);
			} else {
				z->parent->ext.color = TT_BINTREE_BLACK;
				z->parent->parent->ext.color = TT_BINTREE_RED;
				tt_rbtree_rotate_left(tree, z->parent->parent);
			}
		}
	}
	tree->_root->ext.color = TT_BINTREE_BLACK;

	tree->_count++;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x10e0
testq %rax, %rax
je 0x3c06
movq %rax, %r14
movq %r15, (%rax)
movq %r12, 0x8(%rax)
leaq 0x28(%rbx), %r12
xorl %r13d, %r13d
movq %r12, %rax
movq (%rax), %rbp
testq %rbp, %rbp
je 0x3be6
movq (%rbp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x8(%rbx)
notl %eax
shrl $0x1f, %eax
leaq 0x18(,%rax,8), %rax
addq %rbp, %rax
movq %rbp, %r13
jmp 0x3bbc
movq %r13, 0x10(%r14)
testq %r13, %r13
je 0x3c15
movq (%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x8(%rbx)
testl %eax, %eax
js 0x3c0e
addq $0x20, %r13
jmp 0x3c12
pushq $-0x1
popq %rax
jmp 0x3d10
addq $0x18, %r13
movq %r13, %r12
movq %r14, (%r12)
pushq $0x1
popq %rbp
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x3d00
cmpl $0x0, 0x28(%r15)
jne 0x3d00
movq 0x10(%r15), %rcx
movq 0x18(%rcx), %rax
cmpq %rax, %r15
je 0x3c7c
testq %rax, %rax
je 0x3c4c
cmpl $0x0, 0x28(%rax)
je 0x3cba
cmpq 0x18(%r15), %r14
je 0x3cda
movl $0x1, 0x28(%r15)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
andl $0x0, 0x28(%rax)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x3ff1
jmp 0x3c1c
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0x3c8b
cmpl $0x0, 0x28(%rax)
je 0x3cba
cmpq 0x20(%r15), %r14
je 0x3ced
movl $0x1, 0x28(%r15)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
andl $0x0, 0x28(%rax)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x4035
jmp 0x3c1c
movl %ebp, 0x28(%r15)
movl %ebp, 0x28(%rax)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
andl $0x0, 0x28(%rax)
movq 0x10(%r14), %rax
movq 0x10(%rax), %r14
jmp 0x3c1c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4035
movq %r15, %r14
jmp 0x3c1c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3ff1
movq %r15, %r14
jmp 0x3c1c
movq 0x28(%rbx), %rax
movl $0x1, 0x28(%rax)
incl 0x30(%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq