// File: /workspace/github_c_makefile_star2_O1/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
testq %rdi, %rdi
je 0x1420
movl %edx, %r15d
movq %rdi, %rbx
movl %esi, 0xc(%rsp)
movl $0xa8, %edi
callq 0x1070
movq %rax, %r14
xorl %r12d, %r12d
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1040
movl %r15d, (%r14)
movq 0x10(%rbx), %rdi
callq 0x1d78
movq $0x0, (%rsp)
movq 0x8(%rbx), %r13
testq %r13, %r13
je 0x1446
movl 0xc(%rsp), %eax
cmpl %eax, (%r13)
jne 0x147e
movslq 0x48(%r13), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x48(%r13)
movq %r14, 0x8(%r13,%rax,8)
movslq 0x90(%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x90(%r14)
movq %r13, 0x50(%r14,%rax,8)
incl 0x4c(%r13)
incl 0x94(%r14)
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 %r12d, %r12d
jmp 0x15f6
leaq 0xcd9(%rip), %rdi       # 0x2100
leaq 0xce8(%rip), %rsi       # 0x2116
leaq 0xcfa(%rip), %rdx       # 0x212f
movl $0x1d, %ecx
xorl %eax, %eax
callq 0x1030
jmp 0x15f0
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)
jmp 0x15f6
movq %r14, 0x10(%rsp)
cmpl $0x0, 0x48(%r13)
jle 0x14ac
leaq 0x8(%r13), %r12
xorl %ebp, %ebp
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0x1ca1
incq %rbp
movslq 0x48(%r13), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x1490
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1d13
cmpl $-0x2, %eax
sete %bpl
jne 0x14c6
xorl %r12d, %r12d
jmp 0x1524
movq (%rsp), %r12
cmpl %r15d, (%r12)
je 0x1524
movq %rsp, %r14
movq (%rsp), %rsi
cmpl $0x0, 0x48(%rsi)
jle 0x1505
movl $0x8, %r12d
xorl %ebp, %ebp
movq 0x10(%rbx), %rdi
addq %r12, %rsi
callq 0x1ca1
incq %rbp
movq (%rsp), %rsi
movslq 0x48(%rsi), %rax
addq $0x8, %r12
cmpq %rax, %rbp
jl 0x14e5
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x1d13
cmpl $-0x2, %eax
sete %bpl
je 0x14c1
movq (%rsp), %r12
cmpl %r15d, (%r12)
jne 0x14d3
movq 0x10(%rbx), %rdi
callq 0x1d78
cmpl $0x0, 0x48(%r13)
jle 0x1557
leaq 0x8(%r13), %r15
xorl %r14d, %r14d
movq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x1ca1
incq %r14
movslq 0x48(%r13), %rax
addq $0x8, %r15
cmpq %rax, %r14
jl 0x153b
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1d13
cmpl $-0x2, %eax
movl 0xc(%rsp), %r14d
je 0x15be
movq (%rsp), %rax
cmpl %r14d, (%rax)
je 0x1608
movq (%rsp), %rsi
cmpl $0x0, 0x48(%rsi)
jle 0x15ad
movl $0x8, %r13d
xorl %r15d, %r15d
movq 0x10(%rbx), %rdi
addq %r13, %rsi
callq 0x1ca1
incq %r15
movq (%rsp), %rsi
movslq 0x48(%rsi), %rax
addq $0x8, %r13
cmpq %rax, %r15
jl 0x158d
movq 0x10(%rbx), %rdi
movq %rsp, %rsi
callq 0x1d13
cmpl $-0x2, %eax
jne 0x156d
movq 0x10(%rsp), %rdi
callq 0x1060
leaq 0xb6e(%rip), %rdi       # 0x213d
leaq 0xb40(%rip), %rsi       # 0x2116
leaq 0xb52(%rip), %rdx       # 0x212f
leaq 0xb70(%rip), %r8        # 0x2154
movl $0x64, %ecx
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x48(%rax), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x48(%rax)
testb %bpl, %bpl
je 0x1674
movq 0x10(%rsp), %rsi
movq %rsi, 0x8(%rax,%rcx,8)
movq (%rsp), %rax
movslq 0x90(%rsi), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x90(%rsi)
movq %rax, 0x50(%rsi,%rcx,8)
incl 0x4c(%rax)
incl 0x94(%rsi)
movq %rsi, %rax
addq $0x98, %rax
leaq 0x80(%rbx), %rcx
movq 0x88(%rbx), %rdx
movq %rax, 0x88(%rbx)
movq %rcx, 0x98(%rsi)
movq %rdx, 0xa0(%rsi)
jmp 0x1415
movq %r12, 0x8(%rax,%rcx,8)
movq (%rsp), %rax
movslq 0x90(%r12), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x90(%r12)
movq %rax, 0x50(%r12,%rcx,8)
movq (%rsp), %rax
incl 0x4c(%rax)
incl 0x94(%r12)
jmp 0x1418