// File: /workspace/github_c_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rdx, %r14
movq %rdi, %r13
leaq 0x3af8c(%rip), %rsi     # 0xaca36
movl $0x48, %edi
movl $0x6, %edx
callq 0x390f5
movq %rax, %r15
movl (%r14), %eax
movl %eax, 0x40(%r15)
leaq 0x60da6(%rip), %rbp     # 0xd2870
movq (%rbp), %rdi
cmpl $0x3, 0x1bc(%rdi)
movq %r15, 0x38(%rsp)
jl 0x71cfe
movq (%r13), %rsi
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
callq 0x12519
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movaps %xmm2, 0xc0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movq (%r13), %rdi
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movaps 0xc0(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x113c9
movq %rax, %rbx
movq %rdx, %r15
movq 0xa0(%rsp), %rdi
callq 0x39194
movq 0x38(%rsp), %rax
leaq 0x30(%rax), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x41571
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq 0x38(%rsp), %r15
callq 0x3dbad
movq %r12, %rdi
callq 0x39194
movq (%r13), %rdi
movq 0x30(%r15), %rdx
movq 0x38(%r15), %rcx
movl $0x2, %esi
callq 0x10980
movq (%rbp), %rdi
movq (%r13), %rsi
leaq 0xd8(%rsp), %rbx
xorl %edx, %edx
movq %r15, %rcx
movq %rbx, %r8
callq 0x10ef3
movsd (%rbx), %xmm0
subsd (%r15), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl (%r14), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%r15)
movq (%rbp), %rdi
movq (%r13), %rsi
movq %r15, %rcx
addq $0x10, %rcx
leaq 0xd0(%rsp), %rbx
movl $0x1, %edx
movq %rbx, %r8
callq 0x10ef3
movsd (%rbx), %xmm0
subsd 0x10(%r15), %xmm0
movl (%r14), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%r15)
leaq 0x3ae17(%rip), %rbx     # 0xaca36
movl $0x8, %edi
movl $0x4, %esi
movl %ecx, %edx
movq %rbx, %r8
movl $0x13, %r9d
callq 0x39245
movq %rax, %r12
movl (%r14), %ecx
movl $0x8, %edi
movl $0x8, %esi
movl %ecx, %edx
movq %rbx, %r8
movl $0x14, %r9d
callq 0x39245
movq %rax, 0x28(%r15)
movq (%r13), %rbx
testq %rbx, %rbx
je 0x71dba
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2e4b6
movsd %xmm0, 0x30(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x2e4b6
movsd 0x30(%rsp), %xmm1
subsd (%r15), %xmm1
divsd 0x8(%r15), %xmm1
cvttsd2si %xmm1, %ebp
movl (%r14), %eax
xorl %ecx, %ecx
cmpl %ebp, %eax
sete %cl
subl %ecx, %ebp
subsd 0x10(%r15), %xmm0
divsd 0x18(%r15), %xmm0
cvttsd2si %xmm0, %r15d
xorl %ecx, %ecx
cmpl %r15d, %eax
sete %cl
subl %ecx, %r15d
movq %rbx, %rdi
callq 0x2eaee
movq 0x38(%rsp), %rax
movq 0x28(%rax), %rax
movslq %ebp, %rcx
movq (%rax,%rcx,8), %rax
movslq %r15d, %rdx
movq 0x38(%rsp), %r15
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
movq (%r12,%rcx,8), %rax
incl (%rax,%rdx,4)
movq 0x10(%rbx), %rbx
jmp 0x71c61
movq (%r13), %rsi
leaq 0xd8(%rsp), %rbx
xorl %edx, %edx
movq %r15, %rcx
movq %rbx, %r8
callq 0x10ef3
movsd (%rbx), %xmm0
subsd (%r15), %xmm0
movslq (%r14), %rdi
cvtsi2sd %edi, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%r15)
leaq 0x3acfe(%rip), %rdx     # 0xaca36
movl $0x8, %esi
movl $0x56, %ecx
callq 0x39210
movq %rax, 0x20(%r15)
movslq (%r14), %rdi
leaq 0x350b6(%rip), %rdx     # 0xa6e0b
movl $0x4, %esi
movl $0x57, %ecx
callq 0x39210
movq %rax, %r12
movq (%r13), %rbx
testq %rbx, %rbx
je 0x721b7
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2e4b6
subsd (%r15), %xmm0
divsd 0x8(%r15), %xmm0
cvttsd2si %xmm0, %ebp
xorl %eax, %eax
cmpl %ebp, (%r14)
sete %al
subl %eax, %ebp
movq %rbx, %rdi
callq 0x2eaee
movq 0x20(%r15), %rax
movslq %ebp, %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incl (%r12,%rcx,4)
movq 0x10(%rbx), %rbx
jmp 0x71d6b
movslq (%r14), %rax
testq %rax, %rax
movq %r12, %rbx
jle 0x71e01
xorl %ecx, %ecx
movq (%rbx,%rcx,8), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
testl %edi, %edi
je 0x71df1
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movq 0x28(%r15), %rdi
movq (%rdi,%rcx,8), %rdi
movsd (%rdi,%rsi,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdi,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x71dcd
incq %rcx
cmpq %rax, %rcx
jne 0x71dc7
cmpl $0x0, (%r14)
jle 0x721b2
movl $0xffffffff, 0x64(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
cmpl $0x0, (%r14)
jle 0x72195
movq $-0x1, %rcx
xorl %edx, %edx
movq 0x50(%rsp), %rax
movq (%rbx,%rax,8), %rax
cmpl $0x0, (%rax,%rdx,4)
jne 0x72183
movl $0x0, 0x40(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x3, %r12d
movl 0x64(%rsp), %eax
movq %rax, 0x70(%rsp)
movq %rcx, 0x80(%rsp)
movl $0x1, %eax
movl $0x0, 0x44(%rsp)
movq %rdx, 0x88(%rsp)
movq %rdx, %r13
subq %rax, %r13
leaq (%rax,%rdx), %rsi
movq %rsi, 0x48(%rsp)
cmpq %rsi, %r13
movq %rax, 0x68(%rsp)
movq %rcx, 0x78(%rsp)
jg 0x71f22
movq 0x50(%rsp), %rbp
subq %rax, %rbp
movl %r12d, %r15d
movq %rcx, %rbx
movl (%r14), %edx
decl %edx
movl %ebp, %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x71f1a
movl (%r14), %edx
decl %edx
movl %ebx, %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x71f1a
movq 0x58(%rsp), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
je 0x71f1a
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rbp,8), %rcx
mulsd (%rcx,%rbx,8), %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
addl %eax, 0x40(%rsp)
movl $0x1, 0x44(%rsp)
incq %rbx
decl %r15d
jne 0x71eb1
cmpq 0x48(%rsp), %r13
jg 0x71fb4
movq 0x50(%rsp), %rax
movq 0x68(%rsp), %rcx
leaq (%rcx,%rax), %rbp
movl %r12d, %r15d
movq 0x78(%rsp), %rbx
movl (%r14), %edx
decl %edx
movl %ebp, %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x71fac
movl (%r14), %edx
decl %edx
movl %ebx, %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x71fac
movq 0x58(%rsp), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
je 0x71fac
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rbp,8), %rcx
mulsd (%rcx,%rbx,8), %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
addl %eax, 0x40(%rsp)
movl $0x1, 0x44(%rsp)
incq %rbx
decl %r15d
jne 0x71f43
movslq 0x70(%rsp), %rbx
movq 0x50(%rsp), %rax
movq %rax, %rdx
movq 0x68(%rsp), %rcx
subq %rcx, %rdx
addq %rcx, %rax
movq %rdx, 0x98(%rsp)
movq %rax, 0x90(%rsp)
cmpq %rax, %rdx
jle 0x71fe9
movq %rbx, %r15
jmp 0x7206a
xorl %ebp, %ebp
movq %rbx, %r15
movl (%r14), %edx
decl %edx
movl %r13d, %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x72060
movl (%r14), %edx
decl %edx
movq 0x70(%rsp), %rax
leal (%rax,%rbp), %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x72060
movq 0x58(%rsp), %rax
movq (%rax,%rbx,8), %rax
movl (%rax,%r13,4), %eax
testl %eax, %eax
je 0x72060
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rbx,8), %rcx
mulsd (%rcx,%r13,8), %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
addl %eax, 0x40(%rsp)
movl $0x1, 0x44(%rsp)
incq %rbx
incl %ebp
cmpl %ebp, %r12d
jne 0x71fee
movq 0x90(%rsp), %rax
cmpq %rax, 0x98(%rsp)
jle 0x72086
movq 0x58(%rsp), %rbx
jmp 0x7211f
xorl %r13d, %r13d
movq %r15, %rbp
movq 0x58(%rsp), %rbx
movq 0x48(%rsp), %rdi
movl (%r14), %edx
decl %edx
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x7210b
movl (%r14), %edx
decl %edx
leal (%r15,%r13), %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x7210b
movq (%rbx,%rbp,8), %rax
movq 0x48(%rsp), %rdi
movl (%rax,%rdi,4), %eax
testl %eax, %eax
je 0x72110
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rbp,8), %rcx
movq 0x48(%rsp), %rdx
mulsd (%rcx,%rdx,8), %xmm0
movq 0x48(%rsp), %rdi
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
addl %eax, 0x40(%rsp)
movl $0x1, 0x44(%rsp)
jmp 0x72110
movq 0x48(%rsp), %rdi
incq %rbp
incl %r13d
cmpl %r13d, %r12d
jne 0x72096
movq 0x68(%rsp), %rax
incq %rax
movq 0x78(%rsp), %rcx
decq %rcx
decl %r15d
addl $0x2, %r12d
movl %r15d, %edx
movq %rdx, 0x70(%rsp)
cmpl $0x0, 0x44(%rsp)
movq 0x88(%rsp), %rdx
je 0x71e85
xorps %xmm0, %xmm0
cvtsi2sdl 0x40(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x38(%rsp), %r15
movq 0x28(%r15), %rax
movq 0x50(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd %xmm1, (%rax,%rdx,8)
movq 0x80(%rsp), %rcx
incq %rdx
movslq (%r14), %rax
incq %rcx
cmpq %rax, %rdx
jl 0x71e34
movq 0x50(%rsp), %rdx
incq %rdx
movslq (%r14), %rax
incl 0x64(%rsp)
movq %rdx, 0x50(%rsp)
cmpq %rax, %rdx
jl 0x71e21
jmp 0x722ff
movslq (%r14), %rax
testq %rax, %rax
jle 0x721ea
xorl %ecx, %ecx
movl (%r12,%rcx,4), %edx
testl %edx, %edx
je 0x721e2
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movq 0x20(%r15), %rdx
movsd (%rdx,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x721c1
cmpl $0x0, (%r14)
jle 0x722ff
movl $0x1, %ecx
xorl %r13d, %r13d
cmpl $0x0, (%r12,%r13,4)
jne 0x722ed
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movq %rcx, 0x48(%rsp)
movq %rcx, %r15
movq %r13, 0x58(%rsp)
xorl %ebx, %ebx
movl (%r14), %edx
decl %edx
leal -0x1(%r13), %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x7226d
movl -0x4(%r12,%r13,4), %eax
testl %eax, %eax
je 0x7226d
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x20(%rcx), %rcx
mulsd -0x8(%rcx,%r13,8), %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
addl %eax, %ebp
movl $0x1, %ebx
movl (%r14), %edx
decl %edx
movl %r15d, %edi
xorl %esi, %esi
callq 0x38d18
testl %eax, %eax
je 0x722b5
movl (%r12,%r15,4), %eax
testl %eax, %eax
je 0x722b5
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rcx
movq 0x20(%rcx), %rcx
mulsd (%rcx,%r15,8), %xmm0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
addl %eax, %ebp
movl $0x1, %ebx
decq %r13
incq %r15
testl %ebx, %ebx
je 0x72222
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x38(%rsp), %r15
movq 0x20(%r15), %rax
movq 0x58(%rsp), %r13
movsd %xmm1, (%rax,%r13,8)
movq 0x48(%rsp), %rcx
incq %r13
movslq (%r14), %rax
incq %rcx
cmpq %rax, %r13
jl 0x721fc
leaq 0x6056a(%rip), %rax     # 0xd2870
movq (%rax), %rax
cmpl $0x3, 0x1bc(%rax)
jl 0x7231f
movl (%r14), %esi
movq %rbx, %rdi
callq 0x3915f
jmp 0x72327
movq %r12, %rdi
callq 0x39194
movq %r15, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq