// File: /workspace/github_c_makefile_star2_O2/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, 0x58(%rsp)
movl %r8d, %r13d
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %rbp
movl 0xc0(%rsp), %edx
movl 0xb8(%rsp), %esi
cvttss2si 0x38(%rdi), %eax
movq 0x20(%rdi), %r14
movl %r14d, %ecx
subl %eax, %ecx
leal -0x1(%r14), %eax
cmpl $0x1, %edx
cmovel %ecx, %eax
leal 0x1(%r13), %edi
cmpl %r8d, %ecx
movl %edi, %r8d
cmovgl %ecx, %r8d
testl %edx, %edx
cmovnel %ecx, %r8d
movl %r8d, 0xc(%rsp)
testl %esi, %esi
jle 0x3815
cmpl %r13d, %eax
jle 0x3815
movq %rdi, 0x20(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x21b0
movq %rax, 0x28(%rsp)
notl %r13d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x28(%rsp), %r15
cmpq %rbx, %r14
jbe 0x3415
movq 0xb0(%rsp), %rax
cmpb $0x1, (%rax,%rbx)
jne 0x3410
pushq $0x10
popq %rdi
callq 0x21b0
movslq %r12d, %r12
movq %rax, (%r15,%r12,8)
cvtsi2sd %ebx, %xmm0
movsd %xmm0, (%rax)
movq 0x58(%rsp), %r14
movb $0x0, (%r14,%rbx)
movl 0x20(%rbp), %eax
addl %r13d, %eax
movq %rbp, %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 0x3224
addq $0x20, %rsp
movsd 0x30(%rsp), %xmm0
movq (%r15,%r12,8), %rax
movsd %xmm0, 0x8(%rax)
movb $0x1, (%r14,%rbx)
incl %r12d
movq 0x20(%rbp), %r14
incq %rbx
jmp 0x3397
movl 0xb8(%rsp), %eax
movq %rax, %rbx
leal -0x1(%rax), %esi
leaq 0x3fb(%rip), %rcx       # 0x3824
pushq $0x8
popq %rdx
movq 0x28(%rsp), %rdi
callq 0x2060
movq 0x20(%rsp), %rax
leal (%rax,%rbx), %r14d
movq %rax, %rbx
subl 0xc(%rsp), %r14d
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
cmpq %rax, %rcx
je 0x347b
movq 0x28(%rsp), %rdx
movq (%rdx,%rax,8), %rdx
cvttsd2si (%rdx), %edx
movslq %edx, %rdx
movq 0xb0(%rsp), %rsi
movb $0x0, (%rsi,%rdx)
incq %rax
jmp 0x3455
movq 0x20(%rbp), %rdi
callq 0x21b0
movq %rbx, %rcx
movq %rax, %rbx
cmpl $0x1, %r14d
sete %al
cmpl 0xc(%rsp), %ecx
setl %cl
andb %al, %cl
movb %cl, 0xb(%rsp)
movq %r13, 0x38(%rsp)
movslq %r13d, %rax
movq %rax, 0x50(%rsp)
movl %r14d, %ecx
movq 0x40(%rsp), %r14
testl %ecx, %ecx
jle 0x3803
movl %ecx, %r13d
decq %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x70(%rsp)
movq (%rax,%rcx,8), %rax
cvttsd2si (%rax), %r12d
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x20(%rbp), %r15
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq %r15, %rdx
callq 0x2180
movslq %r12d, %rax
movq %rax, 0x68(%rsp)
movb $0x0, (%rbx,%rax)
cmpb $0x0, 0xb(%rsp)
je 0x35a7
addl 0x38(%rsp), %r15d
subq $0x20, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movl %r15d, (%rsp)
movl $0x1, 0x18(%rsp)
movq %rbp, %rdi
movq 0x30(%rsp), %r14
movq %r14, %rdx
movq 0x38(%rsp), %r15
movq %r15, %rcx
movl %r12d, %r9d
callq 0x3224
addq $0x20, %rsp
movl 0xb8(%rsp), %eax
subl %r13d, %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
movq 0x48(%rsp), %r14
movq %r15, %rcx
movq 0x28(%rsp), %r8
movq %rbx, %r9
movl 0xc8(%rsp), %r10d
pushq %r10
pushq %rax
pushq 0xc8(%rsp)
callq 0x32f6
addq $0x20, %rsp
movq 0xb0(%rsp), %rax
movq 0x68(%rsp), %rcx
movb $0x1, (%rax,%rcx)
movq 0x70(%rsp), %rcx
jmp 0x34b3
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0xc0(%rsp), %eax
testl %eax, %eax
je 0x365e
movsd %xmm0, 0x48(%rsp)
addl 0x38(%rsp), %r15d
subq $0x20, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rsp)
movl %r15d, (%rsp)
movl $0x1, 0x18(%rsp)
movq %rbp, %rdi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movl %r12d, %r9d
callq 0x3224
addq $0x20, %rsp
movsd (%r14), %xmm0
movsd 0x60(%rsp), %xmm1
subsd %xmm0, %xmm1
ucomisd 0x1fba1(%rip), %xmm1    # 0x231b8
jb 0x358c
movq 0x20(%rsp), %r8
cmpl 0xc(%rsp), %r8d
jne 0x371f
movsd 0x48(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x358c
movsd %xmm1, (%r14)
movq 0x20(%rbp), %r15
movq %r15, %rax
addq 0x50(%rsp), %rax
js 0x3742
cvtsi2ss %rax, %xmm0
jmp 0x3757
movl 0xc(%rsp), %eax
cmpl %eax, 0x20(%rsp)
jge 0x36b0
addl 0x38(%rsp), %r15d
movq %rbp, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %r12d, %r9d
pushq $0x1
leaq 0x38(%rsp), %rax
pushq %rax
pushq $0x0
pushq %r15
callq 0x3224
addq $0x20, %rsp
movsd 0x30(%rsp), %xmm0
subsd (%r14), %xmm0
ucomisd 0x1fb13(%rip), %xmm0    # 0x231b8
jb 0x358c
jmp 0x37d6
subsd (%r14), %xmm0
ucomisd 0x1fafb(%rip), %xmm0    # 0x231b8
jb 0x358c
addl 0x38(%rsp), %r15d
movq %rbp, %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %r12d, %r9d
pushq $0x0
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
pushq %r15
callq 0x3224
addq $0x20, %rsp
movsd 0x48(%rsp), %xmm0
ucomisd (%r14), %xmm0
jbe 0x37d6
movsd %xmm0, (%r14)
movq 0x20(%rbp), %r15
movq %r15, %rax
addq 0x50(%rsp), %rax
js 0x378e
cvtsi2ss %rax, %xmm0
jmp 0x37a3
movl 0xb8(%rsp), %eax
subl %r13d, %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
jmp 0x356e
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%r14)
cmpq $0x0, 0x8(%r14)
jne 0x3770
movq %r15, %rdi
callq 0x21b0
movq %rax, 0x8(%r14)
xorl %eax, %eax
cmpq %rax, %r15
jbe 0x358c
movb (%rbx,%rax), %cl
movq 0x8(%r14), %rdx
movb %cl, (%rdx,%rax)
incq %rax
movq 0x20(%rbp), %r15
jmp 0x3772
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%r14)
cmpq $0x0, 0x8(%r14)
jne 0x37bc
movq %r15, %rdi
callq 0x21b0
movq %rax, 0x8(%r14)
xorl %eax, %eax
cmpq %rax, %r15
jbe 0x37d6
movb (%rbx,%rax), %cl
movq 0x8(%r14), %rdx
movb %cl, (%rdx,%rax)
incq %rax
movq 0x20(%rbp), %r15
jmp 0x37be
movl 0xb8(%rsp), %eax
subl %r13d, %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movq %rbx, %r9
pushq $0x0
jmp 0x357b
movq 0x28(%rsp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq