// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %r9, 0x60(%rsp)
movl %r8d, %ebp
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, %r13
movl 0xc0(%rsp), %edx
movl 0xb8(%rsp), %edi
cvttss2si 0x38(%r13), %eax
movq 0x20(%r13), %r14
movl %r14d, %ecx
subl %eax, %ecx
leal -0x1(%r14), %eax
cmpl $0x1, %edx
cmovel %ecx, %eax
leal 0x1(%rbp), %r8d
cmpl %ebp, %ecx
movl %r8d, %r9d
cmovgl %ecx, %r9d
testl %edx, %edx
cmovnel %ecx, %r9d
movl %r9d, 0x4(%rsp)
testl %edi, %edi
jle 0x4007
cmpl %ebp, %eax
jle 0x4007
movq %r8, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
movq 0xb0(%rsp), %r15
movl 0xb8(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x21b0
movq %rax, 0x40(%rsp)
notl %ebp
testq %r14, %r14
je 0x3bac
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpb $0x1, (%r15,%rbx)
jne 0x3ba0
movl $0x10, %edi
callq 0x21b0
movslq %r12d, %r12
movq 0x40(%rsp), %r15
movq %rax, (%r15,%r12,8)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, (%rax)
movq 0x60(%rsp), %r14
movb $0x0, (%r14,%rbx)
movl 0x20(%r13), %eax
addl %ebp, %eax
movq %r13, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %ebx, %r9d
pushq $0x0
leaq 0x38(%rsp), %rsi
pushq %rsi
pushq $0x0
pushq %rax
callq 0x39a4
addq $0x20, %rsp
movsd 0x30(%rsp), %xmm0
movq (%r15,%r12,8), %rax
movq 0xb0(%rsp), %r15
movsd %xmm0, 0x8(%rax)
movb $0x1, (%r14,%rbx)
incl %r12d
movq 0x20(%r13), %r14
incq %rbx
cmpq %rbx, %r14
ja 0x3b21
movl 0xb8(%rsp), %eax
movq %rax, %rbx
leal -0x1(%rax), %esi
leaq 0x456(%rip), %rcx       # 0x4016
movl $0x8, %edx
movq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x2060
movq 0x20(%rsp), %r14
addl %r14d, %ebx
subl 0x4(%rsp), %ebx
testl %ebx, %ebx
jle 0x3feb
movq %rbp, 0x28(%rsp)
movl %ebx, %ebp
xorl %eax, %eax
movq (%r12,%rax,8), %rcx
cvttsd2si (%rcx), %ecx
movslq %ecx, %rcx
movb $0x0, (%r15,%rcx)
incq %rax
cmpq %rax, %rbp
jne 0x3bef
movq 0x20(%r13), %rdi
callq 0x21b0
movq %rax, 0x8(%rsp)
testl %ebx, %ebx
movq 0x38(%rsp), %r15
jle 0x3fe4
cmpl $0x1, %ebx
sete %al
cmpl 0x4(%rsp), %r14d
setl %cl
andb %al, %cl
movb %cl, 0x3(%rsp)
movslq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %r14
leaq -0x1(%rbp), %rax
movq %rax, 0x70(%rsp)
movl %eax, %eax
movq (%r12,%rax,8), %rax
cvttsd2si (%rax), %ebx
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x20(%r13), %r12
movq %r14, %rdi
movq 0x60(%rsp), %rsi
movq %r12, %rdx
callq 0x2180
movslq %ebx, %rax
movq %rax, 0x68(%rsp)
movb $0x0, (%r14,%rax)
cmpb $0x0, 0x3(%rsp)
je 0x3d39
addl 0x28(%rsp), %r12d
subq $0x20, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movl %r12d, (%rsp)
movl $0x1, 0x18(%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %r15
movq %r15, %rdx
movq 0x38(%rsp), %r12
movq %r12, %rcx
movl %ebx, %r9d
callq 0x39a4
addq $0x20, %rsp
movl 0xb8(%rsp), %eax
subl %ebp, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
movq 0x40(%rsp), %r15
movq %r12, %rcx
movq 0x28(%rsp), %r8
movq %r14, %r9
movl 0xc8(%rsp), %r10d
pushq %r10
pushq %rax
pushq 0xc8(%rsp)
callq 0x3a76
addq $0x20, %rsp
movq 0xb0(%rsp), %rax
movq 0x68(%rsp), %rcx
movb $0x1, (%rax,%rcx)
cmpl $0x1, %ebp
movq 0x40(%rsp), %r12
movq 0x70(%rsp), %rbp
jg 0x3c45
jmp 0x3fe4
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0xc0(%rsp), %eax
testl %eax, %eax
je 0x3df3
movsd %xmm0, 0x48(%rsp)
addl 0x28(%rsp), %r12d
subq $0x20, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movl %r12d, (%rsp)
movl $0x1, 0x18(%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movl %ebx, %r9d
callq 0x39a4
addq $0x20, %rsp
movsd (%r15), %xmm0
movsd 0x58(%rsp), %xmm1
subsd %xmm0, %xmm1
ucomisd 0x1f48f(%rip), %xmm1    # 0x23238
jb 0x3d10
movq 0x20(%rsp), %r8
cmpl 0x4(%rsp), %r8d
jne 0x3eec
movsd 0x48(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3d10
movsd %xmm1, (%r15)
movq 0x20(%r13), %rbx
movq %rbx, %rax
addq 0x50(%rsp), %rax
js 0x3f13
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x3f2b
movl 0x4(%rsp), %eax
cmpl %eax, 0x20(%rsp)
jge 0x3e6c
addl 0x28(%rsp), %r12d
movq %r13, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %ebx, %r9d
pushq $0x1
leaq 0x38(%rsp), %rax
pushq %rax
pushq $0x0
pushq %r12
callq 0x39a4
addq $0x20, %rsp
movsd 0x30(%rsp), %xmm0
subsd (%r15), %xmm0
ucomisd 0x1f3fe(%rip), %xmm0    # 0x23238
jb 0x3d10
movl 0xb8(%rsp), %eax
subl %ebp, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x10(%rsp), %r9
jmp 0x3fdd
subsd (%r15), %xmm0
ucomisd 0x1f3bf(%rip), %xmm0    # 0x23238
jb 0x3d10
addl 0x28(%rsp), %r12d
movq %r13, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %ebx, %r9d
pushq $0x0
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
pushq %r12
callq 0x39a4
addq $0x20, %rsp
movsd 0x48(%rsp), %xmm0
ucomisd (%r15), %xmm0
movq 0x20(%rsp), %r12
movq 0x8(%rsp), %r9
jbe 0x3fbd
movsd %xmm0, (%r15)
movq 0x20(%r13), %rbx
movq %rbx, %rax
addq 0x50(%rsp), %rax
js 0x3f6c
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x3f84
movl 0xb8(%rsp), %eax
subl %ebp, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %r9
jmp 0x3cf5
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%r15)
cmpq $0x0, 0x8(%r15)
jne 0x3f44
movq %rbx, %rdi
callq 0x21b0
movq %rax, 0x8(%r15)
testq %rbx, %rbx
je 0x3d10
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movb (%rcx,%rax), %cl
movq 0x8(%r15), %rdx
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, 0x20(%r13)
ja 0x3f4f
jmp 0x3d10
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%r15)
cmpq $0x0, 0x8(%r15)
jne 0x3f9d
movq %rbx, %rdi
callq 0x21b0
movq %rax, 0x8(%r15)
testq %rbx, %rbx
movq 0x8(%rsp), %r9
je 0x3fbd
xorl %eax, %eax
movb (%r9,%rax), %cl
movq 0x8(%r15), %rdx
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, 0x20(%r13)
ja 0x3fa9
movl 0xb8(%rsp), %eax
subl %ebp, %eax
subq $0x8, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl %r12d, %r8d
pushq $0x0
jmp 0x3cff
movq 0x8(%rsp), %rbx
jmp 0x3ff7
movq 0x20(%r13), %rdi
callq 0x21b0
movq %rax, %rbx
movq %r12, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq