// File: /workspace/github_cpp_makefile_star2_O2/ADAPTLab[P]MuDBSCAN/RTree.cpp
// Function: RpickSeeds(RhdrNd*, RlstNd**, RlstNd**)
void RpickSeeds(RHdrNd ptrChildLst, RLstNd *lstNdChildOne, RLstNd *lstNdChildTwo)
{
	if(ptrChildLst == NULL)
		return;
	RTreeNode* tnInfoMin = (RTreeNode *)calloc(DIMENSION, sizeof(struct RtreeNode));
	// assert(tnInfoMin != NULL);

	RTreeNode* tnInfoMax = (RTreeNode *)calloc(DIMENSION, sizeof(struct RtreeNode));
	// assert(tnInfoMax != NULL);
	RTreeNode temp;
	RLstNd lstNdTemp = NULL;
    int iCnt = 0;
	Boolean bIsFirst = TRUE;
	double dNormSep = 0;
	double dMaxNormSep = 0;
	dimension dimMaxNormSep = 0;
	Region rgnRectTemp = RinitRgnRect(NULL, NULL);
	// assert(rgnRectTemp != NULL);

	Region rgnRectOut = RinitRgnRect(NULL, NULL);
	// assert(rgnRectOut != NULL);

	switch(ptrChildLst->ptrFirstNd->tnInfo->ndType)
    {	
    	case INTNODE:   
    		lstNdTemp = ptrChildLst->ptrFirstNd;

            while(lstNdTemp != NULL)
            {
                for(iCnt = 0; iCnt < DIMENSION; iCnt++)
                {     
                  	if(bIsFirst)
                    {     
			            rgnRectTemp->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];
			            rgnRectTemp->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
			            rgnRectOut->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];
			            rgnRectOut->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
			            tnInfoMin[iCnt] = lstNdTemp->tnInfo;
			            tnInfoMax[iCnt] = lstNdTemp->tnInfo;
			            continue;
                    }
			 
			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt] > rgnRectTemp->iBottomLeft[iCnt])
                    {     
			            rgnRectTemp->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];
			            tnInfoMin[iCnt] = lstNdTemp->tnInfo;
                    }
			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt] < rgnRectTemp->iTopRight[iCnt])
                    {     
			            rgnRectTemp->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
			            tnInfoMax[iCnt] = lstNdTemp->tnInfo;
                    }
			                        
			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt] < rgnRectOut->iBottomLeft[iCnt])
                        rgnRectOut->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];

			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt] > rgnRectOut->iTopRight[iCnt])
                        rgnRectOut->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
                }
                lstNdTemp = lstNdTemp->ptrNextNd;
		        if(bIsFirst)
                    bIsFirst = FALSE;
            }	
	                    
	        for(iCnt = 0; iCnt < DIMENSION; iCnt++)
            {     

                dNormSep = fabs(rgnRectTemp->iBottomLeft[iCnt] - rgnRectTemp->iTopRight[iCnt]) / fabs(rgnRectOut->iTopRight[iCnt] - rgnRectOut->iBottomLeft[iCnt]);
                if(dNormSep > dMaxNormSep)
                {   
                 	dMaxNormSep = dNormSep;
			        dimMaxNormSep = iCnt;
                               }
                }
                if(tnInfoMin[(int)dimMaxNormSep] == tnInfoMax[(int)dimMaxNormSep])
                {
            	   	lstNdTemp = ptrChildLst->ptrFirstNd;
                   	temp=tnInfoMax[(int)dimMaxNormSep];
                   	if(temp != lstNdTemp->tnInfo)
                   	{
                   		rgnRectTemp->iTopRight[(int)dimMaxNormSep] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep];
                		tnInfoMax[(int)dimMaxNormSep] = lstNdTemp->tnInfo;
                        lstNdTemp = lstNdTemp->ptrNextNd;
                    }
                    else
                    {
                    	rgnRectTemp->iTopRight[(int)dimMaxNormSep] = lstNdTemp->ptrNextNd->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep];
                        tnInfoMax[(int)dimMaxNormSep] = lstNdTemp->ptrNextNd->tnInfo;
                        lstNdTemp = lstNdTemp->ptrNextNd->ptrNextNd;
                    }
                    while(lstNdTemp != NULL)
                    {
                    	if(lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep] < rgnRectTemp->iTopRight[(int)dimMaxNormSep] && temp != lstNdTemp->tnInfo)
                    	{
                        	rgnRectTemp->iTopRight[(int)dimMaxNormSep] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep];
				            tnInfoMax[(int)dimMaxNormSep] = lstNdTemp->tnInfo;
                        }
                        lstNdTemp = lstNdTemp->ptrNextNd;
                    } 
                }
                
                /*if(tnInfoMin[(int)dimMaxNormSep]==tnInfoMax[(int)dimMaxNormSep])
                {
                   	printf("error in the code\n");
                   	exit(-1);
                }*/
                        
	            *lstNdChildOne = RdeleteLstElem(ptrChildLst, tnInfoMin[(int)dimMaxNormSep]);
	  
	            *lstNdChildTwo = RdeleteLstElem(ptrChildLst, tnInfoMax[(int)dimMaxNormSep]);
	                     
	            free(rgnRectTemp->iBottomLeft);
	            free(rgnRectTemp->iTopRight);
	            free(rgnRectTemp);
	            free(rgnRectOut->iBottomLeft);
	            free(rgnRectOut->iTopRight);
	            free(rgnRectOut);
                break;

		case EXTNODE:        
	        lstNdTemp = ptrChildLst->ptrFirstNd;
	        DataPoint iDataMin = (DataPoint)calloc(DIMENSION, sizeof(dataPoint));
	        // assert( iDataMin != NULL);

	        DataPoint iDataMax = (DataPoint)calloc(DIMENSION, sizeof(dataPoint));
	        // assert(iDataMax != NULL);

            bIsFirst = TRUE;
	        while(lstNdTemp != NULL)
            {     
            	for(iCnt = 0; iCnt < DIMENSION; iCnt++)
                {   
                	if(bIsFirst)
                    {    
                        iDataMin[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
				        iDataMax[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
                        tnInfoMin[iCnt] = lstNdTemp->tnInfo;
				        tnInfoMax[iCnt] = lstNdTemp->tnInfo;
			            continue;
                    }

			        if(lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt] <= iDataMin[iCnt])
                    {   
                    	iDataMin[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
				        tnInfoMin[iCnt] = lstNdTemp->tnInfo;
                    }
		            
		            if(lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt] > iDataMax[iCnt])
                    {    
                    	iDataMax[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
                        tnInfoMax[iCnt]  = lstNdTemp->tnInfo;
                    }
                }
                lstNdTemp = lstNdTemp->ptrNextNd;
                if(bIsFirst)
                    bIsFirst = FALSE;
            } 
	        double dSep = 0;
	        double dMaxSep = 0;
	        dimension dimMaxSep = 0;

	        for(iCnt = 0; iCnt < DIMENSION; iCnt++)
            {      
            	dSep = fabs(iDataMax[iCnt] - iDataMin[iCnt]);
		        if(dSep > dMaxSep)
                {   
                	dMaxSep = dSep;
			        dimMaxSep = iCnt;
	            }
            }
	        
	        *lstNdChildOne = RdeleteLstElem(ptrChildLst, tnInfoMin[(int)dimMaxSep]);
	        
	        *lstNdChildTwo = RdeleteLstElem(ptrChildLst, tnInfoMax[(int)dimMaxSep]);
	        free(rgnRectTemp->iBottomLeft);
	        free(rgnRectTemp->iTopRight);
	        free(rgnRectTemp);
	        free(rgnRectOut->iBottomLeft);
	        free(rgnRectOut->iTopRight);
	        free(rgnRectOut);
	        free(iDataMin);
	        free(iDataMax);
            break;
	}

	free(tnInfoMin);
	free(tnInfoMax);

	return;
}

