// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/ripper.c
// Function: optimize_ruleset
void optimize_ruleset(Ruleset* r, int positive, Instanceptr* instances, double proportion, Ripper_parameterptr param)
{
 /* Last Changed 24.11.2003 If after revising and renewing rules there are instances that are not covered by ruleset learn new rules to cover them*/
 /* Last Changed 20.06.2003 Rewritten*/
 /* Last Changed 21.05.2003*/
 int first, rulecount = 0, fp, fn, covered, uncovered;
 Decisionruleptr rule, replacementrule, revisionrule, before, next, newrule;
 Instanceptr grow, prune, removed = NULL, pruneremoved = NULL;
 double replacementdl, revisiondl, dl, smallestdl, dlnow, datadlwithout, ruledl, datadlwith;
 before = NULL;
 fp = 0;
 covered = 0;
 uncovered = data_size(*instances);
 fn = find_class_count(*instances, positive);
 rule = r->start;
 while (rule && rule->classno != positive)
  {
   before = rule;
   rule = rule->next;
   rulecount++;
  }
 while (rule && fn > 0)
  {
   dl = relative_compression(r, positive, rulecount, *instances, NULL, NULL, NULL, proportion);
   divide_instancelist(instances, &prune, &grow, 3);
   pruneremoved = NULL;
   replacementrule = growrule(&grow, positive, param);
   remove_rule_after(r, before, rule);
   add_rule_after(r, before, replacementrule);
   remove_covered_by_ruleset(&prune, &pruneremoved, *r, rulecount + 1);
   if (replacementrule->decisioncount > 0)
     prunerule_in_optimize_phase(replacementrule, prune, positive);
   update_counts(replacementrule, grow, 1);
   update_counts(replacementrule, prune, 0);
   update_counts(replacementrule, pruneremoved, 0);
   replacementdl = relative_compression(r, positive, rulecount, grow, prune, pruneremoved, NULL, proportion);
   revisionrule = grow_revision_rule(&grow, rule, positive, param);
   remove_rule_after(r, before, replacementrule);
   add_rule_after(r, before, revisionrule);
   prunerule_in_optimize_phase(revisionrule, prune, positive);
   update_counts(revisionrule, grow, 1);
   update_counts(revisionrule, prune, 0);
   update_counts(revisionrule, pruneremoved, 0);
   revisiondl = relative_compression(r, positive, rulecount, grow, prune, pruneremoved, NULL, proportion);
   *instances = restore_instancelist(*instances, grow);
   merge_instancelist(instances, prune);
   merge_instancelist(instances, pruneremoved);
   if (dless(replacementdl, dl) || dless(revisiondl, dl))
     if (dless(replacementdl,revisiondl))
      {
       remove_rule_after(r, before, revisionrule);
       add_rule_after(r, before, replacementrule);
       free_rule(*revisionrule);
       safe_free(revisionrule);
       free_rule(*rule);
       safe_free(rule);
       remove_covered(instances, &removed, replacementrule);
       rule = replacementrule;
      }
     else
      {
       free_rule(*replacementrule);
       safe_free(replacementrule);
       free_rule(*rule);
       safe_free(rule);       
       remove_covered(instances, &removed, revisionrule);
       rule = revisionrule;
      }
   else
    {
     remove_rule_after(r, before, revisionrule);
     add_rule_after(r, before, rule);
     free_rule(*revisionrule);
     safe_free(revisionrule);
     free_rule(*replacementrule);
     safe_free(replacementrule);
     remove_covered(instances, &removed, rule);
    }
   covered += (rule->falsepositives + rule->covered);
   uncovered -= (rule->falsepositives + rule->covered);
   fp += rule->falsepositives;
   fn -= rule->covered;
   before = rule;
   rule = rule->next;
   rulecount++;
  }
 if (!rule)
  {
   smallestdl = +INT_MAX;
   first = 1;
   while (fn > 0)
    {
     divide_instancelist(instances, &prune, &grow, 3);
     newrule = growrule(&grow, positive, param);
     if (newrule->start)
       prunerule(newrule, prune, positive, 2);
     if (!(newrule->start))
      {
       *instances = restore_instancelist(*instances, grow);
       merge_instancelist(instances, prune);
       free_rule(*newrule);
       safe_free(newrule);
       break;
      }
     update_counts(newrule, grow, 1);
     update_counts(newrule, prune, 0);
     *instances = restore_instancelist(*instances, grow);
     merge_instancelist(instances, prune);
     datadlwithout = description_length_of_exceptions(covered, uncovered, fp, fn, proportion);
     ruledl = description_length_of_rule(*newrule, r->possibleconditioncount);
     datadlwith = description_length_of_exceptions(covered + newrule->covered + newrule->falsepositives, uncovered - newrule->covered - newrule->falsepositives, fp + newrule->falsepositives, fn - newrule->covered, proportion);
     if (first)
       dlnow = datadlwith + ruledl;
     else
       dlnow = dlnow - datadlwithout + datadlwith + ruledl;
     if (dless(dlnow,smallestdl) && (first != 1 || error_rate_of_rule(*newrule) < 0.5))
      {
       smallestdl = dlnow;
       insert_rule_and_update_counts(newrule, r, &covered, &uncovered, &fp, &fn);
       remove_covered(instances, &removed, newrule);
      }
     else
       if ((dlnow - smallestdl > 64) || (error_rate_of_rule(*newrule) >= 0.5))
        {
         free_rule(*newrule);
         safe_free(newrule);
         break;
        }
       else
        {
         insert_rule_and_update_counts(newrule, r, &covered, &uncovered, &fp, &fn);
         remove_covered(instances, &removed, newrule);
        }
     first = 0;
    }
  }
 else
  {
   while (rule)
    {
     next = rule->next;
     if (rule->classno == positive)
      {
       before->next = next;
       free_rule(*rule);
       safe_free(rule);
       r->rulecount--;
       if (next == NULL)
         r->end = before;
      }
     else
       before = rule;
     rule = next;
    }
   if (before)
     before->next = NULL;
  }
 merge_instancelist(instances, removed);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, 0xc0(%rsp)
movsd %xmm0, 0x90(%rsp)
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
xorl %r12d, %r12d
movq %r12, 0x98(%rsp)
movq %r12, 0x80(%rsp)
movl %r12d, 0x74(%rsp)
movl %r12d, 0x70(%rsp)
movq (%rdx), %rdi
callq 0x30309
movl %eax, 0x6c(%rsp)
movq %rbx, 0x58(%rsp)
movq (%rbx), %rdi
movl %ebp, %esi
callq 0x114f9
movl %eax, 0x4c(%rsp)
movq (%r14), %r13
testq %r13, %r13
movq %r14, 0xa0(%rsp)
movl %ebp, 0x8c(%rsp)
je 0x79153
cmpl %ebp, (%r13)
jne 0x78bf6
xorl %ecx, %ecx
jmp 0x78c12
xorl %r12d, %r12d
movq %r13, %rcx
movq 0x30(%r13), %r13
testq %r13, %r13
je 0x79153
incl %r12d
cmpl %ebp, (%r13)
jne 0x78bf9
movq %rcx, 0x50(%rsp)
testl %eax, %eax
jle 0x790d5
movq 0x58(%rsp), %r15
movq (%r15), %rcx
xorl %ebx, %ebx
movq %rbx, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
movsd 0x90(%rsp), %xmm0
callq 0x9e939
movsd %xmm0, 0xb0(%rsp)
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x60(%rsp), %r15
movq %r15, %rdx
movl $0x3, %ecx
callq 0x31019
movq %rbx, 0x80(%rsp)
movq %r15, %rdi
movl %ebp, %esi
movq 0xc0(%rsp), %rdx
callq 0x9f37f
movq %rax, %r15
movq %r14, %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %r13, %rdx
callq 0x7c89d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x7c86e
incl %r12d
movq 0x20(%r14), %rax
movq %rax, 0x20(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl %r12d, %edx
callq 0x9f84f
cmpl $0x0, 0x28(%r15)
jle 0x78cf3
movq 0x40(%rsp), %rsi
movq %r15, %rdi
movl %ebp, %edx
callq 0x9e2d3
movq 0x60(%rsp), %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0x7c9ed
movq 0x40(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x7c9ed
movq 0x80(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x7c9ed
movq 0x60(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x80(%rsp), %r9
decl %r12d
xorl %eax, %eax
movq %rax, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
movsd 0x90(%rsp), %xmm0
callq 0x9e939
movsd %xmm0, 0x78(%rsp)
leaq 0x60(%rsp), %rdi
movq %r13, 0xb8(%rsp)
movq %r13, %rsi
movl %ebp, %edx
movq 0xc0(%rsp), %rcx
callq 0x789bc
movl %ebp, %ebx
movq %rax, %rbp
movq %r14, %rdi
movq 0x50(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x7c89d
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x7c86e
movq 0x40(%rsp), %rsi
movq %rbp, %rdi
movl %ebx, %edx
callq 0x9e2d3
movq 0x60(%rsp), %rsi
movq %rbp, %rdi
movl $0x1, %edx
callq 0x7c9ed
movq 0x40(%rsp), %rsi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x7c9ed
movq 0x80(%rsp), %rsi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x7c9ed
movq 0x60(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x80(%rsp), %r9
xorl %eax, %eax
movq %rax, (%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
movsd 0x90(%rsp), %xmm0
callq 0x9e939
movsd %xmm0, 0xa8(%rsp)
movq 0x58(%rsp), %r14
movq (%r14), %rdi
movq 0x60(%rsp), %rsi
callq 0x31345
movq %rax, (%r14)
movq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x30648
movq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x30648
movsd 0x78(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
callq 0x39aee
testl %eax, %eax
jne 0x78e84
movsd 0xa8(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
callq 0x39aee
testl %eax, %eax
je 0x78fe0
movsd 0x78(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm1
callq 0x39aee
testl %eax, %eax
je 0x78f4e
movq 0xa0(%rsp), %r14
movq %r14, %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x7c89d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x7c86e
movq 0x30(%rbp), %rax
movq %rax, 0x30(%rsp)
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x7c251
movq %rbp, %rdi
callq 0x3a180
movq 0xb8(%rsp), %rbx
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movupd (%rbx), %xmm0
movupd 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7c251
movq %rbx, %rdi
callq 0x3a180
movq 0x58(%rsp), %rdi
leaq 0x98(%rsp), %rsi
movq %r15, %rdx
callq 0x9f94e
jmp 0x7908f
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 0x7c251
movq %r15, %rdi
callq 0x3a180
movq 0xb8(%rsp), %rbx
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movupd (%rbx), %xmm0
movupd 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7c251
movq %rbx, %rdi
callq 0x3a180
movq 0x58(%rsp), %rdi
leaq 0x98(%rsp), %rsi
movq %rbp, %rdx
callq 0x9f94e
movq %rbp, %r15
movq 0xa0(%rsp), %r14
jmp 0x7908f
movq 0xa0(%rsp), %r14
movq %r14, %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x7c89d
movq %r14, %rdi
movq %rbx, %rsi
movq 0xb8(%rsp), %rbx
movq %rbx, %rdx
callq 0x7c86e
movq 0x30(%rbp), %rax
movq %rax, 0x30(%rsp)
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x7c251
movq %rbp, %rdi
callq 0x3a180
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movupd (%r15), %xmm0
movupd 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7c251
movq %r15, %rdi
callq 0x3a180
movq 0x58(%rsp), %rdi
leaq 0x98(%rsp), %rsi
movq %rbx, %rdx
callq 0x9f94e
movq %rbx, %r15
movl 0x4(%r15), %ecx
movl 0x8(%r15), %eax
leal (%rcx,%rax), %edx
addl %edx, 0x70(%rsp)
subl %edx, 0x6c(%rsp)
addl %eax, 0x74(%rsp)
movl 0x4c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x4c(%rsp)
movq %r15, 0x50(%rsp)
movq 0x30(%r15), %r13
testq %r13, %r13
setne %cl
movl 0x8c(%rsp), %ebp
je 0x790d7
incl %r12d
testl %eax, %eax
jg 0x78c1f
jmp 0x790d7
movb $0x1, %cl
testb %cl, %cl
je 0x79153
testq %r13, %r13
je 0x7943f
movq 0x50(%rsp), %rbx
movq 0x30(%r13), %r15
movl 0x8c(%rsp), %eax
cmpl %eax, (%r13)
jne 0x79143
movq %r15, 0x30(%rbx)
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7c251
movq %r13, %rdi
callq 0x3a180
decl 0x10(%r14)
testq %r15, %r15
jne 0x79146
jmp 0x7944b
movq %r13, %rbx
movq %r15, %r13
testq %r15, %r15
jne 0x790e9
jmp 0x7944f
testl %eax, %eax
jle 0x79457
movsd 0x2c3b5(%rip), %xmm0    # 0xa5518
movsd %xmm0, 0x50(%rsp)
movb $0x1, %bl
xorl %ebp, %ebp
leaq 0x40(%rsp), %r15
leaq 0x60(%rsp), %r12
movq 0x58(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x3, %ecx
callq 0x31019
movq %r12, %rdi
movl 0x8c(%rsp), %r14d
movl %r14d, %esi
movq 0xc0(%rsp), %rdx
callq 0x9f37f
movq %rax, %r13
cmpq $0x0, 0x18(%rax)
je 0x793e4
movq 0x40(%rsp), %rsi
movq %r13, %rdi
movl %r14d, %edx
movl $0x2, %ecx
callq 0x9f536
cmpq $0x0, 0x18(%r13)
je 0x793e4
movq 0x60(%rsp), %rsi
movq %r13, %rdi
movl $0x1, %edx
callq 0x7c9ed
movq 0x40(%rsp), %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x7c9ed
movq 0x58(%rsp), %r14
movq (%r14), %rdi
movq 0x60(%rsp), %rsi
callq 0x31345
movq %rax, (%r14)
movq 0x40(%rsp), %rsi
movq %r14, %rdi
movq 0xa0(%rsp), %r14
callq 0x30648
movl 0x70(%rsp), %edi
movl 0x6c(%rsp), %esi
movl 0x74(%rsp), %edx
movl 0x4c(%rsp), %ecx
movsd 0x90(%rsp), %xmm0
callq 0x7adc2
movsd %xmm0, 0xa8(%rsp)
movl 0x20(%r14), %edi
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movups (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x7ac95
movsd %xmm0, 0xb0(%rsp)
movl 0x4(%r13), %eax
movl 0x8(%r13), %edx
leal (%rdx,%rax), %ecx
movl 0x70(%rsp), %edi
addl %ecx, %edi
movl 0x6c(%rsp), %esi
subl %ecx, %esi
addl 0x74(%rsp), %edx
movl 0x4c(%rsp), %ecx
subl %eax, %ecx
movsd 0x90(%rsp), %xmm0
callq 0x7adc2
testb $0x1, %bpl
je 0x792c9
movsd 0x78(%rsp), %xmm1
subsd 0xa8(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0xb0(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x78(%rsp)
movapd %xmm1, %xmm0
movsd 0x50(%rsp), %xmm1
callq 0x39aee
testl %eax, %eax
je 0x7933f
movsd 0x78(%rsp), %xmm0
testb $0x1, %bl
je 0x79398
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movupd (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7cbcf
movsd 0x2c9e5(%rip), %xmm1    # 0xa5d18
ucomisd %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
ja 0x79398
movsd 0x78(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
ucomisd 0x3698d(%rip), %xmm0    # 0xafce0
ja 0x79406
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7cbcf
ucomisd 0x2c988(%rip), %xmm0    # 0xa5d18
movsd 0x50(%rsp), %xmm0
jae 0x79406
movsd %xmm0, 0x50(%rsp)
movq %r13, %rdi
movq %r14, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x6c(%rsp), %rcx
leaq 0x74(%rsp), %r8
leaq 0x4c(%rsp), %r9
callq 0x9fbb0
movq 0x58(%rsp), %rdi
leaq 0x98(%rsp), %rsi
movq %r13, %rdx
callq 0x9f94e
movb $0x1, %bpl
xorl %ebx, %ebx
cmpl $0x0, 0x4c(%rsp)
jg 0x79177
jmp 0x79457
movq 0x58(%rsp), %rbx
movq (%rbx), %rdi
movq 0x60(%rsp), %rsi
callq 0x31345
movq %rax, (%rbx)
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x30648
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7c251
movq %r13, %rdi
callq 0x3a180
jmp 0x79457
movq 0x50(%rsp), %rbx
testq %rbx, %rbx
jne 0x7944f
jmp 0x79457
movq %rbx, 0x8(%r14)
movq $0x0, 0x30(%rbx)
movq 0x98(%rsp), %rsi
movq 0x58(%rsp), %rdi
callq 0x30648
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq