// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/predicthmmst.cpp
// Function: parse_arguments(int, char**, char*, char*, char*)
void parse_arguments(int argc, char **argv, char *input_file_name, char *model_file_name, char *predict_file_name) {
	// parse command line options, starting from 1 (0 is path to executable)
	// go in pairs, looking at whether first in pair starts with '-', if not, stop parsing arguments
	int i, n;
    char * ch;
	for(i=1;i<argc;i++)
	{
		if(argv[i][0] != '-') break; // end of options stop parsing
		if(++i>=argc)
			exit_with_help();
		switch(argv[i-1][1])
		{
			case 'q':
				task.quiet = (NPAR)atoi(argv[i]);
				if(task.quiet!=0 && task.quiet!=1) {
					fprintf(stderr,"ERROR! Quiet param should be 0 or 1\n");
					exit_with_help();
				}
				break;
            case  'd':
				task.multiskill = argv[i][0]; // just grab first character (later, maybe several)
                break;
			case 'b':
                task.binaryinput = atoi( strtok(argv[i],"\t\n\r"));
                break;
            case  'p':
				task.predictions = atoi(argv[i]);
				if(task.predictions<0 || task.predictions>3) {
					fprintf(stderr,"a flag of whether to report predictions for training data (-p) should be 0, 1, 2, or 3\n");
					exit_with_help();
				}
                break;
			case 't':
				task.sliced = (NPAR)atoi(argv[i]);
				if(task.sliced!=0 && task.sliced!=1) {
					fprintf(stderr,"ERROR! Time parameter should be either 0 (off) or 1(on)\n");
					exit_with_help();
				}
				break;
            case  'U':
                task.update_known = *strtok(argv[i],",\t\n\r");
                ch = strtok(NULL, ",\t\n\r");
                task.update_unknown = ch[0];
                
                if( (task.update_known!='r' && task.update_known!='g') ||
                   (task.update_unknown!='t' && task.update_unknown!='g') ) {
                    fprintf(stderr,"specification of how probabilities of states should be updated (-U) is incorrect, it sould be r|g[,t|g] \n");
                    exit_with_help();
                }
                break;
            case '0': // what was init_params for 'trainhmm' is now default_params for 'predicthmm'
                int len;
                len = (int)strlen( argv[i] );
                // count delimiters
                n = 1; // start with 1
                for(int j=0;j<len;j++) {
                    n += (argv[i][j]==',')?1:0;
                    if( (argv[i][j] >= 'a' && argv[i][j] <= 'z') || (argv[i][j] >= 'A' && argv[i][j] <= 'Z') ) {
                        strcpy(task.initfile, argv[i]);
                        break;
                    }
                }
                // init parameters
                if(task.init_param_values!=NULL) free(task.init_param_values);
                task.init_param_values = Calloc(NUMBER, (size_t)n);
                task.init_param_values_n = (NPAR)n;
                // read params and write to params
                task.init_param_values[0] = atof( strtok(argv[i],",\t\n\r") );
                for(int j=1; j<n; j++) {
                    task.init_param_values[j] = atof( strtok(NULL,",\t\n\r") );
                }
                break;
            case 'k':
                // ELO parameters specified as a comma-delimited list of values, first is an integer, the rest are real
                len = (int)strlen( argv[i] );
                // count delimiters
                n = 1; // start with 1
                for(int j=0;j<len;j++)
                    n += (NPAR)((argv[i][j]==',')?1:0);
                if( n < 3 ) {
                    fprintf(stderr,"To use Elo, one needs at lest three parameters; %d were specified\n",n);
                    exit_with_help();
                }
                // init params
                task.elo_param_values_n = (NPAR)(n-2); // first parameter is Elo type
                if(task.elo_param_values!=NULL) free(task.elo_param_values);
                task.elo_param_values = Calloc(NUMBER, (size_t)(n-2));
                // read params and write to params
                task.elo_type = (NPAR)atoi( strtok(argv[i],",\t\n\r") );
                task.elo_scope = (NCAT)atof( strtok(NULL,",\t\n\r") );
                for(int j=2; j<n; j++) {
                    task.elo_param_values[j-2] = atof( strtok(NULL,",\t\n\r") );
                }
                break;
			default:
				fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
				exit_with_help();
				break;
		}
	}   
    if(task.cv_target_obs>0 && task.metrics>0) { // correct for 0-start coding
        fprintf(stderr,"values for -v and -m cannot be both non-zeros\n");
        exit_with_help();
    }
	
	// next argument should be input (training) file name
	if(i>=argc) // if not
		exit_with_help(); // leave
	
	strcpy(input_file_name, argv[i++]); // copy and advance
	
	if(i>=argc) { // no model file name specified
        fprintf(stderr,"no model file specified\n");
		exit_with_help(); // leave
	}
	else {
		strcpy(model_file_name,argv[i++]); // copy and advance
		if(i>=argc) {// no prediction file name specified
			//strcpy(predict_file_name,"predict_hmm.txt"); // the predict file too
            task.predictions = 0;
        }
		else {
            // task.predictions = 1;
			strcpy(predict_file_name,argv[i]);
        }
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movq %rcx, %r13
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movl %edi, %ebp
movl $0x1, %r12d
cmpl $0x2, %edi
setge %al
jl 0x36c8
movq 0x8(%r14), %rcx
cmpb $0x2d, (%rcx)
jne 0x36c8
movq %r13, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movl %ebp, 0xc(%rsp)
movl %ebp, %ebp
movl $0x1, %r12d
leaq 0xde86(%rip), %r13      # 0x1120d
leaq 0x1(%r12), %rax
cmpq %rbp, %rax
jae 0x3749
movzbl 0x1(%rcx), %edi
cmpl $0x63, %edi
jg 0x33dd
cmpl $0x30, %edi
je 0x3433
cmpl $0x55, %edi
je 0x3489
cmpl $0x62, %edi
jne 0x376e
movq 0x8(%r14,%r12,8), %rdi
leaq 0xde49(%rip), %rsi      # 0x1120e
callq 0x2320
movq %rax, %rdi
callq 0x2310
movl %eax, 0x13ed8(%rip)     # 0x172b0
jmp 0x36a1
leal -0x6b(%rdi), %eax
cmpl $0x9, %eax
ja 0x34dd
leaq 0xdc20(%rip), %rcx      # 0x11010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14,%r12,8), %r15
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
jle 0x35f3
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %ebx
xorl %ecx, %ecx
xorl %edx, %edx
cmpb $0x2c, (%r15,%rcx)
sete %dl
addl %edx, %ebx
incq %rcx
cmpq %rcx, %rax
jne 0x341a
jmp 0x35f8
movq 0x8(%r14,%r12,8), %rbx
movq %rbx, %rdi
callq 0x20e0
testl %eax, %eax
jle 0x3550
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %r15d
xorl %ecx, %ecx
movl %r15d, %edx
movb (%rbx,%rcx), %sil
xorl %r15d, %r15d
cmpb $0x2c, %sil
sete %r15b
addl %edx, %r15d
andb $-0x21, %sil
addb $-0x41, %sil
cmpb $0x19, %sil
jbe 0x3558
incq %rcx
cmpq %rcx, %rax
jne 0x3455
jmp 0x3567
movq 0x8(%r14,%r12,8), %rdi
movq %r13, %rsi
callq 0x2320
movb (%rax), %al
movb %al, 0x13e0e(%rip)      # 0x172ac
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movzbl (%rax), %eax
movb %al, 0x13dfc(%rip)      # 0x172ad
movzbl 0x13df4(%rip), %ecx     # 0x172ac
cmpl $0x72, %ecx
je 0x34c6
cmpl $0x67, %ecx
jne 0x3769
cmpl $0x67, %eax
je 0x36a1
cmpl $0x74, %eax
je 0x36a1
jmp 0x3769
cmpl $0x64, %edi
jne 0x376e
movq 0x8(%r14,%r12,8), %rax
movb (%rax), %al
movb %al, 0x14665(%rip)      # 0x17b58
jmp 0x36a1
movq 0x8(%r14,%r12,8), %rdi
callq 0x2310
movl %eax, 0x13da0(%rip)     # 0x172a8
cmpl $0x4, %eax
jb 0x36a1
jmp 0x377d
movq 0x8(%r14,%r12,8), %rdi
callq 0x2310
movb %al, 0x13d55(%rip)      # 0x1727b
testb $-0x2, %al
je 0x36a1
jmp 0x3773
movq 0x8(%r14,%r12,8), %rdi
callq 0x2310
movb %al, 0x13d41(%rip)      # 0x17284
testb $-0x2, %al
je 0x36a1
jmp 0x3778
movl $0x1, %r15d
jmp 0x3567
leaq 0x13d55(%rip), %rdi     # 0x172b4
movq %rbx, %rsi
callq 0x21a0
movq 0x13cca(%rip), %rdi     # 0x17238
testq %rdi, %rdi
je 0x3578
callq 0x2240
movslq %r15d, %rbx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x13ca9(%rip)     # 0x17238
movb %bl, 0x13cab(%rip)      # 0x17240
movq 0x8(%r14,%r12,8), %rdi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
movq 0x13c87(%rip), %rax     # 0x17238
movsd %xmm0, (%rax)
cmpl $0x2, %ebx
jl 0x36a1
movl %r15d, %ebx
movl $0x1, %r15d
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
movq 0x13c58(%rip), %rax     # 0x17238
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq %r15, %rbx
jne 0x35c7
jmp 0x36a1
movl $0x1, %ebx
cmpl $0x3, %ebx
jb 0x3782
leal -0x2(%rbx), %r15d
movb %r15b, 0x13c5c(%rip)    # 0x17268
movq 0x13c4d(%rip), %rdi     # 0x17260
testq %rdi, %rdi
je 0x361d
callq 0x2240
movslq %r15d, %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x13c2f(%rip)     # 0x17260
movq 0x8(%r14,%r12,8), %rdi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2310
movb %al, 0x13c0c(%rip)      # 0x17258
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
cvttsd2si %xmm0, %eax
movl %eax, 0x13bf4(%rip)     # 0x1725c
cmpl $0x4, %ebx
movl $0x3, %eax
cmovbl %eax, %ebx
addq $-0x2, %rbx
xorl %r15d, %r15d
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
movq 0x13bcd(%rip), %rax     # 0x17260
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq %r15, %rbx
jne 0x367a
addq $0x2, %r12
cmpq %rbp, %r12
setb %al
jae 0x36ba
movq (%r14,%r12,8), %rcx
cmpb $0x2d, (%rcx)
je 0x3387
movq 0x18(%rsp), %r15
movl 0xc(%rsp), %ebp
movq 0x10(%rsp), %r13
cmpb $0x0, 0x13fe7(%rip)     # 0x176b6
jle 0x36de
cmpl $0x0, 0x13bc8(%rip)     # 0x172a0
jg 0x375f
testb %al, %al
je 0x3749
leal 0x1(%r12), %ebx
movl %r12d, %eax
movq (%r14,%rax,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x21a0
cmpl %ebp, %ebx
jge 0x3764
addl $0x2, %r12d
movl %ebx, %eax
movq (%r14,%rax,8), %rsi
movq %r13, %rdi
callq 0x21a0
cmpl %ebp, %r12d
jge 0x3730
movl %r12d, %eax
movq (%r14,%rax,8), %rsi
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21a0
movl $0x0, 0x13b6e(%rip)     # 0x172a8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdbe3(%rip), %rdi      # 0x11333
callq 0x22f0
movl $0x1, %edi
callq 0x2250
callq 0x255d
callq 0x252c
callq 0x2468
callq 0x258e
callq 0x2499
callq 0x24fb
callq 0x24ca
movl %ebx, %edi
callq 0x2439