// File: /workspace/github_c_makefile_star2_O1/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, 0x58(%rsp)
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
movq (%rdi), %rbp
testq %rbp, %rbp
movq %rsi, 0x50(%rsp)
je 0x73391
xorl %r12d, %r12d
movq %r14, 0x60(%rsp)
movq (%r13), %rdi
movq 0x58(%rsp), %rax
movsd 0x10(%rax), %xmm0
movl 0x20(%r15), %ecx
leaq 0xa8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x735c2
testl %eax, %eax
je 0x7352b
movq (%r14), %rdi
testq %rdi, %rdi
je 0x72e8d
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 0x728bf
movsd %xmm0, 0xa0(%rsp)
movq %r13, %rdi
leaq 0x68(%rsp), %rsi
leaq 0x70(%rsp), %rbx
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x30746
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73694
movq %rax, %rbx
cmpl $0x0, 0x30(%rax)
jle 0x72f07
movq 0x68(%rsp), %rsi
jmp 0x72efb
movq (%r13), %rdi
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 0x728bf
movsd %xmm0, 0xa0(%rsp)
movq (%r13), %rdi
callq 0x2fede
movq %rax, 0xb8(%rsp)
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x73694
movq %rax, %rbx
cmpl $0x0, 0x30(%rax)
jle 0x72f07
movq (%r13), %rsi
movl 0x4(%r14), %edx
movq %rbx, %rdi
callq 0x7380b
movl 0x30(%r15), %eax
movl %eax, %ecx
decl %ecx
movl %ecx, 0x30(%r15)
je 0x72f2e
movq 0x38(%rbp), %rcx
testq %rcx, %rcx
jne 0x72f22
movq %r12, 0x8(%r15)
testq %r12, %r12
je 0x72f37
movq %rcx, 0x38(%r12)
jmp 0x72f3a
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
jmp 0x72f3a
movq %rcx, (%r15)
movl %eax, 0x30(%r15)
leaq 0x38(%r12), %rax
testq %r12, %r12
movq %rax, 0x88(%rsp)
cmoveq %r15, %rax
movq (%rax), %rcx
movq %rcx, 0x38(%rbx)
movq %rbx, (%rax)
cmpq $0x0, 0x38(%rbx)
jne 0x72f67
movq %rbx, 0x8(%r15)
movq 0x60(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x72fcb
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 0x728bf
movsd %xmm0, 0x98(%rsp)
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x73919
movq %rax, %r13
cmpl $0x0, 0x30(%rax)
jle 0x7303c
movq 0x68(%rsp), %rsi
jmp 0x73030
movq (%r13), %rdi
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 0x728bf
movsd %xmm0, 0x98(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x2f616
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x73919
movq %r13, %rcx
movq %rax, %r13
cmpl $0x0, 0x30(%rax)
jle 0x7303c
movq (%rcx), %rsi
movl 0x4(%r14), %edx
movq %r13, %rdi
callq 0x7380b
movl 0x30(%r15), %eax
movl %eax, %ecx
decl %ecx
movl %ecx, 0x30(%r15)
je 0x73063
movq 0x38(%rbx), %rcx
testq %rcx, %rcx
jne 0x73057
movq %r12, 0x8(%r15)
testq %r12, %r12
je 0x7306c
movq %rcx, 0x38(%r12)
jmp 0x7306f
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
jmp 0x7306f
movq %rcx, (%r15)
testq %r12, %r12
movl %eax, 0x30(%r15)
movq 0x88(%rsp), %rax
cmoveq %r15, %rax
movq (%rax), %rcx
movq %rcx, 0x38(%r13)
movq %r13, (%rax)
cmpq $0x0, 0x38(%r13)
jne 0x73097
movq %r13, 0x8(%r15)
movq 0x60(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x73108
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 0x728bf
movsd %xmm0, 0xc0(%rsp)
movq 0x50(%rsp), %r14
movq (%r14), %rdi
movq 0x70(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r14)
movq 0x68(%rsp), %rsi
movq %r14, %rdi
callq 0x2fd59
movsd 0xc0(%rsp), %xmm0
jmp 0x73140
movq 0x50(%rsp), %rax
movq (%rax), %rdi
movl 0x4(%r14), %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 0x728bf
movsd 0xa0(%rsp), %xmm2
ucomisd %xmm0, %xmm2
movsd 0x98(%rsp), %xmm1
ja 0x73190
ucomisd %xmm1, %xmm2
ja 0x73190
movl 0x30(%r15), %eax
movl %eax, %ecx
decl %ecx
movl %ecx, 0x30(%r15)
je 0x7323b
movq 0x38(%r13), %rcx
testq %rcx, %rcx
jne 0x7317d
movq %r12, 0x8(%r15)
testq %r12, %r12
je 0x73246
movq %rcx, 0x38(%r12)
jmp 0x73249
ucomisd %xmm1, %xmm0
jae 0x73209
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %rbx, %rdi
callq 0x39194
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %rbp, %rdi
callq 0x39194
movq 0x50(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movq %r13, %rdx
callq 0x73a73
movq 0x60(%rsp), %r14
cmpq $0x0, (%r14)
je 0x731fc
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
movq %r13, %rdx
callq 0x73a73
movq %r13, %rbx
movq 0x50(%rsp), %r13
jmp 0x73381
movl 0x30(%r15), %eax
movl %eax, %ecx
decl %ecx
movl %ecx, 0x30(%r15)
je 0x732e2
movq 0x38(%r13), %rcx
testq %rcx, %rcx
jne 0x73228
movq %r12, 0x8(%r15)
testq %r12, %r12
je 0x732ed
movq %rcx, 0x38(%r12)
jmp 0x732f0
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
jmp 0x73249
movq %rcx, (%r15)
testq %r12, %r12
movl %eax, 0x30(%r15)
movq 0x88(%rsp), %rcx
cmoveq %r15, %rcx
movq (%rcx), %rax
movq %rax, 0x38(%rbp)
movq %rbp, (%rcx)
cmpq $0x0, 0x38(%rbp)
jne 0x73271
movq %rbp, 0x8(%r15)
movl $0x9, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %r13, %rdi
callq 0x39194
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %rbx, %rdi
callq 0x39194
movq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x78(%rsp), %rsi
movq %rbp, %rdx
callq 0x73a73
movq 0x60(%rsp), %r14
cmpq $0x0, (%r14)
je 0x732da
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
movq %rbp, %rdx
callq 0x73a73
movq %rbp, %rbx
jmp 0x73381
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
jmp 0x732f0
movq %rcx, (%r15)
testq %r12, %r12
movl %eax, 0x30(%r15)
movq 0x88(%rsp), %rcx
cmoveq %r15, %rcx
movq (%rcx), %rax
movq %rax, 0x38(%rbx)
movq %rbx, (%rcx)
cmpq $0x0, 0x38(%rbx)
jne 0x73318
movq %rbx, 0x8(%r15)
movl $0x9, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %r13, %rdi
callq 0x39194
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %rbp, %rdi
callq 0x39194
movq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x78(%rsp), %rsi
movq %rbx, %rdx
callq 0x73a73
movq 0x60(%rsp), %r14
cmpq $0x0, (%r14)
je 0x73381
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
movq %rbx, %rdx
callq 0x73a73
movq 0x38(%rbx), %rbp
movq %rbx, %r12
testq %rbp, %rbp
jne 0x72de1
movq (%r13), %rdi
movq 0x58(%rsp), %rax
movsd 0x10(%rax), %xmm0
movl 0x20(%r15), %ecx
leaq 0xa8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x735c2
testl %eax, %eax
je 0x73513
leaq 0x68(%rsp), %rbx
leaq 0x70(%rsp), %rbp
movq 0x50(%rsp), %r12
cmpq $0x0, (%r14)
movq %r13, %rdi
je 0x733ee
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl $0x3, %ecx
callq 0x30746
movq %rbp, %rdi
movq 0x58(%rsp), %rsi
callq 0x73694
testq %rax, %rax
je 0x734e9
movq %rax, %r13
cmpq $0x0, 0x20(%rax)
je 0x734e9
cmpq $0x0, (%r14)
movq %rbx, %rax
cmoveq 0x50(%rsp), %rax
movq (%rax), %rsi
movq 0x58(%rsp), %rax
movl 0x4(%rax), %edx
movq %r13, %rdi
callq 0x7380b
cmpq $0x0, 0x20(%r13)
je 0x734e9
movl 0x30(%r15), %eax
movq %r15, %rcx
testl %eax, %eax
je 0x7344d
movq 0x8(%r15), %rcx
addq $0x38, %rcx
movq %r13, (%rcx)
movq %r13, 0x8(%r15)
movq $0x0, 0x38(%r13)
incl %eax
movl %eax, 0x30(%r15)
cmpq $0x0, (%r14)
je 0x73487
movq (%r12), %rdi
movq 0x70(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r12)
movq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x2fd59
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
movq %r13, %rdx
callq 0x73a73
cmpl $0x1, 0x20(%r15)
jne 0x734b3
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x3dbad
movq 0x50(%rsp), %r13
movq (%r13), %rdi
movq 0x58(%rsp), %rax
movsd 0x10(%rax), %xmm0
movl 0x20(%r15), %ecx
leaq 0xa8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x735c2
testl %eax, %eax
jne 0x733cf
jmp 0x73513
cmpq $0x0, (%r14)
movq 0x50(%rsp), %r13
je 0x73513
movq (%r13), %rdi
movq 0x70(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r13)
movq 0x68(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
cmpl $0x0, 0x20(%r15)
je 0x7357e
movupd 0xa8(%rsp), %xmm0
movupd %xmm0, 0x10(%r15)
jmp 0x7358d
leaq 0x38(%r12), %rbx
testq %r12, %r12
cmoveq %r15, %rbx
movq 0x38(%rbp), %r14
movq %r14, (%rbx)
movl $0x9, %ecx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x72a03
movq %rbp, %rdi
callq 0x39194
decl 0x30(%r15)
movq %r14, %rbp
testq %r14, %r14
jne 0x73537
movq %r12, 0x8(%r15)
testq %r12, %r12
movq 0x60(%rsp), %r14
je 0x7358d
movq $0x0, 0x38(%r12)
jmp 0x7358d
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x28(%r15)
movq 0x78(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
cmpq $0x0, (%r14)
je 0x735b0
movq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x2fd59
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq