// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
pushq $0x18
popq %r12
movq %r12, %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0x10(%rbx)
movq %r12, %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0x18(%rbx)
movq %r12, %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x20(%rbx)
testq %r15, %r15
je 0x209de
movq %r15, %rdi
callq 0x20d90
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2220
movq %rax, (%rbx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x2109c
movq (%r14), %rdi
callq 0x20d90
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2220
movq %rax, 0x8(%rbx)
movq (%r14), %rsi
movq %rax, %rdi
callq 0x2109c
movslq %ebp, %rbp
pushq $0x1
popq %r12
xorl %r15d, %r15d
cmpq %rbp, %r12
jge 0x20994
movq (%r14,%r12,8), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20a0e
cmpb $0x2, %r15b
je 0x208a3
movzbl %r15b, %ecx
cmpl $0x1, %ecx
je 0x208a3
testl %ecx, %ecx
jne 0x2090d
movq %r13, %rdi
testb %al, %al
je 0x208da
callq 0x20d90
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2220
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2109c
movq 0x10(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xecb8
jmp 0x20956
callq 0x20d90
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2220
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2109c
movq 0x20(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xecb8
movb $0x1, %r15b
jmp 0x20959
movq %r13, %rdi
testb %al, %al
je 0x20961
callq 0x20d90
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2220
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2109c
movq 0x10(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xecb8
movq 0x18(%rbx), %rdi
andq $0x0, 0x10(%rsp)
leaq 0x10(%rsp), %rsi
callq 0x7d0a
movb $0x3, %r15b
incq %r12
jmp 0x20878
callq 0x20d90
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2220
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2109c
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xecb8
movb $0x2, %r15b
jmp 0x20959
cmpb $0x3, %r15b
jne 0x209ac
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
andq $0x0, (%rsi)
callq 0x7d0a
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
sarq $0x3, %rdx
cmpq %rdx, %rsi
jne 0x209f8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7f24(%rip), %rdi      # 0x28909
leaq 0x7f5f(%rip), %rsi      # 0x2894b
pushq $0x1a
popq %rdx
xorl %eax, %eax
callq 0x2050
jmp 0x20a06
leaq 0x7f60(%rip), %rdi      # 0x2895f
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21d0
nop