// File: /workspace/github_c_makefile_star2_O0/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 %rbx
subq $0xc0, %rsp
movl 0xe0(%rsp), %eax
movl 0xd8(%rsp), %eax
movq 0xd0(%rsp), %rax
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movl %r8d, 0x9c(%rsp)
movq %r9, 0x90(%rsp)
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movq 0xb8(%rsp), %rcx
cvttss2si 0x38(%rcx), %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movl %eax, 0x8c(%rsp)
cmpl $0x1, 0xe0(%rsp)
jne 0x3eed
movslq 0x8c(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0x3f02
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
subq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movl %eax, 0x88(%rsp)
cmpl $0x0, 0xe0(%rsp)
jne 0x3f39
movl 0x9c(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jl 0x3f39
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x3f53
cmpl $0x0, 0xd8(%rsp)
jg 0x3f58
jmp 0x487b
movslq 0xd8(%rsp), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, 0x80(%rsp)
movl $0x0, 0x6c(%rsp)
movl $0x0, 0x68(%rsp)
movslq 0x68(%rsp), %rax
movq 0xb8(%rsp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x40ba
movq 0xd0(%rsp), %rax
movslq 0x68(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x40a8
movl $0x10, %edi
callq 0x2190
movq %rax, %rdx
movq 0x80(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
cvtsi2sdl 0x68(%rsp), %xmm0
movq 0x80(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd %xmm0, (%rax)
movq 0x90(%rsp), %rax
movslq 0x68(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0x90(%rsp), %r8
movl 0x68(%rsp), %r9d
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x9c(%rsp), %r10
subq %r10, %rax
subq $0x1, %rax
movl %eax, %r10d
xorl %eax, %eax
leaq 0x78(%rsp), %rax
xorl %r11d, %r11d
movl %r10d, (%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0x3d30
movsd 0x78(%rsp), %xmm0
movq 0x80(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd %xmm0, 0x8(%rax)
movq 0x90(%rsp), %rax
movslq 0x68(%rsp), %rcx
movb $0x1, (%rax,%rcx)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x40aa
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x3f81
movq 0x80(%rsp), %rdi
movl 0xd8(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
movl $0x8, %edx
leaq 0x7b5(%rip), %rcx       # 0x4890
callq 0x2040
movl 0xd8(%rsp), %eax
movl 0x8c(%rsp), %ecx
subl 0x9c(%rsp), %ecx
subl $0x1, %ecx
subl %ecx, %eax
movl %eax, 0x64(%rsp)
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0x4141
movq 0xd0(%rsp), %rax
movq 0x80(%rsp), %rcx
movslq 0x60(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
cvttsd2si (%rcx), %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x4106
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rdi
shlq $0x0, %rdi
callq 0x2190
movq %rax, 0x58(%rsp)
movl 0x64(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x54(%rsp)
cmpl $0x0, 0x54(%rsp)
jl 0x4864
movq 0x80(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
cvttsd2si (%rax), %eax
movl %eax, 0x50(%rsp)
movq 0x80(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x58(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0x90(%rsp), %rsi
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rdx
callq 0x2160
movq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
movslq 0x50(%rsp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $0x1, 0x64(%rsp)
jne 0x42d5
movl 0x9c(%rsp), %eax
addl $0x1, %eax
cmpl 0x8c(%rsp), %eax
jge 0x42d5
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0x58(%rsp), %r8
movl 0x50(%rsp), %r9d
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x9c(%rsp), %r10
subq %r10, %rax
subq $0x1, %rax
xorl %r10d, %r10d
movl %eax, (%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x1, 0x18(%rsp)
callq 0x3d30
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movl 0x9c(%rsp), %r8d
addl $0x1, %r8d
movq 0x58(%rsp), %r9
movq 0xd0(%rsp), %r11
movl 0xd8(%rsp), %r10d
subl 0x54(%rsp), %r10d
subl $0x1, %r10d
movl 0xe0(%rsp), %eax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x3e60
jmp 0x4843
cmpl $0x0, 0xe0(%rsp)
je 0x4514
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd %xmm0, 0x70(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0x58(%rsp), %r8
movl 0x50(%rsp), %r9d
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x9c(%rsp), %r10
subq %r10, %rax
subq $0x1, %rax
xorl %r10d, %r10d
movl %eax, (%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x1, 0x18(%rsp)
callq 0x3d30
movsd 0x78(%rsp), %xmm0
movq 0xb0(%rsp), %rax
subsd (%rax), %xmm0
movsd 0x1fe87(%rip), %xmm1    # 0x24200
ucomisd %xmm1, %xmm0
jb 0x450f
movl 0x9c(%rsp), %eax
addl $0x1, %eax
cmpl 0x8c(%rsp), %eax
jne 0x44aa
movsd 0x70(%rsp), %xmm0
movq 0xb0(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x44a8
movsd 0x70(%rsp), %xmm0
movq 0xb0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rcx
movslq 0x9c(%rsp), %rax
notq %rax
addq %rcx, %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, 0x28(%rsp)
testq %rax, %rax
movss %xmm0, 0x2c(%rsp)
js 0x4417
movss 0x28(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x2c(%rsp), %xmm0
movq 0xb0(%rsp), %rax
movss %xmm0, 0x10(%rax)
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x445d
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rdi
shlq $0x0, %rdi
callq 0x2190
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, 0x44(%rsp)
movslq 0x44(%rsp), %rax
movq 0xb8(%rsp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x44a6
movq 0x58(%rsp), %rax
movslq 0x44(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0xb0(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x44(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x4465
jmp 0x44a8
jmp 0x450d
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movl 0x9c(%rsp), %r8d
addl $0x1, %r8d
movq 0x58(%rsp), %r9
movq 0xd0(%rsp), %r11
movl 0xd8(%rsp), %r10d
subl 0x54(%rsp), %r10d
subl $0x1, %r10d
movl 0xe0(%rsp), %eax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x3e60
jmp 0x450f
jmp 0x4841
movl 0x9c(%rsp), %eax
addl $0x1, %eax
cmpl 0x8c(%rsp), %eax
jge 0x462d
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0x58(%rsp), %r8
movl 0x50(%rsp), %r9d
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x9c(%rsp), %r10
subq %r10, %rax
subq $0x1, %rax
movl %eax, %r10d
xorl %eax, %eax
leaq 0x78(%rsp), %rax
movl %r10d, (%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x1, 0x18(%rsp)
callq 0x3d30
movsd 0x78(%rsp), %xmm0
movq 0xb0(%rsp), %rax
subsd (%rax), %xmm0
movsd 0x1fc41(%rip), %xmm1    # 0x24200
ucomisd %xmm1, %xmm0
jb 0x4628
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movl 0x9c(%rsp), %r8d
addl $0x1, %r8d
movq 0x58(%rsp), %r9
movq 0xd0(%rsp), %r11
movl 0xd8(%rsp), %r10d
subl 0x54(%rsp), %r10d
subl $0x1, %r10d
movl 0xe0(%rsp), %eax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x3e60
jmp 0x483f
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
movq 0xb0(%rsp), %rax
subsd (%rax), %xmm0
movsd 0x1fbad(%rip), %xmm1    # 0x24200
ucomisd %xmm1, %xmm0
jb 0x483d
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0x58(%rsp), %r8
movl 0x50(%rsp), %r9d
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x9c(%rsp), %r10
subq %r10, %rax
subq $0x1, %rax
movl %eax, %r11d
leaq 0x70(%rsp), %r10
leaq 0x78(%rsp), %rax
xorl %ebx, %ebx
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0x3d30
movsd 0x70(%rsp), %xmm0
movq 0xb0(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x47da
movsd 0x70(%rsp), %xmm0
movq 0xb0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rcx
movslq 0x9c(%rsp), %rax
notq %rax
addq %rcx, %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, 0x20(%rsp)
testq %rax, %rax
movss %xmm0, 0x24(%rsp)
js 0x4749
movss 0x20(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
movss 0x24(%rsp), %xmm0
movq 0xb0(%rsp), %rax
movss %xmm0, 0x10(%rax)
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x478f
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rdi
shlq $0x0, %rdi
callq 0x2190
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, 0x40(%rsp)
movslq 0x40(%rsp), %rax
movq 0xb8(%rsp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x47d8
movq 0x58(%rsp), %rax
movslq 0x40(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0xb0(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x4797
jmp 0x47da
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movl 0x9c(%rsp), %r8d
addl $0x1, %r8d
movq 0x58(%rsp), %r9
movq 0xd0(%rsp), %r11
movl 0xd8(%rsp), %r10d
subl 0x54(%rsp), %r10d
subl $0x1, %r10d
movl 0xe0(%rsp), %eax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x3e60
jmp 0x483f
jmp 0x4841
jmp 0x4843
movq 0xd0(%rsp), %rax
movslq 0x50(%rsp), %rcx
movb $0x1, (%rax,%rcx)
movl 0x54(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x4166
movq 0x80(%rsp), %rdi
callq 0x2030
movq 0x58(%rsp), %rdi
callq 0x2030
addq $0xc0, %rsp
popq %rbx
retq
nopw %cs:(%rax,%rax)