// File: ADAPTLab[P]MuDBSCAN/MuC_RTree.cpp
// Function: populateMuCRTree(dataHdr*, int, int)
GHdrNd populateMuCRTree(DataHdr dataHdrLst, int gMinEntries, int gMaxEntries)
{
	GHdrNd hdrNdTree = GinitHdrNd();
	hdrNdTree->ptrFirstNd = GinitLstNd(GinitIntNd(NULL, NULL));
	hdrNdTree->uiCnt++;
	hdrNdTree->ptrFirstNd->ptrChildLst = GinitHdrNd();

	int cnt = 0, i;
	vectorc* unassigned = (vectorc*)malloc(sizeof(vectorc));
	VECTOR_INIT(unassigned, INTEGER);
	// int alloc = 0;

	Data currDataPoint;
	Group newGroup;
	for(i=0;i<dataHdrLst->uiCnt;i++)
	{	
		currDataPoint = dataHdrLst->dataClstElem + i; 
		Region currRegion = createCellRegOfPoint(currDataPoint, EPS);
		Region x2currRegion = createCellRegOfPoint(currDataPoint, 2*EPS);

		

		struct assign* assignStruct = GcanAssign2(currDataPoint, hdrNdTree, x2currRegion, EPS);

		if(assignStruct->canBeAssigned == 1) //Implies the current point is in the eps neighborhood of an existing group.
		{
			Group currGroup = groupList->groupItems[assignStruct->groupid];

			insertPointintoGroup(currGroup, currDataPoint);
			currDataPoint->group_id = currGroup->id;

			// if(currGroup != NULL)
			// {
			// 	insertPointintoGroup(currGroup, currDataPoint);
			// 	currDataPoint->group_id = currGroup->id;
			// 	// if(currDataPoint->debugPoint == TRUE)
			// 	// {
			// 	// 	printf("%lf\t%lf\t%lf\t populateMuCRTree Method Group ID %d\n",currDataPoint->iData[0],currDataPoint->iData[1],currDataPoint->iData[2],currDataPoint->group_id);
			// 	// }
			// }
			// else
			// {
			// 	printf("\n\n\nGcanAssign is giving WRONG OUTPUT!!!\n\n\n");
			// 	exit(0);
			// }
		}
		else if(assignStruct->canBeAssigned==0) //Implies the current point is not in the eps nbhd as well as 2*eps nbhd of existing groups.
		{

			newGroup = initGroup(currDataPoint);
			newGroup->bcell = initBCell(currRegion);
			BCell newBCell = newGroup->bcell;
			insertPointintoGroup(newGroup, currDataPoint);
			
			currDataPoint->group_id = newGroup->id;
			insertGroupIntoGroupList(newGroup);
			hdrNdTree = insertGroupIntoRTree(hdrNdTree, newGroup, gMinEntries, gMaxEntries);
			// if(currDataPoint->debugPoint == TRUE)
			// {
			// 	printf("%lf\t%lf\t%lf\t populateMuCRTree Method Can Not Be assigned To any existing group%d\n",currDataPoint->iData[0],currDataPoint->iData[1],currDataPoint->iData[2],currDataPoint->group_id);
			// }

		}			
		else
		{
			addHelper[0] = i;
			VECTOR_ADD(unassigned, addHelper);
		}

		free(currRegion->iBottomLeft);
   		free(currRegion->iTopRight);
    	free(currRegion);
    	free(x2currRegion->iBottomLeft);
   		free(x2currRegion->iTopRight);
    	free(x2currRegion);
	}
	
	// FOR UNASSIGNED POINTS, DON'T CONSTRUCT AUXILLARY R TREE....
	// CONSTRUCT AUX R TREES ALONG WITH INSERTION
	Region currRegion = NULL;
	Region x2currRegion = NULL;
	//Data currDataPoint;
	//Group newGroup;
	for(i=0; i<VECTOR_TOTAL(unassigned); i++)
	{
		
			currDataPoint = dataHdrLst->dataClstElem + unassigned->intItems[i];

			currRegion = createCellRegOfPoint(currDataPoint, EPS);
			x2currRegion = createCellRegOfPoint(currDataPoint, 2*EPS);

			struct assign* assignStruct = GcanAssign2(currDataPoint, hdrNdTree, x2currRegion, EPS);
			
			if(assignStruct->canBeAssigned==1)
			{
				Group currGroup = groupList->groupItems[assignStruct->groupid];

				insertPointintoGroup(currGroup, currDataPoint);
				currDataPoint->group_id=currGroup->id;

				//Modify the implementation of GfindCell to return BCell only when distance between data point and center is less than or equal to Epsilon
				// if(currGroup != NULL){
				// 	insertPointintoGroup(currGroup, currDataPoint);
				// 	currDataPoint->group_id=currGroup->id;			
				// }
				// else{
				// 	printf("\n\n\n GcanAssign is giving WRONG OUTPUT\n\n\n");
				// }
			}
			else
			{
				// createa a new Group and insert it into the R Tree
				// create a BCell and insert it into the R Tree
				// printf("Creating a new BCell and inserting the new point into it\n");
					
				newGroup = initGroup(currDataPoint);//(Group)malloc(sizeof(struct group));
				newGroup->bcell = initBCell(currRegion);
				BCell newBCell = newGroup->bcell;

				insertPointintoGroup(newGroup, currDataPoint);
					
				currDataPoint->group_id = newGroup->id;
				insertGroupIntoGroupList(newGroup);

				hdrNdTree = insertGroupIntoRTree(hdrNdTree, newGroup, gMinEntries, gMaxEntries);
			}			
		
		if(currRegion != NULL)
		{
			free(currRegion->iBottomLeft);
	   		free(currRegion->iTopRight);
	    	free(currRegion);
	    }

	    if(x2currRegion != NULL)
	    {
	    	free(x2currRegion->iBottomLeft);
	   		free(x2currRegion->iTopRight);
	    	free(x2currRegion);	
    	}
	}
	VECTOR_FREE(unassigned);
	free(unassigned);
	return hdrNdTree;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
callq 0x10180
movq %rax, -0x18(%rbp)
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x14a30
movq %rax, %rdi
callq 0x101d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
callq 0x10180
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x1c(%rbp)
movl $0x30, %edi
callq 0x9910
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0xa170
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1834e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x20(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x205f4(%rip), %rax     # 0x387c8
movsd (%rax), %xmm0
callq 0x17b40
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movsd 0x1404b(%rip), %xmm0    # 0x2c238
leaq 0x205d4(%rip), %rax     # 0x387c8
mulsd (%rax), %xmm0
callq 0x17b40
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq 0x205b4(%rip), %rax     # 0x387c8
movsd (%rax), %xmm0
callq 0x16ca0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x18266
leaq 0x205df(%rip), %rax     # 0x38810
movq (%rax), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x17ee0
movq -0x58(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x182fc
movq -0x50(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x182d8
movq -0x30(%rbp), %rdi
callq 0x11790
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
callq 0x118d0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x17ee0
movq -0x38(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x38(%rbp), %rdi
callq 0x18530
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
callq 0x18560
movq %rax, -0x18(%rbp)
jmp 0x182fa
movl -0x20(%rbp), %ecx
leaq 0x2051e(%rip), %rax     # 0x38800
movq (%rax), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rdi
leaq 0x2050e(%rip), %rax     # 0x38800
movq (%rax), %rsi
callq 0xa6e0
jmp 0x182fc
movq -0x40(%rbp), %rax
movq (%rax), %rdi
callq 0x97c0
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x97c0
movq -0x40(%rbp), %rdi
callq 0x97c0
movq -0x48(%rbp), %rax
movq (%rax), %rdi
callq 0x97c0
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x97c0
movq -0x48(%rbp), %rdi
callq 0x97c0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x181a3
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x28(%rbp), %rdi
callq 0xa2b0
movl %eax, %ecx
movl -0x8c(%rbp), %eax
cmpl %ecx, %eax
jge 0x18510
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x20(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x20413(%rip), %rax     # 0x387c8
movsd (%rax), %xmm0
callq 0x17b40
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movsd 0x13e6a(%rip), %xmm0    # 0x2c238
leaq 0x203f3(%rip), %rax     # 0x387c8
mulsd (%rax), %xmm0
callq 0x17b40
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x70(%rbp), %rdx
leaq 0x203d3(%rip), %rax     # 0x387c8
movsd (%rax), %xmm0
callq 0x16ca0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x18444
leaq 0x203fe(%rip), %rax     # 0x38810
movq (%rax), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
movslq 0x4(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x17ee0
movq -0x80(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x184ae
movq -0x30(%rbp), %rdi
callq 0x11790
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rdi
callq 0x118d0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x17ee0
movq -0x38(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x38(%rbp), %rdi
callq 0x18530
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
callq 0x18560
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x184d7
movq -0x68(%rbp), %rax
movq (%rax), %rdi
callq 0x97c0
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x97c0
movq -0x68(%rbp), %rdi
callq 0x97c0
cmpq $0x0, -0x70(%rbp)
je 0x18500
movq -0x70(%rbp), %rax
movq (%rax), %rdi
callq 0x97c0
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x97c0
movq -0x70(%rbp), %rdi
callq 0x97c0
jmp 0x18502
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x18365
movq -0x28(%rbp), %rdi
callq 0xa7f0
movq -0x28(%rbp), %rdi
callq 0x97c0
movq -0x18(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nop