// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movl $0x1, %edi
movl $0x30, %esi
callq 0x10e0
testq %rax, %rax
je 0x409f
movq %rax, %r14
movq %r12, (%rax)
movq %r15, 0x8(%rax)
movl $0x0, 0x28(%rax)
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x40a9
movq %rax, %r15
movq (%r14), %rsi
movq (%rax), %rdx
movq %rbx, %rdi
callq *0x8(%rbx)
notl %eax
shrl $0x1f, %eax
movq 0x18(%r15,%rax,8), %rax
testq %rax, %rax
jne 0x407f
jmp 0x40ac
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x41de
xorl %r15d, %r15d
movq %r15, 0x10(%r14)
testq %r15, %r15
je 0x40cb
movq (%r14), %rsi
movq (%r15), %rdx
movq %rbx, %rdi
callq *0x8(%rbx)
testl %eax, %eax
js 0x40d1
addq $0x20, %r15
jmp 0x40d5
leaq 0x28(%rbx), %r15
jmp 0x40d5
addq $0x18, %r15
movq %r14, (%r15)
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x41ce
movq %r14, %rax
addq $0x10, %rax
movl $0x1, %ebp
cmpl $0x0, 0x28(%r15)
jne 0x41ce
movq 0x10(%r15), %rdx
movq 0x18(%rdx), %rcx
cmpq %rcx, %r15
je 0x4145
testq %rcx, %rcx
je 0x4114
cmpl $0x0, 0x28(%rcx)
je 0x4184
cmpq 0x18(%r15), %r14
je 0x41a2
movl $0x1, 0x28(%r15)
movq (%rax), %rcx
movq 0x10(%rcx), %rcx
movl $0x0, 0x28(%rcx)
movq (%rax), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x4502
jmp 0x417f
movq 0x20(%rdx), %rcx
testq %rcx, %rcx
je 0x4154
cmpl $0x0, 0x28(%rcx)
je 0x4184
cmpq 0x20(%r15), %r14
je 0x41af
movl $0x1, 0x28(%r15)
movq (%rax), %rcx
movq 0x10(%rcx), %rcx
movl $0x0, 0x28(%rcx)
movq (%rax), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x454a
movq %r14, %r15
jmp 0x41ba
movl %ebp, 0x28(%r15)
movl %ebp, 0x28(%rcx)
movq (%rax), %rcx
movq 0x10(%rcx), %rcx
movl $0x0, 0x28(%rcx)
movq (%rax), %rax
movq 0x10(%rax), %r15
jmp 0x41ba
movq %rbx, %rdi
movq %r15, %rsi
callq 0x454a
jmp 0x41ba
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4502
movq %r15, %r14
leaq 0x10(%r15), %rax
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x40f1
movq 0x28(%rbx), %rax
movl $0x1, 0x28(%rax)
incl 0x30(%rbx)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq