// File: /workspace/github_c_makefile_star2_O3/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 0x2f11(%rip), %rdi      # 0xc085
xorl %eax, %eax
callq 0x20c0
movq 0x6e3e(%rip), %rax      # 0xffc0
movq (%rax), %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x94c2
leaq 0x20(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x20d0
movsd 0x2d6d(%rip), %xmm0     # 0xbf18
movsd %xmm0, (%rsp)
movaps 0x2d39(%rip), %xmm7     # 0xbef0
movdqa 0x2d40(%rip), %xmm8     # 0xbf00
leaq 0x14(%rsp), %r15
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
cmpl $0x0, 0x20(%rsp,%r12,4)
jne 0x92c3
movb %r12b, 0x14(%rsp)
movd %r12d, %xmm0
pshufd $0x0, %xmm0, %xmm1      # xmm1 = xmm0[0,0,0,0]
pxor %xmm0, %xmm0
movl $0x8, %eax
movdqa 0x2ce6(%rip), %xmm2     # 0xbee0
pshuflw $0xfe, %xmm2, %xmm3     # xmm3 = xmm2[2,3,3,3,4,5,6,7]
movdqa %xmm1, %xmm4
psrld %xmm3, %xmm4
pshuflw $0x54, %xmm2, %xmm3     # xmm3 = xmm2[0,1,1,1,4,5,6,7]
movdqa %xmm1, %xmm5
psrld %xmm3, %xmm5
punpcklqdq %xmm4, %xmm5    # xmm5 = xmm5[0],xmm4[0]
pshufd $0xee, %xmm2, %xmm3     # xmm3 = xmm2[2,3,2,3]
pshuflw $0xfe, %xmm3, %xmm4     # xmm4 = xmm3[2,3,3,3,4,5,6,7]
movdqa %xmm1, %xmm6
psrld %xmm4, %xmm6
pshuflw $0x54, %xmm3, %xmm3     # xmm3 = xmm3[0,1,1,1,4,5,6,7]
movdqa %xmm1, %xmm4
psrld %xmm3, %xmm4
punpckhqdq %xmm6, %xmm4    # xmm4 = xmm4[1],xmm6[1]
shufps $0xcc, %xmm4, %xmm5     # xmm5 = xmm5[0,3],xmm4[0,3]
andps %xmm7, %xmm5
paddd %xmm5, %xmm0
paddd %xmm8, %xmm2
addl $-0x4, %eax
jne 0x91fa
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x957e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8e73
movq %xmm0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x94be
leal -0x1(%rax), %esi
movq %rbx, %rdi
callq 0x96fd
movdqa 0x2c60(%rip), %xmm8     # 0xbf00
movaps 0x2c49(%rip), %xmm7     # 0xbef0
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
cmoval %r12d, %r13d
minsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incq %r12
cmpq $0x100, %r12            # imm = 0x100
jne 0x91ce
cmpl $-0x1, %r13d
je 0x9394
movb %r13b, 0x14(%rsp)
movzbl %r13b, %eax
movd %eax, %xmm0
pshufd $0x0, %xmm0, %xmm1      # xmm1 = xmm0[0,0,0,0]
pxor %xmm0, %xmm0
movl $0x8, %eax
movdqa 0x2be0(%rip), %xmm2     # 0xbee0
pshuflw $0xfe, %xmm2, %xmm3     # xmm3 = xmm2[2,3,3,3,4,5,6,7]
movdqa %xmm1, %xmm4
psrld %xmm3, %xmm4
pshuflw $0x54, %xmm2, %xmm3     # xmm3 = xmm2[0,1,1,1,4,5,6,7]
movdqa %xmm1, %xmm5
psrld %xmm3, %xmm5
punpcklqdq %xmm4, %xmm5    # xmm5 = xmm5[0],xmm4[0]
pshufd $0xee, %xmm2, %xmm3     # xmm3 = xmm2[2,3,2,3]
pshuflw $0xfe, %xmm3, %xmm4     # xmm4 = xmm3[2,3,3,3,4,5,6,7]
movdqa %xmm1, %xmm6
psrld %xmm4, %xmm6
pshuflw $0x54, %xmm3, %xmm3     # xmm3 = xmm3[0,1,1,1,4,5,6,7]
movdqa %xmm1, %xmm4
psrld %xmm3, %xmm4
punpckhqdq %xmm6, %xmm4    # xmm4 = xmm4[1],xmm6[1]
shufps $0xcc, %xmm4, %xmm5     # xmm5 = xmm5[0,3],xmm4[0,3]
andps %xmm7, %xmm5
paddd %xmm5, %xmm0
paddd %xmm8, %xmm2
addl $-0x4, %eax
jne 0x9300
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x957e
movdqa 0x2b7e(%rip), %xmm8     # 0xbf00
movaps 0x2b67(%rip), %xmm7     # 0xbef0
movslq %r13d, %rax
movl $0x1, 0x20(%rsp,%rax,4)
movl 0x10(%rbx), %edx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %edx, %edx
jle 0x9448
xorl %r12d, %r12d
movsd (%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
decl %edx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x9772
decl 0x10(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8e73
movsd %xmm0, (%rsp)
movl 0x10(%rbx), %esi
leal 0x1(%rsi), %eax
movl %eax, 0x10(%rbx)
movq %rbx, %rdi
movl %r12d, %edx
callq 0x9772
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmoval %r12d, %ebp
minsd %xmm1, %xmm0
incl %r12d
movl 0x10(%rbx), %edx
cmpl %edx, %r12d
jl 0x93ac
movsd %xmm0, (%rsp)
cmpl $-0x1, %ebp
je 0x9433
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9507
movzbl (%rax), %eax
movl $0x0, 0x20(%rsp,%rax,4)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x96fd
jmp 0x9438
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movaps 0x2ab1(%rip), %xmm7     # 0xbef0
movdqa 0x2ab8(%rip), %xmm8     # 0xbf00
andl %r13d, %ebp
cmpl $-0x1, %ebp
jne 0x91c5
leaq 0x2c5b(%rip), %rdi      # 0xc0b6
callq 0x2060
leaq -0xc9d(%rip), %rsi      # 0x87ca
movq %rbx, %rdi
callq 0x9851
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9051
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq