// File: /workspace/github_cpp_makefile_star2_O1/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 0x26977
movq %r15, %rdi
callq 0x26e82
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, (%rbx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x2743a
movq (%r14), %rdi
callq 0x26e82
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rbx)
movq (%r14), %rsi
movq %rax, %rdi
callq 0x2743a
cmpl $0x2, %ebp
jl 0x26945
movl %ebp, %ebp
movl $0x1, %r12d
xorl %r15d, %r15d
movq (%r14,%r12,8), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x269a8
cmpb $0x2, %r15b
je 0x2680d
movzbl %r15b, %ecx
movq %r13, %rdi
cmpl $0x1, %ecx
je 0x26810
testl %ecx, %ecx
je 0x26810
testb %al, %al
je 0x268ac
callq 0x26e82
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2743a
movq 0x10(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x268e9
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x268f3
movq %r13, %rdi
testb %al, %al
je 0x26854
callq 0x26e82
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2743a
movq 0x10(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x26891
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2690a
callq 0x26e82
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2743a
movq 0x20(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2689d
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x268a7
leaq 0x8(%rsp), %rdx
callq 0x12d32
jmp 0x2690a
leaq 0x8(%rsp), %rdx
callq 0x12d32
movb $0x1, %r15b
jmp 0x2691e
callq 0x26e82
addl $0x2, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2743a
movq 0x18(%rbx), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x26911
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2691b
leaq 0x8(%rsp), %rdx
callq 0x12d32
movq 0x18(%rbx), %rdi
movq $0x0, 0x10(%rsp)
leaq 0x10(%rsp), %rsi
callq 0xb476
movb $0x3, %r15b
movb $0x1, %al
jmp 0x26920
leaq 0x8(%rsp), %rdx
callq 0x12d32
movb $0x2, %r15b
xorl %eax, %eax
incq %r12
cmpq %r12, %rbp
jne 0x2679c
testb %al, %al
je 0x26945
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
movq $0x0, (%rsi)
callq 0xb476
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 0x26993
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8f8b(%rip), %rdi      # 0x2f909
leaq 0x8fc6(%rip), %rsi      # 0x2f94b
movl $0x1a, %edx
xorl %eax, %eax
callq 0x2050
jmp 0x269a1
leaq 0x8fc5(%rip), %rdi      # 0x2f95f
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0