// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x1476
leaq 0x1bde(%rip), %rdi      # 0x302e
leaq 0x1bed(%rip), %rsi      # 0x3044
leaq 0x1bff(%rip), %rdx      # 0x305d
movl $0x1d, %ecx
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1842
movl $0xa8, %edi
callq 0x1070
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0xa8, %edx
callq 0x1040
movl -0x18(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x25d0
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x14f1
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rdi
addq $0x98, %rdi
movq -0x10(%rbp), %rsi
addq $0x80, %rsi
callq 0x1850
jmp 0x183b
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x158b
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rsi
movl 0x48(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x48(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rsi
movl 0x90(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x90(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movq -0x30(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
movq -0x20(%rbp), %rax
movl 0x94(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x94(%rax)
movq -0x20(%rbp), %rdi
addq $0x98, %rdi
movq -0x10(%rbp), %rsi
addq $0x80, %rsi
callq 0x1850
movl $0x0, -0x4(%rbp)
jmp 0x1842
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jge 0x15c9
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x8, %rsi
movslq -0x34(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2430
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1592
movb $0x0, -0x35(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2510
cmpl $-0x2, %eax
je 0x1644
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl -0x18(%rbp), %eax
jne 0x1604
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movb $0x1, -0x35(%rbp)
jmp 0x1644
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jge 0x1642
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
movslq -0x44(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2430
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x160b
jmp 0x15d5
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x25d0
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jge 0x168f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x8, %rsi
movslq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2430
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1658
movb $0x0, -0x49(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2510
cmpl $-0x2, %eax
je 0x17f6
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x17b3
movb $0x1, -0x49(%rbp)
movsbl -0x35(%rbp), %eax
cmpl $0x1, %eax
jne 0x1732
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rsi
movl 0x48(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x48(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rax
movq -0x40(%rbp), %rsi
movl 0x90(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x90(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movq -0x28(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
movq -0x40(%rbp), %rax
movl 0x94(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x94(%rax)
jmp 0x17f6
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rsi
movl 0x48(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x48(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rsi
movl 0x90(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x90(%rsi)
movslq %ecx, %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movq -0x28(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
movq -0x20(%rbp), %rax
movl 0x94(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x94(%rax)
movq -0x20(%rbp), %rdi
addq $0x98, %rdi
movq -0x10(%rbp), %rsi
addq $0x80, %rsi
callq 0x1850
jmp 0x17f6
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jge 0x17f1
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
movslq -0x50(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2430
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x17ba
jmp 0x1693
movsbl -0x49(%rbp), %eax
cmpl $0x0, %eax
jne 0x1839
movq -0x20(%rbp), %rdi
callq 0x1060
leaq 0x185c(%rip), %rdi      # 0x306b
leaq 0x182e(%rip), %rsi      # 0x3044
leaq 0x1840(%rip), %rdx      # 0x305d
movl $0x64, %ecx
leaq 0x1859(%rip), %r8       # 0x3082
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1842
jmp 0x183b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)