// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/rbTree.c
// Function: rbTreeAdd
void *rbTreeAdd(struct rbTree *t, void *item)
/* rbTreeAdd() - Inserts an item into the red-black tree pointed to by t,
 * according the the value its key.  The key of an item in the red-black
 * tree must be unique among items in the tree.  If an item with the same key
 * already exists in the tree, a pointer to that item is returned.  Otherwise,
 * NULL is returned, indicating insertion was successful.
 */
{
struct rbTreeNode *x, *p, *q, *m, **attachX;
int (* compare)(void *, void *);
int cmpResult;
rbTreeColor col;
struct rbTreeNode **stack = NULL;
int tos;

tos = 0;    
if((p = t->root) != NULL) 
    {
    compare = t->compare;
    stack = t->stack;

    /* Repeatedly explore either the left branch or the right branch
     * depending on the value of the key, until an empty branch is chosen.
     */
    for(;;) 
        {
	stack[tos++] = p;
	cmpResult = compare(item, p->item);
	if(cmpResult < 0) 
	    {
	    p = p->left;
	    if(!p) 
	        {
		p = stack[--tos];
		attachX = &p->left;
		break;
		}
	    }
	else if(cmpResult > 0) 
	    {
	    p = p->right;
	    if(!p) 
	        {
		p = stack[--tos];
		attachX = &p->right;
		break;
		}
	    }
	else 
	    {
	    return p->item;
	    }
	}
    col = rbTreeRed;
    }
else 
    {
    attachX = &t->root;
    col = rbTreeBlack;
    }

/* Allocate new node and place it in tree. */
if ((x = t->freeList) != NULL)
    t->freeList = x->right;
else
    lmAllocVar(t->lm, x);
x->left = x->right = NULL;
x->item = item;
x->color = col;
*attachX = x;
t->n++;

/* Restructuring or recolouring will be needed if node x and its parent, p,
 * are both red.
 */
if(tos > 0) 
    {
    while(p->color == rbTreeRed) 
	{  /* Double red problem. */

	/* Obtain a pointer to p's parent, m, and sibling, q. */
	m = stack[--tos];
	q = p == m->left ? m->right : m->left;
	
	/* Determine whether restructuring or recolouring is needed. */
	if(!q || q->color == rbTreeBlack) 
	    {
	    /* Sibling is black.  ==>  Perform restructuring. */
	    
	    /* Restructure according to the left to right order, of nodes
	     * m, p, and x.
	     */
	    m = restructure(t, tos, m, p, x);
	    m->color = rbTreeBlack;
	    m->left->color = m->right->color = rbTreeRed;

	    /* Restructuring eliminates the double red problem. */
	    break;
	    }
	/* else just need to flip color */
	
	/* Sibling is also red.  ==>  Perform recolouring. */
	p->color = rbTreeBlack;
	q->color = rbTreeBlack;

	if(tos == 0) break;  /* The root node always remains black. */
	    
	m->color = rbTreeRed;

	/* Continue, checking colouring higher up. */
	x = m;
	p = stack[--tos];
	}
    }

return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x540fa
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rdx
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x5c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rsi
callq *%rax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x54098
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x54096
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x5c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x540f1
jmp 0x540ec
cmpl $0x0, -0x4c(%rbp)
jle 0x540d9
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x540d7
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x5c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
jmp 0x540f1
jmp 0x540ea
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x5429b
jmp 0x540ec
jmp 0x5402d
movl $0x0, -0x50(%rbp)
jmp 0x5410d
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
movl $0x1, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x54131
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x54147
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movl $0x20, %esi
callq 0x3de60
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x50(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
cmpl $0x0, -0x5c(%rbp)
jle 0x54293
jmp 0x54198
movq -0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x54291
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x5c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x541d9
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x541e4
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x541fd
movq -0x30(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x54243
movq -0x10(%rbp), %rdi
movl -0x5c(%rbp), %esi
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %r8
callq 0x542b0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x0, 0x10(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl $0x0, 0x10(%rax)
jmp 0x54291
movq -0x28(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
cmpl $0x0, -0x5c(%rbp)
jne 0x54261
jmp 0x54291
movq -0x38(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x5c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x54198
jmp 0x54293
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)