// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %rcx, 0xa8(%rsp)
movsd %xmm0, 0x88(%rsp)
movq %rdx, %rbx
andq $0x0, 0x90(%rsp)
movl %esi, %ebp
andq $0x0, 0x78(%rsp)
movq %rdi, %r14
andl $0x0, 0x6c(%rsp)
andl $0x0, 0x68(%rsp)
movq (%rdx), %rdi
callq 0x2c3cc
movl %eax, 0x64(%rsp)
movq %rbx, 0x50(%rsp)
movq (%rbx), %rdi
movl %ebp, %esi
callq 0x10827
movl %eax, 0x4c(%rsp)
xorl %edx, %edx
movq %r14, 0x70(%rsp)
movq %r14, %rcx
movl %ebp, %r14d
xorl %ebp, %ebp
movl %r14d, 0x60(%rsp)
movq (%rcx), %r15
testq %r15, %r15
je 0x6e7d9
cmpl %r14d, (%r15)
je 0x6e7d9
leaq 0x30(%r15), %rcx
incl %ebp
movq %r15, %rdx
jmp 0x6e7c1
movq %rdx, 0x38(%rsp)
testq %r15, %r15
je 0x6ec94
testl %eax, %eax
jle 0x6ec94
movq %r15, %rbx
movq 0x50(%rsp), %r15
movq (%r15), %rcx
andq $0x0, (%rsp)
movq 0x70(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
movsd 0x88(%rsp), %xmm0
callq 0x8f6bf
movsd %xmm0, 0x98(%rsp)
leaq 0x40(%rsp), %rsi
leaq 0x80(%rsp), %r13
pushq $0x3
popq %rcx
movq %r15, %rdi
movq %r13, %rdx
callq 0x2cfe5
leaq 0x78(%rsp), %r15
andq $0x0, (%r15)
movq %r13, %rdi
movl %r14d, %esi
movq 0xa8(%rsp), %rdx
callq 0x90098
movq %r12, %rdi
movl %r14d, %r13d
movq 0x38(%rsp), %r14
movq %r14, %rsi
movq %rbx, 0x58(%rsp)
movq %rbx, %rdx
movq %rax, %rbx
callq 0x721ae
movq %r12, %rdi
movq %r14, %rsi
movl %r13d, %r14d
movq %rbx, %rdx
callq 0x7217f
incl %ebp
movq 0x20(%r12), %rax
movq %rax, 0x20(%rsp)
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %r15d
movl %ebp, %edx
callq 0x90483
cmpl $0x0, 0x28(%rbx)
jle 0x6e8d7
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x8f09d
leaq 0x80(%rsp), %r13
movq (%r13), %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x722ae
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x722ae
movq 0x78(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x722ae
movq (%r13), %rcx
movq 0x40(%rsp), %r8
movq 0x78(%rsp), %r9
movl %r15d, %ebp
decl %ebp
andq $0x0, (%rsp)
movq 0x70(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
movsd 0x88(%rsp), %xmm0
callq 0x8f6bf
movsd %xmm0, 0xa0(%rsp)
movq %r13, %rdi
movq 0x58(%rsp), %rsi
movl %r14d, %edx
movq 0xa8(%rsp), %rcx
callq 0x6e5a8
movq %rax, %r15
movq %r12, %rdi
movl %r14d, %r13d
movq 0x38(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x721ae
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7217f
movq 0x40(%rsp), %rsi
movq %r15, %rdi
movl %r13d, %edx
callq 0x8f09d
leaq 0x80(%rsp), %r14
movq (%r14), %rsi
movq %r15, %rdi
pushq $0x1
popq %rdx
callq 0x722ae
movq 0x40(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x722ae
movq 0x78(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x722ae
movq (%r14), %rcx
movq 0x40(%rsp), %r8
movq 0x78(%rsp), %r9
andq $0x0, (%rsp)
movq %r12, %rdi
movl %r13d, %esi
movl %ebp, %r12d
movl %ebp, %edx
movsd 0x88(%rsp), %xmm0
callq 0x8f6bf
movsd %xmm0, 0xb0(%rsp)
movq 0x50(%rsp), %rbp
movq (%rbp), %rdi
movq (%r14), %rsi
callq 0x2d2da
movq %rax, (%rbp)
movq 0x40(%rsp), %rsi
movq %rbp, %rdi
callq 0x2c6e4
movq 0x78(%rsp), %rsi
movq %rbp, %rdi
callq 0x2c6e4
movsd 0xa0(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
callq 0x34c76
testl %eax, %eax
jne 0x6ea68
movsd 0xb0(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
callq 0x34c76
testl %eax, %eax
je 0x6ebb7
movsd 0xa0(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
callq 0x34c76
testl %eax, %eax
je 0x6eb35
movq 0x70(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x721ae
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x7217f
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 0x71be8
movq %r15, %rdi
callq 0x34ff7
movq %rbx, %r14
movq 0x58(%rsp), %rbx
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movups (%rbx), %xmm0
movupd 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x71be8
movq %rbx, %rdi
movq %r14, %rbx
callq 0x34ff7
leaq 0x90(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %r14, %rdx
callq 0x90582
jmp 0x6ec5d
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x71be8
movq %rbx, %rdi
callq 0x34ff7
movq 0x58(%rsp), %rbx
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movups (%rbx), %xmm0
movupd 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x71be8
movq %rbx, %rdi
callq 0x34ff7
leaq 0x90(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %r15, %rdx
callq 0x90582
movq %r15, %rbx
jmp 0x6ec5d
movq 0x70(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x721ae
movq %r14, %rdi
movq %rbp, %rsi
movq 0x58(%rsp), %r14
movq %r14, %rdx
callq 0x7217f
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 0x71be8
movq %r15, %rdi
callq 0x34ff7
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movups (%rbx), %xmm0
movupd 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x71be8
movq %rbx, %rdi
callq 0x34ff7
leaq 0x90(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %r14, %rdx
callq 0x90582
movq %r14, %rbx
movl 0x60(%rsp), %r14d
movl 0x4(%rbx), %ecx
movl 0x8(%rbx), %eax
leal (%rcx,%rax), %edx
addl %edx, 0x68(%rsp)
subl %edx, 0x64(%rsp)
addl %eax, 0x6c(%rsp)
movl 0x4c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x4c(%rsp)
movq 0x30(%rbx), %r15
movl %r12d, %ebp
incl %ebp
movq %rbx, 0x38(%rsp)
jmp 0x6e7de
testq %r15, %r15
je 0x6ed1d
movl %r14d, %ebp
movq 0x38(%rsp), %r14
movq %r15, %rbx
testq %rbx, %rbx
je 0x6ed0a
movq 0x30(%rbx), %r15
cmpl %ebp, (%rbx)
jne 0x6ed05
movq %r15, 0x30(%r14)
movq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
movups (%rbx), %xmm0
movupd 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x71be8
movq %rbx, %rdi
callq 0x34ff7
movq 0x70(%rsp), %rax
decl 0x10(%rax)
movq %r15, %rbx
testq %r15, %r15
jne 0x6eca8
movq 0x70(%rsp), %rax
movq %r14, 0x8(%rax)
jmp 0x6eca5
movq %rbx, %r14
jmp 0x6eca5
testq %r14, %r14
je 0x6f00d
andq $0x0, 0x30(%r14)
jmp 0x6f00d
movb $0x1, %bpl
movsd 0x267f0(%rip), %xmm0    # 0x95518
movsd %xmm0, 0x58(%rsp)
xorl %r14d, %r14d
leaq 0x80(%rsp), %r15
pushq $0x3
popq %r12
testl %eax, %eax
jle 0x6f00d
movq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x2cfe5
movq %r15, %rdi
movl 0x60(%rsp), %ebx
movl %ebx, %esi
movq 0xa8(%rsp), %rdx
callq 0x90098
movq %rax, %r13
cmpq $0x0, 0x18(%rax)
je 0x6efb5
movq 0x40(%rsp), %rsi
movq %r13, %rdi
movl %ebx, %edx
pushq $0x2
popq %rcx
callq 0x90239
cmpq $0x0, 0x18(%r13)
je 0x6efb5
movq 0x80(%rsp), %rsi
movq %r13, %rdi
pushq $0x1
popq %rdx
callq 0x722ae
movq 0x40(%rsp), %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x722ae
movq 0x50(%rsp), %rbx
movq (%rbx), %rdi
movq 0x80(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
movl 0x68(%rsp), %edi
movl 0x64(%rsp), %esi
movl 0x6c(%rsp), %edx
movl 0x4c(%rsp), %ecx
movsd 0x88(%rsp), %xmm0
callq 0x7084f
movsd %xmm0, 0xa0(%rsp)
movq 0x70(%rsp), %rax
movl 0x20(%rax), %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 0x7072e
movsd %xmm0, 0x38(%rsp)
movl 0x4(%r13), %eax
movl 0x8(%r13), %edx
leal (%rdx,%rax), %ecx
movl 0x68(%rsp), %edi
addl %ecx, %edi
movl 0x64(%rsp), %esi
subl %ecx, %esi
addl 0x6c(%rsp), %edx
movl 0x4c(%rsp), %ecx
subl %eax, %ecx
movsd 0x88(%rsp), %xmm0
callq 0x7084f
testb $0x1, %r14b
je 0x6ee8f
movsd 0x98(%rsp), %xmm1
subsd 0xa0(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0x38(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
movapd %xmm1, %xmm0
movsd 0x58(%rsp), %xmm1
callq 0x34c76
testl %eax, %eax
je 0x6ef03
movsd 0x38(%rsp), %xmm0
testb $0x1, %bpl
je 0x6ef5c
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 0x7247e
movsd 0x26e29(%rip), %xmm1    # 0x95d20
ucomisd %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
ja 0x6ef5c
movsd 0x38(%rsp), %xmm0
subsd 0x58(%rsp), %xmm0
ucomisd 0x30ce9(%rip), %xmm0    # 0x9fc00
ja 0x6efda
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 0x7247e
ucomisd 0x26dcc(%rip), %xmm0    # 0x95d20
movsd 0x58(%rsp), %xmm0
jae 0x6efda
movsd %xmm0, 0x58(%rsp)
movq %r13, %rdi
movq 0x70(%rsp), %rsi
leaq 0x68(%rsp), %rdx
leaq 0x64(%rsp), %rcx
leaq 0x6c(%rsp), %r8
leaq 0x4c(%rsp), %r9
callq 0x907d7
movq 0x50(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq %r13, %rdx
callq 0x90582
movb $0x1, %r14b
xorl %ebp, %ebp
movl 0x4c(%rsp), %eax
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x6ed3d
movq 0x50(%rsp), %rbx
movq (%rbx), %rdi
movq 0x80(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x71be8
movq %r13, %rdi
callq 0x34ff7
movq 0x90(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x2c6e4
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq