// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/regression.c
// Function: train_quantizereg
void* train_quantizereg(Instanceptr* trdata, Instanceptr *cvdata, void* parameters)
{
	/*!Last Changed 21.12.2004*/
	Model_quantizeregptr model;
	Quantizereg_parameterptr param;
	int i, j, k, m, x, y, **countstwo, *countsone, found, totalcount;
	double maxx, maxy, xvalue, yvalue, total, *eigenvalues;
	Instanceptr tmp;
	matrix cov;
	Instance mean;
	model = safemalloc(sizeof(Model_quantizereg), "train_quantizereg", 6);
	param = (Quantizereg_parameterptr) parameters;
	model->partitioncount = param->partitioncount;
	if (current_dataset->featurecount > 2)
	 {
   mean = find_mean(*trdata);
  	cov = covariance(*trdata, mean);
	  safe_free(mean.values);
   eigenvalues = find_eigenvalues_eigenvectors_symmetric(&(model->eigenvectors), cov);
  	matrix_free(cov);
   safe_free(eigenvalues);
   dimension_reduce_with_pca(*trdata, 2, model->eigenvectors);
	  find_bounds_of_feature(current_dataset, *trdata, 0, &(model->minx), &maxx);
			model->binx = (maxx - model->minx) / param->partitioncount;
	  find_bounds_of_feature(current_dataset, *trdata, 1, &(model->miny), &maxy);
			model->biny = (maxy - model->miny) / param->partitioncount;
			countstwo = (int**) safecalloc_2d(sizeof(int *), sizeof(int), param->partitioncount, param->partitioncount, "train_quantizereg", 19);
			model->meanstwo = (double**) safecalloc_2d(sizeof(double *), sizeof(double), param->partitioncount, param->partitioncount, "train_quantizereg", 20);
			tmp = *trdata;
			while (tmp)
			 {
				 xvalue = real_feature(tmp, 0);
				 yvalue = real_feature(tmp, 1);
					x = (int) ((xvalue - model->minx) / model->binx);
					if (x == param->partitioncount)
						 x--;
					y = (int) ((yvalue - model->miny) / model->biny);
					if (y == param->partitioncount)
						 y--;
					model->meanstwo[x][y] += give_regressionvalue(tmp);
					(countstwo[x][y])++;
				 tmp = tmp->next;
			 }
			for (i = 0; i < param->partitioncount; i++)
				 for (j = 0; j < param->partitioncount; j++)
						 if (countstwo[i][j] != 0)
								 model->meanstwo[i][j] /= countstwo[i][j];
			for (i = 0; i < param->partitioncount; i++)
				 for (j = 0; j < param->partitioncount; j++)
						 if (countstwo[i][j] == 0)
							 {
								 found = 0;
								 k = 1;
									totalcount = 0;
									total = 0;
         while (!found)
									 {
										 for (m = j - k; m <= j + k; m++)
												 if (between(i - k, 0, param->partitioncount - 1) && between(m, 0, param->partitioncount - 1) && countstwo[i - k][m] != 0)
													 {
														 total += countstwo[i - k][m] * model->meanstwo[i - k][m];
															totalcount += countstwo[i - k][m];
														 found = 1;
													 }
										 for (m = j - k; m <= j + k; m++)
												 if (between(i + k, 0, param->partitioncount - 1) && between(m, 0, param->partitioncount - 1) && countstwo[i + k][m] != 0)
													 {
														 total += countstwo[i + k][m] * model->meanstwo[i + k][m];
															totalcount += countstwo[i + k][m];
														 found = 1;
													 }
											for (m = i - k; m <= i + k; m++)
												 if (between(j - k, 0, param->partitioncount - 1) && between(m, 0, param->partitioncount - 1) && countstwo[m][j - k] != 0)
													 {
														 total += countstwo[m][j - k] * model->meanstwo[m][j - k];
															totalcount += countstwo[m][j - k];
															found = 1;
													 }
											for (m = i - k; m <= i + k; m++)
												 if (between(j + k, 0, param->partitioncount - 1) && between(m, 0, param->partitioncount - 1) && countstwo[m][j + k] != 0)
													 {
														 total += countstwo[m][j + k] * model->meanstwo[m][j + k];
															totalcount += countstwo[m][j + k];
															found = 1;
													 }
											k++;
									 }
									model->meanstwo[i][j] = total / totalcount; 
							 } 
	 }
	else
	 {
	  find_bounds_of_feature(current_dataset, *trdata, 0, &(model->minx), &maxx);
			model->binx = (maxx - model->minx) / (param->partitioncount);
			model->meansone = safecalloc(param->partitioncount, sizeof(double), "train_quantizereg", 86);
			countsone = safecalloc(param->partitioncount, sizeof(int), "quantizereg", 87);
			tmp = *trdata;
			while (tmp)
			 {
				 xvalue = real_feature(tmp, 0);
					x = (int) ((xvalue - model->minx) / model->binx);
					if (x == param->partitioncount)
						 x--;
					model->meansone[x] += give_regressionvalue(tmp);
					(countsone[x])++;
				 tmp = tmp->next;
			 }
			for (i = 0; i < param->partitioncount; i++)
					if (countsone[i] != 0)
							model->meansone[i] /= countsone[i];
   for (i = 0; i < param->partitioncount; i++)
				 if (countsone[i] == 0)
					 {
						 found = 0;
							totalcount = 0;
							total = 0;
							k = 1;
							while (!found)
							 {
								 if (between(i - k, 0, param->partitioncount - 1) && countsone[i - k] != 0)
									 {
										 total += countsone[i - k] * model->meansone[i - k];
											totalcount += countsone[i - k];
											found = 1;
									 }
									if (between(i + k, 0, param->partitioncount - 1) && countsone[i + k] != 0)
									 {
										 total += countsone[i + k] * model->meansone[i + k];
											totalcount += countsone[i + k];
											found = 1;
									 }
								 k++;
							 }
							model->meansone[i] = total / totalcount;
					 }
	 }
	if (current_dataset->featurecount > 2)
			free_2d((void**)countstwo, param->partitioncount);
	else
			safe_free(countsone);
	return model;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %r14
movq %rdi, %r12
leaq 0x36229(%rip), %rsi     # 0x9fa2e
pushq $0x48
popq %rdi
pushq $0x6
popq %rdx
callq 0x34f59
movq %rax, %rbp
movl (%r14), %eax
movl %eax, 0x40(%rbp)
leaq 0x64050(%rip), %rax     # 0xcd870
movq (%rax), %rdi
cmpl $0x3, 0x1bc(%rdi)
movq %rbp, 0x38(%rsp)
jl 0x69a59
movq (%r12), %rsi
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x116b6
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movaps %xmm2, 0xb0(%rsp)
movaps %xmm1, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq (%r12), %rdi
movaps 0x90(%rsp), %xmm0
movaps 0xa0(%rsp), %xmm1
movdqa 0xb0(%rsp), %xmm2
movdqu %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x106d9
movq %rax, %rbx
movq %rdx, %r15
movq 0x90(%rsp), %rdi
callq 0x34ff7
leaq 0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3c5c1
movq %rax, %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3929f
movq %r13, %rdi
callq 0x34ff7
movq (%r12), %rdi
movq 0x30(%rbp), %rdx
movq 0x38(%rbp), %rcx
pushq $0x2
popq %rsi
callq 0xfc9a
leaq 0x63f80(%rip), %r15     # 0xcd870
movq (%r15), %rdi
movq (%r12), %rsi
leaq 0xc8(%rsp), %rbx
xorl %edx, %edx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x101e4
movsd (%rbx), %xmm0
subsd (%rbp), %xmm0
cvtsi2sdl (%r14), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rbp)
movq (%r15), %rdi
movq (%r12), %rsi
movq %rbp, %rcx
addq $0x10, %rcx
pushq $0x1
popq %rdx
leaq 0xc0(%rsp), %rbx
movq %rbx, %r8
callq 0x101e4
movsd (%rbx), %xmm0
subsd 0x10(%rbp), %xmm0
movl (%r14), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rbp)
leaq 0x360ca(%rip), %rbx     # 0x9fa2e
pushq $0x8
popq %r15
pushq $0x4
popq %rsi
pushq $0x13
popq %r9
movq %r15, %rdi
movl %ecx, %edx
movq %rbx, %r8
callq 0x3509c
movq %rax, 0x48(%rsp)
movl (%r14), %ecx
pushq $0x14
popq %r9
movq %r15, %rdi
movq %r15, %rsi
movl %ecx, %edx
movq %rbx, %r8
callq 0x3509c
movq %rax, 0x28(%rbp)
movq 0x38(%rsp), %r15
movq (%r12), %r12
testq %r12, %r12
je 0x69b12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2aea2
movapd %xmm0, 0x50(%rsp)
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x2aea2
unpcklpd 0x50(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movsd 0x10(%r15), %xmm1
movsd 0x18(%r15), %xmm2
movhpd (%r15), %xmm1           # xmm1 = xmm1[0],mem[0]
subpd %xmm1, %xmm0
movhpd 0x8(%r15), %xmm2        # xmm2 = xmm2[0],mem[0]
divpd %xmm2, %xmm0
cvttpd2dq %xmm0, %xmm0
movd (%r14), %xmm1
pshufd $0x50, %xmm0, %xmm2     # xmm2 = xmm0[0,0,1,1]
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
pcmpeqd %xmm2, %xmm1
movmskpd %xmm1, %eax
movl %eax, %ecx
shrb %cl
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %ebx
movzbl %cl, %ecx
subl %ecx, %ebx
movd %xmm0, %ebp
andl $0x1, %eax
subl %eax, %ebp
movq %r12, %rdi
callq 0x2b4a0
movq 0x28(%r15), %rax
movslq %ebx, %rcx
movq (%rax,%rcx,8), %rdx
movslq %ebp, %rsi
addsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
movq 0x48(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
incl (%rcx,%rsi,4)
addq $0x10, %r12
jmp 0x699a1
movq (%r12), %rsi
leaq 0xc8(%rsp), %rbx
xorl %edx, %edx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x101e4
movsd (%rbx), %xmm0
subsd (%rbp), %xmm0
movslq (%r14), %rdi
cvtsi2sd %edi, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rbp)
leaq 0x35f9c(%rip), %rdx     # 0x9fa2e
pushq $0x8
popq %rsi
pushq $0x56
popq %rcx
callq 0x35067
movq %rax, 0x20(%rbp)
movslq (%r14), %rdi
leaq 0x302f8(%rip), %rdx     # 0x99da3
pushq $0x4
popq %rsi
pushq $0x57
popq %rcx
callq 0x35067
movq %rax, %r13
movq 0x38(%rsp), %r15
movq (%r12), %r12
testq %r12, %r12
je 0x69b67
movq %r12, %rdi
xorl %esi, %esi
callq 0x2aea2
subsd (%r15), %xmm0
divsd 0x8(%r15), %xmm0
cvttsd2si %xmm0, %ebx
xorl %eax, %eax
cmpl %ebx, (%r14)
sete %al
subl %eax, %ebx
movq %r12, %rdi
callq 0x2b4a0
movq 0x20(%r15), %rax
movslq %ebx, %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incl (%r13,%rcx,4)
addq $0x10, %r12
jmp 0x69abe
movl (%r14), %esi
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
movq 0x38(%rsp), %rbx
cmpq %rdx, %rcx
je 0x69ba8
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x69b62
movq 0x48(%rsp), %r8
movq (%r8,%rcx,8), %r8
movl (%r8,%rdi,4), %r8d
testl %r8d, %r8d
je 0x69b5d
cvtsi2sd %r8d, %xmm0
movq (%rax,%rcx,8), %r8
movsd (%r8,%rdi,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r8,%rdi,8)
incq %rdi
jmp 0x69b2d
incq %rcx
jmp 0x69b26
movl (%r14), %esi
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
movq 0x38(%rsp), %rbx
cmpq %rax, %rcx
je 0x69e91
movl (%r13,%rax,4), %edx
testl %edx, %edx
je 0x69ba3
cvtsi2sd %edx, %xmm0
movq 0x20(%rbx), %rdx
movsd (%rdx,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rax,8)
incq %rax
jmp 0x69b7b
pushq $-0x1
popq %rax
xorl %ebp, %ebp
movl %eax, 0x68(%rsp)
movslq %esi, %rax
cmpq %rax, %rbp
jge 0x69fa2
xorl %ecx, %ecx
pushq $-0x1
popq %rax
movl %eax, 0x6c(%rsp)
movq %rbp, 0x80(%rsp)
movslq %esi, %rax
cmpq %rax, %rcx
jge 0x69e85
movq 0x48(%rsp), %rax
movq (%rax,%rbp,8), %rax
cmpl $0x0, (%rax,%rcx,4)
jne 0x69e79
xorpd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
xorl %edx, %edx
pushq $0x1
popq %rax
movq %rax, 0x70(%rsp)
movl 0x68(%rsp), %r15d
movl 0x6c(%rsp), %eax
movl $0x0, 0x44(%rsp)
movq %rcx, 0x88(%rsp)
testl %edx, %edx
jne 0x69e59
movl %r15d, 0x7c(%rsp)
movslq %r15d, %r12
movl %eax, 0x78(%rsp)
movslq %eax, %r15
movq 0x70(%rsp), %rax
leaq (%rax,%rcx), %r13
subq %rax, %rbp
movl $0x0, 0x60(%rsp)
movq %r15, %rbx
cmpq %r13, %rbx
jg 0x69cb7
movl (%r14), %edx
decl %edx
movl %ebp, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69cb2
movl (%r14), %edx
decl %edx
movl %ebx, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69cb2
movq 0x48(%rsp), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
je 0x69cb2
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rbp,8), %rcx
mulsd (%rcx,%rbx,8), %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addl %eax, 0x44(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x60(%rsp)
incq %rbx
jmp 0x69c48
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
leaq (%rcx,%rax), %rbp
movq 0x38(%rsp), %rbx
cmpq %r13, %r15
jg 0x69d3a
movl (%r14), %edx
decl %edx
movl %ebp, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69d35
movl (%r14), %edx
decl %edx
movl %r15d, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69d35
movq 0x48(%rsp), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%r15,4), %eax
testl %eax, %eax
je 0x69d35
cvtsi2sd %eax, %xmm0
movq 0x28(%rbx), %rcx
movq (%rcx,%rbp,8), %rcx
mulsd (%rcx,%r15,8), %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addl %eax, 0x44(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x60(%rsp)
incq %r15
jmp 0x69ccd
movq 0x88(%rsp), %rbx
subq 0x70(%rsp), %rbx
movq %r12, %r15
cmpq %rbp, %r15
jg 0x69dba
movl (%r14), %edx
decl %edx
movl %ebx, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69db5
movl (%r14), %edx
decl %edx
movl %r15d, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69db5
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
je 0x69db5
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%r15,8), %rcx
mulsd (%rcx,%rbx,8), %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addl %eax, 0x44(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x60(%rsp)
incq %r15
jmp 0x69d4a
movq 0x38(%rsp), %rbx
movl 0x7c(%rsp), %r15d
cmpq %rbp, %r12
jg 0x69e32
movl (%r14), %edx
decl %edx
movl %r13d, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69e2d
movl (%r14), %edx
decl %edx
movl %r12d, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69e2d
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %rax
movl (%rax,%r13,4), %eax
testl %eax, %eax
je 0x69e2d
cvtsi2sd %eax, %xmm0
movq 0x28(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
mulsd (%rcx,%r13,8), %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addl %eax, 0x44(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x60(%rsp)
incq %r12
jmp 0x69dc4
incq 0x70(%rsp)
movl 0x78(%rsp), %eax
decl %eax
decl %r15d
movq 0x80(%rsp), %rbp
movq 0x88(%rsp), %rcx
movl 0x60(%rsp), %edx
jmp 0x69c1a
cvtsi2sdl 0x44(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rax
movsd %xmm1, (%rax,%rcx,8)
movl (%r14), %esi
incq %rcx
incl 0x6c(%rsp)
jmp 0x69bce
incq %rbp
incl 0x68(%rsp)
jmp 0x69bb1
pushq $-0x1
popq %rcx
xorl %r12d, %r12d
movslq %esi, %rax
cmpq %rax, %r12
jge 0x69fa4
cmpl $0x0, (%r13,%r12,4)
jne 0x69f97
xorpd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
xorl %r15d, %r15d
movq %rcx, 0x48(%rsp)
movq %rcx, %rbx
movq %r12, 0x60(%rsp)
xorl %ebp, %ebp
testl %r15d, %r15d
jne 0x69f6d
movl (%r14), %edx
decl %edx
xorl %r15d, %r15d
movl %ebx, %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69f1a
movl (%r13,%rbx,4), %eax
testl %eax, %eax
je 0x69f1a
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x20(%rcx), %rcx
mulsd (%rcx,%rbx,8), %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addl %eax, %ebp
pushq $0x1
popq %r15
movl (%r14), %edx
decl %edx
leal 0x1(%r12), %edi
xorl %esi, %esi
callq 0x34c00
testl %eax, %eax
je 0x69f62
movl 0x4(%r13,%r12,4), %eax
testl %eax, %eax
je 0x69f62
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x20(%rcx), %rcx
mulsd 0x8(%rcx,%r12,8), %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addl %eax, %ebp
pushq $0x1
popq %r15
incq %r12
decq %rbx
jmp 0x69ecb
cvtsi2sd %ebp, %xmm0
movsd 0x50(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x38(%rsp), %rbx
movq 0x20(%rbx), %rax
movq 0x60(%rsp), %r12
movsd %xmm1, (%rax,%r12,8)
movl (%r14), %esi
movq 0x48(%rsp), %rcx
incq %r12
incq %rcx
jmp 0x69e97
jmp 0x69fa4
leaq 0x638c5(%rip), %rax     # 0xcd870
movq (%rax), %rax
cmpl $0x3, 0x1bc(%rax)
jl 0x69fc3
movq 0x48(%rsp), %rdi
callq 0x34fc3
jmp 0x69fcb
movq %r13, %rdi
callq 0x34ff7
movq %rbx, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq