// File: /workspace/github_cpp_makefile_star2_O1/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 -0x80(%rbp), %r15
movq %r15, -0x10(%r15)
xorl %eax, %eax
movq %rax, -0x8(%r15)
movb %al, (%r15)
leaq -0x60(%rbp), %r12
movq %r12, -0x10(%r12)
movq %rax, -0x8(%r12)
movb %al, (%r12)
leaq -0x2b8(%rbp), %rdi
callq 0x2280
cmpl $0x2, %r14d
jne 0x2acd
movq 0x8(%rbx), %rsi
leaq -0x4d0(%rbp), %rdi
movl $0x8, %edx
callq 0x22c0
movq -0x4d0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x4b0(%rbp,%rax)
jne 0x2b1a
leaq (%rax,%rbp), %rdi
addq $-0x4d0, %rdi           # imm = 0xFB30
movl $0xa, %esi
callq 0x2160
movsbl %al, %edx
leaq -0x4d0(%rbp), %rbx
leaq -0x90(%rbp), %rsi
movq %rbx, %rdi
callq 0x22d0
movq -0x90(%rbp), %r15
callq 0x2040
movq %rax, %r14
movl (%rax), %r13d
movl $0x0, (%rax)
leaq -0x50(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x21f0
cmpq %r15, (%r12)
je 0x2b43
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x2b4f
movl (%r14), %ecx
cmpl $0x22, %ecx
je 0x2b4f
testl %ecx, %ecx
jne 0x2690
movl %r13d, (%r14)
cmpl $0x3, %eax
jl 0x2b5b
leaq -0x2c8(%rbp), %rdi
movl %eax, %esi
callq 0x2ee0
leaq -0x2c8(%rbp), %rdi
callq 0x395e
leaq -0x90(%rbp), %r14
leaq -0x70(%rbp), %r15
leaq -0x50(%rbp), %r13
movq -0x4d0(%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x29af
movq -0x68(%rbp), %rdx
movl $0x1, %ecx
movq %r15, %rdi
xorl %esi, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x22e0
movl $0x6, %edx
movq %r14, %rdi
xorl %esi, %esi
leaq 0x3942(%rip), %rcx      # 0x6066
callq 0x21e0
testl %eax, %eax
je 0x27cd
movl $0x6, %edx
movq %r14, %rdi
xorl %esi, %esi
leaq 0x392b(%rip), %rcx      # 0x606d
callq 0x21e0
testl %eax, %eax
jne 0x26c2
movl $0x1, %ecx
movq %r14, %rdi
leaq 0x398a(%rip), %rsi      # 0x60e8
xorl %edx, %edx
callq 0x2290
movq %rax, %r12
cmpl $-0x1, %r12d
jne 0x28e3
movq -0x88(%rbp), %rcx
addq $-0x8, %rcx
movl $0x7, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2180
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
leaq -0x40(%rbp), %rax
cmpq %rax, %r15
je 0x27b4
movq %r15, %rdi
movsd %xmm0, -0x30(%rbp)
callq 0x2150
movsd -0x30(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
leaq -0x2c8(%rbp), %rdi
callq 0x3194
leaq -0x70(%rbp), %r15
jmp 0x26c2
movl $0x1, %ecx
movq %r14, %rdi
leaq 0x390c(%rip), %rsi      # 0x60e8
xorl %edx, %edx
callq 0x2290
movq %rax, %r12
shlq $0x20, %r12
movabsq $-0x700000000, %rax     # imm = 0xFFFFFFF900000000
leaq (%r12,%rax), %rcx
sarq $0x20, %rcx
movl $0x7, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2180
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
movsd %xmm0, -0x30(%rbp)
leaq -0x40(%rbp), %rax
cmpq %rax, %r15
je 0x282e
movq %r15, %rdi
callq 0x2150
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq (%r12,%rax), %rdx
sarq $0x20, %rdx
movq -0x88(%rbp), %rax
sarq $0x20, %r12
negq %r12
leaq (%rax,%r12), %rcx
addq $-0x2, %rcx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2180
leaq -0x70(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x21b0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x2882
callq 0x2150
leaq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0xb8(%rbp), %rdi
callq 0x2c66
xorps %xmm0, %xmm0
cvtsd2ss -0x30(%rbp), %xmm0
leaq -0x2c8(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x2ef0
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x26c2
callq 0x2150
jmp 0x26c2
shlq $0x20, %r12
movabsq $-0x700000000, %rax     # imm = 0xFFFFFFF900000000
leaq (%r12,%rax), %rcx
sarq $0x20, %rcx
movl $0x7, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2180
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
movsd %xmm0, -0x30(%rbp)
leaq -0x40(%rbp), %rax
cmpq %rax, %r15
je 0x292b
movq %r15, %rdi
callq 0x2150
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq (%r12,%rax), %rdx
sarq $0x20, %rdx
movq -0x88(%rbp), %rax
sarq $0x20, %r12
negq %r12
leaq (%rax,%r12), %rcx
addq $-0x2, %rcx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2180
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
movaps %xmm0, %xmm1
leaq -0x40(%rbp), %rax
cmpq %rax, %r15
je 0x298e
movq %r15, %rdi
movsd %xmm1, -0x98(%rbp)
callq 0x2150
movsd -0x98(%rbp), %xmm1
xorps %xmm0, %xmm0
cvtsd2ss -0x30(%rbp), %xmm0
cvtsd2ss %xmm1, %xmm1
leaq -0x2c8(%rbp), %rdi
callq 0x3474
leaq -0x70(%rbp), %r15
jmp 0x26c2
leaq -0x2c8(%rbp), %rdi
callq 0x3978
movq 0x6616(%rip), %rdi      # 0x8fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq 0x65fb(%rip), %rdi      # 0x8fd8
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movq %rax, %rbx
leaq 0x3680(%rip), %rsi      # 0x6074
movl $0x6, %edx
movq %rax, %rdi
callq 0x2190
leaq 0x3673(%rip), %rsi      # 0x607b
movl $0xf, %edx
movq %rbx, %rdi
callq 0x2190
leaq 0x366f(%rip), %rsi      # 0x608b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x2190
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
leaq -0x4d0(%rbp), %rdi
callq 0x2060
leaq -0x2c8(%rbp), %rdi
callq 0x3986
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
leaq -0x80(%rbp), %rbx
je 0x2aa8
callq 0x2150
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2ab9
callq 0x2150
xorl %eax, %eax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6504(%rip), %rdi      # 0x8fd8
leaq 0x3529(%rip), %rsi      # 0x6004
movl $0x11, %edx
callq 0x2190
movq 0x64ec(%rip), %rdi      # 0x8fd8
callq 0x20b0
movq 0x64e0(%rip), %rdi      # 0x8fd8
leaq 0x3517(%rip), %rsi      # 0x6016
movl $0x24, %edx
callq 0x2190
movq 0x64c8(%rip), %rdi      # 0x8fd8
callq 0x20b0
jmp 0x2bc8
movq 0x64b7(%rip), %rdi      # 0x8fd8
leaq 0x3513(%rip), %rsi      # 0x603b
movl $0x13, %edx
callq 0x2190
movq 0x649f(%rip), %rdi      # 0x8fd8
callq 0x20b0
jmp 0x2bc8
leaq 0x354f(%rip), %rdi      # 0x6099
callq 0x20e0
leaq 0x3543(%rip), %rdi      # 0x6099
callq 0x2210
movq 0x6476(%rip), %rdi      # 0x8fd8
leaq 0x34e6(%rip), %rsi      # 0x604f
movl $0x16, %edx
callq 0x2190
movq 0x645e(%rip), %rdi      # 0x8fd8
callq 0x20b0
jmp 0x2bc8
jmp 0x2b83
movq %rax, %rbx
jmp 0x2c24
movq %rax, %rbx
jmp 0x2c30
cmpl $0x0, (%r14)
jne 0x2b9c
movl %r13d, (%r14)
movq %rax, %rdi
callq 0x20a0
movq 0x642d(%rip), %rdi      # 0x8fd8
leaq 0x349d(%rip), %rsi      # 0x604f
movl $0x16, %edx
callq 0x2190
movq 0x6415(%rip), %rdi      # 0x8fd8
callq 0x20b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21a0
movq %rax, %rbx
callq 0x2270
jmp 0x2c10
movq %rax, %rdi
callq 0x2c57
jmp 0x2c0d
jmp 0x2c0d
jmp 0x2c0d
jmp 0x2c0d
jmp 0x2c0d
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x2c10
callq 0x2150
jmp 0x2c10
jmp 0x2c0d
movq %rax, %rbx
leaq -0x4d0(%rbp), %rdi
callq 0x2060
leaq -0x80(%rbp), %r15
leaq -0x60(%rbp), %r12
leaq -0x2c8(%rbp), %rdi
callq 0x3986
movq -0x70(%rbp), %rdi
cmpq %r12, %rdi
je 0x2c3e
callq 0x2150
movq -0x90(%rbp), %rdi
cmpq %r15, %rdi
je 0x2c4f
callq 0x2150
movq %rbx, %rdi
callq 0x22a0