// File: /workspace/github_c_makefile_star2_O2/UCLA-StarAI[P]TrimBN/src/trim/utils.c
// Function: read_search_data
SearchData* read_search_data(const char* lmap_filename, const char* input_filename) {
  FILE* input_fp = fopen(input_filename, "rb");
  if (input_fp == NULL) {
    printf("Could not open input file %s\n", input_filename);
    exit(1);
  }

  size_t var_count, node_count;
  char** sorted_node_names;
  SddLiteral** sorted_node_indicators;
  SddSize* sorted_node_num_indicators;
  SddWmc *weights;
  parse_lmap(lmap_filename, &var_count, &node_count, &sorted_node_names,
             &sorted_node_indicators, &sorted_node_num_indicators, &weights);

  SearchData* data = (SearchData*) malloc(sizeof(SearchData));
  data->var_count = var_count;
  data->node_count = node_count;

  char* line = NULL;
  size_t len = 0;
  ssize_t read;
  int n = 0;
  while((read = getline(&line, &len, input_fp)) != -1) {
    if (read < 3) continue;
    if (line[0] == '$' && line[1] == ' ') {
      // Search metadata specified as: "$ [num_features] [threshold] [budget]"
      data->num_features = strtoul(strtok(line+2," \n"),NULL,10);
      data->threshold = strtof(strtok(NULL," \n"),NULL);
      data->budget = strtof(strtok(NULL," \n"),NULL);
      data->costs = (float*) malloc(data->num_features * sizeof(float));
      data->features = (Feature**) malloc(data->num_features * sizeof(Feature*));      
    } else if (line[0] == 'd' && line[1] == ' ') {
      // Decision variable specified as: "d [decision_node_name]"
      int decision_index =
          bsearch_locate(sorted_node_names, node_count, strtok(line+2," \n"));
      data->decision = sorted_node_indicators[decision_index][0];
    } else if (line[0] == 'f' && line[1] == ' ') {
      // Features are specified as: "f [feature_node_name]"
      int index =
          bsearch_locate(sorted_node_names, node_count, strtok(line+2," \n"));
      SddSize num_indicators = sorted_node_num_indicators[index];
      data->features[n] = (Feature*) malloc(sizeof(Feature));
      data->features[n]->num_indicators = num_indicators;
      data->features[n]->indicators =
          (SddLiteral*) malloc(num_indicators * sizeof(SddLiteral));
      for (int i = 0; i < num_indicators; i++) {
        data->features[n]->indicators[i] = sorted_node_indicators[index][i];
      }
      data->costs[n] = strtof(strtok(NULL," \n"),NULL);
      n++;
    }
  }

  // Make literal_weights easier to index (1.0 weight to all negative literals)
  SddLiteral literal_count = 2 * data->var_count + 1;
  data->literal_weights = (SddWmc*) malloc(literal_count * sizeof(SddWmc));
  data->literal_weights += data->var_count;
  for (int i = 0; i < data->var_count; i++) {
    data->literal_weights[i+1] = weights[i];
    data->literal_weights[-(i+1)] = 1.0;
  }

  // Free all auxiliary variables
  for (int i = 0; i < node_count; i++) {
    free(sorted_node_names[i]);
    free(sorted_node_indicators[i]);
  }
  free(sorted_node_names);
  free(sorted_node_indicators);
  free(sorted_node_num_indicators);  
  free(weights);
  free(line);
  fclose(input_fp);

  return data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x1ef30(%rip), %rsi     # 0x23111
movq %r15, %rdi
callq 0x2220
testq %rax, %rax
je 0x4572
movq %rax, %rbx
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
leaq 0x80(%rsp), %r15
leaq 0x78(%rsp), %r12
leaq 0x68(%rsp), %rbp
leaq 0x40(%rsp), %r8
leaq 0x38(%rsp), %r9
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x3e48
pushq $0x48
popq %rdi
callq 0x21b0
movq (%r15), %r14
movq %r14, (%rax)
movq (%r12), %r15
leaq 0x30(%rsp), %r12
andq $0x0, (%r12)
leaq 0x70(%rsp), %r13
andq $0x0, (%r13)
movq %rax, 0x8(%rsp)
movq %r15, 0x10(%rax)
movq (%rbp), %rax
movq %rax, 0x28(%rsp)
leaq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
xorl %eax, %eax
leaq 0x40(%rsp), %rcx
movq (%rcx), %rbp
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2270
cmpq $-0x1, %rax
je 0x449d
cmpq $0x3, %rax
jl 0x427f
movq 0x30(%rsp), %rdi
movzbl (%rdi), %eax
cmpl $0x66, %eax
je 0x42fa
cmpl $0x64, %eax
je 0x42bf
cmpl $0x24, %eax
jne 0x427f
cmpb $0x20, 0x1(%rdi)
jne 0x427f
jmp 0x43e6
cmpb $0x20, 0x1(%rdi)
jne 0x427f
addq $0x2, %rdi
leaq 0x1ef50(%rip), %rsi     # 0x23220
callq 0x2230
movq 0x28(%rsp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x3dd0
cltq
movq (%rbp,%rax,8), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x18(%rcx)
jmp 0x427f
cmpb $0x20, 0x1(%rdi)
jne 0x427f
addq $0x2, %rdi
leaq 0x1ef11(%rip), %rsi     # 0x23220
callq 0x2230
movq 0x28(%rsp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x3dd0
movslq %eax, %rcx
movq %rcx, 0x50(%rsp)
movq 0x58(%rsp), %rax
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
pushq $0x10
popq %rdi
callq 0x21b0
movq %rax, 0x48(%rsp)
movq 0x10(%rsp), %rcx
movslq %ecx, %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
movq 0x20(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq (,%rcx,8), %rdi
callq 0x21b0
movq 0x20(%rsp), %r9
movq 0x50(%rsp), %r8
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
cmpq %rax, %r9
je 0x43af
movq (%rbp,%r8,8), %rcx
movq (%rcx,%rax,8), %rcx
movq (%rdi,%rsi,8), %rdx
movq (%rdx), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
jmp 0x4391
xorl %edi, %edi
leaq 0x1ee68(%rip), %rsi     # 0x23220
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x10(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
incl %ecx
movq %rcx, 0x10(%rsp)
jmp 0x427f
addq $0x2, %rdi
leaq 0x1ee2f(%rip), %rsi     # 0x23220
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2250
movq %rax, %rcx
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
xorl %edi, %edi
leaq 0x1ee03(%rip), %rsi     # 0x23220
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
cvtss2sd %xmm0, %xmm0
movq 0x8(%rsp), %rax
movsd %xmm0, 0x30(%rax)
xorl %edi, %edi
leaq 0x1eddd(%rip), %rsi     # 0x23220
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movss %xmm0, 0x38(%rax)
movq 0x18(%rsp), %rax
leaq (,%rax,4), %rdi
callq 0x21b0
movq 0x8(%rsp), %rcx
movq %rax, 0x40(%rcx)
movq 0x18(%rsp), %rdi
shlq $0x3, %rdi
callq 0x21b0
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rcx
movq %rax, 0x28(%rcx)
movq 0x10(%rsp), %rax
jmp 0x427a
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movq %r14, %rdi
shlq $0x4, %rdi
orq $0x8, %rdi
callq 0x21b0
leaq (%rax,%r14,8), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x8(%rcx)
movq 0x60(%rsp), %r12
xorl %ecx, %ecx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %r13, %rsi
cmpq %rcx, %r14
je 0x44fc
movsd (%r12,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rax,%rcx,8)
incq %rcx
movq %rsi, %rdi
sarq $0x1d, %rdi
movq %rdx, (%rax,%rdi)
addq %r13, %rsi
jmp 0x44d8
movq 0x68(%rsp), %rbp
movq 0x40(%rsp), %r13
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x4527
movq (%rbp,%r14,8), %rdi
callq 0x2030
movq (%r13,%r14,8), %rdi
callq 0x2030
incq %r14
jmp 0x4509
movq %rbp, %rdi
callq 0x2030
movq %r13, %rdi
callq 0x2030
movq 0x38(%rsp), %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
movq 0x30(%rsp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x20a0
movq 0x8(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ed56(%rip), %rdi     # 0x232cf
movq %r15, %rsi
xorl %eax, %eax
callq 0x20e0
pushq $0x1
popq %rdi
callq 0x2280