// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
xorl %r12d, %r12d
movq %r12, 0x10(%rax)
movq %rax, 0x10(%rbx)
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r12, 0x10(%rax)
movq %rax, 0x18(%rbx)
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r12, 0x10(%rax)
movq %rax, 0x20(%rbx)
testq %r15, %r15
je 0x26357
movq %r15, %rdi
callq 0x26856
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, (%rbx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x26e0e
movq (%r14), %rdi
callq 0x26856
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rbx)
movq (%r14), %rsi
movq %rax, %rdi
callq 0x26e0e
cmpl $0x2, %ebp
jl 0x26325
movl %ebp, %ebp
movl $0x1, %r12d
xorl %r15d, %r15d
movq (%r14,%r12,8), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x26388
cmpb $0x2, %r15b
je 0x261ed
movzbl %r15b, %ecx
movq %r13, %rdi
cmpl $0x1, %ecx
je 0x261f0
testl %ecx, %ecx
je 0x261f0
testb %al, %al
je 0x2628c
callq 0x26856
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x26e0e
movq 0x10(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x262c9
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x262d3
movq %r13, %rdi
testb %al, %al
je 0x26234
callq 0x26856
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x26e0e
movq 0x10(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x26271
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x262ea
callq 0x26856
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x26e0e
movq 0x20(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2627d
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x26287
leaq 0x8(%rsp), %rdx
callq 0x12a12
jmp 0x262ea
leaq 0x8(%rsp), %rdx
callq 0x12a12
movb $0x1, %r15b
jmp 0x262fe
callq 0x26856
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x26e0e
movq 0x18(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x262f1
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x262fb
leaq 0x8(%rsp), %rdx
callq 0x12a12
movq 0x18(%rbx), %rdi
movq $0x0, 0x10(%rsp)
leaq 0x10(%rsp), %rsi
callq 0xb27c
movb $0x3, %r15b
movb $0x1, %al
jmp 0x26300
leaq 0x8(%rsp), %rdx
callq 0x12a12
movb $0x2, %r15b
xorl %eax, %eax
incq %r12
cmpq %r12, %rbp
jne 0x2617c
testb %al, %al
je 0x26325
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
movq $0x0, (%rsi)
callq 0xb27c
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 0x26373
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x85ab(%rip), %rdi      # 0x2e909
leaq 0x85e6(%rip), %rsi      # 0x2e94b
movl $0x1a, %edx
xorl %eax, %eax
callq 0x2050
jmp 0x26381
leaq 0x85e5(%rip), %rdi      # 0x2e95f
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0