// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/predicthmm.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':
				param.quiet = (NPAR)atoi(argv[i]);
				if(param.quiet!=0 && param.quiet!=1) {
					fprintf(stderr,"ERROR! Quiet param should be 0 or 1\n");
					exit_with_help();
				}
				break;
            case  'd':
				param.multiskill = argv[i][0]; // just grab first character (later, maybe several)
                break;
			case 'b':
                param.binaryinput = atoi( strtok(argv[i],"\t\n\r"));
                break;
            case  'p':
				param.predictions = atoi(argv[i]);
				if(param.predictions<0 || param.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':
				param.sliced = (NPAR)atoi(argv[i]);
				if(param.sliced!=0 && param.sliced!=1) {
					fprintf(stderr,"ERROR! Time parameter should be either 0 (off) or 1(on)\n");
					exit_with_help();
				}
				break;
            case  'U':
                param.update_known = *strtok(argv[i],",\t\n\r");
                ch = strtok(NULL, ",\t\n\r");
                param.update_unknown = ch[0];
                
                if( (param.update_known!='r' && param.update_known!='g') ||
                   (param.update_unknown!='t' && param.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(param.initfile, argv[i]);
                        break;
                    }
                }
                // init parameters
                if(param.init_params!=NULL) free(param.init_params);
                param.init_params = Calloc(NUMBER, (size_t)n);
                param.init_params_n = (NPAR)n;
                // read params and write to params
                param.init_params[0] = atof( strtok(argv[i],",\t\n\r") );
                for(int j=1; j<n; j++) {
                    param.init_params[j] = atof( strtok(NULL,",\t\n\r") );
                }
                break;
			default:
				fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
				exit_with_help();
				break;
		}
	}   
    if(param.cv_target_obs>0 && param.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
            param.predictions = 0;
        }
		else {
            // param.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, %ebp
movslq %edi, %r12
pushq $0x1
popq %r15
leaq 0x273bc(%rip), %rbx     # 0x2c1f0
movl %edi, 0xc(%rsp)
cmpq %r12, %r15
jge 0x502d
movq (%r14,%r15,8), %rax
cmpb $0x2d, (%rax)
jne 0x502d
leal 0x1(%r15), %ecx
cmpl %ebp, %ecx
jge 0x50b3
movzbl 0x1(%rax), %eax
cmpl $0x30, %eax
je 0x4f68
cmpl $0x55, %eax
je 0x4f10
cmpl $0x62, %eax
je 0x4eaa
cmpl $0x64, %eax
je 0x4ece
cmpl $0x70, %eax
je 0x4edd
cmpl $0x74, %eax
je 0x4ef7
cmpl $0x71, %eax
jne 0x50b8
movq 0x8(%r14,%r15,8), %rdi
callq 0x43b0
movb %al, 0x34457(%rip)      # 0x392f4
testb $-0x2, %al
je 0x4f5b
jmp 0x50d8
movq 0x8(%r14,%r15,8), %rdi
leaq 0x2733b(%rip), %rsi     # 0x2c1f1
callq 0x43c0
movq %rax, %rdi
callq 0x43b0
movl %eax, 0x34457(%rip)     # 0x39320
jmp 0x4f5b
movq 0x8(%r14,%r15,8), %rax
movb (%rax), %al
movb %al, 0x34cf5(%rip)      # 0x39bd0
jmp 0x4f5b
movq 0x8(%r14,%r15,8), %rdi
callq 0x43b0
movl %eax, 0x3442b(%rip)     # 0x39318
cmpl $0x4, %eax
jb 0x4f5b
jmp 0x5102
movq 0x8(%r14,%r15,8), %rdi
callq 0x43b0
movb %al, 0x343e4(%rip)      # 0x392eb
testb $-0x2, %al
je 0x4f5b
jmp 0x50ed
movq 0x8(%r14,%r15,8), %rdi
movq %rbx, %rsi
callq 0x43c0
movb (%rax), %al
movb %al, 0x343f7(%rip)      # 0x3931c
xorl %edi, %edi
movq %rbx, %rsi
callq 0x43c0
movzbl (%rax), %eax
movb %al, 0x343e5(%rip)      # 0x3931d
movzbl 0x343dd(%rip), %ecx     # 0x3931c
cmpl $0x72, %ecx
je 0x4f4d
cmpl $0x67, %ecx
jne 0x5117
cmpl $0x67, %eax
je 0x4f5b
cmpl $0x74, %eax
jne 0x5117
addq $0x2, %r15
movl 0xc(%rsp), %ebp
jmp 0x4e38
movq 0x8(%r14,%r15,8), %r13
movq %r13, %rdi
callq 0x40e0
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
pushq $0x1
popq %rbp
cmpq %rcx, %rax
je 0x4fb6
movb (%r13,%rcx), %dl
xorl %esi, %esi
cmpb $0x2c, %dl
sete %sil
addl %esi, %ebp
andb $-0x21, %dl
addb $-0x41, %dl
incq %rcx
cmpb $0x19, %dl
ja 0x4f84
leaq 0x34376(%rip), %rdi     # 0x39324
movq %r13, %rsi
callq 0x41f0
movq 0x34303(%rip), %rdi     # 0x392c0
callq 0x4290
movslq %ebp, %r13
movq %r13, %rdi
pushq $0x8
popq %rsi
callq 0x4120
movq %rax, 0x342e9(%rip)     # 0x392c0
movb %r13b, 0x342ea(%rip)    # 0x392c8
movq 0x8(%r14,%r15,8), %rdi
movq %rbx, %rsi
callq 0x43c0
movq %rax, %rdi
callq 0x42c0
movq 0x342c6(%rip), %rax     # 0x392c0
movsd %xmm0, (%rax)
pushq $0x1
popq %rbp
cmpq %r13, %rbp
jge 0x4f5b
xorl %edi, %edi
movq %rbx, %rsi
callq 0x43c0
movq %rax, %rdi
callq 0x42c0
movq 0x3429d(%rip), %rax     # 0x392c0
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
jmp 0x5001
cmpb $0x0, 0x346f2(%rip)     # 0x39726
jle 0x5043
cmpl $0x0, 0x342d3(%rip)     # 0x39310
jg 0x512c
cmpq %r12, %r15
jge 0x50b3
leal 0x1(%r15), %ebx
movl %r15d, %eax
movq (%r14,%rax,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x41f0
cmpl %ebp, %ebx
jge 0x5141
addl $0x2, %r15d
movl %ebx, %eax
movq (%r14,%rax,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x41f0
cmpl %ebp, %r15d
jge 0x509d
movl %r15d, %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 0x41f0
andl $0x0, 0x34274(%rip)     # 0x39318
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5162
movsbl %al, %edx
movq 0x33f16(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x27193(%rip), %rsi     # 0x2c25f
xorl %eax, %eax
callq 0x4350
callq 0x5162
movq 0x33ef9(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x27051(%rip), %rdi     # 0x2c13a
pushq $0x24
jmp 0x5154
movq 0x33ee4(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x270b9(%rip), %rdi     # 0x2c1b7
pushq $0x38
jmp 0x5154
movq 0x33ecf(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x2704c(%rip), %rdi     # 0x2c15f
pushq $0x57
jmp 0x5154
movq 0x33eba(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x270cd(%rip), %rdi     # 0x2c1f5
pushq $0x69
jmp 0x5154
movq 0x33ea5(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x27137(%rip), %rdi     # 0x2c274
pushq $0x2e
jmp 0x5154
movq 0x33e90(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x27151(%rip), %rdi     # 0x2c2a3
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
callq 0x5162