// File: /workspace/github_cpp_makefile_star2_O2/deepaktabraham[P]BPlus-Tree/src/main.cpp
// Function: main
int main(int argc, char* argv[])
{
	int m = ERROR, pos = ERROR;
	float key = ERROR, key1 = ERROR, key2 = ERROR;
	string line, value;
	class BPlusTree tree;

	// check command-line arguments
	if(NUM_ARGS != argc)
	{
		cout<<"Invalid argument!"<<endl;
		cout<<"Usage: ./treesearch <input_filename>"<<endl;
		exit(-1);
	}

	// check if input file is valid
	ifstream file(argv[1]);
	if(!file)
	{
		cout<<"Invalid input file!"<<endl;
		exit(-1);
	}

	// get the B+ tree order
	getline(file, line);
	try
	{
		m = stoi(line);
	}
	catch(...)
	{
		cout<<"Invalid B+ tree order!"<<endl;
		exit(-1);
	}

	// check if order is atleast 3
	if(m < MIN_ORDER)
	{
		cout<<"Invalid B+ tree order!"<<endl;
		exit(-1);
	}

	// initialize the B+ tree of order m
	tree.Initialize(m);

	// open the output file for writing results
	tree.Open_Output_File();

	// read the input file and perform the corresponding operations
	while (getline(file, line))
	{
		pos   = ERROR;
		key   = ERROR;
		value = ERROR;
		key1  = ERROR;
		key2  = ERROR;

		// operation: Insert(key, value)
		if(0 == line.compare(0, 6, "Insert"))
		{
			pos = line.find(",");
			key = atof(line.substr(7, pos - 7).c_str());
			value = line.substr(pos + 1, line.size() - pos - 2);

			// insert the key-value pair
			tree.Insert(key, value);		
		}

		// if it is a search operation
		else if (0 == line.compare(0, 6, "Search"))
		{
			// operation: Search(key)
			pos = line.find(",");
			if(ERROR == pos)
			{
				key = atof(line.substr(7, line.size() - 8).c_str());

				// search for the key and display the correpsonding value(s)
				tree.Search(key);
			}

			// operation: Search(key1, key2)
			else
			{
				key1 = atof(line.substr(7, pos - 7).c_str());
				key2 = atof(line.substr(pos + 1, line.size() - pos -2).c_str());

				// search for keys between key1 and key2, and display the corresponding value(s)
				tree.Search(key1, key2);	
			}
		}
	}

	// close the output file
	tree.Close_Output_File();

#ifdef DEBUG
	// print the current state of the B+ tree
	tree.Print_Tree();
#endif

	cout<<endl<<"Check "<<OUTPUT_FILE<<" for results!"<<endl<<endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movq %rsi, %rbx
movl %edi, %r14d
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x88(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
xorl %edx, %edx
movb %dl, (%rax)
movb %dl, (%rcx)
leaq -0x2b8(%rbp), %rdi
callq 0x2260
cmpl $0x2, %r14d
jne 0x2831
movq 0x8(%rbx), %rsi
leaq -0x4d0(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x2280
movq -0x4d0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x4b0(%rbp,%rax)
jne 0x2869
leaq -0x4d0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2050
leaq -0x58(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x2941
cmpl $0x3, %eax
jl 0x2886
leaq -0x2c8(%rbp), %rdi
movl %eax, %esi
callq 0x2bac
leaq -0x2c8(%rbp), %rdi
callq 0x3330
leaq -0x58(%rbp), %r14
leaq -0x98(%rbp), %r15
pushq $0x6
popq %r12
leaq -0x78(%rbp), %r13
leaq -0x4d0(%rbp), %rdi
movq %r14, %rsi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x279b
movq %r15, %rdi
pushq $-0x1
popq %rsi
callq 0x2250
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
leaq 0x3abc(%rip), %rcx      # 0x6066
callq 0x21c0
testl %eax, %eax
je 0x262e
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
leaq 0x3aab(%rip), %rcx      # 0x606d
callq 0x21c0
testl %eax, %eax
jne 0x256f
movq %r14, %rdi
leaq 0x3ad7(%rip), %rsi      # 0x60ac
xorl %edx, %edx
callq 0x2290
movq %rax, %rbx
cmpl $-0x1, %ebx
jne 0x26f2
movq -0x50(%rbp), %rcx
addq $-0x8, %rcx
movq %r13, %rdi
movq %r14, %rsi
pushq $0x7
popq %rdx
callq 0x2160
movq -0x78(%rbp), %rdi
callq 0x2190
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
movq %r13, %rdi
callq 0x20a0
leaq -0x2c8(%rbp), %rdi
movss -0x30(%rbp), %xmm0
callq 0x2dec
jmp 0x256f
movq %r14, %rdi
leaq 0x3a74(%rip), %rsi      # 0x60ac
xorl %edx, %edx
callq 0x2290
movq %rax, %rbx
shlq $0x20, %rbx
movabsq $-0x700000000, %rax     # imm = 0xFFFFFFF900000000
leaq (%rbx,%rax), %rcx
sarq $0x20, %rcx
movq %r13, %rdi
movq %r14, %rsi
pushq $0x7
popq %rdx
callq 0x2160
movq -0x78(%rbp), %rdi
callq 0x2190
movsd %xmm0, -0x30(%rbp)
movq %r13, %rdi
callq 0x20a0
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq (%rbx,%rax), %rdx
sarq $0x20, %rdx
movq -0x50(%rbp), %rax
sarq $0x20, %rbx
negq %rbx
leaq (%rax,%rbx), %rcx
addq $-0x2, %rcx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2160
movq %r15, %rdi
movq %r13, %rsi
callq 0x2180
movq %r13, %rdi
callq 0x20a0
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21d0
cvtsd2ss -0x30(%rbp), %xmm0
leaq -0x2c8(%rbp), %rdi
movq %rbx, %rsi
callq 0x2bba
movq %rbx, %rdi
callq 0x20a0
jmp 0x256f
shlq $0x20, %rbx
movabsq $-0x700000000, %rax     # imm = 0xFFFFFFF900000000
leaq (%rbx,%rax), %rcx
sarq $0x20, %rcx
movq %r13, %rdi
movq %r14, %rsi
pushq $0x7
popq %rdx
callq 0x2160
movq -0x78(%rbp), %rdi
callq 0x2190
movsd %xmm0, -0x30(%rbp)
movq %r13, %rdi
callq 0x20a0
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq (%rbx,%rax), %rdx
sarq $0x20, %rdx
movq -0x50(%rbp), %rax
sarq $0x20, %rbx
negq %rbx
leaq (%rax,%rbx), %rcx
addq $-0x2, %rcx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2160
cvtsd2ss -0x30(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x78(%rbp), %rdi
callq 0x2190
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movq %r13, %rdi
callq 0x20a0
leaq -0x2c8(%rbp), %rdi
movss -0x30(%rbp), %xmm0
movss -0x34(%rbp), %xmm1
callq 0x2fd0
jmp 0x256f
leaq -0x2c8(%rbp), %rdi
callq 0x334a
movq 0x7822(%rip), %rdi      # 0x9fd0
callq 0x20b0
leaq 0x38ba(%rip), %rsi      # 0x6074
movq %rax, %rdi
callq 0x2140
leaq 0x38b2(%rip), %rsi      # 0x607b
movq %rax, %rdi
callq 0x2140
leaq 0x38b3(%rip), %rsi      # 0x608b
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x20b0
leaq -0x4d0(%rbp), %rdi
callq 0x2060
leaq -0x2c8(%rbp), %rdi
callq 0x3358
leaq -0x98(%rbp), %rdi
callq 0x20a0
leaq -0x58(%rbp), %rdi
callq 0x20a0
xorl %eax, %eax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7798(%rip), %rdi      # 0x9fd0
leaq 0x37c5(%rip), %rsi      # 0x6004
callq 0x2140
movq %rax, %rdi
callq 0x20b0
movq 0x777d(%rip), %rdi      # 0x9fd0
leaq 0x37bc(%rip), %rsi      # 0x6016
callq 0x2140
movq %rax, %rdi
callq 0x20b0
jmp 0x28c8
movq 0x7760(%rip), %rdi      # 0x9fd0
leaq 0x37c4(%rip), %rsi      # 0x603b
callq 0x2140
movq %rax, %rdi
callq 0x20b0
jmp 0x28c8
movq 0x7743(%rip), %rdi      # 0x9fd0
leaq 0x37bb(%rip), %rsi      # 0x604f
callq 0x2140
movq %rax, %rdi
callq 0x20b0
jmp 0x28c8
jmp 0x28e2
movq %rax, %rdi
callq 0x2090
movq 0x771c(%rip), %rdi      # 0x9fd0
leaq 0x3794(%rip), %rsi      # 0x604f
callq 0x2140
movq %rax, %rdi
callq 0x20b0
pushq $-0x1
popq %rdi
callq 0x2170
movq %rax, %rbx
callq 0x2240
jmp 0x290c
movq %rax, %rdi
callq 0x2962
movq %rax, %rbx
jmp 0x2918
movq %rax, %rbx
jmp 0x2924
jmp 0x2909
jmp 0x2909
jmp 0x2909
jmp 0x2909
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x20a0
jmp 0x290c
jmp 0x2909
jmp 0x2909
movq %rax, %rbx
leaq -0x4d0(%rbp), %rdi
callq 0x2060
leaq -0x2c8(%rbp), %rdi
callq 0x3358
leaq -0x98(%rbp), %rdi
callq 0x20a0
leaq -0x58(%rbp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2270