// File: /workspace/github_cpp_makefile_star2_O1/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 0x5b4b
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbx
movq %rsi, -0x58(%rbp)
leaq 0x11a1c(%rip), %r15     # 0x17364
movslq (%r15), %r14
movq %rdi, -0x50(%rbp)
movl $0x10, %esi
movq %r14, %rdi
callq 0x31b0
movq %rax, -0x38(%rbp)
movl $0x10, %esi
movq %r14, %rdi
callq 0x31b0
movq %rax, %r14
xorl %edi, %edi
xorl %esi, %esi
callq 0x51e4
movq %rax, -0x30(%rbp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x51e4
movq %rax, %r13
movq -0x50(%rbp), %rax
movq 0x8(%rax), %r8
movq (%r8), %rax
movl (%rax), %ecx
cmpl $0x1, %ecx
je 0x5b4c
testl %ecx, %ecx
jne 0x5dbb
movq %rbx, -0x48(%rbp)
movl (%r15), %ecx
xorl %edx, %edx
movq %r8, -0x40(%rbp)
movq %r8, %rsi
testl %ecx, %ecx
jle 0x5aad
movq (%rsi), %rdi
movq 0x8(%rdi), %r8
movq (%r8), %r8
movq (%r8), %r9
movq -0x30(%rbp), %r10
movq (%r10), %r10
xorl %r11d, %r11d
movsd (%r9,%r11,8), %xmm0
testb $0x1, %dl
je 0x5a56
ucomisd (%r10,%r11,8), %xmm0
jbe 0x59f7
movsd %xmm0, (%r10,%r11,8)
movq -0x38(%rbp), %rbx
movq %rdi, (%rbx,%r11,8)
movq 0x8(%r8), %rbx
movsd (%rbx,%r11,8), %xmm0
movq -0x30(%rbp), %r12
movq 0x8(%r12), %r12
movsd (%r12,%r11,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5a20
movsd %xmm0, (%r12,%r11,8)
movq %rdi, (%r14,%r11,8)
movsd (%r9,%r11,8), %xmm0
movq (%r13), %r12
movsd (%r12,%r11,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5a3c
movsd %xmm0, (%r12,%r11,8)
movsd (%rbx,%r11,8), %xmm0
movq 0x8(%r13), %rbx
ucomisd (%rbx,%r11,8), %xmm0
jbe 0x5aa1
movsd %xmm0, (%rbx,%r11,8)
jmp 0x5aa1
movsd %xmm0, (%r10,%r11,8)
movq 0x8(%r8), %rbx
movsd (%rbx,%r11,8), %xmm0
movq -0x30(%rbp), %r12
movq 0x8(%r12), %r12
movsd %xmm0, (%r12,%r11,8)
movsd (%r9,%r11,8), %xmm0
movq (%r13), %r12
movsd %xmm0, (%r12,%r11,8)
movsd (%rbx,%r11,8), %xmm0
movq 0x8(%r13), %rbx
movsd %xmm0, (%rbx,%r11,8)
movq -0x38(%rbp), %rbx
movq %rdi, (%rbx,%r11,8)
movq %rdi, (%r14,%r11,8)
incq %r11
cmpq %r11, %rcx
jne 0x59d6
movq 0x10(%rsi), %rsi
movb $0x1, %dl
testq %rsi, %rsi
jne 0x59b7
movslq (%r15), %rcx
testq %rcx, %rcx
jle 0x5c58
movq -0x30(%rbp), %rsi
movq (%rsi), %rdx
movq 0x8(%rsi), %rsi
movq (%r13), %rdi
movq 0x8(%r13), %r8
xorpd %xmm0, %xmm0
xorl %r9d, %r9d
movapd 0xc616(%rip), %xmm1     # 0x12100
xorpd %xmm2, %xmm2
movsd (%rdx,%r9,8), %xmm3
subsd (%rsi,%r9,8), %xmm3
movsd (%r8,%r9,8), %xmm4
subsd (%rdi,%r9,8), %xmm4
divsd %xmm4, %xmm3
andpd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r9d, %xmm4
movapd %xmm3, %xmm5
maxsd %xmm2, %xmm5
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm4
andnpd %xmm0, %xmm2
orpd %xmm4, %xmm2
movapd %xmm2, %xmm0
incq %r9
movapd %xmm5, %xmm2
cmpq %r9, %rcx
jne 0x5aee
cvttsd2si %xmm0, %ecx
movslq %ecx, %rbx
jmp 0x5c5a
retq
movq %rbx, -0x48(%rbp)
movslq (%r15), %r15
movl %r15d, %ebx
movl $0x8, %esi
movq %r15, %rdi
movq %r8, -0x40(%rbp)
callq 0x31b0
movq %rax, %r12
movl $0x8, %esi
movq %r15, %rdi
callq 0x31b0
movq -0x40(%rbp), %r8
movq %rax, %r15
xorl %eax, %eax
testl %ebx, %ebx
jle 0x5be7
movq (%r8), %rcx
movq 0x8(%rcx), %rdx
movq 0x8(%rdx), %rdx
movq (%rdx), %rdx
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
testb $0x1, %al
je 0x5bc7
movsd (%r12,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x5bb7
movsd %xmm0, (%r12,%rsi,8)
movq -0x38(%rbp), %rdi
movq %rcx, (%rdi,%rsi,8)
ucomisd (%r15,%rsi,8), %xmm0
jbe 0x5bdf
movsd %xmm0, (%r15,%rsi,8)
jmp 0x5bdb
movsd %xmm0, (%r12,%rsi,8)
movsd %xmm0, (%r15,%rsi,8)
movq -0x38(%rbp), %rdi
movq %rcx, (%rdi,%rsi,8)
movq %rcx, (%r14,%rsi,8)
incq %rsi
cmpq %rsi, %rbx
jne 0x5b94
movq 0x10(%r8), %r8
movb $0x1, %al
testq %r8, %r8
jne 0x5b80
testl %ebx, %ebx
jle 0x5d39
xorpd %xmm0, %xmm0
xorl %eax, %eax
movapd 0xc4f8(%rip), %xmm1     # 0x12100
xorpd %xmm2, %xmm2
movsd (%r15,%rax,8), %xmm3
subsd (%r12,%rax,8), %xmm3
andpd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
movapd %xmm3, %xmm5
maxsd %xmm2, %xmm5
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm4
andnpd %xmm0, %xmm2
orpd %xmm4, %xmm2
movapd %xmm2, %xmm0
incq %rax
movapd %xmm5, %xmm2
cmpq %rax, %rbx
jne 0x5c0c
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
jmp 0x5d3b
xorl %ebx, %ebx
movq -0x38(%rbp), %rcx
movq (%rcx,%rbx,8), %rsi
movq (%r14,%rbx,8), %rcx
cmpq %rcx, %rsi
movq -0x40(%rbp), %rdi
jne 0x5ce7
cmpq %rax, %rcx
jne 0x5c7b
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
movq 0x8(%rax), %rdx
movq (%rdx), %rdx
movq 0x8(%rdx), %rdx
movsd (%rdx,%rbx,8), %xmm0
movq -0x30(%rbp), %rdx
movq 0x8(%rdx), %rdx
movsd %xmm0, (%rdx,%rbx,8)
movq %rax, (%r14,%rbx,8)
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x5ce7
movq -0x30(%rbp), %rdx
movq 0x8(%rdx), %rdx
movsd (%rdx,%rbx,8), %xmm0
movq (%rax), %rdi
cmpq %rdi, %rcx
je 0x5cde
movq 0x8(%rdi), %r8
movq (%r8), %r8
movq 0x8(%r8), %r8
movsd (%r8,%rbx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5cde
movsd %xmm1, (%rdx,%rbx,8)
movq %rdi, (%r14,%rbx,8)
movapd %xmm1, %xmm0
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x5cb2
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x44a0
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq (%r14,%rbx,8), %rsi
movq %r15, %rdi
callq 0x44a0
movq -0x48(%rbp), %rcx
movq %rax, (%rcx)
movq -0x30(%rbp), %rbx
movq (%rbx), %rdi
callq 0x3310
movq 0x8(%rbx), %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3310
movq (%r13), %rdi
callq 0x3310
movq 0x8(%r13), %r12
jmp 0x5dab
xorl %ecx, %ecx
movq %rcx, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq (%rax,%rcx,8), %rsi
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x44a0
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq -0x40(%rbp), %rax
movq (%r14,%rax,8), %rsi
movq %rbx, %rdi
callq 0x44a0
movq -0x48(%rbp), %rcx
movq %rax, (%rcx)
movq -0x30(%rbp), %rbx
movq (%rbx), %rdi
callq 0x3310
movq 0x8(%rbx), %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3310
movq (%r13), %rdi
callq 0x3310
movq 0x8(%r13), %rdi
callq 0x3310
movq %r13, %rdi
callq 0x3310
movq %r15, %r13
movq %r12, %rdi
callq 0x3310
movq %r13, %rdi
callq 0x3310
movq -0x38(%rbp), %rdi
callq 0x3310
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3310