// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/redblack.c
// Function: rb_traverse
static struct RB_ENTRY(node) *
RB_ENTRY(_traverse)(int insert, const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
{
	struct RB_ENTRY(node) *x,*y,*z;
	int cmp;
	int found=0;
	int cmpmods();

	y=RBNULL; /* points to the parent of x */
	x=rbinfo->rb_root;

	/* walk x down the tree */
	while(x!=RBNULL && found==0)
	{
		y=x;
		/* printf("key=%s, RB_GET(x, key)=%s\n", key, RB_GET(x, key)); */
#ifndef RB_CUSTOMIZE
		cmp=RB_CMP(key, RB_GET(x, key), rbinfo->rb_config);
#else
		cmp=RB_CMP(key, RB_GET(x, key));
#endif /* RB_CUSTOMIZE */

		if (cmp<0)
			x=x->left;
		else if (cmp>0)
			x=x->right;
		else
			found=1;
	}

	if (found || !insert)
		return(x);

	if ((z=RB_ENTRY(_alloc)())==NULL)
	{
		/* Whoops, no memory */
		return(RBNULL);
	}

	RB_SET(z, key, key);
	z->up=y;
	if (y==RBNULL)
	{
		rbinfo->rb_root=z;
	}
	else
	{
#ifndef RB_CUSTOMIZE
		cmp=RB_CMP(RB_GET(z, key), RB_GET(y, key), rbinfo->rb_config);
#else
		cmp=RB_CMP(RB_GET(z, key), RB_GET(y, key));
#endif /* RB_CUSTOMIZE */
		if (cmp<0)
			y->left=z;
		else
			y->right=z;
	}

	z->left=RBNULL;
	z->right=RBNULL;

	/* colour this new node red */
	z->colour=RED;

	/* Having added a red node, we must now walk back up the tree balancing
	** it, by a series of rotations and changing of colours
	*/
	x=z;

	/* While we are not at the top and our parent node is red
	** N.B. Since the root node is garanteed black, then we
	** are also going to stop if we are the child of the root
	*/

	while(x != rbinfo->rb_root && (x->up->colour == RED))
	{
		/* if our parent is on the left side of our grandparent */
		if (x->up == x->up->up->left)
		{
			/* get the right side of our grandparent (uncle?) */
			y=x->up->up->right;
			if (y->colour == RED)
			{
				/* make our parent black */
				x->up->colour = BLACK;
				/* make our uncle black */
				y->colour = BLACK;
				/* make our grandparent red */
				x->up->up->colour = RED;

				/* now consider our grandparent */
				x=x->up->up;
			}
			else
			{
				/* if we are on the right side of our parent */
				if (x == x->up->right)
				{
					/* Move up to our parent */
					x=x->up;
					RB_ENTRY(_left_rotate)(&rbinfo->rb_root, x);
				}

				/* make our parent black */
				x->up->colour = BLACK;
				/* make our grandparent red */
				x->up->up->colour = RED;
				/* right rotate our grandparent */
				RB_ENTRY(_right_rotate)(&rbinfo->rb_root, x->up->up);
			}
		}
		else
		{
			/* everything here is the same as above, but
			** exchanging left for right
			*/

			y=x->up->up->left;
			if (y->colour == RED)
			{
				x->up->colour = BLACK;
				y->colour = BLACK;
				x->up->up->colour = RED;

				x=x->up->up;
			}
			else
			{
				if (x == x->up->left)
				{
					x=x->up;
					RB_ENTRY(_right_rotate)(&rbinfo->rb_root, x);
				}

				x->up->colour = BLACK;
				x->up->up->colour = RED;
				RB_ENTRY(_left_rotate)(&rbinfo->rb_root, x->up->up);
			}
		}
	}

	/* Set the root node black */
	(rbinfo->rb_root)->colour = BLACK;

	return(z);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movl %edi, %ebp
movq 0x10(%rdx), %rax
leaq 0x10f909(%rip), %r13    # 0x5191e8
movq %r13, %rbx
cmpq %r13, %rax
je 0x40991b
leaq 0x10f8fa(%rip), %r12    # 0x5191e8
movq %rax, %r13
movq %r13, %rbx
movq 0x20(%r13), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq *(%r14)
movq %r13, %rcx
testl %eax, %eax
js 0x409913
je 0x409b39
leaq 0x8(%rbx), %rcx
movq (%rcx), %r13
cmpq %r12, %r13
jne 0x4098f1
testl %ebp, %ebp
je 0x409b45
movl $0x28, %edi
callq 0x4458c0
testq %rax, %rax
je 0x409b3e
movq %rax, %r13
leaq 0x10(%r14), %r12
movq %r15, 0x20(%rax)
movq %rbx, 0x10(%rax)
leaq 0x10f89c(%rip), %rbp    # 0x5191e8
movq %r12, %rax
cmpq %rbp, %rbx
je 0x40996b
movq 0x20(%rbx), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq *(%r14)
notl %eax
shrl $0x1f, %eax
leaq (%rbx,%rax,8), %rax
movq %r13, (%rax)
movq %rbp, (%r13)
movq %rbp, 0x8(%r13)
movl $0x1, 0x18(%r13)
movq (%r12), %rdx
cmpq %rdx, %r13
je 0x409b30
movq 0x10(%r13), %rcx
cmpl $0x1, 0x18(%rcx)
jne 0x409b30
leaq 0x18(%rcx), %r8
xorl %eax, %eax
movq %r13, %rdi
movq 0x10(%rcx), %rdx
movq (%rdx), %rsi
cmpq %rsi, %rcx
je 0x4099bc
cmpl $0x1, 0x18(%rsi)
jne 0x4099dd
movl %eax, (%r8)
movl %eax, 0x18(%rsi)
jmp 0x4099ce
movq 0x8(%rdx), %r9
cmpl $0x1, 0x18(%r9)
jne 0x4099ed
movl %eax, (%r8)
movl %eax, 0x18(%r9)
movl $0x1, 0x18(%rdx)
movq %rdx, %rdi
jmp 0x409b15
movq (%rcx), %rsi
cmpq %rsi, %rdi
je 0x409a05
movq %rcx, %rsi
movq %rdi, %rcx
jmp 0x409a41
movq 0x8(%rcx), %r8
cmpq %r8, %rdi
je 0x409a8c
movq %rcx, %r8
movq %rdi, %rcx
jmp 0x409aca
movq 0x8(%rsi), %rdi
movq %rdi, (%rcx)
cmpq %rbp, %rdi
je 0x409a19
movq %rcx, 0x10(%rdi)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rsi)
movq %r12, %rdi
cmpq %rbp, %rdx
je 0x409a32
xorl %edi, %edi
cmpq %rcx, (%rdx)
setne %dil
leaq (%rdx,%rdi,8), %rdi
movq %rsi, (%rdi)
movq %rcx, 0x8(%rsi)
movq %rsi, 0x10(%rcx)
movq 0x10(%rsi), %rdx
movl $0x0, 0x18(%rsi)
movl $0x1, 0x18(%rdx)
movq 0x8(%rdx), %rsi
movq (%rsi), %rdi
movq %rdi, 0x8(%rdx)
cmpq %rbp, %rdi
je 0x409a63
movq %rdx, 0x10(%rdi)
movq 0x10(%rdx), %rdi
movq %rdi, 0x10(%rsi)
movq %r12, %r8
cmpq %rbp, %rdi
je 0x409a81
xorl %r8d, %r8d
cmpq %rdx, (%rdi)
setne %r8b
leaq (%rdi,%r8,8), %r8
movq %rsi, (%r8)
movq %rdx, (%rsi)
jmp 0x409b0e
movq (%r8), %rsi
movq %rsi, 0x8(%rcx)
cmpq %rbp, %rsi
je 0x409aa0
movq %rcx, 0x10(%rsi)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%r8)
movq %r12, %rsi
cmpq %rbp, %rdx
je 0x409ab9
xorl %esi, %esi
cmpq %rcx, (%rdx)
setne %sil
leaq (%rdx,%rsi,8), %rsi
movq %r8, (%rsi)
movq %rcx, (%r8)
movq %r8, 0x10(%rcx)
movq 0x10(%r8), %rdx
movq (%rdx), %rsi
movl $0x0, 0x18(%r8)
movl $0x1, 0x18(%rdx)
movq 0x8(%rsi), %rdi
movq %rdi, (%rdx)
cmpq %rbp, %rdi
je 0x409ae9
movq %rdx, 0x10(%rdi)
movq 0x10(%rdx), %rdi
movq %rdi, 0x10(%rsi)
movq %r12, %r8
cmpq %rbp, %rdi
je 0x409b07
xorl %r8d, %r8d
cmpq %rdx, (%rdi)
setne %r8b
leaq (%rdi,%r8,8), %r8
movq %rsi, (%r8)
movq %rdx, 0x8(%rsi)
movq %rsi, 0x10(%rdx)
movq %rcx, %rdi
movq (%r12), %rdx
cmpq %rdx, %rdi
je 0x409b30
movq 0x10(%rdi), %rcx
leaq 0x18(%rcx), %r8
cmpl $0x1, 0x18(%rcx)
je 0x4099a2
movl $0x0, 0x18(%rdx)
jmp 0x409b45
movq %rbx, %r13
jmp 0x409b45
leaq 0x10f6a3(%rip), %r13    # 0x5191e8
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq