// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
cmpl $0x2, %edi
jl 0x3798
movq %rsi, %r15
movq %rdx, 0x20(%rsp)
movl %edi, 0xc(%rsp)
movl %edi, %edx
movl $0x1, %r12d
leaq 0xee6e(%rip), %r13      # 0x1220d
movq %rsi, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
movq (%r15,%r12,8), %rax
movb (%rax), %bpl
cmpb $0x2d, %bpl
jne 0x36de
leaq 0x1(%r12), %rcx
cmpq %rdx, %rcx
jae 0x37af
movzbl 0x1(%rax), %edi
cmpl $0x63, %edi
jg 0x3410
cmpl $0x30, %edi
je 0x3510
cmpl $0x55, %edi
je 0x3566
cmpl $0x62, %edi
jne 0x3776
movq 0x8(%r15,%r12,8), %rdi
leaq 0xee16(%rip), %rsi      # 0x1220e
callq 0x2320
movq %rax, %rdi
callq 0x2310
movl %eax, 0x14ea5(%rip)     # 0x182b0
jmp 0x36cc
leal -0x6b(%rdi), %eax
cmpl $0x9, %eax
ja 0x35ba
leaq 0xebed(%rip), %rcx      # 0x12010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r15,%r12,8), %rbx
movq %rbx, %rdi
callq 0x20e0
testl %eax, %eax
jle 0x378a
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %r15d
xorl %ecx, %ecx
xorl %edx, %edx
cmpb $0x2c, (%rbx,%rcx)
sete %dl
addl %edx, %r15d
incq %rcx
cmpq %rcx, %rax
jne 0x344e
cmpl $0x3, %r15d
jb 0x3790
leal -0x2(%r15), %ebx
movb %bl, 0x14df2(%rip)      # 0x18268
movq 0x14de3(%rip), %rdi     # 0x18260
testq %rdi, %rdi
je 0x3487
callq 0x2240
movslq %ebx, %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x14dc5(%rip)     # 0x18260
movq 0x10(%rsp), %r14
movq 0x8(%r14,%r12,8), %rdi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2310
movb %al, 0x14d9d(%rip)      # 0x18258
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
cvttsd2si %xmm0, %eax
movl %eax, 0x14d85(%rip)     # 0x1825c
movl %r15d, %ebx
movq %r14, %r15
addq $-0x2, %rbx
xorl %r14d, %r14d
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
movq 0x14d63(%rip), %rax     # 0x18260
movsd %xmm0, (%rax,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x34e4
jmp 0x36cc
movq 0x8(%r15,%r12,8), %r15
movq %r15, %rdi
callq 0x20e0
testl %eax, %eax
jle 0x362d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %r14d
xorl %ecx, %ecx
movl %r14d, %edx
movb (%r15,%rcx), %sil
xorl %r14d, %r14d
cmpb $0x2c, %sil
sete %r14b
addl %edx, %r14d
andb $-0x21, %sil
addb $-0x41, %sil
cmpb $0x19, %sil
jbe 0x3635
incq %rcx
cmpq %rcx, %rax
jne 0x3532
jmp 0x3644
movq 0x8(%r15,%r12,8), %rdi
movq %r13, %rsi
callq 0x2320
movb (%rax), %al
movb %al, 0x14d31(%rip)      # 0x182ac
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movzbl (%rax), %eax
movb %al, 0x14d1f(%rip)      # 0x182ad
movzbl 0x14d17(%rip), %ecx     # 0x182ac
cmpl $0x72, %ecx
je 0x35a3
cmpl $0x67, %ecx
jne 0x3771
cmpl $0x67, %eax
je 0x36cc
cmpl $0x74, %eax
je 0x36cc
jmp 0x3771
cmpl $0x64, %edi
jne 0x3776
movq 0x8(%r15,%r12,8), %rax
movb (%rax), %al
movb %al, 0x15588(%rip)      # 0x18b58
jmp 0x36cc
movq 0x8(%r15,%r12,8), %rdi
callq 0x2310
movl %eax, 0x14cc3(%rip)     # 0x182a8
cmpl $0x4, %eax
jb 0x36cc
jmp 0x3785
movq 0x8(%r15,%r12,8), %rdi
callq 0x2310
movb %al, 0x14c78(%rip)      # 0x1827b
testb $-0x2, %al
je 0x36cc
jmp 0x377b
movq 0x8(%r15,%r12,8), %rdi
callq 0x2310
movb %al, 0x14c64(%rip)      # 0x18284
testb $-0x2, %al
je 0x36cc
jmp 0x3780
movl $0x1, %r14d
jmp 0x3644
leaq 0x14c78(%rip), %rdi     # 0x182b4
movq %r15, %rsi
callq 0x21a0
movq 0x14bed(%rip), %rdi     # 0x18238
testq %rdi, %rdi
je 0x3655
callq 0x2240
movslq %r14d, %rbx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x14bcc(%rip)     # 0x18238
movb %bl, 0x14bce(%rip)      # 0x18240
movq 0x10(%rsp), %r15
movq 0x8(%r15,%r12,8), %rdi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
movq 0x14ba5(%rip), %rax     # 0x18238
movsd %xmm0, (%rax)
cmpl $0x2, %ebx
jl 0x36cc
movl %r14d, %ebx
movl $0x1, %r14d
xorl %edi, %edi
movq %r13, %rsi
callq 0x2320
movq %rax, %rdi
callq 0x2270
movq 0x14b7a(%rip), %rax     # 0x18238
movsd %xmm0, (%rax,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x36a5
addq $0x2, %r12
movq 0x30(%rsp), %rdx
cmpq %rdx, %r12
jb 0x33a9
cmpb $0x0, 0x14fd1(%rip)     # 0x186b6
jle 0x36f4
cmpl $0x0, 0x14bb2(%rip)     # 0x182a0
jg 0x37aa
cmpb $0x2d, %bpl
je 0x37af
leal 0x1(%r12), %ebx
movl %r12d, %eax
movq (%r15,%rax,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x21a0
movl 0xc(%rsp), %ebp
cmpl %ebp, %ebx
jge 0x37c5
addl $0x2, %r12d
movl %ebx, %eax
movq (%r15,%rax,8), %rsi
movq 0x28(%rsp), %rdi
callq 0x21a0
cmpl %ebp, %r12d
jge 0x3758
movl %r12d, %eax
movq (%r15,%rax,8), %rsi
movq 0x18(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21a0
movl $0x0, 0x14b46(%rip)     # 0x182a8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2468
callq 0x258e
callq 0x2499
callq 0x24fb
callq 0x24ca
movl $0x1, %r15d
movl %r15d, %edi
callq 0x2439
cmpb $0x0, 0x14f17(%rip)     # 0x186b6
jle 0x37af
cmpl $0x0, 0x14af8(%rip)     # 0x182a0
jle 0x37af
callq 0x255d
leaq 0xeb7d(%rip), %rdi      # 0x12333
callq 0x22f0
movl $0x1, %edi
callq 0x2250
callq 0x252c