// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/multiplemodel.c
// Function: mmelin1
int mmelin1(Instanceptr* traindata)
{
	/*!Last Changed 11.04.2005*/
	matrix X, r, w, bestw, *models;
	Instanceptr removed = NULL, modelremoved = NULL, tmp;
	int i, finished = 1, iteration, size, j, mindatasize, modelcount, first;
	double totalerror, *residuals, threshold, upperbound, upperbefore, bestthreshold, ratio, bestratio;
	size = data_size(*traindata);
 mindatasize = size / 20;
	modelcount = 0;
	while (finished)
	 {
		 iteration = size / mindatasize;
			bestratio = 0;
			first = 1;
		 for (i = 0; i < iteration - 2; i++)
			 {
     create_regression_matrices(*traindata, &X, &r);
    	w = multivariate_regression(X, r);
	  		matrix_free(X);
		  	matrix_free(r);
    	size = data_size(*traindata);
					residuals = safemalloc(size * sizeof(double), "mmelin", 20);
					j = 0;
					totalerror = 0;
					tmp = *traindata;
					while (tmp)
					 {
						 residuals[j] = fabs(linear_fit_value(w, tmp) - give_regressionvalue(tmp));
							totalerror += residuals[j] * residuals[j]; 
						 tmp = tmp->next;
							j++;
					 }
					totalerror /= j;
     upperbound = srm_regression(totalerror, current_dataset->multifeaturecount, size, 1.0, 1.0);
					if (i != 0)
						 ratio = upperbefore / upperbound;
					else
						 ratio = 0;
					upperbefore = upperbound;
     if (ratio > bestratio)
					 {
						 if (!first)
								 matrix_free(bestw);
       bestw = matrix_copy(w);
							bestratio = ratio;
							bestthreshold = threshold;
					 }
     qsort(residuals, size, sizeof(double), sort_function_double_ascending);
					threshold = (residuals[size - mindatasize - 1] + residuals[size - mindatasize]) / 2;
     remove_instances_according_to_threshold(traindata, &modelremoved, threshold, w, '>');
					matrix_free(w);
			 }
			merge_instancelist(traindata, modelremoved);
   remove_instances_according_to_threshold(traindata, &removed, bestthreshold, bestw, '<');
			modelremoved = NULL;
			modelcount++;
			models = alloc(models, modelcount * sizeof(matrix), modelcount);
			models[modelcount - 1] = matrix_copy(bestw);
			matrix_free(bestw);
			size = data_size(*traindata);
			if (size <= 2 * mindatasize)
				 finished = 0;
	 }
	merge_instancelist(traindata, removed);
	removed = NULL;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %r13
xorl %ebx, %ebx
leaq 0x28(%rsp), %rax
movq %rbx, (%rax)
leaq 0x8(%rsp), %rax
movq %rbx, (%rax)
movq (%rdi), %rdi
callq 0x2fa14
movslq %eax, %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rcx
shrq $0x3f, %rcx
sarq $0x23, %rdx
addl %ecx, %edx
movq %rdx, 0x18(%rsp)
leal (%rdx,%rdx), %ecx
movl %ecx, 0x10(%rsp)
movq %r13, 0x90(%rsp)
movq %rbx, 0x70(%rsp)
cltd
idivl 0x18(%rsp)
cmpl $0x3, %eax
jl 0x4a681
addl $-0x3, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %ecx, %ecx
movl %eax, 0x14(%rsp)
movsd %xmm1, 0x98(%rsp)
movq %rcx, 0xb0(%rsp)
movq (%r13), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x75733
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
callq 0x7526c
movq %rax, %rbp
movq %rdx, %r14
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x3dbad
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x3dbad
movq (%r13), %rdi
callq 0x2fa14
movl %eax, %r15d
cltq
movq %rax, 0xa8(%rsp)
leaq (,%rax,8), %rdi
leaq 0x5d170(%rip), %rsi     # 0xa7654
movl $0x14, %edx
callq 0x390f5
movq %rax, %r12
movq (%r13), %rbx
movsd 0x58813(%rip), %xmm1    # 0xa2d10
testq %rbx, %rbx
je 0x4a573
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movsd %xmm1, (%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x75608
movapd %xmm0, 0x50(%rsp)
movq %rbx, %rdi
callq 0x2eaee
movsd (%rsp), %xmm1
movapd 0x50(%rsp), %xmm2
subsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andpd 0x58c9b(%rip), %xmm0    # 0xa31e0
movlpd %xmm0, (%r12,%r13,8)
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
incq %r13
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x4a509
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd %xmm0, %xmm1
movq 0x90(%rsp), %r13
leaq 0x882f6(%rip), %rax     # 0xd2870
movq (%rax), %rax
movl 0x1c0(%rax), %edi
movapd %xmm1, %xmm0
movl %r15d, %esi
movsd 0x57f8e(%rip), %xmm1    # 0xa2520
movapd %xmm1, %xmm2
callq 0x4768a
xorpd %xmm1, %xmm1
movq 0xb0(%rsp), %rbx
testl %ebx, %ebx
je 0x4a5b8
movsd 0x98(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm0, (%rsp)
ucomisd 0x20(%rsp), %xmm1
jbe 0x4a601
movq %rbp, %rdi
movq %r14, %rsi
movsd %xmm1, 0x50(%rsp)
callq 0x3ca3c
movq %rax, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x8, %edx
movq %r12, %rdi
movq 0xa8(%rsp), %rsi
leaq -0x11031(%rip), %rcx    # 0x395e7
callq 0x4240
subl 0x18(%rsp), %r15d
movslq %r15d, %rax
movsd -0x8(%r12,%rax,8), %xmm0
addsd (%r12,%rax,8), %xmm0
mulsd 0x586de(%rip), %xmm0    # 0xa2d18
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movsd %xmm0, 0xa0(%rsp)
movq %rbp, %rdx
movq %r14, %rcx
movl $0x3e, %r8d
callq 0x4a2ca
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3dbad
leal 0x1(%rbx), %ecx
movsd (%rsp), %xmm0
movapd %xmm0, %xmm1
movl 0x14(%rsp), %eax
cmpl %eax, %ebx
jne 0x4a45e
jmp 0x4a685
movapd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
movsd 0x78(%rsp), %xmm0
movq 0x88(%rsp), %r12
movq %r12, %rdx
movq 0x80(%rsp), %rbp
movq %rbp, %rcx
movl $0x3c, %r8d
callq 0x4a2ca
movq $0x0, 0x8(%rsp)
movq 0x70(%rsp), %r14
leaq 0x1(%r14), %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
movq 0x68(%rsp), %rdi
movl %ebx, %edx
callq 0x390c8
movq %rax, %r15
shlq $0x4, %r14
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3ca3c
movq %rax, (%r15,%r14)
movq %r15, 0x68(%rsp)
movq %rdx, 0x8(%r15,%r14)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3dbad
movq (%r13), %rdi
callq 0x2fa14
movsd (%rsp), %xmm1
cmpl 0x10(%rsp), %eax
jg 0x4a438
movq 0x28(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop