// File: /workspace/github_c_makefile_star2_O1/negge[P]xlink/src/paq.c
// Function: xlink_modeler_search
void xlink_modeler_search(xlink_modeler *mod, xlink_list *models) {
  int contains[256];
  double best;
  int add_index;
  int del_index;
  printf("Searching for best context... ");
  fflush(stdout);
  xlink_list_empty(models);
  memset(contains, 0, sizeof(contains));
  best = DBL_MAX;
  do {
    int i;
    xlink_model model;
    add_index = del_index = -1;
    /* Try to add a model to the working set */
    for (i = 0; i < 256; i++) {
      if (!contains[i]) {
        double entropy;
        xlink_model_init(&model, i);
        xlink_list_add(models, &model);
        entropy = xlink_modeler_get_entropy(mod, models);
        xlink_list_remove(models, xlink_list_length(models) - 1);
        if (entropy < best) {
          best = entropy;
          add_index = i;
        }
      }
    }
    if (add_index != -1) {
      xlink_model_init(&model, add_index);
      xlink_list_add(models, &model);
      contains[add_index] = 1;
    }
    /* Try to remove a model from the working set */
    for (i = 0; i < models->length; i++) {
      double entropy;
      xlink_list_swap(models, i, models->length - 1);
      models->length--;
      entropy = xlink_modeler_get_entropy(mod, models);
      models->length++;
      xlink_list_swap(models, models->length - 1, i);
      if (entropy < best) {
        best = entropy;
        del_index = i;
      }
    }
    if (del_index != -1) {
      contains[xlink_list_get_model(models, del_index)->mask] = 0;
      xlink_list_remove(models, del_index);
    }
  }
  while (add_index != -1 || del_index != -1);
  printf("done\n");
  xlink_list_sort(models, model_comp);
  xlink_modeler_print(mod, models);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x2fae(%rip), %rdi      # 0xc095
xorl %eax, %eax
callq 0x20c0
movq 0x6ecb(%rip), %rax      # 0xffc0
movq (%rax), %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x931e
leaq 0x20(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x20d0
movsd 0x2e0a(%rip), %xmm0     # 0xbf28
leaq 0xc(%rsp), %r15
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
cmpl $0x0, 0x20(%rsp,%r12,4)
jne 0x91a6
movsd %xmm0, (%rsp)
movb %r12b, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
btl %eax, %r12d
adcl $0x0, %ecx
incl %eax
cmpl $0x8, %eax
jne 0x914a
movl %ecx, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x93e4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8de6
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
callq 0x931a
leal -0x1(%rax), %esi
movq %rbx, %rdi
callq 0x9569
movsd (%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
cmoval %r12d, %r13d
minsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %r12
cmpq $0x100, %r12            # imm = 0x100
jne 0x912c
cmpl $-0x1, %r13d
je 0x9202
movsd %xmm0, (%rsp)
movb %r13b, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movzbl %r13b, %eax
xorl %ecx, %ecx
xorl %edx, %edx
btl %ecx, %eax
adcl $0x0, %edx
incl %ecx
cmpl $0x8, %ecx
jne 0x91d6
movl %edx, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x93e4
movslq %r13d, %rax
movl $0x1, 0x20(%rsp,%rax,4)
movsd (%rsp), %xmm0
movl 0x10(%rbx), %edx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %edx, %edx
jle 0x9275
xorl %r12d, %r12d
movapd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
decl %edx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x95e4
decl 0x10(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8de6
movsd %xmm0, (%rsp)
movl 0x10(%rbx), %esi
leal 0x1(%rsi), %eax
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
movl %r12d, %edx
callq 0x95e4
movsd (%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmoval %r12d, %ebp
minsd %xmm1, %xmm0
incl %r12d
movl 0x10(%rbx), %edx
movapd %xmm0, %xmm1
cmpl %edx, %r12d
jl 0x9215
cmpl $-0x1, %ebp
je 0x92a3
movq %rbx, %rdi
movl %ebp, %esi
movsd %xmm0, (%rsp)
callq 0x9366
movzbl (%rax), %eax
movl $0x0, 0x20(%rsp,%rax,4)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9569
movsd (%rsp), %xmm0
andl %r13d, %ebp
cmpl $-0x1, %ebp
jne 0x9123
leaq 0x2e10(%rip), %rdi      # 0xc0c6
callq 0x2060
leaq -0xb87(%rip), %rsi      # 0x873b
movq %rbx, %rdi
callq 0x96c7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8fc4
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop