// File: gersteinlab[P]MUSIC/src/ms_ansi_cli.cpp
// Function: t_ansi_cli::t_ansi_cli(int, char**, char const*)
t_ansi_cli::t_ansi_cli(int argc, 
					   char* argv[], 
					   const char* _option_prefix)
{
	this->options = new vector<char*>();
	this->values = new vector<char*>();
	this->inputs = new vector<char*>();

	if(_option_prefix != NULL)
	{
		this->option_prefix = (char*)malloc(sizeof(char) * (t_string::string_length(_option_prefix) + 2));
		t_string::copy(this->option_prefix, _option_prefix);
	}
	else
	{
		printf("Cannot use NULL option prefix for parsing command line @ %s(%d).\n", __FILE__, __LINE__);
		exit(0);
	}

	// Copy the exe file name.
	this->exe_cmd = (char*)malloc(sizeof(char) * (t_string::string_length(argv[0]) + 2));
	t_string::copy(this->exe_cmd, argv[0]);

	// Go over all the arguments.
	//for(int i_arg = 1; i_arg < argc; i_arg++)
	enum{START, INP, VAL, OPT};
	int i_arg = 1;
	char state = START;
	while(i_arg < argc)
	{
		// New argument to be parsed.
		char* cur_arg = argv[i_arg];

		if(state == START)
		{
			// Is this an option?
			if(this->is_option(cur_arg))
			{
				char* new_option = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_option, cur_arg);
				this->options->push_back(new_option); // Must also push a new value for this option.

				state = OPT;
				i_arg++;
			}
			else
			{
				// Has to be an input.
				char* new_input = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_input, cur_arg);
				this->inputs->push_back(new_input); // Must also push a new value for this option.

				state = INP;
				i_arg++;
			}
		}
		else if(state == INP)
		{
			// Option or a new input.
			// Is this an option?
			if(this->is_option(cur_arg))
			{
				char* new_option = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_option, cur_arg);
				this->options->push_back(new_option); // Must also push a new value for this option.

				state = OPT;
				i_arg++;
			}
			else
			{
				// Has to be an input.
				char* new_input = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_input, cur_arg);
				this->inputs->push_back(new_input); // Must also push a new value for this option.

				state = INP;
				i_arg++;
			}
		}
		else if(state == VAL)
		{
			// Option or a new input.
			// Is this an option?
			if(this->is_option(cur_arg))
			{
				char* new_option = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_option, cur_arg);
				this->options->push_back(new_option); // Must also push a new value for this option.

				state = OPT;
				i_arg++;
			}
			else
			{
				// Has to be an input.
				char* new_input = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_input, cur_arg);
				this->inputs->push_back(new_input); // Must also push a new value for this option.

				state = INP;
				i_arg++;
			}
		}
		else if(state == OPT)
		{
			// Option or a new value. Cannot go to an input!
			// Is this an option?
			if(this->is_option(cur_arg))
			{
				char* new_option = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_option, cur_arg);
				this->options->push_back(new_option); // Must also push a new value for this option.

				this->values->push_back(NULL); // OPT->OPT transition: Implies that current option has no values, push NULL for the value of that option.

				state = OPT;
				i_arg++;
			}
			else
			{
				// Has to be a value.
				char* new_value = (char*)malloc(sizeof(char) * (t_string::string_length(cur_arg) + 2));
				t_string::copy(new_value, cur_arg);
				this->values->push_back(new_value); // Must also push a new value for this option.

				state = VAL;
				i_arg++;
			}
		}
		else
		{
			printf("Could not understand the state while parsing command line.\n");
			exit(0);
		}
	} // i_arg loop.

	if(state == OPT)
	{
		this->values->push_back(NULL);
	}

	if(this->options->size() != this->values->size())
	{
		printf("The size of options is not same as size of values %ld, %ld!\n", this->options->size(), this->values->size());
		exit(0);
	}

	//printf("Options - Values: (%d)\n", this->options->size());
	//for(int i = 0; i < this->options->size(); i++)
	//{
	//	printf("%s: %s\n", this->options->at(i), this->values->at(i));
	//}

	//printf("Inputs: (%d)\n", this->inputs->size());
	//for(int i = 0; i < this->inputs->size(); i++)
	//{
	//	printf("%s\n", this->inputs->at(i));
	//}
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movl %esi, 0xac(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x18(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x18(%rsp), %rdi
callq 0xc630
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x20(%rsp), %rdi
callq 0xc630
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x28(%rsp), %rdi
callq 0xc630
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x20(%rax)
cmpq $0x0, 0x98(%rsp)
je 0x46979
movq 0x98(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movq (%rax), %rdi
movq 0x98(%rsp), %rsi
callq 0x47bc0
jmp 0x4699a
leaq 0x10324(%rip), %rdi     # 0x56ca4
leaq 0x1035f(%rip), %rsi     # 0x56ce6
movl $0x1a, %edx
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x8(%rax), %rdi
movq 0xa0(%rsp), %rax
movq (%rax), %rsi
callq 0x47bc0
movl $0x1, 0x94(%rsp)
movb $0x0, 0x93(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xac(%rsp), %eax
jge 0x46e16
movq 0xa0(%rsp), %rax
movslq 0x94(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x88(%rsp)
movsbl 0x93(%rsp), %eax
cmpl $0x0, %eax
jne 0x46b13
movq 0x30(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x46ec0
testb $0x1, %al
jne 0x46a45
jmp 0x46aaf
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1fbe0
movb $0x3, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46b0e
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1fbe0
movb $0x1, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46e11
movsbl 0x93(%rsp), %eax
cmpl $0x1, %eax
jne 0x46c01
movq 0x30(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x46ec0
testb $0x1, %al
jne 0x46b3c
jmp 0x46b9d
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1fbe0
movb $0x3, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46bfc
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1fbe0
movb $0x1, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46e0f
movsbl 0x93(%rsp), %eax
cmpl $0x2, %eax
jne 0x46cef
movq 0x30(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x46ec0
testb $0x1, %al
jne 0x46c2a
jmp 0x46c8b
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1fbe0
movb $0x3, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46cea
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1fbe0
movb $0x1, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46e0d
movsbl 0x93(%rsp), %eax
cmpl $0x3, %eax
jne 0x46df6
movq 0x30(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x46ec0
testb $0x1, %al
jne 0x46d18
jmp 0x46d95
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1fbe0
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rdi
movq $0x0, 0x48(%rsp)
leaq 0x48(%rsp), %rsi
callq 0xc640
movb $0x3, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46df4
movq 0x88(%rsp), %rdi
callq 0x47640
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2230
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x47bc0
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1fbe0
movb $0x2, 0x93(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x46e0b
leaq 0xfefd(%rip), %rdi      # 0x56cfa
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
jmp 0x46e0d
jmp 0x46e0f
jmp 0x46e11
jmp 0x469ec
movsbl 0x93(%rsp), %eax
cmpl $0x3, %eax
jne 0x46e3f
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rdi
movq $0x0, 0x38(%rsp)
leaq 0x38(%rsp), %rsi
callq 0xc640
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
je 0x46eb1
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x8(%rsp)
movq 0x18(%rax), %rdi
callq 0xc220
movq 0x8(%rsp), %rsi
movq %rax, %rdx
leaq 0xfe93(%rip), %rdi      # 0x56d36
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
addq $0xb8, %rsp
retq
nopl (%rax)