// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/regressionrule.c
// Function: optimize_regression_ruleset
void optimize_regression_ruleset(Regressionruleset* r, Instanceptr* traindata, Instanceptr* cvdata, Regrule_parameterptr param)
{
	/*!Last Changed 03.04.2005*/
	/*!Last Changed 05.03.2005*/
	Regressionruleptr rule, before = NULL, replacementrule, revisionrule, next, newrule;
	Instanceptr removedtrain = NULL, removedcv = NULL, newsample, grow, prune;
	matrix w;
	int exitnow;
	double error, revisionerror, replacementerror, regvalue;
	rule = r->start;
	while (rule && can_be_divided_rule(*traindata, param->sigma, &w, &regvalue, r->leaftype))
	 {
			if (*cvdata)
				{
		   error = generalization_error_of_regression_ruleset(*r, *cvdata, param->modelselectionmethod);
     divide_instancelist(traindata, &prune, &grow, 3);
     replacementrule = grow_regression_rule(&grow, param);
  			if (replacementrule->conditioncount > 0)
						 prune_regression_rule(replacementrule, prune, param->modelselectionmethod);
				}
			else
			 {
		   error = generalization_error_of_regression_ruleset(*r, *traindata, param->modelselectionmethod);
				 newsample = bootstrap_sample_without_stratification(*traindata);
					replacementrule = grow_regression_rule(&newsample, param);
  			if (replacementrule->conditioncount > 0)
						 prune_regression_rule(replacementrule, *traindata, param->modelselectionmethod);
			 }
			remove_regression_rule_after(r, before, rule);
			add_regression_rule_after(r, before, replacementrule);
			if (*cvdata)
			 {
  		 replacementerror = generalization_error_of_regression_ruleset(*r, *cvdata, param->modelselectionmethod);
				 revisionrule = grow_regression_revision_rule(&grow, rule, param);
					if (revisionrule->conditioncount > 0)
						 prune_regression_rule(revisionrule, prune, param->modelselectionmethod);
			 }
			else
			 {
				 replacementerror = generalization_error_of_regression_ruleset(*r, *traindata, param->modelselectionmethod);
  			deallocate(newsample);
				 revisionrule = grow_regression_revision_rule(traindata, rule, param);
					if (revisionrule->conditioncount > 0)
       prune_regression_rule(revisionrule, *traindata, param->modelselectionmethod);
			 }
			remove_regression_rule_after(r, before, replacementrule);
			add_regression_rule_after(r, before, revisionrule);
			if (*cvdata)
			 {
  		 revisionerror = generalization_error_of_regression_ruleset(*r, *cvdata, param->modelselectionmethod);
  			*traindata = restore_instancelist(*traindata, grow);
  			merge_instancelist(traindata, prune);
			 }
			else
				 revisionerror = generalization_error_of_regression_ruleset(*r, *traindata, param->modelselectionmethod);
   if ((revisionerror < error) || (replacementerror < error))
				 if (replacementerror <= revisionerror)
					 {
						 remove_regression_rule_after(r, before, revisionrule);
							add_regression_rule_after(r, before, replacementrule);
							free_regression_rule(*revisionrule);
							safe_free(revisionrule);
							free_regression_rule(*rule);
							safe_free(rule);
  				 remove_regression_covered(traindata, &removedtrain, replacementrule);
    			if (*cvdata)
    				 remove_regression_covered(cvdata, &removedcv, replacementrule);
							rule = replacementrule;
					 }
					else
					 {
							free_regression_rule(*replacementrule);
							safe_free(replacementrule);
							free_regression_rule(*rule);
							safe_free(rule);						 
  				 remove_regression_covered(traindata, &removedtrain, revisionrule);
    			if (*cvdata)
    				 remove_regression_covered(cvdata, &removedcv, revisionrule);
							rule = revisionrule;
					 }
			else
			 {
					remove_regression_rule_after(r, before, revisionrule);
					add_regression_rule_after(r, before, rule);
					free_regression_rule(*revisionrule);
					safe_free(revisionrule);
					free_regression_rule(*replacementrule);
					safe_free(replacementrule);
  			remove_regression_covered(traindata, &removedtrain, rule);
  			if (*cvdata)
    		 remove_regression_covered(cvdata, &removedcv, rule);
			 }
			before = rule;
			rule = rule->next;
	 }
	if (!rule)
	 {
  	while (can_be_divided_rule(*traindata, param->sigma, &w, &regvalue, r->leaftype))
			 {
	  	 exitnow = 0;
  			if (*cvdata)
					 {
       divide_instancelist(traindata, &prune, &grow, 3);
       newrule = grow_regression_rule(&grow, param);
					 }
			  else
				  	newrule = grow_regression_rule(traindata, param);
  	  if (newrule && newrule->start)
					 {
    			if (*cvdata)
				  		 prune_regression_rule(newrule, prune, param->modelselectionmethod);
				  	else
					  	 prune_regression_rule(newrule, *traindata, param->modelselectionmethod);
					  if (newrule->start)
							 {
				  		 add_regression_rule(r, newrule);
      			if (*cvdata)
									 {
  			  	   *traindata = restore_instancelist(*traindata, grow);
  				  	  merge_instancelist(traindata, prune);
									 }
							 }
				  	else
					  	 exitnow = 1;
					 }
			  else
				   exitnow = 1;
			  if (!exitnow)
					 {
			  	 remove_regression_covered(traindata, &removedtrain, newrule);
  	  	 if (r->leaftype == LINEARLEAF)
      		 matrix_free(w);				 
					 }
			  else
					 {
    			if (*cvdata)
								{
  			  	 *traindata = restore_instancelist(*traindata, grow);
  				  	merge_instancelist(traindata, prune);
								}
				   break;
					 }
			 }
	  if (r->leaftype == CONSTANTLEAF)
	  	 r->regressionvalue = regvalue;
	  else
    	r->w = w;
	 }
	else
	 {
  	while (rule)
			 {
	  	 next = rule->next;
					if (before)
		  		 before->next = next;
					else
							r->start = next;
     free_regression_rule(*rule);
		  	safe_free(rule);
     r->rulecount--;
     if (next == NULL)
			  		r->end = before;
			  rule = next;
			 }
	  if (before)
	    before->next = NULL;
	 }
	merge_instancelist(traindata, removedtrain);
	if (*cvdata)
		 merge_instancelist(cvdata, removedcv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %rbp
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0x70(%rsp)
movq %rax, 0xb8(%rsp)
movq (%rdi), %rbx
testq %rbx, %rbx
movq %rdx, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
je 0x74c46
xorl %r12d, %r12d
movq %r14, 0x88(%rsp)
movq (%r14), %rdi
movsd 0x10(%rbp), %xmm0
movl 0x20(%r15), %ecx
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x74e80
testl %eax, %eax
je 0x74ddf
movq (%r13), %rdi
testq %rdi, %rdi
movq %r12, 0x78(%rsp)
je 0x7473e
movl 0x4(%rbp), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x74192
movsd %xmm0, 0x98(%rsp)
movq %r14, %r13
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x68(%rsp), %r12
movq %r12, %rdx
movl $0x3, %ecx
callq 0x31019
movq %r12, %rdi
movq %rbp, %rsi
callq 0x74f52
movq %rbp, %rcx
movq %rax, %rbp
cmpl $0x0, 0x30(%rax)
jle 0x7472f
movq 0x60(%rsp), %rsi
movl 0x4(%rcx), %edx
movq %rbp, %rdi
callq 0x750c1
movq 0x78(%rsp), %r12
movq %r13, %r14
movq 0x50(%rsp), %r13
jmp 0x747b1
movq (%r14), %rdi
movl 0x4(%rbp), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x74192
movsd %xmm0, 0x98(%rsp)
movq (%r14), %rdi
callq 0x307e8
movq %rax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rdi
movq %rbp, %rsi
callq 0x74f52
movq %rbp, %rcx
movq %rax, %rbp
cmpl $0x0, 0x30(%rax)
jle 0x747b1
movq (%r14), %rsi
movl 0x4(%rcx), %edx
movq %rbp, %rdi
callq 0x750c1
movl 0x30(%r15), %ecx
cmpl $0x1, %ecx
jne 0x747d1
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movl %ecx, 0x30(%r15)
testq %r12, %r12
je 0x747fe
movq 0x38(%r12), %rax
jmp 0x747e7
movq 0x38(%rbx), %rax
testq %rax, %rax
jne 0x747de
movq %r12, 0x8(%r15)
movl %ecx, 0x30(%r15)
testq %r12, %r12
je 0x74800
movq %rax, 0x38(%rbp)
movq %rbp, 0x38(%r12)
movq 0x38(%rbp), %rax
movl $0x0, 0x4c(%rsp)
jmp 0x7480d
xorl %eax, %eax
movq %rax, 0x38(%rbp)
movq %rbp, (%r15)
movb $0x1, %cl
movl %ecx, 0x4c(%rsp)
testq %rax, %rax
jne 0x74816
movq %rbp, 0x8(%r15)
movq (%r13), %rdi
testq %rdi, %rdi
je 0x7488b
movq 0x58(%rsp), %r14
movl 0x4(%r14), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x74192
movsd %xmm0, 0x90(%rsp)
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x751e2
movq %rax, %r13
cmpl $0x0, 0x30(%rax)
jle 0x74906
movq 0x60(%rsp), %rsi
movl 0x4(%r14), %edx
movq %r13, %rdi
callq 0x750c1
jmp 0x74906
movq (%r14), %rdi
movq %r14, %r12
movq 0x58(%rsp), %r14
movl 0x4(%r14), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x74192
movsd %xmm0, 0x90(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x2ff55
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x751e2
movq %rax, %r13
cmpl $0x0, 0x30(%rax)
jle 0x74901
movq (%r12), %rsi
movl 0x4(%r14), %edx
movq %r13, %rdi
callq 0x750c1
movq 0x78(%rsp), %r12
movl 0x30(%r15), %ecx
cmpl $0x1, %ecx
jne 0x7492d
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movl %ecx, 0x30(%r15)
cmpb $0x0, 0x4c(%rsp)
movq 0x88(%rsp), %r14
je 0x74956
xorl %eax, %eax
jmp 0x7494d
movq 0x38(%rbp), %rax
testq %rax, %rax
movq 0x88(%rsp), %r14
jne 0x74942
movq %r12, 0x8(%r15)
movl %ecx, 0x30(%r15)
cmpb $0x0, 0x4c(%rsp)
je 0x7495b
movq %rax, 0x38(%r13)
movq %r13, (%r15)
jmp 0x74968
movq 0x38(%r12), %rax
movq %rax, 0x38(%r13)
movq %r13, 0x38(%r12)
movq 0x38(%r13), %rax
testq %rax, %rax
jne 0x74971
movq %r13, 0x8(%r15)
movq 0x50(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x749e1
movq 0x58(%rsp), %rax
movl 0x4(%rax), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x74192
movsd %xmm0, 0xc0(%rsp)
movq (%r14), %rdi
movq 0x68(%rsp), %rsi
callq 0x31345
movq %rax, (%r14)
movq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x30648
movsd 0xc0(%rsp), %xmm0
jmp 0x74a18
movq (%r14), %rdi
movq 0x58(%rsp), %rax
movl 0x4(%rax), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movupd (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x74192
movsd 0x98(%rsp), %xmm2
ucomisd %xmm0, %xmm2
movsd 0x90(%rsp), %xmm1
ja 0x74a62
ucomisd %xmm1, %xmm2
ja 0x74a62
movl 0x30(%r15), %ecx
cmpl $0x1, %ecx
jne 0x74aed
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
movl %ecx, 0x30(%r15)
cmpb $0x0, 0x4c(%rsp)
je 0x74b36
xorl %eax, %eax
jmp 0x74b05
ucomisd %xmm1, %xmm0
jae 0x74ac8
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %rbp, %rdi
callq 0x3a180
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %rbx, %rdi
callq 0x3a180
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
movq %r13, %rdx
callq 0x7533e
movq 0x50(%rsp), %rax
cmpq $0x0, (%rax)
movq %r13, %rbp
movq %rax, %r13
jne 0x74ba6
jmp 0x74bb9
movl 0x30(%r15), %ecx
cmpl $0x1, %ecx
jne 0x74b0e
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
movl %ecx, 0x30(%r15)
cmpb $0x0, 0x4c(%rsp)
je 0x74bd0
xorl %eax, %eax
jmp 0x74b2a
movq 0x38(%r13), %rax
testq %rax, %rax
jne 0x74afa
movq %r12, 0x8(%r15)
movl %ecx, 0x30(%r15)
cmpb $0x0, 0x4c(%rsp)
je 0x74b3b
movq %rax, 0x38(%rbx)
movq %rbx, (%r15)
jmp 0x74b48
movq 0x38(%r13), %rax
testq %rax, %rax
jne 0x74b1b
movq %r12, 0x8(%r15)
movl %ecx, 0x30(%r15)
cmpb $0x0, 0x4c(%rsp)
je 0x74bd5
movq %rax, 0x38(%rbp)
movq %rbp, (%r15)
jmp 0x74be2
movq 0x38(%r12), %rax
movq %rax, 0x38(%rbx)
movq %rbx, 0x38(%r12)
movq 0x38(%rbx), %rax
testq %rax, %rax
jne 0x74b51
movq %rbx, 0x8(%r15)
movl $0x9, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %r13, %rdi
callq 0x3a180
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %rbp, %rdi
callq 0x3a180
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
callq 0x7533e
movq 0x50(%rsp), %r13
cmpq $0x0, (%r13)
movq %rbx, %rbp
je 0x74bb9
movq %r13, %rdi
leaq 0xb8(%rsp), %rsi
movq %rbp, %rdx
callq 0x7533e
movq 0x38(%rbp), %rbx
movq %rbp, %r12
testq %rbx, %rbx
movq 0x58(%rsp), %rbp
jne 0x7467a
jmp 0x74c46
movq 0x38(%r12), %rax
movq %rax, 0x38(%rbp)
movq %rbp, 0x38(%r12)
movq 0x38(%rbp), %rax
testq %rax, %rax
jne 0x74beb
movq %rbp, 0x8(%r15)
movl $0x9, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %r13, %rdi
callq 0x3a180
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %rbx, %rdi
callq 0x3a180
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
movq %rbp, %rdx
callq 0x7533e
movq 0x50(%rsp), %r13
cmpq $0x0, (%r13)
jne 0x74ba6
jmp 0x74bb9
movq (%r14), %rdi
movsd 0x10(%rbp), %xmm0
movl 0x20(%r15), %ecx
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x74e80
testl %eax, %eax
je 0x74dc7
leaq 0x60(%rsp), %rbx
leaq 0x70(%rsp), %r12
leaq 0x68(%rsp), %rbp
cmpq $0x0, (%r13)
movq %r14, %rdi
je 0x74c9e
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x3, %ecx
callq 0x31019
movq %rbp, %rdi
movq 0x58(%rsp), %rsi
callq 0x74f52
testq %rax, %rax
je 0x74d9e
movq %rax, %r13
cmpq $0x0, 0x20(%rax)
je 0x74d9e
movq 0x50(%rsp), %rax
cmpq $0x0, (%rax)
movq 0x58(%rsp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rax
cmoveq %r14, %rax
movq (%rax), %rsi
movq %r13, %rdi
callq 0x750c1
cmpq $0x0, 0x20(%r13)
je 0x74d9e
movl 0x30(%r15), %eax
movq %r15, %rcx
testl %eax, %eax
je 0x74d00
movq 0x8(%r15), %rcx
addq $0x38, %rcx
movq %r13, (%rcx)
movq %r13, 0x8(%r15)
movq $0x0, 0x38(%r13)
incl %eax
movl %eax, 0x30(%r15)
movq 0x50(%rsp), %rax
cmpq $0x0, (%rax)
je 0x74d3d
movq (%r14), %rdi
movq 0x68(%rsp), %rsi
callq 0x31345
movq %rax, (%r14)
movq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x30648
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x7533e
movl 0x20(%r15), %ecx
cmpl $0x1, %ecx
jne 0x74d6d
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x3ed51
movl 0x20(%r15), %ecx
movq (%r14), %rdi
movq 0x58(%rsp), %rax
movsd 0x10(%rax), %xmm0
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x74e80
testl %eax, %eax
movq 0x50(%rsp), %r13
jne 0x74c7e
jmp 0x74dc7
movq 0x50(%rsp), %r13
cmpq $0x0, (%r13)
je 0x74dc7
movq (%r14), %rdi
movq 0x68(%rsp), %rsi
callq 0x31345
movq %rax, (%r14)
movq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x30648
cmpl $0x0, 0x20(%r15)
je 0x74e3b
movupd 0xa0(%rsp), %xmm0
movupd %xmm0, 0x10(%r15)
jmp 0x74e4a
leaq 0x38(%r12), %r13
testq %r12, %r12
movq %r13, 0x58(%rsp)
cmoveq %r15, %r13
movq 0x38(%rbx), %rbp
movq %rbp, (%r13)
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x742e0
movq %rbx, %rdi
callq 0x3a180
decl 0x30(%r15)
movq %rbp, %rbx
testq %rbp, %rbp
jne 0x74df0
movq %r12, 0x8(%r15)
testq %r12, %r12
movq 0x50(%rsp), %r13
je 0x74e4a
movq 0x58(%rsp), %rax
movq $0x0, (%rax)
jmp 0x74e4a
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x28(%r15)
movq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x30648
cmpq $0x0, (%r13)
je 0x74e6e
movq 0xb8(%rsp), %rsi
movq %r13, %rdi
callq 0x30648
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq