// File: /workspace/github_c_makefile_star2_O1/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, 0xb8(%rsp)
movsd %xmm0, 0x90(%rsp)
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, %r14
movq %rdi, 0x80(%rsp)
xorl %ebp, %ebp
movq %rbp, 0x98(%rsp)
movq %rbp, 0x88(%rsp)
movl %ebp, 0x74(%rsp)
movl %ebp, 0x70(%rsp)
movq (%rdx), %rdi
callq 0x2fa14
movl %eax, 0x6c(%rsp)
movq (%rbx), %rdi
movl %r15d, %esi
callq 0x11534
movl %eax, 0x44(%rsp)
movq (%r14), %r12
testq %r12, %r12
je 0x77212
cmpl %r15d, (%r12)
jne 0x77a23
xorl %ebp, %ebp
xorl %r13d, %r13d
testq %r12, %r12
setne %al
movq %rbx, 0x60(%rsp)
movq %rbp, 0x50(%rsp)
je 0x77701
cmpl $0x0, 0x44(%rsp)
jle 0x77701
movl %r15d, 0xc4(%rsp)
movq (%rbx), %rcx
xorl %ebp, %ebp
movq %rbp, (%rsp)
movq 0x80(%rsp), %r14
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
movsd 0x90(%rsp), %xmm0
callq 0x9b8f8
movsd %xmm0, 0xa8(%rsp)
movq %rbx, %rdi
leaq 0x48(%rsp), %rsi
leaq 0x58(%rsp), %rbx
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x30746
movq %rbp, 0x88(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movq 0xb8(%rsp), %rdx
callq 0x9c351
movq %rax, %rbp
movq %r14, %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
callq 0x7afd2
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x7afad
incl %r13d
movq 0x20(%r14), %rax
movq %rax, 0x20(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl %r13d, %edx
callq 0x9c7df
cmpl $0x0, 0x28(%rbp)
jle 0x77317
movq 0x48(%rsp), %rsi
movq %rbp, %rdi
movl %r15d, %edx
callq 0x9b2c1
movq 0x58(%rsp), %rsi
movq %rbp, %rdi
movl $0x1, %edx
callq 0x7b122
movq 0x48(%rsp), %rsi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x7b122
movq 0x88(%rsp), %rsi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x7b122
movq 0x58(%rsp), %rcx
movq 0x48(%rsp), %r8
movq 0x88(%rsp), %r9
decl %r13d
xorl %eax, %eax
movq %rax, (%rsp)
movq 0x80(%rsp), %r14
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
movsd 0x90(%rsp), %xmm0
callq 0x9b8f8
movsd %xmm0, 0x78(%rsp)
leaq 0x58(%rsp), %rdi
movq %r12, 0xb0(%rsp)
movq %r12, %rsi
movl %r15d, %edx
movq 0xb8(%rsp), %rcx
callq 0x76ff0
movq %rax, %rbx
movq %r14, %rdi
movq 0x50(%rsp), %r12
movq %r12, %rsi
movq %rbp, %rdx
callq 0x7afd2
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x7afad
movq 0x48(%rsp), %rsi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x9b2c1
movq 0x58(%rsp), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x7b122
movq 0x48(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7b122
movq 0x88(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7b122
movq 0x58(%rsp), %rcx
movq 0x48(%rsp), %r8
movq 0x88(%rsp), %r9
xorl %eax, %eax
movq %rax, (%rsp)
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
movsd 0x90(%rsp), %xmm0
callq 0x9b8f8
movsd %xmm0, 0xa0(%rsp)
movq 0x60(%rsp), %r14
movq (%r14), %rdi
movq 0x58(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r14)
movq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0x2fd59
movq 0x88(%rsp), %rsi
movq %r14, %rdi
callq 0x2fd59
movsd 0x78(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm1
callq 0x38d8e
testl %eax, %eax
jne 0x774b2
movsd 0xa0(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm1
callq 0x38d8e
testl %eax, %eax
je 0x7760f
movsd 0x78(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
callq 0x38d8e
testl %eax, %eax
je 0x7757e
movq 0x80(%rsp), %r14
movq %r14, %rdi
movq 0x50(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
callq 0x7afd2
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x7afad
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 0x7a989
movq %rbx, %rdi
callq 0x39194
movq 0xb0(%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 0x7a989
movq %rbx, %rdi
callq 0x39194
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x98(%rsp), %rsi
movq %rbp, %rdx
callq 0x9c951
jmp 0x776be
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 0x7a989
movq %rbp, %rdi
callq 0x39194
movq 0xb0(%rsp), %r14
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movupd (%r14), %xmm0
movupd 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7a989
movq %r14, %rdi
callq 0x39194
movq 0x60(%rsp), %r14
movq %r14, %rdi
leaq 0x98(%rsp), %rsi
movq %rbx, %rdx
callq 0x9c951
movq %rbx, %rbp
movq %r14, %rbx
jmp 0x776be
movq 0x80(%rsp), %r14
movq %r14, %rdi
movq 0x50(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
callq 0x7afd2
movq %r14, %rdi
movq %r15, %rsi
movq 0xb0(%rsp), %r14
movq %r14, %rdx
callq 0x7afad
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 0x7a989
movq %rbx, %rdi
callq 0x39194
movq 0x30(%rbp), %rax
movq %rax, 0x30(%rsp)
movupd (%rbp), %xmm0
movupd 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7a989
movq %rbp, %rdi
callq 0x39194
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x98(%rsp), %rsi
movq %r14, %rdx
callq 0x9c951
movq %r14, %rbp
movl 0x4(%rbp), %eax
movl 0x8(%rbp), %ecx
leal (%rax,%rcx), %edx
addl %edx, 0x70(%rsp)
subl %edx, 0x6c(%rsp)
addl %ecx, 0x74(%rsp)
movl 0x44(%rsp), %ecx
subl %eax, %ecx
movl %ecx, 0x44(%rsp)
movq %rbp, 0x50(%rsp)
movq 0x30(%rbp), %r12
testq %r12, %r12
setne %al
movl 0xc4(%rsp), %r15d
je 0x77701
incl %r13d
testl %ecx, %ecx
jg 0x7723e
testb %al, %al
je 0x7777b
testq %r12, %r12
je 0x77a4b
movq 0x80(%rsp), %r14
movq 0x50(%rsp), %r13
movq %r12, %rbx
movq 0x30(%r12), %r12
cmpl %r15d, (%rbx)
jne 0x7776e
movq %r12, 0x30(%r13)
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 0x7a989
movq %rbx, %rdi
callq 0x39194
decl 0x10(%r14)
testq %r12, %r12
jne 0x77771
movq %r13, 0x8(%r14)
jmp 0x77771
movq %rbx, %r13
testq %r12, %r12
jne 0x7771b
jmp 0x77a50
cmpl $0x0, 0x44(%rsp)
jle 0x77b08
movl %r15d, %r14d
movsd 0x2ad87(%rip), %xmm0    # 0xa2518
movsd %xmm0, 0x50(%rsp)
movb $0x1, %bpl
xorl %r13d, %r13d
leaq 0x58(%rsp), %r15
movq %rbx, %rdi
leaq 0x48(%rsp), %rsi
movq %r15, %rdx
movl $0x3, %ecx
callq 0x30746
movq %r15, %rdi
movl %r14d, %ebx
movl %r14d, %esi
movq 0xb8(%rsp), %rdx
callq 0x9c351
movq %rax, %r12
cmpq $0x0, 0x18(%rax)
je 0x777eb
movq 0x48(%rsp), %rsi
movq %r12, %rdi
movl %ebx, %edx
movl $0x2, %ecx
callq 0x9c4fc
cmpq $0x0, 0x18(%r12)
je 0x77aac
movq 0x58(%rsp), %rsi
movq %r12, %rdi
movl $0x1, %edx
callq 0x7b122
movq 0x48(%rsp), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x7b122
movq 0x60(%rsp), %rbx
movq (%rbx), %rdi
movq 0x58(%rsp), %rsi
callq 0x30ad5
movq %rax, (%rbx)
movq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x2fd59
movl 0x70(%rsp), %edi
movl 0x6c(%rsp), %esi
movl 0x74(%rsp), %edx
movl 0x44(%rsp), %ecx
movsd 0x90(%rsp), %xmm0
callq 0x79482
movsd %xmm0, 0xa0(%rsp)
movq 0x80(%rsp), %rax
movl 0x20(%rax), %edi
movq 0x30(%r12), %rax
movq %rax, 0x30(%rsp)
movups (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x79355
movsd %xmm0, 0xa8(%rsp)
movl 0x4(%r12), %eax
movl 0x8(%r12), %edx
leal (%rdx,%rax), %ecx
movl 0x70(%rsp), %edi
addl %ecx, %edi
movl 0x6c(%rsp), %esi
subl %ecx, %esi
addl 0x74(%rsp), %edx
movl 0x44(%rsp), %ecx
subl %eax, %ecx
movsd 0x90(%rsp), %xmm0
callq 0x79482
testb $0x1, %r13b
je 0x778ef
movsd 0x78(%rsp), %xmm1
subsd 0xa0(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0xa8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x78(%rsp)
movapd %xmm1, %xmm0
movsd 0x50(%rsp), %xmm1
callq 0x38d8e
testl %eax, %eax
movq 0x80(%rsp), %rbx
je 0x77971
movsd 0x78(%rsp), %xmm0
testb $0x1, %bpl
je 0x779d1
movq 0x30(%r12), %rax
movq %rax, 0x30(%rsp)
movupd (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7b300
movsd 0x2b3b3(%rip), %xmm1    # 0xa2d18
ucomisd %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
ja 0x779d1
movsd 0x78(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
ucomisd 0x35283(%rip), %xmm0    # 0xacc08
ja 0x77a6b
movq 0x30(%r12), %rax
movq %rax, 0x30(%rsp)
movupd (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7b300
ucomisd 0x2b353(%rip), %xmm0    # 0xa2d18
movsd 0x50(%rsp), %xmm0
jae 0x77a6b
movsd %xmm0, 0x50(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x6c(%rsp), %rcx
leaq 0x74(%rsp), %r8
leaq 0x44(%rsp), %r9
callq 0x9cbb3
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x98(%rsp), %rsi
movq %r12, %rdx
callq 0x9c951
movb $0x1, %r13b
xorl %ebp, %ebp
cmpl $0x0, 0x44(%rsp)
jg 0x777a2
jmp 0x77b08
movl $0x1, %eax
movq %r12, %rbp
movl %eax, %r13d
movq 0x30(%r12), %r12
testq %r12, %r12
je 0x77215
leal 0x1(%r13), %eax
cmpl %r15d, (%r12)
jne 0x77a28
jmp 0x77215
movq 0x50(%rsp), %r13
testq %r13, %r13
movq 0x60(%rsp), %rbx
je 0x77b08
movq $0x0, 0x30(%r13)
jmp 0x77b08
movq 0x30(%r12), %rax
movq %rax, 0x30(%rsp)
movupd (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7a989
movq %r12, %rdi
callq 0x39194
movq 0x60(%rsp), %rbx
jmp 0x77b08
movq 0x60(%rsp), %rbx
movq (%rbx), %rdi
movq 0x58(%rsp), %rsi
callq 0x30ad5
movq %rax, (%rbx)
movq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x2fd59
movq 0x30(%r12), %rax
movq %rax, 0x30(%rsp)
movupd (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rsp)
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x7a989
movq %r12, %rdi
callq 0x39194
movq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0x2fd59
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq