// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
leaq 0x1ea45(%rip), %rsi     # 0x2415b
callq 0x2200
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
jne 0x574e
movq 0xb8(%rsp), %rsi
leaq 0x1ebe0(%rip), %rdi     # 0x2431d
movb $0x0, %al
callq 0x20c0
movl $0x1, %edi
callq 0x2260
movq 0xc0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0x98(%rsp), %rcx
leaq 0x90(%rsp), %r8
leaq 0x88(%rsp), %r9
leaq 0x80(%rsp), %rax
movq %rax, (%rsp)
callq 0x5190
movl $0x48, %edi
callq 0x2190
movq %rax, 0x78(%rsp)
movq 0xa8(%rsp), %rcx
movq 0x78(%rsp), %rax
movq %rcx, (%rax)
movq 0xa0(%rsp), %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x10(%rax)
movq $0x0, 0x70(%rsp)
movq $0x0, 0x68(%rsp)
movl $0x0, 0x5c(%rsp)
movq 0xb0(%rsp), %rdx
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2250
movq %rax, 0x60(%rsp)
cmpq $-0x1, %rax
je 0x5ae7
cmpq $0x3, 0x60(%rsp)
jge 0x5809
jmp 0x57d9
movq 0x70(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
jne 0x58ef
movq 0x70(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x20, %eax
jne 0x58ef
movq 0x70(%rsp), %rdi
addq $0x2, %rdi
leaq 0x1ea24(%rip), %rsi     # 0x24260
callq 0x2210
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x2230
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x20(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1e9f7(%rip), %rsi     # 0x24260
callq 0x2210
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20e0
cvtss2sd %xmm0, %xmm0
movq 0x78(%rsp), %rax
movsd %xmm0, 0x30(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1e9cd(%rip), %rsi     # 0x24260
callq 0x2210
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20e0
movq 0x78(%rsp), %rax
movss %xmm0, 0x38(%rax)
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rdi
shlq $0x2, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x28(%rax)
jmp 0x5ae2
movq 0x70(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
jne 0x596f
movq 0x70(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x20, %eax
jne 0x596f
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xa0(%rsp), %rax
movl %eax, 0x2c(%rsp)
movq 0x70(%rsp), %rdi
addq $0x2, %rdi
leaq 0x1e92d(%rip), %rsi     # 0x24260
callq 0x2210
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %esi
movq %rax, %rdx
callq 0x50c0
movl %eax, 0x58(%rsp)
movq 0x90(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x18(%rax)
jmp 0x5ae0
movq 0x70(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x66, %eax
jne 0x5ade
movq 0x70(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x20, %eax
jne 0x5ade
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xa0(%rsp), %rax
movl %eax, 0x1c(%rsp)
movq 0x70(%rsp), %rdi
addq $0x2, %rdi
leaq 0x1e8a5(%rip), %rsi     # 0x24260
callq 0x2210
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %esi
movq %rax, %rdx
callq 0x50c0
movl %eax, 0x54(%rsp)
movq 0x88(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x48(%rsp)
movl $0x10, %edi
callq 0x2190
movq %rax, %rdx
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movslq 0x5c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x48(%rsp), %rcx
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movslq 0x5c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movslq 0x5c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, (%rax)
movl $0x0, 0x44(%rsp)
movslq 0x44(%rsp), %rax
cmpq 0x48(%rsp), %rax
jae 0x5aa4
movq 0x90(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movslq 0x5c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movslq 0x44(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x5a53
xorl %eax, %eax
movl %eax, %edi
leaq 0x1e7b1(%rip), %rsi     # 0x24260
callq 0x2210
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20e0
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rax
movslq 0x5c(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x5ae0
jmp 0x5ae2
jmp 0x57d9
movq 0x78(%rsp), %rax
movq (%rax), %rax
shlq %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x78(%rsp), %rax
movq (%rax), %rcx
movq 0x78(%rsp), %rax
shlq $0x3, %rcx
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x34(%rsp)
movslq 0x34(%rsp), %rax
movq 0x78(%rsp), %rcx
cmpq (%rcx), %rax
jae 0x5ba0
movq 0x80(%rsp), %rax
movslq 0x34(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x34(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x34(%rsp), %edx
addl $0x1, %edx
xorl %ecx, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movsd 0x1e76a(%rip), %xmm0    # 0x242f8
movsd %xmm0, (%rax,%rcx,8)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x5b36
movl $0x0, 0x30(%rsp)
movslq 0x30(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jae 0x5bf0
movq 0x98(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2030
movq 0x90(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2030
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x5ba8
movq 0x98(%rsp), %rdi
callq 0x2030
movq 0x90(%rsp), %rdi
callq 0x2030
movq 0x88(%rsp), %rdi
callq 0x2030
movq 0x80(%rsp), %rdi
callq 0x2030
movq 0x70(%rsp), %rdi
callq 0x2030
movq 0xb0(%rsp), %rdi
callq 0x2080
movq 0x78(%rsp), %rax
addq $0xc8, %rsp
retq
nopl (%rax,%rax)