// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movl %edi, %r15d
movslq %edi, %rbp
pushq $0x1
popq %r13
leaq 0xc6fc(%rip), %rbx      # 0x1020d
movl %edi, 0xc(%rsp)
cmpq %rbp, %r13
jge 0x3dfa
movq (%r14,%r13,8), %rax
cmpb $0x2d, (%rax)
jne 0x3dfa
leal 0x1(%r13), %ecx
cmpl %r15d, %ecx
jge 0x3e81
movzbl 0x1(%rax), %eax
leal -0x6b(%rax), %ecx
cmpl $0x9, %ecx
jbe 0x3b7a
cmpl $0x30, %eax
je 0x3cdd
cmpl $0x55, %eax
je 0x3da1
cmpl $0x62, %eax
je 0x3cb9
cmpl $0x64, %eax
jne 0x3e86
movq 0x8(%r14,%r13,8), %rax
movb (%rax), %al
movb %al, 0x13003(%rip)      # 0x16b78
jmp 0x3dec
leaq 0xc48f(%rip), %rdx      # 0x10010
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x8(%r14,%r13,8), %r12
movq %r12, %rdi
callq 0x30d0
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
pushq $0x1
popq %rdx
movl %edx, %r15d
cmpq %rcx, %rax
je 0x3c18
xorl %edx, %edx
cmpb $0x2c, (%r12,%rcx)
sete %dl
addl %edx, %r15d
incq %rcx
jmp 0x3ba9
movq 0x8(%r14,%r13,8), %rdi
callq 0x3320
movb %al, 0x126d4(%rip)      # 0x162a4
testb $-0x2, %al
je 0x3dec
jmp 0x3ede
movq 0x8(%r14,%r13,8), %rdi
callq 0x3320
movb %al, 0x126ae(%rip)      # 0x1629b
testb $-0x2, %al
je 0x3dec
jmp 0x3e9c
movq 0x8(%r14,%r13,8), %rdi
callq 0x3320
movl %eax, 0x126be(%rip)     # 0x162c8
cmpl $0x4, %eax
jb 0x3dec
jmp 0x3eb4
cmpl $0x3, %r15d
jb 0x3ef3
leal -0x2(%r15), %r12d
movb %r12b, 0x1265b(%rip)    # 0x16288
movq 0x1264c(%rip), %rdi     # 0x16280
callq 0x3230
movslq %r12d, %rdi
pushq $0x8
popq %rsi
callq 0x3100
movq %rax, 0x12635(%rip)     # 0x16280
movq 0x8(%r14,%r13,8), %rdi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x3320
movb %al, 0x12612(%rip)      # 0x16278
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x3260
cvttsd2si %xmm0, %eax
movl %eax, 0x125fa(%rip)     # 0x1627c
movl %r15d, %r15d
addq $-0x2, %r15
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x3dec
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x3260
movq 0x125d2(%rip), %rax     # 0x16280
movsd %xmm0, (%rax,%r12,8)
incq %r12
jmp 0x3c8c
movq 0x8(%r14,%r13,8), %rdi
leaq 0xc549(%rip), %rsi      # 0x1020e
callq 0x3330
movq %rax, %rdi
callq 0x3320
movl %eax, 0x125f8(%rip)     # 0x162d0
jmp 0x3dec
movq 0x8(%r14,%r13,8), %r15
movq %r15, %rdi
callq 0x30d0
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
pushq $0x1
popq %r12
cmpq %rcx, %rax
je 0x3d2c
movb (%r15,%rcx), %dl
xorl %esi, %esi
cmpb $0x2c, %dl
sete %sil
addl %esi, %r12d
andb $-0x21, %dl
addb $-0x41, %dl
incq %rcx
cmpb $0x19, %dl
ja 0x3cfa
leaq 0x125b0(%rip), %rdi     # 0x162d4
movq %r15, %rsi
callq 0x31a0
movq 0x12525(%rip), %rdi     # 0x16258
callq 0x3230
movslq %r12d, %r15
movq %r15, %rdi
pushq $0x8
popq %rsi
callq 0x3100
movq %rax, 0x1250b(%rip)     # 0x16258
movb %r15b, 0x1250c(%rip)    # 0x16260
movq 0x8(%r14,%r13,8), %rdi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x3260
movq 0x124e8(%rip), %rax     # 0x16258
movsd %xmm0, (%rax)
pushq $0x1
popq %r12
cmpq %r15, %r12
jge 0x3dec
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x3260
movq 0x124c2(%rip), %rax     # 0x16258
movsd %xmm0, (%rax,%r12,8)
incq %r12
jmp 0x3d78
movq 0x8(%r14,%r13,8), %rdi
movq %rbx, %rsi
callq 0x3330
movb (%rax), %al
movb %al, 0x12516(%rip)      # 0x162cc
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3330
movzbl (%rax), %eax
movb %al, 0x12504(%rip)      # 0x162cd
movzbl 0x124fc(%rip), %ecx     # 0x162cc
cmpl $0x72, %ecx
je 0x3dde
cmpl $0x67, %ecx
jne 0x3ec9
cmpl $0x67, %eax
je 0x3dec
cmpl $0x74, %eax
jne 0x3ec9
addq $0x2, %r13
movl 0xc(%rsp), %r15d
jmp 0x3b15
cmpb $0x0, 0x128d5(%rip)     # 0x166d6
jle 0x3e10
cmpl $0x0, 0x124b6(%rip)     # 0x162c0
jg 0x3f13
cmpq %rbp, %r13
jge 0x3e81
leal 0x1(%r13), %ebx
movl %r13d, %eax
movq (%r14,%rax,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x31a0
cmpl %r15d, %ebx
jge 0x3f28
addl $0x2, %r13d
movl %ebx, %eax
movq (%r14,%rax,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x31a0
cmpl %r15d, %r13d
jge 0x3e6b
movl %r13d, %eax
movq (%r14,%rax,8), %rsi
movq 0x10(%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31a0
andl $0x0, 0x12456(%rip)     # 0x162c8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3f49
movsbl %al, %edx
movq 0x12148(%rip), %rax     # 0x15fd8
movq (%rax), %rdi
leaq 0xc425(%rip), %rsi      # 0x102bf
jmp 0x3f07
movq 0x12135(%rip), %rax     # 0x15fd8
movq (%rax), %rcx
leaq 0xc327(%rip), %rdi      # 0x101d4
pushq $0x38
jmp 0x3f3b
movq 0x1211d(%rip), %rax     # 0x15fd8
movq (%rax), %rcx
leaq 0xc2b7(%rip), %rdi      # 0x1017c
pushq $0x57
jmp 0x3f3b
movq 0x12108(%rip), %rax     # 0x15fd8
movq (%rax), %rcx
leaq 0xc338(%rip), %rdi      # 0x10212
pushq $0x69
jmp 0x3f3b
movq 0x120f3(%rip), %rax     # 0x15fd8
movq (%rax), %rcx
leaq 0xc268(%rip), %rdi      # 0x10157
pushq $0x24
jmp 0x3f3b
movq 0x120de(%rip), %rax     # 0x15fd8
movq (%rax), %rdi
leaq 0xc378(%rip), %rsi      # 0x1027c
movl %r15d, %edx
xorl %eax, %eax
callq 0x32d0
callq 0x3f49
movq 0x120be(%rip), %rax     # 0x15fd8
movq (%rax), %rcx
leaq 0xc3b0(%rip), %rdi      # 0x102d4
pushq $0x2e
jmp 0x3f3b
movq 0x120a9(%rip), %rax     # 0x15fd8
movq (%rax), %rcx
leaq 0xc3ca(%rip), %rdi      # 0x10303
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x3400
callq 0x3f49