// Assembly:
testq %rdi, %rdi
je 0x4465
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rax
movq %rsi, -0x50(%rbp)
movq %rdx, -0x58(%rbp)
leaq 0x1003c(%rip), %rcx     # 0x14334
movslq (%rcx), %rbx
pushq $0x10
popq %r14
movq %rbx, %rdi
movq %rax, -0x38(%rbp)
movq %r14, %rsi
callq 0x2180
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2180
movq %rax, -0x40(%rbp)
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
callq 0x3c84
movq %rax, %r13
xorl %edi, %edi
xorl %esi, %esi
callq 0x3c84
movq %rax, %rbx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r8
movq (%r8), %rax
movl (%rax), %ecx
cmpl $0x1, %ecx
movq %r8, %r15
je 0x453e
testl %ecx, %ecx
movq -0x40(%rbp), %r14
jne 0x46c9
leaq 0xffcd(%rip), %rcx      # 0x14334
movl (%rcx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movq %r8, %rsi
testq %rsi, %rsi
je 0x4466
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x4457
movq (%rsi), %r8
movq 0x8(%r8), %r9
movq (%r9), %r10
movq (%r10), %r9
movsd (%r9,%rdi,8), %xmm0
movq (%r13), %r11
testb $0x1, %dl
je 0x440e
ucomisd (%r11,%rdi,8), %xmm0
jbe 0x43b5
movsd %xmm0, (%r11,%rdi,8)
movq %r8, (%r12,%rdi,8)
movq 0x8(%r10), %r10
movsd (%r10,%rdi,8), %xmm0
movq 0x8(%r13), %r11
movsd (%r11,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x43d9
movsd %xmm0, (%r11,%rdi,8)
movq %r8, (%r14,%rdi,8)
movsd (%r9,%rdi,8), %xmm0
movq (%rbx), %r8
movsd (%r8,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x43f4
movsd %xmm0, (%r8,%rdi,8)
movsd (%r10,%rdi,8), %xmm0
movq 0x8(%rbx), %r8
ucomisd (%r8,%rdi,8), %xmm0
jbe 0x444f
movsd %xmm0, (%r8,%rdi,8)
jmp 0x444f
movsd %xmm0, (%r11,%rdi,8)
movq 0x8(%r10), %r10
movsd (%r10,%rdi,8), %xmm0
movq 0x8(%r13), %r11
movsd %xmm0, (%r11,%rdi,8)
movsd (%r9,%rdi,8), %xmm0
movq (%rbx), %r9
movsd %xmm0, (%r9,%rdi,8)
movsd (%r10,%rdi,8), %xmm0
movq 0x8(%rbx), %r9
movsd %xmm0, (%r9,%rdi,8)
movq %r8, (%r12,%rdi,8)
movq %r8, (%r14,%rdi,8)
incq %rdi
jmp 0x437e
movq 0x10(%rsi), %rsi
movb $0x1, %dl
movq %r15, %r8
jmp 0x4373
retq
xorpd %xmm1, %xmm1
xorl %edx, %edx
movapd 0xac6c(%rip), %xmm0     # 0xf0e0
xorpd %xmm2, %xmm2
cmpq %rdx, %rcx
je 0x44d2
movq (%r13), %rsi
movq 0x8(%r13), %rdi
movsd (%rsi,%rdx,8), %xmm3
subsd (%rdi,%rdx,8), %xmm3
movq (%rbx), %rsi
movq 0x8(%rbx), %rdi
movsd (%rdi,%rdx,8), %xmm4
subsd (%rsi,%rdx,8), %xmm4
divsd %xmm4, %xmm3
andpd %xmm0, %xmm3
cvtsi2sd %edx, %xmm4
movapd %xmm3, %xmm5
maxsd %xmm1, %xmm5
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm4
andnpd %xmm2, %xmm1
orpd %xmm4, %xmm1
movapd %xmm1, %xmm2
incq %rdx
movapd %xmm5, %xmm1
jmp 0x4478
cvttsd2si %xmm2, %ecx
movslq %ecx, %rcx
movq (%r12,%rcx,8), %rsi
movq (%r14,%rcx,8), %r15
cmpq %r15, %rsi
je 0x46e7
movq %r12, -0x30(%rbp)
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x310d
movq -0x50(%rbp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
movq %r15, %rsi
callq 0x310d
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq (%r13), %rdi
callq 0x2280
movq 0x8(%r13), %rdi
callq 0x2280
movq %r13, %rdi
callq 0x2280
movq (%rbx), %rdi
callq 0x2280
movq 0x8(%rbx), %r12
jmp 0x46b5
movq %r12, -0x30(%rbp)
leaq 0xfdeb(%rip), %rax      # 0x14334
movslq (%rax), %rdi
movq %rdi, -0x48(%rbp)
pushq $0x8
popq %rsi
callq 0x2180
movq %rax, %r12
movq -0x48(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x2180
movq %r15, %rdi
movq %rax, %r15
movq -0x48(%rbp), %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
testq %rdi, %rdi
je 0x45f4
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x45eb
movq (%rdi), %rdx
movq 0x8(%rdx), %rsi
movq 0x8(%rsi), %rsi
movq (%rsi), %rsi
movsd (%rsi,%rcx,8), %xmm0
testb $0x1, %r14b
je 0x45ca
movsd (%r12,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x45ba
movsd %xmm0, (%r12,%rcx,8)
movq -0x30(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
ucomisd (%r15,%rcx,8), %xmm0
jbe 0x45e6
movsd %xmm0, (%r15,%rcx,8)
jmp 0x45de
movsd %xmm0, (%r12,%rcx,8)
movsd %xmm0, (%r15,%rcx,8)
movq -0x30(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
movq -0x40(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
jmp 0x4582
movq 0x10(%rdi), %rdi
movb $0x1, %r14b
jmp 0x457b
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movapd 0xaade(%rip), %xmm0     # 0xf0e0
xorpd %xmm2, %xmm2
cmpq %rcx, %rax
je 0x4645
movsd (%r15,%rcx,8), %xmm3
subsd (%r12,%rcx,8), %xmm3
andpd %xmm0, %xmm3
cvtsi2sd %ecx, %xmm4
movapd %xmm3, %xmm5
maxsd %xmm1, %xmm5
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm4
andnpd %xmm2, %xmm1
orpd %xmm4, %xmm1
movapd %xmm1, %xmm2
incq %rcx
movapd %xmm5, %xmm1
jmp 0x4606
cvttsd2si %xmm2, %eax
movslq %eax, %r14
movq -0x30(%rbp), %rax
movq (%rax,%r14,8), %rsi
movq -0x38(%rbp), %rdi
callq 0x310d
movq -0x50(%rbp), %rcx
movq %rax, (%rcx)
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rsi
movq %rax, %r14
movq -0x38(%rbp), %rdi
callq 0x310d
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq (%r13), %rdi
callq 0x2280
movq 0x8(%r13), %rdi
callq 0x2280
movq %r13, %rdi
callq 0x2280
movq (%rbx), %rdi
callq 0x2280
movq 0x8(%rbx), %rdi
callq 0x2280
movq %rbx, %rdi
callq 0x2280
movq %r15, %rbx
movq %r12, %rdi
callq 0x2280
movq %rbx, %rdi
callq 0x2280
movq -0x30(%rbp), %r12
movq %r12, %rdi
callq 0x2280
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
cmpq %rax, %rsi
jne 0x46f3
movq 0x10(%r8), %r8
movq (%r8), %rax
movq 0x8(%rax), %rdx
movq (%rdx), %rdx
movq 0x8(%rdx), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq 0x8(%r13), %rdx
movsd %xmm0, (%rdx,%rcx,8)
movq %rax, (%r14,%rcx,8)
movq %rax, %r15
movq 0x10(%r8), %r8
testq %r8, %r8
je 0x44ea
movq (%r8), %rax
cmpq %rax, %rsi
je 0x4713
movq 0x8(%rax), %rdi
movq (%rdi), %rdi
movq 0x8(%rdi), %rdi
movsd (%rdi,%rcx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4713
movsd %xmm1, (%rdx,%rcx,8)
movq %rax, (%r14,%rcx,8)
movq %rax, %r15
movapd %xmm1, %xmm0
jmp 0x4713