// File: /workspace/github_c_makefile_star2_O1/UCLA-StarAI[P]TrimBN/src/trim/search.c
// Function: bnb_search_aux
void bnb_search_aux(SearchData* data, SearchResult* result, SddNode** node, SddManager* manager,
    int cur_level, char* cur_subset, char* avail_features, int num_avail, int is_nb) {
  int soft_depth = data->num_features - (int)data->budget;
  int hard_depth = (is_nb == 1) ? soft_depth : data->num_features - 1;
  if (!is_nb && cur_level >= soft_depth) soft_depth = cur_level + 1;

  if (cur_level >= hard_depth || num_avail <= 0) return;

  // sort available features in ascending order of MPA
  SddWmc** sorted_avail = (SddWmc**) malloc(num_avail * sizeof(SddWmc*));
  SddWmc bound, maa;
  int j = 0;
  for (int i = 0; i < data->num_features; i++) {
    if (avail_features[i] == 1) {
      sorted_avail[j] = (SddWmc*) malloc(2 * sizeof(SddWmc));
      sorted_avail[j][0] = i; // feature id
      cur_subset[i] = 0;
      compute_maa_with_sdd(data,result,node,manager,cur_subset,i,data->num_features-cur_level-1,NULL,&bound,0);
      sorted_avail[j][1] = bound;
      cur_subset[i] = 1;
      j++;
    }
  }
  qsort((void*)sorted_avail, num_avail-1, sizeof(SddWmc*), cmp_by_bound_inc);

  // num_successors = num_avail - (n - m - k - 1)
  // choose the first num_successors features among them (let these Qk)
  // Remove Qk from avail; remove num_successors from num_avail
  int num_successors = num_avail - (soft_depth - cur_level - 1);
  for (int i = 0; i < num_successors; i++) {
    avail_features[(int)sorted_avail[i][0]] = 0;
  }

  // From right-most descendant node (qk) to left:
  char* new_subset = (char*) malloc(data->num_features * sizeof(char));
  for (int i = num_successors-1; i >= 0; i--) {
    int feature_to_remove = (int) sorted_avail[i][0];
    SddWmc maa_decrease = sorted_avail[i][1];
    new_subset = (char*) memcpy(new_subset, cur_subset, data->num_features);
    new_subset[feature_to_remove] = 0;

    if (num_successors == 1 && cur_level+1 < soft_depth) {
      // move cur feature inside Y-constrained node in vtree
      compute_maa_with_sdd(data, result, node, manager, new_subset, feature_to_remove, data->num_features-cur_level-1, NULL, NULL, 1);
      bnb_search_aux(data, result, node, manager, cur_level+1, new_subset, avail_features, num_avail-i-1, is_nb);
    } else {
      if (is_nb) {
        maa = bound = maa_decrease;
        compute_maa_with_sdd(data, result, node, manager, new_subset, feature_to_remove, data->num_features-cur_level-1, NULL, NULL, 1);
        if (bound - result->best_score >= DBL_EPSILON) { // Prune subtree if bound < best_esdp
          if (cur_level + 1 == soft_depth) { // Leaf node. Update best ESDP
            if (maa > result->best_score) {
              result->best_score = maa;
              result->cost = data->num_features - cur_level - 1;
              if (result->best_subset == NULL) result->best_subset = (char*) malloc(data->num_features * sizeof(char));
              for (int j = 0; j < data->num_features; j++) result->best_subset[j] = new_subset[j];
            }
          } else { // Internal node. Search subtree
            bnb_search_aux(data, result, node, manager, cur_level+1, new_subset, avail_features, num_avail-i-1, is_nb);
          }
        }
      } else if (cur_level+1 < soft_depth) { // general network.
        bound = maa_decrease;
        compute_maa_with_sdd(data, result, node, manager, new_subset, feature_to_remove, data->num_features-cur_level-1, NULL, &bound, 1);
        if (bound - result->best_score >= DBL_EPSILON) { // Prune subtree if bound < best_esdp
          bnb_search_aux(data, result, node, manager, cur_level+1, new_subset, avail_features, num_avail-i-1, is_nb);
        }
      } else { // general network. need to compute actual eca
        bound = maa_decrease;
        if (bound - result->best_score >= DBL_EPSILON) { // Prune subtree if bound < best_esdp
          compute_maa_with_sdd(data, result, node, manager, new_subset, feature_to_remove, data->num_features-cur_level-1, &maa, &bound, 0);
          if (maa > result->best_score) {
            result->best_score = maa;
            result->cost = data->num_features - cur_level - 1;
            if (result->best_subset == NULL) result->best_subset = (char*) malloc(data->num_features * sizeof(char));
            for (int j = 0; j < data->num_features; j++) result->best_subset[j] = new_subset[j];            
          }
          bnb_search_aux(data, result, node, manager, cur_level+1, new_subset, avail_features, num_avail-i-1, is_nb);          
        }
      }
    }

    avail_features[feature_to_remove] = 1;
  }
  free(sorted_avail);
  free(new_subset);  
  // All descendants have been tested. Return
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x48(%rsp)
movl %r8d, %r14d
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %rbp
movq %rdi, %r13
movl 0xb0(%rsp), %esi
movl 0xa8(%rsp), %edi
cvttss2si 0x38(%r13), %ecx
movl 0x20(%r13), %eax
movl %eax, %edx
subl %ecx, %edx
decl %eax
cmpl $0x1, %esi
cmovel %edx, %eax
leal 0x1(%r14), %ecx
cmpl %r8d, %edx
movl %ecx, %r8d
cmovgl %edx, %r8d
testl %esi, %esi
cmovnel %edx, %r8d
movl %r8d, 0xc(%rsp)
testl %edi, %edi
jle 0x3fcb
cmpl %r14d, %eax
jle 0x3fcb
movl %ecx, 0x4(%rsp)
movq 0xa0(%rsp), %rbx
movl 0xa8(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x21b0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x20(%r13)
movl %r14d, %eax
notl %eax
movl %eax, 0x8(%rsp)
movq %rbp, 0x30(%rsp)
je 0x3ba0
movq %rbx, %rcx
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x38(%rsp), %r15
cmpb $0x1, (%rcx,%rbx)
jne 0x3b93
movl $0x10, %edi
callq 0x21b0
movslq %r12d, %r12
movq %rax, (%r15,%r12,8)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, (%rax)
movq 0x48(%rsp), %rbp
movb $0x0, (%rbp,%rbx)
movl 0x20(%r13), %eax
addl 0x8(%rsp), %eax
movq %r13, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %ebx, %r9d
pushq $0x0
leaq 0x28(%rsp), %rsi
pushq %rsi
pushq $0x0
pushq %rax
callq 0x3988
movq 0xc0(%rsp), %rcx
addq $0x20, %rsp
movsd 0x20(%rsp), %xmm0
movq (%r15,%r12,8), %rax
movsd %xmm0, 0x8(%rax)
movb $0x1, (%rbp,%rbx)
movq 0x30(%rsp), %rbp
incl %r12d
incq %rbx
cmpq %rbx, 0x20(%r13)
ja 0x3b17
movl 0xa8(%rsp), %eax
movq %rax, %rbx
leal -0x1(%rax), %esi
leaq 0x426(%rip), %rcx       # 0x3fda
movl $0x8, %edx
movq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x2060
movq 0xa0(%rsp), %rdx
addl %ebx, %r14d
subl 0xc(%rsp), %r14d
incl %r14d
movl %r14d, %r12d
testl %r14d, %r14d
jle 0x3bfa
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
cvttsd2si (%rcx), %ecx
movslq %ecx, %rcx
movb $0x0, (%rdx,%rcx)
incq %rax
cmpq %rax, %r12
jne 0x3be3
movq 0x20(%r13), %rdi
callq 0x21b0
movq %rax, %rbx
testl %r14d, %r14d
jle 0x3fbb
cmpl $0x1, %r14d
sete %al
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x4(%rsp)
setl %cl
andb %al, %cl
movb %cl, 0x3(%rsp)
movslq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq -0x1(%r12), %rax
movq %rax, 0x60(%rsp)
movl %eax, %eax
movq (%r15,%rax,8), %rax
cvttsd2si (%rax), %r15d
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x20(%r13), %r14
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
callq 0x2180
movslq %r15d, %rax
movq %rax, 0x58(%rsp)
movb $0x0, (%rbx,%rax)
cmpb $0x0, 0x3(%rsp)
je 0x3d28
addl 0x8(%rsp), %r14d
subq $0x20, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movl %r14d, (%rsp)
movl $0x1, 0x18(%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rdx
movq 0x38(%rsp), %r14
movq %r14, %rcx
movl %r15d, %r9d
callq 0x3988
addq $0x20, %rsp
movl 0xa8(%rsp), %eax
subl %r12d, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq %rbp, %rdx
movq 0x38(%rsp), %rbp
movq %r14, %rcx
movl 0xc(%rsp), %r8d
movq %rbx, %r9
movl 0xb8(%rsp), %r10d
pushq %r10
pushq %rax
pushq 0xb8(%rsp)
callq 0x3a5a
addq $0x20, %rsp
movq 0xa0(%rsp), %rax
movq 0x58(%rsp), %rcx
movb $0x1, (%rax,%rcx)
cmpl $0x1, %r12d
movq 0x38(%rsp), %r15
movq 0x60(%rsp), %r12
jg 0x3c31
jmp 0x3fbb
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0xb0(%rsp), %eax
testl %eax, %eax
je 0x3de2
movsd %xmm0, 0x40(%rsp)
addl 0x8(%rsp), %r14d
subq $0x20, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movl %r14d, (%rsp)
movl $0x1, 0x18(%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movl %r15d, %r9d
callq 0x3988
addq $0x20, %rsp
movsd (%rbp), %xmm0
movsd 0x50(%rsp), %xmm1
subsd %xmm0, %xmm1
ucomisd 0x1f4a0(%rip), %xmm1    # 0x23238
jb 0x3cfe
movl 0x4(%rsp), %r8d
cmpl 0xc(%rsp), %r8d
jne 0x3ed2
movsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3cfe
movsd %xmm1, (%rbp)
movq 0x20(%r13), %rdi
movq %rdi, %rax
addq 0x28(%rsp), %rax
js 0x3ef5
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x3f0d
movl 0xc(%rsp), %eax
cmpl %eax, 0x4(%rsp)
jge 0x3e57
addl 0x8(%rsp), %r14d
movq %r13, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %r15d, %r9d
pushq $0x1
leaq 0x28(%rsp), %rax
pushq %rax
pushq $0x0
pushq %r14
callq 0x3988
addq $0x20, %rsp
movsd 0x20(%rsp), %xmm0
subsd (%rbp), %xmm0
ucomisd 0x1f40f(%rip), %xmm0    # 0x23238
jb 0x3cfe
movl 0xa8(%rsp), %eax
subl %r12d, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl 0xc(%rsp), %r8d
jmp 0x3fb1
subsd (%rbp), %xmm0
ucomisd 0x1f3d4(%rip), %xmm0    # 0x23238
jb 0x3cfe
addl 0x28(%rsp), %r14d
movq %r13, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %r15d, %r9d
pushq $0x0
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
pushq %r14
callq 0x3988
addq $0x20, %rsp
movsd 0x40(%rsp), %xmm0
ucomisd (%rbp), %xmm0
movl 0x4(%rsp), %r14d
jbe 0x3f90
movsd %xmm0, (%rbp)
movq 0x20(%r13), %rdi
movq %rdi, %rax
addq 0x28(%rsp), %rax
js 0x3f47
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x3f5f
movl 0xa8(%rsp), %eax
subl %r12d, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
jmp 0x3ce0
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%rbp)
cmpq $0x0, 0x8(%rbp)
jne 0x3f22
callq 0x21b0
movq %rax, 0x8(%rbp)
cmpq $0x0, 0x20(%r13)
je 0x3cfe
xorl %eax, %eax
movb (%rbx,%rax), %cl
movq 0x8(%rbp), %rdx
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, 0x20(%r13)
ja 0x3f2f
jmp 0x3cfe
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%rbp)
cmpq $0x0, 0x8(%rbp)
jne 0x3f74
callq 0x21b0
movq %rax, 0x8(%rbp)
cmpq $0x0, 0x20(%r13)
je 0x3f90
xorl %eax, %eax
movb (%rbx,%rax), %cl
movq 0x8(%rbp), %rdx
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, 0x20(%r13)
ja 0x3f7d
movl 0xa8(%rsp), %eax
subl %r12d, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl %r14d, %r8d
movq %rbx, %r9
pushq $0x0
jmp 0x3ced
movq %r15, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq