// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/lerils.c
// Function: generate_rule_pool
Ruleset generate_rule_pool(Instanceptr instances, int positive, int M, double p1)
{
 double **thresholds;
 int i, j, k, p, *counts, size, pos, **relevant, bestbit, bestp, bestdirection;
 Decisionconditionptr** included;
 Decisionconditionptr current;
 Decisionruleptr current_rule;
 Instanceptr tmp;
 Ruleset result;
 double prob, best_description_length, description_length;
 empty_ruleset(&result);
 size = data_size(instances);
 thresholds = safecalloc(current_dataset->featurecount, sizeof(double*), "learn_ruleset_lerils_oneclass", 4);
 counts = safecalloc(current_dataset->featurecount, sizeof(int), "learn_ruleset_lerils_oneclass", 5);
 for (i = 0; i < current_dataset->featurecount; i++)
   if (in_list(current_dataset->features[i].type, 2, INTEGER, REAL))
     thresholds[i] = possible_split_points(instances, i, &(counts[i]));
 included = (Decisionconditionptr**) safemalloc_2d(sizeof(Decisionconditionptr*), sizeof(Decisionconditionptr), M, current_dataset->featurecount, "generate_rule_pool", 10);
 relevant = (int**) safemalloc_2d(sizeof(int *), sizeof(int), M, current_dataset->featurecount, "generate_rule_pool", 11);
 for (i = 0; i < M; i++)
  {
   do{
      pos = myrand() % size;
      tmp = data_x(instances, pos);
   }while (give_classno(tmp) != positive);
   for (j = 0; j < current_dataset->featurecount; j++)
     if (current_dataset->features[j].type != CLASSDEF)
      {
       current = empty_condition();
       current->conditiontype = UNIVARIATE_CONDITION;
       current->freeparam = 1;
       current->featureindex = j;
       included[i][j] = current;
       prob = produce_random_value(0.0, 1.0);
       if (prob < 0.5)
         relevant[i][j] = YES;
       else
         relevant[i][j] = NO;
       switch (current_dataset->features[j].type)
        {
         case INTEGER:pos = split_point_lower_and_upper_limits(tmp->values[j].intvalue, thresholds[j], counts[j]);
                      current->comparison = '*';
                      current->upperlimit = thresholds[j][pos + 1];
                      current->lowerlimit = thresholds[j][pos];
                      current->datasize = pos;
                      current->value = pos + 1;
                      break;
         case    REAL:pos = split_point_lower_and_upper_limits(tmp->values[j].floatvalue, thresholds[j], counts[j]);
                      current->comparison = '*';
                      current->upperlimit = thresholds[j][pos + 1];
                      current->lowerlimit = thresholds[j][pos];
                      current->datasize = pos;
                      current->value = pos + 1;
                      break;
         case  STRING:current->comparison = '=';
                      current->value = tmp->values[j].stringindex;
                      break;
        }
      }
     else
       relevant[i][j] = NO;
   /*Random walk*/
   for (j = 0; j < 3 * (current_dataset->featurecount - 1); j++)
    {
     prob = produce_random_value(0.0, 1.0);
     if (prob < p1)
      {
       do{
         pos = myrand() % current_dataset->featurecount;
       }while (current_dataset->features[pos].type == CLASSDEF); 
       relevant[i][pos] = 1 - relevant[i][pos];
      }
     else
      {
       best_description_length = description_length_of_rule_lerils(included[i], relevant[i], instances, positive);
       bestbit = -1;
       for (k = 0; k < current_dataset->featurecount; k++)
         if (current_dataset->features[k].type != CLASSDEF)
          {
           if (current_dataset->features[k].type == STRING || relevant[i][k])
            {
             relevant[i][k] = 1 - relevant[i][k];
             if (rule_cover_instance_lerils(included[i], relevant[i], tmp))
              {
               description_length = description_length_of_rule_lerils(included[i], relevant[i], instances, positive);
               if (description_length < best_description_length)
                {
                 bestbit = k;
                 best_description_length = description_length;
                }
              }
             relevant[i][k] = 1 - relevant[i][k];
            }
           else
             if (!relevant[i][k])
              {
               relevant[i][k] = 1 - relevant[i][k];
               for (p = included[i][k]->datasize - 1; p >= 0; p--)
                {
                 included[i][k]->lowerlimit = thresholds[k][p];
                 if (rule_cover_instance_lerils(included[i], relevant[i], tmp))
                  {
                   description_length = description_length_of_rule_lerils(included[i], relevant[i], instances, positive);
                   if (description_length < best_description_length)
                    {
                     bestbit = k;
                     bestp = p;
                     bestdirection = -1;
                     best_description_length = description_length;
                    }
                  }
                }
               included[i][k]->lowerlimit = thresholds[k][included[i][k]->datasize];
               for (p = ((int)included[i][k]->value) + 1; p < counts[k]; p++)
                {
                 included[i][k]->upperlimit = thresholds[k][p];
                 if (rule_cover_instance_lerils(included[i], relevant[i], tmp))
                  {
                   description_length = description_length_of_rule_lerils(included[i], relevant[i], instances, positive);
                   if (description_length < best_description_length)
                    {
                     bestbit = k;
                     bestp = p;
                     bestdirection = +1;
                     best_description_length = description_length;
                    }
                  }
                }
               included[i][k]->upperlimit = thresholds[k][(int)included[i][k]->value];
               relevant[i][k] = 1 - relevant[i][k];
              }
          }
       if (bestbit != -1)
        {
         relevant[i][bestbit] = 1 - relevant[i][bestbit];
         if (relevant[i][bestbit] && current_dataset->features[bestbit].type != STRING)
          {
           if (bestdirection == -1)
             included[i][bestbit]->lowerlimit = thresholds[bestbit][bestp];
           else
             included[i][bestbit]->upperlimit = thresholds[bestbit][bestp];
          }
        }
      }
    }
   current_rule = empty_rule(positive);
   for (j = 0; j < current_dataset->featurecount; j++)
     if (relevant[i][j])
       add_condition(current_rule, create_copy_of_condition(included[i][j]));
   if (!rule_exists(result, current_rule))
     add_rule(&result, current_rule);
   else
    {
     free_rule(*current_rule);
     safe_free(current_rule);
    }
  }
 free_2d((void**)relevant, M);
 for (i = 0; i < M; i++)
  {
   for (j = 0; j < current_dataset->featurecount; j++)
     if (current_dataset->features[j].type != CLASSDEF)
      {
       free_condition(*(included[i][j]));
       safe_free(included[i][j]);
      }
   safe_free(included[i]);
  }
 safe_free(included);
 for (i = 0; i < current_dataset->featurecount; i++)
   if (counts[i] > 0)
     safe_free(thresholds[i]);
 safe_free(thresholds);
 safe_free(counts); 
 return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movsd %xmm0, 0xe0(%rsp)
movl %ecx, %r12d
movl %edx, 0x7c(%rsp)
movq %rsi, %r13
movq %rdi, 0xd0(%rsp)
callq 0x71c9d
movq %r13, %rdi
callq 0x2c3cc
movl %eax, 0xc8(%rsp)
leaq 0x9b4e9(%rip), %r14     # 0xcd870
movq (%r14), %rax
movslq 0x1bc(%rax), %rdi
leaq 0x6733a(%rip), %rbx     # 0x996d2
pushq $0x8
popq %rsi
pushq $0x4
popq %rcx
movq %rbx, %rdx
callq 0x35067
movq %rax, 0x90(%rsp)
movq (%r14), %rax
movslq 0x1bc(%rax), %rdi
pushq $0x4
popq %rsi
pushq $0x5
popq %rcx
movq %rbx, %rdx
callq 0x35067
movq %rax, %r15
movl $0xc3c2, %r14d          # imm = 0xC3C2
pushq $0x2
popq %rbx
movq %rax, 0xb8(%rsp)
xorl %ebp, %ebp
leaq 0x9b48d(%rip), %rax     # 0xcd870
movq (%rax), %rax
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %rbp
jge 0x32430
movl (%rax,%r14,4), %edi
movl %ebx, %esi
xorl %edx, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x35812
testl %eax, %eax
je 0x32420
movq %r13, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x32088
movq 0x90(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
addq $0x4, %r15
addq $0xd0, %r14
jmp 0x323dc
leaq 0x672b9(%rip), %r15     # 0x996f0
pushq $0x8
popq %rbp
pushq $0xa
popq %r9
movq %rbp, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movq %r15, %r8
callq 0x351c3
movq %rax, 0x70(%rsp)
leaq 0x9b415(%rip), %r14     # 0xcd870
movq (%r14), %rax
movl 0x1bc(%rax), %ecx
pushq $0x4
popq %rsi
pushq $0xb
popq %r9
movq %rbp, %rdi
movl %r12d, %edx
movq %r15, %r8
callq 0x351c3
movq %rax, %rbp
testl %r12d, %r12d
movl $0x0, %eax
movl %r12d, 0xc4(%rsp)
cmovgl %r12d, %eax
movq %rax, 0xd8(%rsp)
xorl %r12d, %r12d
movq %r13, 0xa0(%rsp)
cmpq 0xd8(%rsp), %r12
je 0x32ade
movl 0xc8(%rsp), %r15d
xorl %eax, %eax
callq 0x35c7f
cltd
idivl %r15d
movq %r13, %rdi
movl %edx, %esi
callq 0x2d761
movq %rax, %rbx
movq %rax, %rdi
callq 0x2b47e
cmpl 0x7c(%rsp), %eax
jne 0x324b9
movq (%r14), %rax
movl $0x61e1, %r15d          # imm = 0x61E1
xorl %r13d, %r13d
movq %rbx, 0xa8(%rsp)
movq %rax, %rbx
movq 0x70(%rsp), %rcx
movslq 0x1bc(%rbx), %rax
cmpq %rax, %r13
jge 0x32648
cmpl $0x3, (%rbx,%r15,8)
jne 0x32521
movq (%rbp,%r12,8), %rax
andl $0x0, (%rax,%r13,4)
jmp 0x3263c
xorl %eax, %eax
movq %r15, %rbx
movq %rcx, %r15
callq 0x71cd1
movq $0x1, 0x3c(%rax)
movl %r13d, (%rax)
movq (%r15,%r12,8), %rcx
movq %rbx, %r15
movq %rax, 0x80(%rsp)
movq %rax, (%rcx,%r13,8)
xorpd %xmm0, %xmm0
movsd 0x62fc8(%rip), %xmm1    # 0x95520
callq 0x35cae
xorl %eax, %eax
movsd 0x637b9(%rip), %xmm1    # 0x95d20
ucomisd %xmm0, %xmm1
seta %al
movq (%rbp,%r12,8), %rcx
movl %eax, (%rcx,%r13,4)
movq (%r14), %rbx
movl (%rbx,%r15,8), %eax
cmpl $0x2, %eax
je 0x325c7
cmpl $0x1, %eax
je 0x325a3
testl %eax, %eax
jne 0x32637
movq 0xa8(%rsp), %rax
movq (%rax), %rax
cvtsi2sdl (%rax,%r13,8), %xmm0
jmp 0x325d8
movq 0x80(%rsp), %rcx
movb $0x3d, 0x4(%rcx)
movq 0xa8(%rsp), %rax
movq (%rax), %rax
cvtsi2sdl (%rax,%r13,8), %xmm0
movsd %xmm0, 0x8(%rcx)
jmp 0x32637
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movsd (%rax,%r13,8), %xmm0
movq 0x90(%rsp), %rax
movq (%rax,%r13,8), %rdi
movq %rdi, 0x88(%rsp)
movq 0xb8(%rsp), %rax
movl (%rax,%r13,4), %esi
callq 0x32310
movq 0x80(%rsp), %rdx
movb $0x2a, 0x4(%rdx)
movslq %eax, %rcx
incl %eax
movq 0x88(%rsp), %rsi
movsd 0x8(%rsi,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rdx)
movsd (%rsi,%rcx,8), %xmm0
movsd %xmm0, 0x18(%rdx)
movl %ecx, 0x38(%rdx)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rdx)
movq 0x70(%rsp), %rcx
incq %r13
addq $0x68, %r15
jmp 0x324fb
xorl %ebx, %ebx
movl 0x7c(%rsp), %r15d
movq 0xa0(%rsp), %r13
leal (%rax,%rax,2), %eax
addl $-0x3, %eax
cmpl %eax, %ebx
jge 0x32a04
xorpd %xmm0, %xmm0
movsd 0x62eaf(%rip), %xmm1    # 0x95520
callq 0x35cae
movsd 0xe0(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x326bd
xorl %eax, %eax
callq 0x35c7f
movq (%r14), %rcx
cltd
idivl 0x1bc(%rcx)
movslq %edx, %rax
imulq $0x340, %rax, %rdx      # imm = 0x340
cmpl $0x3, 0x30f08(%rcx,%rdx)
je 0x32685
movq (%rbp,%r12,8), %rdx
pushq $0x1
popq %rsi
subl (%rdx,%rax,4), %esi
movl %esi, (%rdx,%rax,4)
jmp 0x329e6
movl %ebx, 0xcc(%rsp)
movq 0x70(%rsp), %rbx
movq (%rbx,%r12,8), %rdi
movq (%rbp,%r12,8), %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x32252
movq %rbx, %r8
movsd %xmm0, 0x80(%rsp)
xorl %r13d, %r13d
pushq $-0x1
popq %rax
movl %eax, 0x88(%rsp)
movq (%r14), %rcx
movslq 0x1bc(%rcx), %rax
cmpq %rax, %r13
jge 0x3296a
imulq $0x340, %r13, %rax      # imm = 0x340
movl 0x30f08(%rcx,%rax), %eax
cmpl $0x3, %eax
je 0x327b2
cmpl $0x1, %eax
jne 0x32734
movq (%rbp,%r12,8), %rsi
movl (%rsi,%r13,4), %eax
movq (%r8,%r12,8), %rdi
jmp 0x32745
movq (%rbp,%r12,8), %rsi
movl (%rsi,%r13,4), %eax
movq (%r8,%r12,8), %rdi
testl %eax, %eax
je 0x327ba
pushq $0x1
popq %rcx
subl %eax, %ecx
movl %ecx, (%rsi,%r13,4)
movq 0xa8(%rsp), %rdx
callq 0x321ef
testl %eax, %eax
je 0x3279d
movq 0x70(%rsp), %rax
movq (%rax,%r12,8), %rdi
movq (%rbp,%r12,8), %rsi
movq 0xa0(%rsp), %rdx
movl %r15d, %ecx
callq 0x32252
movsd 0x80(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3279d
movl %r13d, 0x88(%rsp)
movsd %xmm0, 0x80(%rsp)
movq 0x70(%rsp), %r8
movq (%rbp,%r12,8), %rax
pushq $0x1
popq %rcx
subl (%rax,%r13,4), %ecx
movl %ecx, (%rax,%r13,4)
incq %r13
jmp 0x326f6
movl $0x1, (%rsi,%r13,4)
movq (%rdi,%r13,8), %rax
movl 0x38(%rax), %r14d
movl %r14d, %r15d
decq %r14
movq 0x90(%rsp), %rax
movq (%rax,%r13,8), %rax
testl %r15d, %r15d
jle 0x32875
movl %r14d, %ecx
movsd (%rax,%rcx,8), %xmm0
movq (%r8,%r12,8), %rdi
movq (%rdi,%r13,8), %rax
movsd %xmm0, 0x18(%rax)
movq (%rbp,%r12,8), %rsi
movq 0xa8(%rsp), %rdx
movq %r8, %rbx
callq 0x321ef
testl %eax, %eax
je 0x3283c
movq (%rbx,%r12,8), %rdi
movq (%rbp,%r12,8), %rsi
movq 0xa0(%rsp), %rdx
movl 0x7c(%rsp), %ecx
callq 0x32252
movsd 0x80(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x32847
decl %r15d
decq %r14
movq %rbx, %r8
jmp 0x327d0
decl %r15d
movl %r13d, 0x88(%rsp)
movl %r15d, 0xb4(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x9c(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %rbx, %r8
jmp 0x327ca
movq (%r8,%r12,8), %rcx
movq (%rcx,%r13,8), %rcx
movslq 0x38(%rcx), %rdx
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, 0x18(%rcx)
cvttsd2si 0x8(%rcx), %eax
movslq %eax, %r14
movq 0xb8(%rsp), %r15
incq %r14
movslq (%r15,%r13,4), %rcx
movq 0x90(%rsp), %rax
movq (%rax,%r13,8), %rax
cmpq %rcx, %r14
jge 0x3293f
movsd (%rax,%r14,8), %xmm0
movq (%r8,%r12,8), %rdi
movq (%rdi,%r13,8), %rax
movsd %xmm0, 0x10(%rax)
movq (%rbp,%r12,8), %rsi
movq 0xa8(%rsp), %rdx
movq %r8, %rbx
callq 0x321ef
testl %eax, %eax
je 0x3290c
movq (%rbx,%r12,8), %rdi
movq (%rbp,%r12,8), %rsi
movq 0xa0(%rsp), %rdx
movl 0x7c(%rsp), %ecx
callq 0x32252
movsd 0x80(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x32914
incq %r14
movq %rbx, %r8
jmp 0x3289e
movl %r13d, 0x88(%rsp)
movl %r14d, 0xb4(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x9c(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %rbx, %r8
jmp 0x3289b
movq (%r8,%r12,8), %rcx
movq (%rcx,%r13,8), %rcx
cvttsd2si 0x8(%rcx), %edx
movslq %edx, %rdx
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, 0x10(%rcx)
leaq 0x9af10(%rip), %r14     # 0xcd870
movl 0x7c(%rsp), %r15d
jmp 0x327a2
movl 0x88(%rsp), %eax
cmpl $-0x1, %eax
movl 0xcc(%rsp), %ebx
je 0x329de
movq (%rbp,%r12,8), %rdx
cltq
movl (%rdx,%rax,4), %esi
leal -0x1(%rsi), %edi
negl %edi
movl %edi, (%rdx,%rax,4)
decl %esi
movq 0xa0(%rsp), %r13
je 0x329e6
imulq $0x340, %rax, %rdx      # imm = 0x340
cmpl $0x1, 0x30f08(%rcx,%rdx)
je 0x329e6
movq 0x90(%rsp), %rdx
movq (%rdx,%rax,8), %rdx
movslq 0xb4(%rsp), %rsi
movsd (%rdx,%rsi,8), %xmm0
movq (%r8,%r12,8), %rdx
movq (%rdx,%rax,8), %rax
cmpl $-0x1, 0x9c(%rsp)
je 0x329f3
movsd %xmm0, 0x10(%rax)
jmp 0x329e6
movq 0xa0(%rsp), %r13
incl %ebx
movl 0x1bc(%rcx), %eax
jmp 0x32657
movsd %xmm0, 0x18(%rax)
pushq $-0x1
popq %rax
movl %eax, 0x9c(%rsp)
jmp 0x329e6
movl %r15d, %edi
callq 0x719a4
movq %rax, %r15
movq (%r14), %rax
xorl %r14d, %r14d
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r14
jge 0x32a59
movq (%rbp,%r12,8), %rcx
cmpl $0x0, (%rcx,%r14,4)
je 0x32a54
movq 0x70(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
callq 0x71730
movq %r15, %rdi
movq %rax, %rsi
callq 0x719fe
leaq 0x9ae1f(%rip), %rax     # 0xcd870
movq (%rax), %rax
incq %r14
jmp 0x32a15
movq 0xd0(%rsp), %r14
movq 0x20(%r14), %rax
movq %rax, 0x20(%rsp)
movupd (%r14), %xmm0
movupd 0x10(%r14), %xmm1
movupd %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r15, %rdi
callq 0x9072b
testl %eax, %eax
je 0x32ac4
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 0x71be8
movq %r15, %rdi
callq 0x34ff7
jmp 0x32acf
movq %r14, %rdi
movq %r15, %rsi
callq 0x7228a
leaq 0x9ad9a(%rip), %r14     # 0xcd870
incq %r12
jmp 0x324a3
movq %rbp, %rdi
movl 0xc4(%rsp), %esi
callq 0x34fc3
xorl %r14d, %r14d
pushq $0xe
popq %r15
cmpq 0xd8(%rsp), %r14
je 0x32b7a
leaq 0x9ad6b(%rip), %rax     # 0xcd870
movq (%rax), %rax
movl $0x61e1, %r12d          # imm = 0x61E1
xorl %r13d, %r13d
movq 0x70(%rsp), %rdx
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r13
jge 0x32b69
cmpl $0x3, (%rax,%r12,8)
je 0x32b60
movq (%rdx,%r14,8), %rax
movq (%rax,%r13,8), %rsi
movq %r15, %rcx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x71b91
movq 0x70(%rsp), %rax
movq (%rax,%r14,8), %rax
movq (%rax,%r13,8), %rdi
callq 0x34ff7
movq 0x70(%rsp), %rdx
leaq 0x9ad13(%rip), %rax     # 0xcd870
movq (%rax), %rax
incq %r13
addq $0x68, %r12
jmp 0x32b16
movq (%rdx,%r14,8), %rdi
callq 0x34ff7
incq %r14
jmp 0x32af4
movq 0x70(%rsp), %rdi
callq 0x34ff7
leaq 0x9ace5(%rip), %rax     # 0xcd870
movq (%rax), %rax
xorl %ebx, %ebx
movq 0xd0(%rsp), %r14
movq 0xb8(%rsp), %r15
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %rbx
jge 0x32bd3
cmpl $0x0, (%r15,%rbx,4)
jle 0x32bce
movq 0x90(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x34ff7
leaq 0x9aca5(%rip), %rax     # 0xcd870
movq (%rax), %rax
incq %rbx
jmp 0x32ba0
movq 0x90(%rsp), %rdi
callq 0x34ff7
movq %r15, %rdi
callq 0x34ff7
movq %r14, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq