// File: /workspace/github_cpp_makefile_star2_O2/deepaktabraham[P]BPlus-Tree/src/tree.cpp
// Function: BPlusTree::Search(float, float)
void BPlusTree :: Search(float key1, float key2)
{
	// check if tree is empty
	if(NULL == root)
	{
		outputFile<<"Null"<<endl;
	}

	// if it is a valid range search
	else
	{
		int i = 0;
		bool firstPass = true;
		float firstKey = ERROR;
		
		// obtain the search path from root to leaf node and push it on to a stack
		stack<Node*>* path = new stack<Node*>;
		Search_Path(root, key1, path);
		
		// search for the key in the leaf node, which is at the top of the stack
		vector<float> keys = path->top()->Get_Keys();
		vector< vector <string> > values = path->top()->Get_Values(); 
		Node* next = path->top()->Get_Next();
		vector<float>::iterator index = lower_bound(keys.begin(), keys.end(), key1);

		// display all the keys in the search range, along with their corresponding values
		while(1)
		{
			// check if end of the current leaf node is reached
			if((index - keys.begin()) == keys.size())
			{
 				// go to the next leaf node
				keys = next->Get_Keys();
                		values = next->Get_Values();
                		next = next->Get_Next();
				index = keys.begin();
			}	
			
			// save the smallest key in the given search range
			if(firstPass)
			{	
				firstKey = keys[index - keys.begin()];						
			}


			// check if already iterated through the doubly linked list once
			if(!(firstPass || (keys[index - keys.begin()] != firstKey)))
			{
				// exit the loop
				break;
			}

			// check if key is within the search range
			if((key1 <= keys[index - keys.begin()]) && (keys[index - keys.begin()] <= key2)) 
			{
				if(!firstPass)
				{
					outputFile<<",";
				}

				// display the key and its corresponding values
				for(i = 0; i < values[index - keys.begin()].size() - 1; i++)
				{
					outputFile<<"("<<keys[index - keys.begin()]<<","<<values[index - keys.begin()][i]<<"),";
				}
				outputFile<<"("<<keys[index - keys.begin()]<<","<<values[index - keys.begin()][i]<<")";
			}

			// if key is not within the search range
			else
			{
				// check if atleast one key was in the search range
				if(!firstPass)
				{
					outputFile<<endl;
				}

				// if no keys belonged within the search range
				else
				{
					outputFile<<"Null"<<endl;
				}

				// exit the loop
				break;
			}

			firstPass = false;
			index++;
		}

		delete(path);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movss %xmm1, -0x38(%rbp)
movq %rdi, %r14
movss %xmm0, -0x2c(%rbp)
cmpq $0x0, 0x8(%rdi)
je 0x32c5
pushq $0x50
popq %rdi
callq 0x2150
movq %rax, %r15
movq %rax, %rdi
callq 0x33aa
movq 0x8(%r14), %rsi
movss -0x2c(%rbp), %xmm0
movq %r14, %rdi
movq %r15, %rdx
callq 0x2a48
movq 0x30(%r15), %rax
cmpq 0x38(%r15), %rax
jne 0x3037
movq 0x48(%r15), %rcx
movl $0x200, %eax            # imm = 0x200
addq -0x8(%rcx), %rax
movq -0x8(%rax), %rsi
leaq -0x50(%rbp), %rdi
callq 0x3b06
movq 0x30(%r15), %rax
cmpq 0x38(%r15), %rax
jne 0x305b
movq 0x48(%r15), %rcx
movl $0x200, %eax            # imm = 0x200
addq -0x8(%rcx), %rax
movq -0x8(%rax), %rsi
movq (%rsi), %rax
leaq -0x70(%rbp), %rdi
callq *0x38(%rax)
movq 0x30(%r15), %rax
cmpq 0x38(%r15), %rax
jne 0x3080
movq 0x48(%r15), %rcx
movl $0x200, %eax            # imm = 0x200
addq -0x8(%rcx), %rax
movq %r15, -0x78(%rbp)
movq -0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x3788
movq %rax, %r15
addq $0x10, %r14
movb $0x1, %r12b
movss 0x2feb(%rip), %xmm0     # 0x60a0
movss %xmm0, -0x30(%rbp)
leaq 0x2feb(%rip), %rbx      # 0x60ac
cmpq -0x48(%rbp), %r15
jne 0x3122
leaq -0x90(%rbp), %r13
movq %r13, %rdi
movq -0x58(%rbp), %r15
movq %r15, %rsi
callq 0x3b06
leaq -0x50(%rbp), %rdi
movq %r13, %rsi
callq 0x3a24
movq %r13, %rdi
callq 0x3760
movq (%r15), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x38(%rax)
leaq -0x70(%rbp), %rdi
movq %r13, %rsi
callq 0x3a90
movq %r13, %rdi
callq 0x33ca
movq (%r15), %rax
movq %r15, %rdi
callq *0x40(%rax)
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %r15
movss (%r15), %xmm0
movss %xmm0, -0x34(%rbp)
testb $0x1, %r12b
jne 0x3147
movss -0x30(%rbp), %xmm1
ucomiss %xmm1, %xmm0
movss %xmm1, -0x34(%rbp)
jne 0x3147
jnp 0x3290
ucomiss -0x2c(%rbp), %xmm0
jb 0x3270
movss -0x38(%rbp), %xmm1
ucomiss %xmm0, %xmm1
jb 0x3270
testb $0x1, %r12b
jne 0x3170
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2140
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r15, %rax
subq -0x50(%rbp), %rax
shrq $0x2, %rax
movq -0x70(%rbp), %rcx
imulq $0x18, %rax, %rax
movq 0x8(%rcx,%rax), %rdx
subq (%rcx,%rax), %rdx
sarq $0x5, %rdx
decq %rdx
cmpq %r13, %rdx
jbe 0x3202
movq %r14, %rdi
leaq 0x2f01(%rip), %rsi      # 0x60a9
callq 0x2140
movss (%r15), %xmm0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2140
movq %r15, %rcx
subq -0x50(%rbp), %rcx
shrq $0x2, %rcx
movq -0x70(%rbp), %rdx
imulq $0x18, %rcx, %rcx
movq (%rdx,%rcx), %rsi
addq %r12, %rsi
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
leaq 0x2eba(%rip), %rsi      # 0x60ab
callq 0x2140
incq %r13
addq $0x20, %r12
jmp 0x3176
movq %r14, %rdi
leaq 0x2e9d(%rip), %rsi      # 0x60a9
callq 0x2140
movss (%r15), %xmm0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2140
movq %r15, %rcx
subq -0x50(%rbp), %rcx
shrq $0x2, %rcx
movq -0x70(%rbp), %rdx
imulq $0x18, %rcx, %rcx
addq (%rdx,%rcx), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, %rdi
leaq 0x2e87(%rip), %rsi      # 0x60dc
callq 0x2140
addq $0x4, %r15
xorl %r12d, %r12d
movss -0x34(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
jmp 0x30c1
testb $0x1, %r12b
je 0x3288
leaq 0x2e27(%rip), %rsi      # 0x60a4
movq %r14, %rdi
callq 0x2140
movq %rax, %r14
movq %r14, %rdi
callq 0x20b0
movq -0x78(%rbp), %rbx
movq %rbx, %rdi
callq 0x3424
movq %rbx, %rdi
callq 0x2130
leaq -0x70(%rbp), %rdi
callq 0x33ca
leaq -0x50(%rbp), %rdi
callq 0x3760
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x10, %r14
leaq 0x2dd4(%rip), %rsi      # 0x60a4
movq %r14, %rdi
callq 0x2140
movq %rax, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20b0
jmp 0x3312
movq %rax, %rbx
jmp 0x331e
movq %rax, %rbx
movq %r15, %rdi
callq 0x2130
jmp 0x3327
jmp 0x3312
jmp 0x3312
jmp 0x3312
jmp 0x3312
jmp 0x3312
jmp 0x3312
jmp 0x3312
jmp 0x3312
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x33ca
leaq -0x50(%rbp), %rdi
callq 0x3760
movq %rbx, %rdi
callq 0x2270
nop