// File: /workspace/github_cpp_makefile_star2_O2/qmmlib[P]QmmLibrary/do_predict_iter.cpp
// Function: do_predict_iter(char*, model const*)
double do_predict_iter(char* file_name, const struct model* model_)
{
	FILE *input = fopen(file_name,"r");
	int max_nr_attr = 64;
	struct feature_node *x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node));
	
	int correct = 0;
	int total = 0;
	double error = 0;
	double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;
	
	
	int nr_class=get_nr_class(model_);
	int j, n;
	int nr_feature=get_nr_feature(model_);
	if(model_->bias>=0)
		n=nr_feature+1;
	else
		n=nr_feature;

	int max_line_len = 1024;
	char * line = (char *)malloc(max_line_len*sizeof(char));
	int len;
	while(fgets(line,max_line_len,input) != NULL)
	{
		while(strrchr(line,'\n') == NULL)
		{
			max_line_len *= 2;
			line = (char *) realloc(line,max_line_len);
			len = (int) strlen(line);
			if(fgets(line+len,max_line_len-len,input) == NULL)
				break;
		}
	
		int i = 0;
		double target_label, predict_label;
		char *idx, *val, *label, *endptr;
		int inst_max_index = 0; // strtol gives 0 if wrong format

		label = strtok(line," \t\n");
		if(label == NULL) // empty line
		{
			fprintf(stderr,"Wrong input format at line %d\n", total+1);
			exit(1);
		}

		target_label = strtod(label,&endptr);
		if(endptr == label || *endptr != '\0')
		{
			fprintf(stderr,"Wrong input format at line %d\n", total+1);
			exit(1);
		}

		while(1)
		{
			if(i>=max_nr_attr-2)	// need one more for index = -1
			{
				max_nr_attr *= 2;
				x = (struct feature_node *) realloc(x,max_nr_attr*sizeof(struct feature_node));
			}

			idx = strtok(NULL,":");
			val = strtok(NULL," \t");

			if(val == NULL)
				break;
			errno = 0;
			x[i].index = (int) strtol(idx,&endptr,10);
			if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index)
			{
				fprintf(stderr,"Wrong input format at line %d\n", total+1);
				exit(1);
			}
			else
				inst_max_index = x[i].index;

			errno = 0;
			x[i].value = strtod(val,&endptr);
			if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr)))
			{
				fprintf(stderr,"Wrong input format at line %d\n", total+1);
				exit(1);
			}

			// feature indices larger than those in training are not used
			if(x[i].index <= nr_feature)
				++i;
		}

		if(model_->bias>=0)
		{
			x[i].index = n;
			x[i].value = model_->bias;
			i++;
		}
		x[i].index = -1;

		predict_label = predict(model_,x);

		if(predict_label == target_label)
			++correct;
		error += (predict_label-target_label)*(predict_label-target_label);
                sump += predict_label;
                sumt += target_label;
                sumpp += predict_label*predict_label;
                sumtt += target_label*target_label;
                sumpt += predict_label*target_label;
                ++total;
	}
	free(line);
	free(x);
	fclose(input);
	
	return ((double)correct/(double)total)*100.0f;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
leaq 0x1a4c(%rip), %rsi      # 0x152b4
callq 0x32a0
movq %rax, 0x30(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x3320
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x121c2
movq %rbx, %rdi
callq 0x121bb
movq %rbx, 0x50(%rsp)
movsd 0xc8(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl %eax, 0x40(%rsp)
sbbl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x3320
movq %rax, %r12
pushq $0x40
popq %r15
xorl %ebx, %ebx
movl $0x400, 0xc(%rsp)       # imm = 0x400
movl $0x0, 0x24(%rsp)
movq %r12, %rdi
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %rdx
callq 0x33d0
testq %rax, %rax
je 0x13b18
movq %r12, %rdi
pushq $0xa
popq %rsi
callq 0x3270
testq %rax, %rax
jne 0x1393e
movl 0xc(%rsp), %eax
addl %eax, %eax
movl %eax, 0xc(%rsp)
movslq %eax, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x3410
movq %rax, %r12
movq %rax, %rdi
callq 0x30f0
cltq
movq %r12, %rdi
addq %rax, %rdi
subl %eax, %r14d
movl %r14d, %esi
movq 0x30(%rsp), %rdx
callq 0x33d0
testq %rax, %rax
jne 0x138ee
movq %r12, %rdi
leaq 0x198d(%rip), %rsi      # 0x152d5
callq 0x33b0
testq %rax, %rax
je 0x13b80
movq %rax, %r14
movq %rbx, 0x28(%rsp)
movq %rax, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3180
movsd %xmm0, 0x58(%rsp)
movq 0x18(%rsp), %rax
cmpq %r14, %rax
je 0x13b59
cmpb $0x0, (%rax)
jne 0x13b59
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r12, 0x48(%rsp)
leal -0x2(%r15), %eax
cmpl %eax, %r14d
jl 0x139b4
addl %r15d, %r15d
movslq %r15d, %rsi
shlq $0x4, %rsi
movq 0x10(%rsp), %rdi
callq 0x3410
movq %rax, 0x10(%rsp)
xorl %edi, %edi
leaq 0x1916(%rip), %rsi      # 0x152d3
callq 0x33b0
movq %rax, %r12
xorl %edi, %edi
leaq 0x1902(%rip), %rsi      # 0x152d0
callq 0x33b0
movl %r14d, %r13d
testq %rax, %rax
je 0x13aa9
movq %rax, %rbp
movl %r14d, 0x44(%rsp)
movq %r15, 0x60(%rsp)
callq 0x3040
movq %rax, %r14
andl $0x0, (%rax)
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x3360
movq %rax, %r15
shlq $0x4, %r13
movq 0x10(%rsp), %rcx
movl %r15d, (%rcx,%r13)
movq 0x18(%rsp), %rax
cmpq %r12, %rax
je 0x13b59
cmpl $0x0, (%r14)
jne 0x13b59
cmpb $0x0, (%rax)
jne 0x13b59
cmpl %r15d, %ebx
jge 0x13b59
addq %rcx, %r13
andl $0x0, (%r14)
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3180
movsd %xmm0, 0x8(%r13)
movq 0x18(%rsp), %rax
cmpq %rbp, %rax
je 0x13b59
cmpl $0x0, (%r14)
jne 0x13b59
movsbl (%rax), %edi
testl %edi, %edi
je 0x13a87
callq 0x3060
testl %eax, %eax
je 0x13b59
xorl %eax, %eax
movl 0x40(%rsp), %ecx
cmpl %ecx, (%r13)
setle %al
movl 0x44(%rsp), %r14d
addl %eax, %r14d
movl %r15d, %ebx
movq 0x60(%rsp), %r15
jmp 0x13992
movq 0x50(%rsp), %rax
movsd 0xc8(%rax), %xmm0
ucomisd 0x1582(%rip), %xmm0     # 0x15040
jae 0x13ac7
movq 0x10(%rsp), %rsi
jmp 0x13ae2
shlq $0x4, %r13
movq 0x10(%rsp), %rsi
movl 0x3c(%rsp), %eax
movl %eax, (%rsi,%r13)
movsd %xmm0, 0x8(%rsi,%r13)
incl %r14d
movq 0x48(%rsp), %r12
movslq %r14d, %rax
shlq $0x4, %rax
orl $-0x1, (%rsi,%rax)
movq 0x50(%rsp), %rdi
callq 0x116de
cmpeqsd 0x58(%rsp), %xmm0
movq %xmm0, %rax
subl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rbx
incl %ebx
jmp 0x138d4
movq %r12, %rdi
callq 0x32b0
movq 0x10(%rsp), %rdi
callq 0x32b0
movq 0x30(%rsp), %rdi
callq 0x3220
cvtsi2sdl 0x24(%rsp), %xmm0
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
mulsd 0x14fe(%rip), %xmm0     # 0x15048
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6478(%rip), %rax      # 0x19fd8
movq (%rax), %rdi
movq 0x28(%rsp), %rdx
incl %edx
leaq 0x15e7(%rip), %rsi      # 0x15158
xorl %eax, %eax
callq 0x3370
pushq $0x1
popq %rdi
callq 0x32c0
movq 0x6451(%rip), %rax      # 0x19fd8
movq (%rax), %rdi
incl %ebx
leaq 0x15c5(%rip), %rsi      # 0x15158
movl %ebx, %edx
jmp 0x13b71
nop