// File: 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
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movss %xmm0, -0xc(%rbp)
movss %xmm1, -0x10(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x120(%rbp)
xorl %eax, %eax
cmpq 0x8(%rcx), %rax
jne 0x3947
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
leaq 0xc770(%rip), %rsi      # 0x1009e
callq 0x2190
movq %rax, %rdi
movq 0x14683(%rip), %rsi     # 0x17fc0
callq 0x21c0
jmp 0x4009
movl $0x0, -0x14(%rbp)
movb $0x1, -0x15(%rbp)
movl $0xbf800000, -0x1c(%rbp) # imm = 0xBF800000
movl $0x50, %edi
callq 0x21a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x128(%rbp)
callq 0x4430
jmp 0x3977
movq -0x120(%rbp), %rdi
movq -0x128(%rbp), %rax
movq %rax, -0x28(%rbp)
movq 0x8(%rdi), %rsi
movss -0xc(%rbp), %xmm0
movq -0x28(%rbp), %rdx
callq 0x2ed0
movq -0x28(%rbp), %rdi
callq 0x4460
movq (%rax), %rsi
leaq -0x50(%rbp), %rdi
callq 0x6c60
movq -0x28(%rbp), %rdi
callq 0x4460
movq %rax, -0x130(%rbp)
jmp 0x39c2
movq -0x130(%rbp), %rax
movq (%rax), %rsi
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0x68(%rbp), %rdi
callq *%rax
jmp 0x39db
movq -0x28(%rbp), %rdi
callq 0x4460
movq %rax, -0x138(%rbp)
jmp 0x39ed
movq -0x138(%rbp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
callq *%rax
movq %rax, -0x140(%rbp)
jmp 0x3a09
movq -0x140(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x4160
movq -0x150(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x4190
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
leaq -0xc(%rbp), %rdx
callq 0x4110
movq %rax, -0x148(%rbp)
jmp 0x3a58
movq -0x148(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x3a65
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0x90(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x41e0
movq %rax, -0x158(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4480
movq %rax, %rcx
movq -0x158(%rbp), %rax
cmpq %rcx, %rax
jne 0x3b9c
movq -0x70(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x6c60
jmp 0x3aba
leaq -0x50(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movq %rsi, -0x160(%rbp)
callq 0x45c0
movq -0x160(%rbp), %rdi
callq 0x42a0
movq -0x70(%rbp), %rsi
movq (%rsi), %rax
movq 0x38(%rax), %rax
leaq -0xc0(%rbp), %rdi
callq *%rax
jmp 0x3af3
leaq -0x68(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movq %rsi, -0x170(%rbp)
callq 0x45f0
movq -0x170(%rbp), %rdi
callq 0x4560
movq -0x70(%rbp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
callq *%rax
movq %rax, -0x168(%rbp)
jmp 0x3b2c
movq -0x168(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x3b9c
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2180
jmp 0x4012
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x3ffe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4560
jmp 0x3ffe
testb $0x1, -0x15(%rbp)
je 0x3bd7
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0xd0(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x41c0
movss (%rax), %xmm0
movss %xmm0, -0x1c(%rbp)
movb $0x1, %al
testb $0x1, -0x15(%rbp)
movb %al, -0x171(%rbp)
jne 0x3c27
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0xd8(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x41c0
movss (%rax), %xmm0
ucomiss -0x1c(%rbp), %xmm0
setne %al
setp %cl
orb %cl, %al
movb %al, -0x171(%rbp)
movb -0x171(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3c35
jmp 0x3c3a
jmp 0x3fc1
movss -0xc(%rbp), %xmm0
movss %xmm0, -0x178(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0xe0(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x41c0
movss -0x178(%rbp), %xmm1
movss (%rax), %xmm0
xorl %eax, %eax
ucomiss %xmm1, %xmm0
movb %al, -0x172(%rbp)
jb 0x3ccd
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0xe8(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x41c0
movss (%rax), %xmm1
movss -0x10(%rbp), %xmm0
ucomiss %xmm1, %xmm0
setae %al
movb %al, -0x172(%rbp)
movb -0x172(%rbp), %al
testb $0x1, %al
jne 0x3cdc
jmp 0x3f4c
testb $0x1, -0x15(%rbp)
jne 0x3cfd
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
leaq 0xc3b2(%rip), %rsi      # 0x100a6
callq 0x2190
jmp 0x3cfb
jmp 0x3cfd
movl $0x0, -0x14(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0xf0(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
callq 0x4500
movq %rax, %rdi
callq 0x4520
movq %rax, %rcx
movq -0x180(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x3e5a
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
leaq 0xc337(%rip), %rsi      # 0x100a3
callq 0x2190
movq %rax, -0x188(%rbp)
jmp 0x3d7a
leaq -0x50(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x4160
movq %rax, -0xf8(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x41e0
movq -0x198(%rbp), %rdi
movq %rax, %rsi
callq 0x41c0
movq -0x188(%rbp), %rdi
movss (%rax), %xmm0
callq 0x2140
movq %rax, -0x190(%rbp)
jmp 0x3dc9
movq -0x190(%rbp), %rdi
leaq 0xc2cf(%rip), %rsi      # 0x100a6
callq 0x2190
movq %rax, -0x1a0(%rbp)
jmp 0x3de5
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0x100(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
callq 0x4500
movq %rax, %rdi
movslq -0x14(%rbp), %rsi
callq 0x4540
movq -0x1a0(%rbp), %rdi
movq %rax, %rsi
callq 0x2170
movq %rax, -0x1a8(%rbp)
jmp 0x3e35
movq -0x1a8(%rbp), %rdi
leaq 0xc262(%rip), %rsi      # 0x100a5
callq 0x2190
jmp 0x3e4a
jmp 0x3e4c
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3d04
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
leaq 0xc237(%rip), %rsi      # 0x100a3
callq 0x2190
movq %rax, -0x1b0(%rbp)
jmp 0x3e7a
leaq -0x50(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x4160
movq %rax, -0x108(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x41e0
movq -0x1c0(%rbp), %rdi
movq %rax, %rsi
callq 0x41c0
movq -0x1b0(%rbp), %rdi
movss (%rax), %xmm0
callq 0x2140
movq %rax, -0x1b8(%rbp)
jmp 0x3ec9
movq -0x1b8(%rbp), %rdi
leaq 0xc1cf(%rip), %rsi      # 0x100a6
callq 0x2190
movq %rax, -0x1c8(%rbp)
jmp 0x3ee5
leaq -0x50(%rbp), %rdi
callq 0x4160
movq %rax, -0x110(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x41e0
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
callq 0x4500
movq %rax, %rdi
movslq -0x14(%rbp), %rsi
callq 0x4540
movq -0x1c8(%rbp), %rdi
movq %rax, %rsi
callq 0x2170
movq %rax, -0x1d0(%rbp)
jmp 0x3f35
movq -0x1d0(%rbp), %rdi
leaq 0xc193(%rip), %rsi      # 0x100d6
callq 0x2190
jmp 0x3f4a
jmp 0x3fa6
testb $0x1, -0x15(%rbp)
jne 0x3f6d
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
movq 0x1405c(%rip), %rsi     # 0x17fc0
callq 0x21c0
jmp 0x3f6b
jmp 0x3fa4
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
leaq 0xc11f(%rip), %rsi      # 0x1009e
callq 0x2190
movq %rax, -0x1d8(%rbp)
jmp 0x3f8d
movq -0x1d8(%rbp), %rdi
movq 0x14025(%rip), %rsi     # 0x17fc0
callq 0x21c0
jmp 0x3fa2
jmp 0x3fa4
jmp 0x3fc1
movb $0x0, -0x15(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x4620
movq %rax, -0x118(%rbp)
jmp 0x3a63
movq -0x28(%rbp), %rax
movq %rax, -0x1e0(%rbp)
cmpq $0x0, %rax
je 0x3fea
movq -0x1e0(%rbp), %rdi
callq 0x44e0
movq -0x1e0(%rbp), %rdi
callq 0x2180
leaq -0x68(%rbp), %rdi
callq 0x4560
leaq -0x50(%rbp), %rdi
callq 0x42a0
jmp 0x4009
leaq -0x50(%rbp), %rdi
callq 0x42a0
jmp 0x4012
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x22d0
nopl (%rax,%rax)