// File: /workspace/github_c_makefile_star2_O3/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 0x1e794(%rip), %rsi     # 0x23190
movq %r15, %rdi
callq 0x2220
testq %rax, %rax
je 0x4da8
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0x80(%rsp), %r15
leaq 0x78(%rsp), %r12
leaq 0x68(%rsp), %rbp
leaq 0x48(%rsp), %r8
leaq 0x40(%rsp), %r9
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x462f
movl $0x48, %edi
callq 0x21b0
movq (%r15), %r14
movq %r14, (%rax)
movq (%r12), %r15
movq %rax, 0x8(%rsp)
movq %r15, 0x10(%rax)
xorl %eax, %eax
leaq 0x30(%rsp), %r12
movq %rax, (%r12)
leaq 0x70(%rsp), %r13
movq $0x0, 0x18(%rsp)
movq %rax, (%r13)
movq (%rbp), %rax
movq %rax, 0x28(%rsp)
leaq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x60(%rsp)
leaq 0x48(%rsp), %rax
movq (%rax), %rbp
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2270
cmpq $-0x1, %rax
je 0x4cc8
cmpq $0x3, %rax
jl 0x4a9e
movq 0x30(%rsp), %rdi
movzbl (%rdi), %eax
cmpl $0x66, %eax
je 0x4bd7
cmpl $0x64, %eax
je 0x4b95
cmpl $0x24, %eax
jne 0x4a9e
cmpb $0x20, 0x1(%rdi)
jne 0x4a9e
addq $0x2, %rdi
leaq 0x1e7b4(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2250
movq %rax, %rcx
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
xorl %edi, %edi
leaq 0x1e786(%rip), %rsi     # 0x232a0
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 0x1e760(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movss %xmm0, 0x38(%rax)
movq 0x10(%rsp), %rax
leaq (,%rax,4), %rdi
callq 0x21b0
movq 0x8(%rsp), %rcx
movq %rax, 0x40(%rcx)
movq 0x10(%rsp), %rdi
shlq $0x3, %rdi
callq 0x21b0
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rcx
movq %rax, 0x28(%rcx)
jmp 0x4a9e
cmpb $0x20, 0x1(%rdi)
jne 0x4a9e
addq $0x2, %rdi
leaq 0x1e6f6(%rip), %rsi     # 0x232a0
callq 0x2230
movq 0x28(%rsp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x45c0
cltq
movq (%rbp,%rax,8), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x18(%rcx)
jmp 0x4a9e
cmpb $0x20, 0x1(%rdi)
jne 0x4a9e
addq $0x2, %rdi
leaq 0x1e6b4(%rip), %rsi     # 0x232a0
callq 0x2230
movq 0x28(%rsp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x45c0
movslq %eax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x50(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movl $0x10, %edi
callq 0x21b0
movq %rax, 0x58(%rsp)
movq 0x18(%rsp), %rdx
movslq %edx, %rdx
movq %rdx, 0x18(%rsp)
movq 0x20(%rsp), %rcx
movq %rax, (%rcx,%rdx,8)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq (,%rcx,8), %rdi
callq 0x21b0
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
testq %rdi, %rdi
je 0x4c91
movq 0x50(%rsp), %rax
movq (%rbp,%rax,8), %rax
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
movq %rsi, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rdi
jne 0x4c81
xorl %edi, %edi
leaq 0x1e606(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x18(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
incl %ecx
movq %rcx, 0x18(%rsp)
jmp 0x4a9e
movq %r14, %rdi
shlq $0x4, %rdi
orq $0x8, %rdi
callq 0x21b0
leaq (%rax,%r14,8), %rcx
movq 0x8(%rsp), %rbp
movq %rcx, 0x8(%rbp)
testq %r14, %r14
je 0x4d30
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
movq 0x38(%rsp), %rsi
leaq (%rax,%r14,8), %rax
addq $0x8, %rax
xorl %edi, %edi
movabsq $0x3ff0000000000000, %r8 # imm = 0x3FF0000000000000
movq %rdx, %r9
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
movq %r9, %r10
sarq $0x1d, %r10
movq %r8, (%rcx,%r10)
addq %rdx, %r9
cmpq %rdi, %r14
jne 0x4d10
movq 0x68(%rsp), %r13
movq 0x48(%rsp), %r12
testq %r15, %r15
je 0x4d5d
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x2030
movq (%r12,%r14,8), %rdi
callq 0x2030
incq %r14
cmpq %r14, %r15
jne 0x4d42
movq %r13, %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
movq 0x40(%rsp), %rdi
callq 0x2030
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x30(%rsp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x20a0
movq %rbp, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e5a0(%rip), %rdi     # 0x2334f
movq %r15, %rsi
xorl %eax, %eax
callq 0x20e0
movl $0x1, %edi
callq 0x2280