// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rsi, %rbx
leaq 0x1832(%rip), %rsi      # 0x16286
callq 0x3270
movq %rax, %r14
movl $0x400, %edi            # imm = 0x400
callq 0x32e0
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x133ad
movq %rbx, %rdi
callq 0x133a6
movq %rbx, 0x30(%rsp)
movsd 0xc8(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl %eax, 0x28(%rsp)
sbbl $-0x1, %eax
movl %eax, 0x20(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x32e0
movq %rax, %r15
movl $0x400, %ebp            # imm = 0x400
movq %rax, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdx
callq 0x3380
testq %rax, %rax
je 0x14d3f
xorl %r12d, %r12d
movl $0x40, %ebx
xorl %r13d, %r13d
movq %r14, 0x38(%rsp)
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
movl $0xa, %esi
callq 0x3230
testq %rax, %rax
jne 0x14b21
addl %ebp, %ebp
movslq %ebp, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x33c0
movq %rax, %r15
movq %rax, %rdi
callq 0x30e0
cltq
movq %r15, %rdi
addq %rax, %rdi
subl %eax, %ebx
movl %ebx, %esi
movq %r14, %rdx
callq 0x3380
testq %rax, %rax
jne 0x14adb
movq %r15, %rdi
leaq 0x177c(%rip), %rsi      # 0x162a7
callq 0x3360
testq %rax, %rax
je 0x14da3
movq %rax, %rbx
movl %r13d, 0x4(%rsp)
movq %r12, 0x48(%rsp)
movl %ebp, 0x24(%rsp)
movq %r15, 0x50(%rsp)
movq %rax, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3160
movsd %xmm0, 0x40(%rsp)
movq 0x18(%rsp), %rax
cmpq %rbx, %rax
je 0x14d9a
cmpb $0x0, (%rax)
jne 0x14d9a
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%rsp), %rbx
leal -0x2(%rbx), %eax
cmpl %eax, %r13d
jl 0x14ba4
addl %ebx, %ebx
movslq %ebx, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
callq 0x33c0
movq %rax, 0x8(%rsp)
xorl %edi, %edi
leaq 0x16f8(%rip), %rsi      # 0x162a5
callq 0x3360
movq %rax, %r14
xorl %edi, %edi
leaq 0x16e4(%rip), %rsi      # 0x162a2
callq 0x3360
testq %rax, %rax
je 0x14c9c
movq %rax, %rbp
movq %rbx, 0x10(%rsp)
callq 0x3040
movq %rax, %r15
movl $0x0, (%rax)
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
movl $0xa, %edx
callq 0x3310
movq %rax, %rbx
movl %r13d, 0x2c(%rsp)
movl %r13d, %r13d
shlq $0x4, %r13
movq 0x8(%rsp), %rcx
movl %ebx, (%rcx,%r13)
movq 0x18(%rsp), %rax
cmpq %r14, %rax
je 0x14d88
cmpl $0x0, (%r15)
jne 0x14d88
cmpb $0x0, (%rax)
jne 0x14d88
cmpl %ebx, %r12d
jge 0x14d88
addq %rcx, %r13
movl $0x0, (%r15)
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3160
movsd %xmm0, 0x8(%r13)
movq 0x18(%rsp), %rax
cmpq %rbp, %rax
je 0x14d91
cmpl $0x0, (%r15)
jne 0x14d91
movsbl (%rax), %edi
testl %edi, %edi
je 0x14c7f
callq 0x3060
testl %eax, %eax
je 0x14d91
xorl %eax, %eax
movl 0x28(%rsp), %ecx
cmpl %ecx, (%r13)
setle %al
movl 0x2c(%rsp), %r13d
addl %eax, %r13d
movl %ebx, %r12d
jmp 0x14b7f
movq 0x30(%rsp), %rax
movsd 0xc8(%rax), %xmm0
ucomisd 0x135f(%rip), %xmm0     # 0x16010
jae 0x14cba
movq 0x8(%rsp), %rsi
jmp 0x14cd6
movl %r13d, %eax
shlq $0x4, %rax
movq 0x8(%rsp), %rsi
movl 0x20(%rsp), %ecx
movl %ecx, (%rsi,%rax)
movsd %xmm0, 0x8(%rsi,%rax)
incl %r13d
movl %r13d, %eax
movl 0x4(%rsp), %r13d
cltq
shlq $0x4, %rax
movl $0xffffffff, (%rsi,%rax) # imm = 0xFFFFFFFF
movq 0x30(%rsp), %rdi
callq 0x128be
cmpeqsd 0x40(%rsp), %xmm0
movq %xmm0, %rax
movq 0x48(%rsp), %r12
subl %eax, %r12d
incl %r13d
movq 0x50(%rsp), %r15
movq %r15, %rdi
movl 0x24(%rsp), %ebp
movl %ebp, %esi
movq 0x38(%rsp), %r14
movq %r14, %rdx
callq 0x3380
testq %rax, %rax
jne 0x14ad6
cvtsi2sd %r12d, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
jmp 0x14d47
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %r15, %rdi
callq 0x3280
movq 0x8(%rsp), %rdi
callq 0x3280
movq %r14, %rdi
callq 0x31f0
movsd 0x10(%rsp), %xmm0
mulsd 0x129f(%rip), %xmm0     # 0x16018
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %edi
callq 0x39b8
movl 0x4(%rsp), %edi
callq 0x3993
movl 0x4(%rsp), %edi
callq 0x39dd
movl %r13d, %edi
callq 0x3a02
nop