// File: /workspace/github_c_makefile_star2_O3/xtanitfy[P]Murphy/src/muti_tree.c
// Function: MTREE_addItem
int MTREE_addItem(MTREE_s *pTree,int prev,int val)
{
	CHECK_EXPR_RET(pTree == NULL,-1);
	
	MTREE_ITEM_s *pItem = (MTREE_ITEM_s *)malloc(sizeof(MTREE_ITEM_s));
	memset(pItem,'\0',sizeof(MTREE_ITEM_s));
	pItem->valCurr = val;

	QUEUE_reset(pTree->pQAdd);
	MTREE_ITEM_s *pItemTmp = NULL;
	MTREE_ITEM_s *pRoot = pTree->root;
	if (pRoot == NULL) {
		pTree->root = pItem;
		list_add_tail(&pItem->list,&pTree->head);
	} else {
		if (pRoot->valCurr == prev) {
			pRoot->next[pRoot->nNext++] = pItem;
			pItem->prev[pItem->nPrev++] = pRoot;
			pRoot->syncNext++;
 			pItem->syncPrev++;
			list_add_tail(&pItem->list,&pTree->head);
			return 0;
		}
	
		for (int i = 0;i < pRoot->nNext;i++) {
			QUEUE_enqueue(pTree->pQAdd,&pRoot->next[i]);
		}

		BOOL isFindCurrVal = FALSE;
		MTREE_ITEM_s *currItem = NULL;
		/*���ж����ӵ�val�Ƿ�����*/
		while(QUEUE_dequeue(pTree->pQAdd,&pItemTmp) != QUEUE_EMPTY) {
			if (pItemTmp->valCurr == val) {
				currItem = pItemTmp;
				isFindCurrVal = TRUE;
				break;
			}
			for (int i = 0;i < pItemTmp->nNext;i++) {
				QUEUE_enqueue(pTree->pQAdd,&pItemTmp->next[i]);
			}
		}
		
		QUEUE_reset(pTree->pQAdd);
		for (int i = 0;i < pRoot->nNext;i++) {
			QUEUE_enqueue(pTree->pQAdd,&pRoot->next[i]);
		}
		BOOL isFindPrevVal = FALSE;
		while(QUEUE_dequeue(pTree->pQAdd,&pItemTmp) != QUEUE_EMPTY) {
			if (pItemTmp->valCurr == prev) {
				isFindPrevVal = TRUE;
				/*�����Ѿ������д�ֵ����ʹ���Ѿ����ڵĽڵ�*/
				if (isFindCurrVal == TRUE) {
					pItemTmp->next[pItemTmp->nNext++] = currItem;
					currItem->prev[currItem->nPrev++] = pItemTmp;
					pItemTmp->syncNext++;
					currItem->syncPrev++;
					break;
				} else {
				/*����û�з��ִ�ֵ����ʹ�÷����Ľڵ�*/
					pItemTmp->next[pItemTmp->nNext++] = pItem;
					pItem->prev[pItem->nPrev++] = pItemTmp;
					pItemTmp->syncNext++;
					pItem->syncPrev++;
					list_add_tail(&pItem->list,&pTree->head);
				}
				break;
			}				
			for (int i = 0;i < pItemTmp->nNext;i++) {
				QUEUE_enqueue(pTree->pQAdd,&pItemTmp->next[i]);
			}
		}
		if (isFindPrevVal == FALSE) {
			free(pItem);
			ERROR_DEBUG("AddItem failed:not find its parents!\n");
			return -1;
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, 0x8(%rsp)
testq %rdi, %rdi
je 0x13cf
movl %edx, %ebp
movq %rdi, %rbx
movl $0x1, %edi
movl $0xa8, %esi
callq 0x1040
movq %rax, %r14
movl %ebp, (%rax)
movq 0x10(%rbx), %rdi
callq 0x1d2c
movq $0x0, (%rsp)
movq 0x8(%rbx), %r13
testq %r13, %r13
je 0x13f5
movl 0x8(%rsp), %eax
cmpl %eax, (%r13)
jne 0x142f
movslq 0x90(%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x90(%r14)
movq 0x48(%r13), %rcx
movq %rcx, %xmm0
movslq %ecx, %rcx
movq %r14, 0x8(%r13,%rcx,8)
movq %r13, 0x50(%r14,%rax,8)
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x48(%r13)
incl 0x94(%r14)
jmp 0x13f9
leaq 0xd2a(%rip), %rdi       # 0x2100
leaq 0xd39(%rip), %rsi       # 0x2116
leaq 0xd4b(%rip), %rdx       # 0x212f
movl $0x1d, %ecx
xorl %eax, %eax
callq 0x1030
jmp 0x159f
movq %r14, 0x8(%rbx)
movq %r14, %rax
addq $0x98, %rax
leaq 0x80(%rbx), %rcx
movq 0x88(%rbx), %rdx
movq %rax, 0x88(%rbx)
movq %rcx, 0x98(%r14)
movq %rdx, 0xa0(%r14)
movq %rax, (%rdx)
xorl %eax, %eax
jmp 0x15a4
movq %r14, 0x10(%rsp)
cmpl $0x0, 0x48(%r13)
jle 0x145e
leaq 0x8(%r13), %r15
xorl %r14d, %r14d
movq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x1c55
incq %r14
movslq 0x48(%r13), %rax
addq $0x8, %r15
cmpq %rax, %r14
jl 0x1442
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1cc9
cmpl $-0x2, %eax
jne 0x1477
movb $0x1, %bpl
xorl %r15d, %r15d
jmp 0x14d6
movq (%rsp), %r15
cmpl %ebp, (%r15)
setne %al
je 0x14d4
movl %eax, 0xc(%rsp)
cmpl $0x0, 0x48(%r15)
jle 0x14ba
movl $0x8, %r14d
xorl %r12d, %r12d
movq 0x10(%rbx), %rdi
addq %r14, %r15
movq %r15, %rsi
callq 0x1c55
incq %r12
movq (%rsp), %r15
movslq 0x48(%r15), %rax
addq $0x8, %r14
cmpq %rax, %r12
jl 0x1497
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1cc9
cmpl $-0x2, %eax
jne 0x1477
xorl %r15d, %r15d
movl 0xc(%rsp), %ebp
jmp 0x14d6
xorl %ebp, %ebp
movq 0x10(%rbx), %rdi
callq 0x1d2c
cmpl $0x0, 0x48(%r13)
jle 0x1509
leaq 0x8(%r13), %r12
xorl %r14d, %r14d
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0x1c55
incq %r14
movslq 0x48(%r13), %rax
addq $0x8, %r12
cmpq %rax, %r14
jl 0x14ed
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1cc9
cmpl $-0x2, %eax
je 0x156d
movq %rsp, %r12
movq (%rsp), %rsi
movl 0x8(%rsp), %eax
cmpl %eax, (%rsi)
je 0x15b3
cmpl $0x0, 0x48(%rsi)
jle 0x155c
movl $0x8, %r13d
xorl %r14d, %r14d
movq 0x10(%rbx), %rdi
addq %r13, %rsi
callq 0x1c55
incq %r14
movq (%rsp), %rsi
movslq 0x48(%rsi), %rax
addq $0x8, %r13
cmpq %rax, %r14
jl 0x153c
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0x1cc9
cmpl $-0x2, %eax
jne 0x151d
movq 0x10(%rsp), %rdi
callq 0x1060
leaq 0xbbf(%rip), %rdi       # 0x213d
leaq 0xb91(%rip), %rsi       # 0x2116
leaq 0xba3(%rip), %rdx       # 0x212f
leaq 0xbc1(%rip), %r8        # 0x2154
movl $0x64, %ecx
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x48(%rsi), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x48(%rsi)
testb %bpl, %bpl
je 0x1628
movq 0x10(%rsp), %rdi
movq %rdi, 0x8(%rsi,%rax,8)
movq (%rsp), %rax
incl 0x4c(%rax)
movq 0x90(%rdi), %rcx
movq %rcx, %xmm0
movslq %ecx, %rcx
movq %rax, 0x50(%rdi,%rcx,8)
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x90(%rdi)
movq %rdi, %rax
addq $0x98, %rax
leaq 0x80(%rbx), %rcx
movq 0x88(%rbx), %rdx
movq %rax, 0x88(%rbx)
movq %rcx, 0x98(%rdi)
movq %rdx, 0xa0(%rdi)
jmp 0x1425
movq %r15, 0x8(%rsi,%rax,8)
movq (%rsp), %rax
incl 0x4c(%rax)
movq 0x90(%r15), %rcx
movq %rcx, %xmm0
movslq %ecx, %rcx
movq %rax, 0x50(%r15,%rcx,8)
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x90(%r15)
jmp 0x1428