// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/rbTree.c
// Function: rbTreeRemove
void *rbTreeRemove(struct rbTree *t, void *item)
/* rbTreeRemove() - Delete an item in the red-black tree with the same key as
 * the item pointed to by `item'.  Returns a pointer to the deleted item,
 * and NULL if no item was found.
 */
{
struct rbTreeNode *p, *r, *x, *y, *z, *b, *newY;
struct rbTreeNode *m;
rbTreeColor removeCol;
void *returnItem;
int (* compare)(void *, void *);
int cmpResult;
struct rbTreeNode **stack;
int i, tos;


/* Attempt to locate the item to be deleted. */
if((p = t->root)) 
    {
    compare = t->compare;
    stack = t->stack;
    tos = 0;
    
    for(;;) 
	{
	stack[tos++] = p;
	cmpResult = compare(item, p->item);
	if(cmpResult < 0) 
	    p = p->left;
	else if(cmpResult > 0) 
	    p = p->right;
	else 
	    /* Item found. */
	    break;
	if(!p) return NULL;
	}
    }
else 
    return NULL;

/* p points to the node to be deleted, and is currently on the top of the
 * stack.
 */
if(!p->left) 
    {
    tos--;  /* Adjust tos to remove p. */
    /* Right child replaces p. */
    if(tos == 0) 
	{
	r = t->root = p->right;
	x = y = NULL;
	}
    else 
	{
	x = stack[--tos];
	if(p == x->left) 
	    {
	    r = x->left = p->right;
	    y = x->right;
	    }
	else 
	    {
	    r = x->right = p->right;
	    y = x->left;
	    }
	}
    removeCol = p->color;
    }
else if(!p->right) 
    {
    tos--;  /* Adjust tos to remove p. */
    /* Left child replaces p. */
    if(tos == 0) 
	{
	r = t->root = p->left;
	x = y = NULL;
	}
    else 
	{
	x = stack[--tos];
	if(p == x->left) 
	    {
	    r = x->left = p->left;
	    y = x->right;
	    }
	else 
	    {
	    r = x->right = p->left;
	    y = x->left;
	    }
	}
    removeCol = p->color;
    }
else 
    {
    /* Save p's stack position. */
    i = tos-1;
    
    /* Minimum child, m, in the right subtree replaces p. */
    m = p->right;
    do 
	{
	stack[tos++] = m;
	m = m->left;
	} while(m);
    m = stack[--tos];

    /* Update either the left or right child pointers of p's parent. */
    if(i == 0) 
	{
	t->root = m;
	}
    else 
	{
	x = stack[i-1];  /* p's parent. */
	if(p == x->left) 
	    {
	    x->left = m;
	    }
	else 
	    {
	    x->right = m;
	    }
	}
    
    /* Update the tree part m is removed from, and assign m the child
     * pointers of p (only if m is not the right child of p).
     */
    stack[i] = m;  /* Node m replaces node p on the stack. */
    x = stack[--tos];
    r = m->right;
    if(tos != i) 
	{  /* x is equal to the parent of m. */
	y = x->right;
	x->left = r;
	m->right = p->right;
	}
    else 
	{ /* m was the right child of p, and x is equal to m. */
	y = p->left;
	}
    m->left = p->left;

    /* We treat node m as the node which has been removed. */
    removeCol = m->color;
    m->color = p->color;
    }

/* Get return value and reuse the space used by node p. */
returnItem = p->item;
p->right = t->freeList;
t->freeList = p;

t->n--;

/* The pointers x, y, and r point to nodes which may be involved in
 * restructuring and recolouring.
 *  x - the parent of the removed node.
 *  y - the sibling of the removed node.
 *  r - the node which replaced the removed node.
 * From the above code, the next entry off the stack will be the parent of
 * node x.
 */

/* The number of black nodes on paths to all external nodes (NULL child
 * pointers) must remain the same for all paths.  Restructuring or
 * recolouring of nodes may be necessary to enforce this.
 */
if(removeCol == rbTreeBlack) 
    {
    /* Removal of a black node requires some adjustment. */
    
    if(!r || r->color == rbTreeBlack) 
	{
	/* A black node replaced the deleted black node.  Note that
	 * external nodes (NULL child pointers) are always black, so
	 * if r is NULL it is treated as a black node.
	 */

	/* This causes a double-black problem, since node r would need to
	 * be coloured double-black in order for the black color on
	 * paths through r to remain the same as for other paths.
	 */

	/* If r is the root node, the double-black color is not necessary
	 * to maintain the color balance.  Otherwise, some adjustment of
	 * nearby nodes is needed in order to eliminate the double-black
	 * problem.  NOTE:  x points to the parent of r.
	 */
	if(x) for(;;) 
	    {

	    /* There are three adjustment cases:
	     *  1.  r's sibling, y, is black and has a red child, z.
	     *  2.  r's sibling, y, is black and has two black children.
	     *  3.  r's sibling, y, is red.
	     */
	    if(y->color == rbTreeBlack) 
		{

		/* Note the conditional evaluation for assigning z. */
		if(((z = y->left) && z->color == rbTreeRed) ||
		   ((z = y->right) && z->color == rbTreeRed)) 
		       {		    
		    /* Case 1:  perform a restructuring of nodes x, y, and
		     * z.
		     */
		    
		    b = restructure(t, tos, x, y, z);
		    b->color = x->color;
		    b->left->color = b->right->color = rbTreeBlack;
		    
		    break;
		    }
		else 
		    {
		    /* Case 2:  recolour node y red. */
		    
		    y->color = rbTreeRed;
		    
		    if(x->color == rbTreeRed) 
			{
			x->color = rbTreeBlack;
			break;
			}
		    /* else */

		    if(tos == 0) break;  /* Root level reached. */
		    /* else */
		    
		    r = x;
		    x = stack[--tos];  /* x <- parent of x. */
		    y = x->left == r ? x->right : x->left;
		    }
		}
	    else 
		{
		/* Case 3:  Restructure nodes x, y, and z, where:
		 *  - If node y is the left child of x, then z is the left
		 *    child of y.  Otherwise z is the right child of y.
		 */
		if(x->left == y) 
		    {
		    newY = y->right;
		    z = y->left;
		    }
		else 
		    {
		    newY = y->left;
		    z = y->right;
		    }
		
		restructure(t, tos, x, y, z);
		y->color = rbTreeBlack;
		x->color = rbTreeRed;

		/* Since x has moved down a place in the tree, and y is the
		 * new the parent of x, the stack must be adjusted so that
		 * the parent of x is correctly identified in the next call
		 * to restructure().
		 */
		stack[tos++] = y;

		/* After restructuring, node r has a black sibling, newY,
		 * so either case 1 or case 2 applies.  If case 2 applies
		 * the double-black problem does not reappear.
		 */
		y = newY;
		
		/* Note the conditional evaluation for assigning z. */
		if(((z = y->left) && z->color == rbTreeRed) ||
		   ((z = y->right) && z->color == rbTreeRed)) 
		   {		    
		    /* Case 1:  perform a restructuring of nodes x, y, and
		     * z.
		     */
		    
		    b = restructure(t, tos, x, y, z);
		    b->color = rbTreeRed;  /* Since node x was red. */
		    b->left->color = b->right->color = rbTreeBlack;
		    }
		else 
		    {
		    /* Case 2:  recolour node y red. */

		    /* Note that node y is black and node x is red. */
		    
		    y->color = rbTreeRed;
		    x->color = rbTreeBlack;
		    }

		break;
		}
	    }
	}
    else 
	{
	/* A red node replaced the deleted black node. */

	/* In this case we can simply color the red node black. */
	r->color = rbTreeBlack;
	}
    }
return returnItem;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x54593
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x88(%rbp)
movq -0x20(%rbp), %rdx
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x88(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rcx
movq 0x18(%rcx), %rsi
callq *%rax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jge 0x54563
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x5457b
cmpl $0x0, -0x74(%rbp)
jle 0x54577
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x54579
jmp 0x54591
jmp 0x5457b
cmpq $0x0, -0x20(%rbp)
jne 0x5458f
movq $0x0, -0x8(%rbp)
jmp 0x54b6d
jmp 0x5451b
jmp 0x545a0
movq $0x0, -0x8(%rbp)
jmp 0x54b6d
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x54668
movl -0x88(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x88(%rbp)
cmpl $0x0, -0x88(%rbp)
jne 0x545ec
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq %rax, 0x8(%rcx)
movq %rax, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
jmp 0x54659
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x88(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x54638
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rax, (%rcx)
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x54657
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rax, 0x8(%rcx)
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x54659
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x54897
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5472e
movl -0x88(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x88(%rbp)
cmpl $0x0, -0x88(%rbp)
jne 0x546b4
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq %rax, 0x8(%rcx)
movq %rax, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
jmp 0x5471f
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x88(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x546ff
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movq %rax, (%rcx)
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x5471d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movq %rax, 0x8(%rcx)
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x5471f
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x54895
movl -0x88(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x84(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdx
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x88(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x54749
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x88(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x84(%rbp)
jne 0x547b0
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x547f0
movq -0x80(%rbp), %rax
movl -0x84(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x547e2
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x547ee
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x547f0
movq -0x58(%rbp), %rdx
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x88(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x84(%rbp), %eax
je 0x54864
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x5486f
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x54897
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
cmpl $0x1, -0x5c(%rbp)
jne 0x54b65
cmpq $0x0, -0x28(%rbp)
je 0x548eb
movq -0x28(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x54b58
cmpq $0x0, -0x30(%rbp)
je 0x54b56
jmp 0x548f8
movq -0x38(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x54a21
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x54921
movq -0x40(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x5493d
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x5498c
movq -0x40(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x5498c
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %esi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x542b0
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x1, 0x10(%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl $0x1, 0x10(%rax)
jmp 0x54b54
movq -0x38(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x549b1
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0x54b54
cmpl $0x0, -0x88(%rbp)
jne 0x549bf
jmp 0x54b54
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x88(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq -0x28(%rbp), %rax
jne 0x54a03
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
jmp 0x54a11
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x54b4f
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq -0x38(%rbp), %rax
jne 0x54a47
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x54a5e
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %esi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x542b0
movq -0x38(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x38(%rbp), %rdx
movq -0x80(%rbp), %rax
movl -0x88(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x88(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x54ad2
movq -0x40(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x54aee
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x54b37
movq -0x40(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x54b37
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %esi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x542b0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x1, 0x10(%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl $0x1, 0x10(%rax)
jmp 0x54b4d
movq -0x38(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0x54b54
jmp 0x548f8
jmp 0x54b56
jmp 0x54b63
movq -0x28(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0x54b65
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)