// File: /workspace/github_c_makefile_star2_O3/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 0x30309
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 0x4bb0b
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 0x77036
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
callq 0x76b4b
movq %rax, %rbp
movq %rdx, %r14
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x3ed51
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x3ed51
movq (%r13), %rdi
callq 0x30309
movl %eax, %r15d
cltq
movq %rax, 0xa8(%rsp)
leaq (,%rax,8), %rdi
leaq 0x5ed86(%rip), %rsi     # 0xaa6f4
movl $0x14, %edx
callq 0x3a0e1
movq %rax, %r12
movq (%r13), %rbx
movsd 0x5a389(%rip), %xmm1    # 0xa5d10
testq %rbx, %rbx
je 0x4b9fd
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movsd %xmm1, (%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x76f0b
movapd %xmm0, 0x50(%rsp)
movq %rbx, %rdi
callq 0x2f35e
movsd (%rsp), %xmm1
movapd 0x50(%rsp), %xmm2
subsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andpd 0x5a811(%rip), %xmm0    # 0xa61e0
movlpd %xmm0, (%r12,%r13,8)
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
incq %r13
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x4b993
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd %xmm0, %xmm1
movq 0x90(%rsp), %r13
leaq 0x89e6c(%rip), %rax     # 0xd5870
movq (%rax), %rax
movl 0x1c0(%rax), %edi
movapd %xmm1, %xmm0
movl %r15d, %esi
movsd 0x59b04(%rip), %xmm1    # 0xa5520
movapd %xmm1, %xmm2
callq 0x48a31
xorpd %xmm1, %xmm1
movq 0xb0(%rsp), %rbx
testl %ebx, %ebx
je 0x4ba42
movsd 0x98(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm0, (%rsp)
ucomisd 0x20(%rsp), %xmm1
jbe 0x4ba8b
movq %rbp, %rdi
movq %r14, %rsi
movsd %xmm1, 0x50(%rsp)
callq 0x3dae3
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 -0x114af(%rip), %rcx    # 0x3a5f3
callq 0x4240
subl 0x18(%rsp), %r15d
movslq %r15d, %rax
movsd -0x8(%r12,%rax,8), %xmm0
addsd (%r12,%rax,8), %xmm0
mulsd 0x5a254(%rip), %xmm0    # 0xa5d18
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movsd %xmm0, 0xa0(%rsp)
movq %rbp, %rdx
movq %r14, %rcx
movl $0x3e, %r8d
callq 0x4b754
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3ed51
leal 0x1(%rbx), %ecx
movsd (%rsp), %xmm0
movapd %xmm0, %xmm1
movl 0x14(%rsp), %eax
cmpl %eax, %ebx
jne 0x4b8e8
jmp 0x4bb0f
movapd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x30648
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 0x4b754
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 0x3a0b4
movq %rax, %r15
shlq $0x4, %r14
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3dae3
movq %rax, (%r15,%r14)
movq %r15, 0x68(%rsp)
movq %rdx, 0x8(%r15,%r14)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3ed51
movq (%r13), %rdi
callq 0x30309
movsd (%rsp), %xmm1
cmpl 0x10(%rsp), %eax
jg 0x4b8c2
movq 0x28(%rsp), %rsi
movq %r13, %rdi
callq 0x30648
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)