// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x1e787(%rip), %rsi     # 0x23190
movq %r15, %rdi
callq 0x2220
testq %rax, %rax
je 0x4dca
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
leaq 0x90(%rsp), %r15
leaq 0x88(%rsp), %r12
leaq 0x58(%rsp), %rbp
leaq 0x20(%rsp), %r8
leaq 0x50(%rsp), %r9
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x4609
movl $0x48, %edi
callq 0x21b0
movq (%r15), %r14
movq %r14, (%rax)
movq (%r12), %r15
movq %r15, 0x10(%rax)
xorl %ecx, %ecx
leaq 0x40(%rsp), %r12
movq %rcx, (%r12)
leaq 0x80(%rsp), %r13
movq $0x0, 0x18(%rsp)
movq %rcx, (%r13)
movq (%rbp), %rcx
movq %rcx, 0x38(%rsp)
movq %rax, %rbp
leaq 0x50(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x78(%rsp)
leaq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2270
cmpq $-0x1, %rax
je 0x4cec
cmpq $0x3, %rax
jl 0x4ab4
movq 0x40(%rsp), %rdi
movzbl (%rdi), %eax
cmpl $0x66, %eax
je 0x4bf7
cmpl $0x64, %eax
je 0x4bb1
cmpl $0x24, %eax
jne 0x4ab4
cmpb $0x20, 0x1(%rdi)
jne 0x4ba3
addq $0x2, %rdi
leaq 0x1e79a(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2250
movq %rbp, 0x10(%rsp)
movq %rax, %rbp
movq 0x10(%rsp), %rax
movq %rbp, 0x20(%rax)
xorl %edi, %edi
leaq 0x1e76c(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
cvtss2sd %xmm0, %xmm0
movq 0x10(%rsp), %rax
movsd %xmm0, 0x30(%rax)
xorl %edi, %edi
leaq 0x1e746(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x10(%rsp), %rax
movss %xmm0, 0x38(%rax)
leaq (,%rbp,4), %rdi
callq 0x21b0
movq 0x10(%rsp), %rcx
movq %rax, 0x40(%rcx)
shlq $0x3, %rbp
movq %rbp, %rdi
movq 0x10(%rsp), %rbp
callq 0x21b0
movq %rax, 0x28(%rbp)
jmp 0x4ab4
cmpl $0x66, %eax
je 0x4bf7
cmpl $0x64, %eax
jne 0x4ab4
cmpb $0x20, 0x1(%rdi)
jne 0x4bee
addq $0x2, %rdi
leaq 0x1e6de(%rip), %rsi     # 0x232a0
callq 0x2230
movq 0x38(%rsp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x4594
cltq
movq 0x30(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq (%rax), %rax
movq %rax, 0x18(%rbp)
jmp 0x4ab4
cmpl $0x66, %eax
jne 0x4ab4
cmpb $0x20, 0x1(%rdi)
jne 0x4ab4
addq $0x2, %rdi
leaq 0x1e694(%rip), %rsi     # 0x232a0
callq 0x2230
movq 0x38(%rsp), %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x4594
movslq %eax, %rcx
movq %rbp, 0x10(%rsp)
movq 0x78(%rsp), %rax
movq %rcx, 0x68(%rsp)
movq (%rax,%rcx,8), %rbp
movl $0x10, %edi
callq 0x21b0
movq %rax, 0x70(%rsp)
movq 0x10(%rsp), %rcx
movq 0x28(%rcx), %rcx
movl 0x18(%rsp), %edx
movq %rdx, 0x28(%rsp)
movq %rcx, 0x60(%rsp)
movq %rax, (%rcx,%rdx,8)
movq %rbp, 0x8(%rax)
leaq (,%rbp,8), %rdi
callq 0x21b0
movq 0x70(%rsp), %rcx
movq %rax, (%rcx)
testq %rbp, %rbp
je 0x4cb0
movq 0x30(%rsp), %rax
movq 0x68(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x28(%rsp), %rcx
movq 0x60(%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, %rbp
jne 0x4ca0
xorl %edi, %edi
leaq 0x1e5e7(%rip), %rsi     # 0x232a0
callq 0x2230
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x10(%rsp), %rbp
movq 0x40(%rbp), %rax
movq 0x28(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
jmp 0x4ab4
movq %r14, %rdi
shlq $0x4, %rdi
orq $0x8, %rdi
callq 0x21b0
leaq (%rax,%r14,8), %rcx
movq %rcx, 0x8(%rbp)
testq %r14, %r14
je 0x4d4f
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
movq 0x48(%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 0x4d2f
testq %r15, %r15
je 0x4d7b
movq 0x58(%rsp), %r14
movq 0x20(%rsp), %r12
xorl %r13d, %r13d
movq (%r14,%r13,8), %rdi
callq 0x2030
movq (%r12,%r13,8), %rdi
callq 0x2030
incq %r13
cmpq %r13, %r15
jne 0x4d61
movq 0x58(%rsp), %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0x2030
movq 0x50(%rsp), %rdi
callq 0x2030
movq 0x48(%rsp), %rdi
callq 0x2030
movq 0x40(%rsp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x20a0
movq %rbp, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e57e(%rip), %rdi     # 0x2334f
movq %r15, %rsi
xorl %eax, %eax
callq 0x20e0
movl $0x1, %edi
callq 0x2280