// File: /workspace/github_cpp_makefile_star2_O2/qmmlib[P]QmmLibrary/predict.c
// Function: do_predict(_IO_FILE*, _IO_FILE*)
void do_predict(FILE *input, FILE *output)
{
	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_);
	double *prob_estimates=NULL;
	int j, n;
	int nr_feature=get_nr_feature(model_);
	if(model_->bias>=0)
		n=nr_feature+1;
	else
		n=nr_feature;

	if(flag_predict_probability)
	{
		int *labels;

		if(!check_probability_model(model_))
		{
			fprintf(stderr, "probability output is only supported for logistic regression\n");
			exit(1);
		}

		labels=(int *) malloc(nr_class*sizeof(int));
		get_labels(model_,labels);
		prob_estimates = (double *) malloc(nr_class*sizeof(double));
		fprintf(output,"labels");		
		for(j=0;j<nr_class;j++)
			fprintf(output," %d",labels[j]);
		fprintf(output,"\n");
		free(labels);
	}

	max_line_len = 1024;
	line = (char *)malloc(max_line_len*sizeof(char));
	while(readline(input) != NULL)
	{
		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
			exit_input_error(total+1);

		target_label = strtod(label,&endptr);
		if(endptr == label || *endptr != '\0')
			exit_input_error(total+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)
				exit_input_error(total+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)))
				exit_input_error(total+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;

		if(flag_predict_probability)
		{
			int j;
			predict_label = predict_probability(model_,x,prob_estimates);
			fprintf(output,"%g",predict_label);
			for(j=0;j<model_->nr_class;j++)
				fprintf(output," %g",prob_estimates[j]);
			fprintf(output,"\n");
		}
		else
		{
			predict_label = predict(model_,x);
			fprintf(output,"%g\n",predict_label);
		}

		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;
        }
        if(model_->param.solver_type==L2R_L2LOSS_SVR || 
           model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || 
           model_->param.solver_type==L2R_L2LOSS_SVR_DUAL)
        {
                printf("Mean squared error = %g (regression)\n",error/total);
                printf("Squared correlation coefficient = %g (regression)\n",
                       ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/
                       ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt))
                       );
        }
	else
		printf("Accuracy = %g%% (%d/%d)\n",(double) correct/total*100,correct,total);
	if(flag_predict_probability)
		free(prob_estimates);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, 0x48(%rsp)
