// File: /workspace/github_c_makefile_star2_O2/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 0x13b9
movl %edx, %r13d
movq %rdi, %rbx
pushq $0x1
popq %rdi
movl $0xa8, %esi
callq 0x1040
movq %rax, %r14
movl %r13d, (%rax)
movq 0x10(%rbx), %rdi
callq 0x1bfc
andq $0x0, (%rsp)
movq 0x8(%rbx), %rbp
testq %rbp, %rbp
je 0x13dd
movl 0x8(%rsp), %eax
cmpl %eax, (%rbp)
jne 0x1417
pushq $0x1
popq %rax
movl %eax, 0x90(%r14)
movq 0x48(%rbp), %rcx
movq %rcx, %xmm0
movslq %ecx, %rcx
movq %r14, 0x8(%rbp,%rcx,8)
movq %rbp, 0x50(%r14)
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x48(%rbp)
movl %eax, 0x94(%r14)
jmp 0x13e1
leaq 0xd40(%rip), %rdi       # 0x2100
leaq 0xd4f(%rip), %rsi       # 0x2116
leaq 0xd61(%rip), %rdx       # 0x212f
pushq $0x1d
popq %rcx
xorl %eax, %eax
callq 0x1030
jmp 0x153b
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 0x153e
movq %r14, 0x10(%rsp)
leaq 0x8(%rbp), %r12
xorl %r15d, %r15d
movq %r12, %r14
movslq 0x48(%rbp), %rax
cmpq %rax, %r15
jge 0x1444
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x1b31
incq %r15
addq $0x8, %r14
jmp 0x1426
movl %r13d, 0xc(%rsp)
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1b9e
movl %eax, %r14d
cmpl $-0x2, %eax
je 0x1496
movq (%rsp), %r15
movl 0xc(%rsp), %eax
cmpl %eax, (%r15)
je 0x1499
pushq $0x8
popq %r14
xorl %r13d, %r13d
movslq 0x48(%r15), %rax
cmpq %rax, %r13
jge 0x1449
movq 0x10(%rbx), %rdi
addq %r14, %r15
movq %r15, %rsi
callq 0x1b31
incq %r13
movq (%rsp), %r15
addq $0x8, %r14
jmp 0x1471
xorl %r15d, %r15d
movq 0x10(%rbx), %rdi
callq 0x1bfc
xorl %r13d, %r13d
movslq 0x48(%rbp), %rax
cmpq %rax, %r13
jge 0x14c3
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0x1b31
incq %r13
addq $0x8, %r12
jmp 0x14a5
movq %rsp, %r12
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0x1b9e
cmpl $-0x2, %eax
je 0x150b
movq (%rsp), %rsi
movl 0x8(%rsp), %eax
cmpl %eax, (%rsi)
je 0x154d
pushq $0x8
popq %rbp
xorl %r13d, %r13d
movslq 0x48(%rsi), %rax
cmpq %rax, %r13
jge 0x14c6
movq 0x10(%rbx), %rdi
addq %rbp, %rsi
callq 0x1b31
incq %r13
movq (%rsp), %rsi
addq $0x8, %rbp
jmp 0x14e9
movq 0x10(%rsp), %rdi
callq 0x1060
leaq 0xc21(%rip), %rdi       # 0x213d
leaq 0xbf3(%rip), %rsi       # 0x2116
leaq 0xc05(%rip), %rdx       # 0x212f
leaq 0xc23(%rip), %r8        # 0x2154
pushq $0x64
popq %rcx
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
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)
cmpl $-0x2, %r14d
jne 0x15c3
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 0x140d
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 0x1410