// File: /workspace/github_cpp_makefile_star2_O3/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 0x1b74(%rip), %rsi      # 0x182b4
callq 0x3280
movq %rax, %r14
movl $0x400, %edi            # imm = 0x400
callq 0x32f0
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x14ff6
movq %rbx, %rdi
callq 0x14fef
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 0x32f0
movq %rax, %r15
movl $0x400, %ebp            # imm = 0x400
movq %rax, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdx
callq 0x3390
testq %rax, %rax
je 0x16a2b
xorl %r12d, %r12d
movl $0x40, %ebx
xorl %r13d, %r13d
movq %r14, 0x38(%rsp)
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
movl $0xa, %esi
callq 0x3240
testq %rax, %rax
jne 0x1680d
addl %ebp, %ebp
movslq %ebp, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x33d0
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 0x3390
testq %rax, %rax
jne 0x167c7
movq %r15, %rdi
leaq 0x1abe(%rip), %rsi      # 0x182d5
callq 0x3370
testq %rax, %rax
je 0x16a8f
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 0x3170
movsd %xmm0, 0x40(%rsp)
movq 0x18(%rsp), %rax
cmpq %rbx, %rax
je 0x16a86
cmpb $0x0, (%rax)
jne 0x16a86
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%rsp), %rbx
leal -0x2(%rbx), %eax
cmpl %eax, %r13d
jl 0x16890
addl %ebx, %ebx
movslq %ebx, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
callq 0x33d0
movq %rax, 0x8(%rsp)
xorl %edi, %edi
leaq 0x1a3a(%rip), %rsi      # 0x182d3
callq 0x3370
movq %rax, %r14
xorl %edi, %edi
leaq 0x1a26(%rip), %rsi      # 0x182d0
callq 0x3370
testq %rax, %rax
je 0x16988
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 0x3320
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 0x16a74
cmpl $0x0, (%r15)
jne 0x16a74
cmpb $0x0, (%rax)
jne 0x16a74
cmpl %ebx, %r12d
jge 0x16a74
addq %rcx, %r13
movl $0x0, (%r15)
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3170
movsd %xmm0, 0x8(%r13)
movq 0x18(%rsp), %rax
cmpq %rbp, %rax
je 0x16a7d
cmpl $0x0, (%r15)
jne 0x16a7d
movsbl (%rax), %edi
testl %edi, %edi
je 0x1696b
callq 0x3060
testl %eax, %eax
je 0x16a7d
xorl %eax, %eax
movl 0x28(%rsp), %ecx
cmpl %ecx, (%r13)
setle %al
movl 0x2c(%rsp), %r13d
addl %eax, %r13d
movl %ebx, %r12d
jmp 0x1686b
movq 0x30(%rsp), %rax
movsd 0xc8(%rax), %xmm0
ucomisd 0x16a3(%rip), %xmm0     # 0x18040
jae 0x169a6
movq 0x8(%rsp), %rsi
jmp 0x169c2
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 0x144c1
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 0x3390
testq %rax, %rax
jne 0x167c2
cvtsi2sd %r12d, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
jmp 0x16a33
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %r15, %rdi
callq 0x3290
movq 0x8(%rsp), %rdi
callq 0x3290
movq %r14, %rdi
callq 0x3200
movsd 0x10(%rsp), %xmm0
mulsd 0x15e3(%rip), %xmm0     # 0x18048
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %edi
callq 0x39c8
movl 0x4(%rsp), %edi
callq 0x39a3
movl 0x4(%rsp), %edi
callq 0x39ed
movl %r13d, %edi
callq 0x3a12
nop