movq 0x14c99(%rip), %rdi     # 0x18268
callq 0x117ba
movl %eax, %ebx
movq 0x14c8b(%rip), %rdi     # 0x18268
callq 0x117b3
movq 0x14c7f(%rip), %rdi     # 0x18268
movsd 0xc8(%rdi), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl %eax, 0x3c(%rsp)
sbbl $-0x1, %eax
movl %eax, 0x38(%rsp)
xorl %ebp, %ebp
cmpl $0x0, 0x14c63(%rip)     # 0x18270
movq %r14, 0x18(%rsp)
je 0x36b3
callq 0x10f3d
testl %eax, %eax
je 0x3b67
movslq %ebx, %r15
leaq (,%r15,4), %rdi
callq 0x32e0
movq %rax, %rbx
movq 0x14c29(%rip), %rdi     # 0x18268
movq %rax, %rsi
callq 0x117c1
leaq (,%r15,8), %rdi
callq 0x32e0
movq %rax, %r13
leaq 0x10a17(%rip), %rdi     # 0x14075
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x3450
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq 0x10a00(%rip), %r14     # 0x1407c
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x369c
movl (%rbx,%r12,4), %edx
movq 0x18(%rsp), %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3330
incq %r12
jmp 0x367f
pushq $0xa
popq %rdi
movq 0x18(%rsp), %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x3270
jmp 0x36b6
xorl %r13d, %r13d
movl $0x400, 0x14bb4(%rip)   # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x32e0
movq %rax, %rdi
movq %rax, 0x14ba4(%rip)     # 0x18278
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movl $0x400, %esi            # imm = 0x400
movl $0x0, 0x14(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movq %r13, 0x40(%rsp)
movq 0x48(%rsp), %rdx
callq 0x3380
testq %rax, %rax
je 0x3a57
movq 0x14b62(%rip), %rbx     # 0x18278
movq %rbx, %rdi
pushq $0xa
popq %rsi
callq 0x3230
testq %rax, %rax
jne 0x3778
movslq 0x14b47(%rip), %r14     # 0x18274
addq %r14, %r14
movl %r14d, 0x14b3d(%rip)    # 0x18274
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33c0
movq %rax, %rbx
movq %rax, 0x14b2c(%rip)     # 0x18278
movq %rax, %rdi
callq 0x30c0
cltq
addq %rax, %rbx
subl %eax, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movq 0x48(%rsp), %rdx
callq 0x3380
testq %rax, %rax
jne 0x370f
movq 0x14b00(%rip), %rbx     # 0x18278
testq %rbx, %rbx
je 0x3a57
movq %rbx, %rdi
leaq 0x108f5(%rip), %rsi     # 0x14080
callq 0x3370
testq %rax, %rax
je 0x3b5e
movq %rax, %rbx
movq %rbp, (%rsp)
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3140
movq 0x8(%rsp), %rax
cmpq %rbx, %rax
je 0x3b53
cmpb $0x0, (%rax)
jne 0x3b53
movapd %xmm0, 0x80(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq 0x14a67(%rip), %rsi     # 0x18240
leal -0x2(%rsi), %eax
cmpl %eax, %ebp
jl 0x3800
leal (%rsi,%rsi), %eax
movl %eax, 0x14a57(%rip)     # 0x18240
movq 0x14a70(%rip), %rdi     # 0x18260
shlq $0x5, %rsi
callq 0x33c0
movq %rax, 0x14a60(%rip)     # 0x18260
xorl %edi, %edi
leaq 0x1087b(%rip), %rsi     # 0x14084
callq 0x3370
movq %rax, %r14
xorl %edi, %edi
leaq 0x1086c(%rip), %rsi     # 0x14086
callq 0x3370
movl %ebp, %r13d
testq %rax, %rax
je 0x38f2
movq %rax, %rbx
movl %ebp, 0x20(%rsp)
movl %r15d, %ebp
callq 0x3040
movq %rax, %r15
andl $0x0, (%rax)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x3320
movq %rax, %r12
movq 0x14a06(%rip), %rax     # 0x18260
shlq $0x4, %r13
movl %r12d, (%rax,%r13)
movq 0x8(%rsp), %rax
cmpq %r14, %rax
je 0x3b53
cmpl $0x0, (%r15)
jne 0x3b53
cmpb $0x0, (%rax)
jne 0x3b53
cmpl %r12d, %ebp
jge 0x3b53
andl $0x0, (%r15)
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3140
movq 0x149bc(%rip), %r14     # 0x18260
movsd %xmm0, 0x8(%r14,%r13)
movq 0x8(%rsp), %rax
cmpq %rbx, %rax
je 0x3b53
cmpl $0x0, (%r15)
jne 0x3b53
movsbl (%rax), %edi
testl %edi, %edi
je 0x38d7
callq 0x3060
testl %eax, %eax
je 0x3b53
xorl %eax, %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, (%r14,%r13)
setle %al
movl 0x20(%rsp), %ebp
addl %eax, %ebp
movl %r12d, %r15d
jmp 0x37d2
movq 0x1496f(%rip), %rdi     # 0x18268
movsd 0xc8(%rdi), %xmm0
ucomisd 0x10707(%rip), %xmm0    # 0x14010
movq 0x14950(%rip), %rsi     # 0x18260
jb 0x3927
shlq $0x4, %r13
movl 0x38(%rsp), %eax
movl %eax, (%rsi,%r13)
movsd %xmm0, 0x8(%rsi,%r13)
incl %ebp
movq 0x18(%rsp), %r14
movslq %ebp, %rax
shlq $0x4, %rax
orl $-0x1, (%rsi,%rax)
cmpl $0x0, 0x14932(%rip)     # 0x18270
je 0x39ad
movq 0x40(%rsp), %r13
movq %r13, %rdx
callq 0x10e45
movq %r14, %rdi
leaq 0x10733(%rip), %rsi     # 0x1408a
movapd %xmm0, 0x20(%rsp)
movb $0x1, %al
callq 0x3330
xorl %ebx, %ebx
movq (%rsp), %rbp
movq 0x148f7(%rip), %rax     # 0x18268
movslq 0xb0(%rax), %rax
cmpq %rax, %rbx
jge 0x399a
movsd (%r13,%rbx,8), %xmm0
movq %r14, %rdi
leaq 0x106fb(%rip), %rsi     # 0x14089
movb $0x1, %al
callq 0x3330
incq %rbx
jmp 0x396a
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x32a0
movapd 0x20(%rsp), %xmm4
jmp 0x39d8
callq 0x10cd6
movapd %xmm0, 0x20(%rsp)
movq %r14, %rdi
leaq 0x106cb(%rip), %rsi     # 0x1408d
movb $0x1, %al
callq 0x3330
movapd 0x20(%rsp), %xmm4
movq 0x40(%rsp), %r13
movq (%rsp), %rbp
movapd %xmm4, %xmm0
movapd 0x80(%rsp), %xmm3
cmpeqsd %xmm3, %xmm0
movq %xmm0, %rax
subl %eax, 0x14(%rsp)
movapd %xmm4, %xmm0
subsd %xmm3, %xmm0
movapd %xmm4, %xmm1
unpcklpd %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0]
movapd 0x60(%rsp), %xmm2
addpd %xmm1, %xmm2
movapd %xmm2, 0x60(%rsp)
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
mulpd %xmm3, %xmm3
movapd 0x50(%rsp), %xmm0
addpd %xmm3, %xmm0
movapd %xmm0, 0x50(%rsp)
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
mulpd %xmm1, %xmm4
movapd 0x70(%rsp), %xmm0
addpd %xmm4, %xmm0
movapd %xmm0, 0x70(%rsp)
incl %ebp
movq 0x1482c(%rip), %rdi     # 0x18278
movl 0x14822(%rip), %esi     # 0x18274
jmp 0x36fc
movq 0x1480a(%rip), %rax     # 0x18268
movl (%rax), %eax
addl $-0xb, %eax
cmpl $0x2, %eax
ja 0x3af7
cvtsi2sd %ebp, %xmm1
movapd %xmm1, 0x20(%rsp)
movapd 0x50(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
divsd %xmm1, %xmm0
leaq 0x10606(%rip), %rdi     # 0x14091
movb $0x1, %al
callq 0x3050
movapd 0x60(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
mulsd %xmm0, %xmm0
movapd 0x50(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
mulsd %xmm3, %xmm2
subsd %xmm0, %xmm2
movapd %xmm1, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
mulpd %xmm1, %xmm0
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd 0x70(%rsp), %xmm3
subpd %xmm0, %xmm3
shufpd $0x2, %xmm3, %xmm2      # xmm2 = xmm2[0],xmm3[1]
mulpd %xmm3, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
divsd %xmm2, %xmm0
leaq 0x105c9(%rip), %rdi     # 0x140b7
movb $0x1, %al
callq 0x3050
jmp 0x3b1f
movl 0x14(%rsp), %esi
cvtsi2sd %esi, %xmm0
cvtsi2sd %ebp, %xmm1
divsd %xmm1, %xmm0
mulsd 0x104f9(%rip), %xmm0    # 0x14008
leaq 0x105d4(%rip), %rdi     # 0x140ea
movl %ebp, %edx
movb $0x1, %al
callq 0x3050
cmpl $0x0, 0x1474a(%rip)     # 0x18270
je 0x3b41
movq %r13, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3270
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
incl %edi
callq 0x358c
incl %ebp
movl %ebp, %edi
callq 0x358c
movq 0x1446a(%rip), %rax     # 0x17fd8
movq (%rax), %rcx
leaq 0x104bf(%rip), %rdi     # 0x14037
pushq $0x3d
popq %rsi
pushq $0x1
popq %rdx
callq 0x3450
pushq $0x1
popq %rdi
callq 0x3280