// File: /workspace/github_c_makefile_star2_O3/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1ef32(%rip), %rdi     # 0x23260
xorl %eax, %eax
callq 0x20e0
movq 0x26c84(%rip), %r13     # 0x2afc0
movq (%r13), %rdi
callq 0x21c0
movq (%r15), %rdi
xorl %esi, %esi
callq 0x5e60
movq %rax, %r14
movq %r12, %rdi
movq %rax, %rsi
callq 0x6840
leaq 0x1ef11(%rip), %rdi     # 0x23275
xorl %eax, %eax
callq 0x20e0
movq (%r13), %rdi
callq 0x21c0
movq %r15, %rdi
movq %r14, %rsi
callq 0x2aa1
movq %rax, %r15
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x10c70
movq %rax, %rdi
callq 0x5060
movq %rax, %r13
leaq 0x1eee2(%rip), %rdi     # 0x23283
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
callq 0x2030
movq %r15, %rdi
callq 0xdca0
movq %rax, %rdi
callq 0x5060
movq %rax, %r13
leaq 0x1eed6(%rip), %rdi     # 0x232a3
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x40(%r12)
je 0x442f
leaq 0x1eed4(%rip), %rdi     # 0x232c2
xorl %eax, %eax
callq 0x20e0
movq %r15, %rdi
movq %r14, %rsi
callq 0xd4e0
movq %rax, %r15
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x10c70
movq %rax, %r12
movq %r15, %rdi
callq 0xdca0
leaq 0x1eebb(%rip), %rdi     # 0x232dd
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20e0
movq %r14, %rdi
callq 0x6140
movq %r15, %rdi
movq %r14, %rsi
callq 0x99a0
movq %r14, %rdi
callq 0x6870
movl 0x20(%rbx), %eax
decl %eax
js 0x44e1
movl %eax, %r13d
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %r15, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x3847
movq %rax, %r15
movq %rax, 0x10(%rsp)
movq %r14, %rdi
callq 0x6870
movq %rax, %r12
cmpq %r13, 0x20(%rbx)
je 0x44b0
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x6940
movq %rax, %r12
incq %rbp
movq 0x20(%rbx), %rax
subq %r13, %rax
cmpq %rbp, %rax
ja 0x4496
movq %r15, %rdi
movq %r14, %rsi
callq 0x99a0
movq %r12, %rdi
movq %r14, %rsi
callq 0x81d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x9a90
leaq -0x1(%r13), %rax
testq %r13, %r13
movq %rax, %r13
jg 0x4458
movq %r15, %rdi
movq %r14, %rsi
callq 0x9a90
movq %r15, %rdi
callq 0x10c70
movq %rax, %rdi
callq 0x5060
movq %rax, %r12
leaq 0x1edf6(%rip), %rdi     # 0x232fc
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq %r15, %rdi
callq 0xdca0
movq %rax, %rdi
callq 0x5060
movq %rax, %r12
leaq 0x1ede5(%rip), %rdi     # 0x23317
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq %r15, %rdi
movq %r14, %rsi
callq 0x99a0
movq 0x20(%rbx), %rdi
callq 0x4ed8
movq %rax, %r15
movq 0x20(%rbx), %rdi
movl $0x1, %esi
callq 0x2130
movq %rax, %r12
movl $0x0, (%rsp)
leaq 0x10(%rsp), %r13
xorps %xmm0, %xmm0
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movq %rax, %r9
callq 0x4055
movq %r12, %rdi
callq 0x2030
movq (%r13), %rdi
movq %r14, %rsi
callq 0x9a90
movq %r14, %rdi
callq 0x5ee0
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq