// File: /workspace/github_c_makefile_star2_O2/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 0x1f6a7(%rip), %rdi     # 0x231e0
xorl %eax, %eax
callq 0x20e0
movq 0x27479(%rip), %r14     # 0x2afc0
movq (%r14), %rdi
callq 0x21c0
movq (%r15), %rdi
xorl %esi, %esi
callq 0x5600
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
callq 0x5fe0
leaq 0x1f687(%rip), %rdi     # 0x231f5
xorl %eax, %eax
callq 0x20e0
movq (%r14), %rdi
movq %r13, %r14
callq 0x21c0
movq %r15, %rdi
movq %r13, %rsi
callq 0x291c
movq %rax, %r15
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x10410
movq %rax, %rdi
callq 0x4800
movq %rax, %r13
leaq 0x1f656(%rip), %rdi     # 0x23203
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
callq 0x2030
movq %r15, %rdi
callq 0xd440
movq %rax, %rdi
callq 0x4800
movq %rax, %r13
leaq 0x1f64a(%rip), %rdi     # 0x23223
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x40(%r12)
je 0x3c3b
leaq 0x1f648(%rip), %rdi     # 0x23242
xorl %eax, %eax
callq 0x20e0
movq %r15, %rdi
movq %r14, %rsi
callq 0xcc80
movq %rax, %r15
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x10410
movq %rax, %r12
movq %r15, %rdi
callq 0xd440
leaq 0x1f62f(%rip), %rdi     # 0x2325d
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x20e0
movq %r14, %rdi
callq 0x58e0
movq %r15, %rdi
movq %r14, %rsi
callq 0x9140
movq %r14, %rdi
callq 0x6010
movl 0x20(%rbx), %ebp
decq %rbp
testl %ebp, %ebp
js 0x3cf4
movq 0x28(%rbx), %rax
movl %ebp, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq (%rax,%r13,8), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %r15, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0x30cc
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq %r14, %r15
movq %r14, %rdi
callq 0x6010
movq %rax, %r12
xorl %r14d, %r14d
movq 0x20(%rbx), %rax
subq %r13, %rax
cmpq %r14, %rax
jbe 0x3cc6
movq %r12, %rdi
callq 0x60e0
movq %rax, %r12
incq %r14
jmp 0x3caa
movq 0x10(%rsp), %rdi
movq %r15, %r14
movq %rdi, %r15
movq %r14, %rsi
callq 0x9140
movq %r12, %rdi
movq %r14, %rsi
callq 0x7970
movq %r15, %rdi
movq %r14, %rsi
callq 0x9230
jmp 0x3c59
movq %r15, %rdi
movq %r14, %rsi
callq 0x9230
movq %r15, %rdi
callq 0x10410
movq %rax, %rdi
callq 0x4800
movq %rax, %r12
leaq 0x1f563(%rip), %rdi     # 0x2327c
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq %r15, %rdi
callq 0xd440
movq %rax, %rdi
callq 0x4800
movq %rax, %r12
leaq 0x1f552(%rip), %rdi     # 0x23297
movq %rax, %rsi
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x2030
movq %r15, %rdi
movq %r14, %rsi
callq 0x9140
movq 0x20(%rbx), %rdi
callq 0x468c
movq %rax, %r15
movq 0x20(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0x2130
movq %rax, %r12
andl $0x0, (%rsp)
leaq 0x8(%rsp), %r13
xorps %xmm0, %xmm0
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movq %rax, %r9
callq 0x3863
movq %r12, %rdi
callq 0x2030
movq (%r13), %rdi
movq %r14, %rsi
callq 0x9230
movq %r14, %rdi
callq 0x5680
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop