// File: /workspace/github_c_makefile_star2_O1/UCLA-StarAI[P]TrimBN/src/trim/search.c
// Function: search_best_subset
SearchResult* search_best_subset(SearchData* data, Fnf* fnf,
      SddCompilerOptions* options) {
  // Compile an unconstrained SDD
  printf("\ncreating manager..."); fflush(stdout);
  SddManager* manager = sdd_manager_create(fnf->var_count,0);
  sdd_manager_set_options(options,manager);
  printf("\ncompiling..."); fflush(stdout);
  SddNode* node = fnf_to_sdd(fnf,manager);
  char* s;  
  printf("\n sdd size               : %s \n",s=ppc(sdd_size(node))); free(s);
  printf(" sdd node count         : %s \n",s=ppc(sdd_count(node))); free(s);
  if(options->minimize_cardinality) {
    printf("\nminimizing cardinality...");
    node = sdd_minimize_cardinality(node,manager);
    printf("size = %zu / node count = %zu\n",sdd_size(node),sdd_count(node));
  }
  sdd_manager_auto_gc_and_minimize_off(manager);
  sdd_ref(node,manager);

  // Move feature variables to make a constrained SDD
  Feature* feature;
  Vtree* vtree = sdd_manager_vtree(manager);
  for (int i = data->num_features-1; i >= 0; i--) {
    feature = data->features[i];
    node = sdd_move_feature_to_pos(node, manager, feature->indicators,
                                   feature->num_indicators, 0, 1);
    // Minimize XY-constrained vtree node so far
    vtree = sdd_manager_vtree(manager);
    for (int j = 0; j < data->num_features - i; j++) {
      vtree = sdd_vtree_right(vtree);
    } // vtree now points to XY-constrained node
    sdd_ref(node,manager);
    sdd_vtree_minimize_limited(vtree,manager);
    sdd_deref(node,manager);
  }
  sdd_deref(node,manager);
  printf(" sdd size           : %s \n", s=ppc(sdd_size(node))); free(s);
  printf(" sdd node count     : %s \n", s=ppc(sdd_count(node))); free(s);

  // Search for an optimal subset using recursive helper func
  sdd_ref(node, manager);

  SearchResult* result = new_search_result(data->num_features);
  char* subset = (char*) calloc(data->num_features, sizeof(char));
  search_best_subset_aux(&node, manager, data, result, 0, subset, 0, 0);
  free(subset);

  sdd_deref(node, manager);

  sdd_manager_free(manager);
  return result;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x1ef65(%rip), %rdi     # 0x23260
xorl %eax, %eax
callq 0x20e0
movq 0x26cb7(%rip), %r13     # 0x2afc0
movq (%r13), %rdi
callq 0x21c0
movq (%r12), %rdi
xorl %esi, %esi
callq 0x5e80
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x6860
leaq 0x1ef43(%rip), %rdi     # 0x23275
xorl %eax, %eax
callq 0x20e0
movq (%r13), %rdi
callq 0x21c0
movq %r12, %rdi
movq %r14, %rsi
callq 0x2aa1
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x10c90
movq %rax, %rdi
callq 0x5080
movq %rax, %r12
leaq 0x1ef17(%rip), %rdi     # 0x23283
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq 0x8(%rsp), %rdi
callq 0xdcc0
movq %rax, %rdi
callq 0x5080
movq %rax, %r12
leaq 0x1ef09(%rip), %rdi     # 0x232a3
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
cmpl $0x0, 0x40(%r15)
je 0x43fc
leaq 0x1ef08(%rip), %rdi     # 0x232c2
xorl %eax, %eax
callq 0x20e0
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0xd500
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x10c90
movq %rax, %r15
movq 0x8(%rsp), %rdi
callq 0xdcc0
leaq 0x1eeee(%rip), %rdi     # 0x232dd
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20e0
movq %r14, %rdi
callq 0x6160
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x99c0
movq %r14, %rdi
callq 0x6890
movl 0x20(%rbx), %eax
decl %eax
js 0x44b5
movl %eax, %r12d
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rsp), %rdi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %r14, %rsi
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x3825
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x6890
movq %rax, %r15
cmpq %r12, 0x20(%rbx)
je 0x447f
xorl %r13d, %r13d
movq %r15, %rdi
callq 0x6960
movq %rax, %r15
incq %r13
movq 0x20(%rbx), %rax
subq %r12, %rax
cmpq %r13, %rax
ja 0x4465
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x99c0
movq %r15, %rdi
movq %r14, %rsi
callq 0x81f0
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x9ab0
leaq -0x1(%r12), %rax
testq %r12, %r12
movq %rax, %r12
jg 0x4427
leaq 0x8(%rsp), %r15
movq (%r15), %rdi
movq %r14, %rsi
callq 0x9ab0
movq (%r15), %rdi
callq 0x10c90
movq %rax, %rdi
callq 0x5080
movq %rax, %r12
leaq 0x1ee1d(%rip), %rdi     # 0x232fc
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq (%r15), %rdi
callq 0xdcc0
movq %rax, %rdi
callq 0x5080
movq %rax, %r12
leaq 0x1ee0c(%rip), %rdi     # 0x23317
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq (%r15), %rdi
movq %r14, %rsi
callq 0x99c0
movq 0x20(%rbx), %rdi
callq 0x4efa
movq %rax, %r12
movq 0x20(%rbx), %rdi
movl $0x1, %esi
callq 0x2130
movq %rax, %r13
movl $0x0, (%rsp)
xorps %xmm0, %xmm0
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
movq %rax, %r9
callq 0x4019
movq %r13, %rdi
callq 0x2030
movq (%r15), %rdi
movq %r14, %rsi
callq 0x9ab0
movq %r14, %rdi
callq 0x5f00
movq %r12, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop