// File: /workspace/github_c_makefile_star2_O2/UCLA-StarAI[P]TrimBN/src/trim/utils.c
// Function: parse_lmap
void parse_lmap(const char* filename, size_t* var_count, size_t* node_count,
    char*** sorted_node_names, SddLiteral*** sorted_node_indicators, 
    SddSize** sorted_node_num_indicators, SddWmc** weights) {
  FILE* fp = fopen(filename, "rb");
  if (fp == NULL) {
    fprintf(stderr, "Could not open lmap file %s\n", filename);
    exit(1);
  }

  char* line = NULL;
  size_t len = 0;
  ssize_t read;
  int node = 0, var = 0;
  while((read = getline(&line, &len, fp)) != -1) {
    if (read < 5) continue;
    if (line[3] == 'N' && line[4] == '$') {
      // Number of CNF variables specified as: "cc$N$[var_count]"
      *var_count = strtoul(line+5,NULL,10);
      *weights = (SddWmc*) malloc(*var_count * sizeof(SddWmc));
    } else if (line[3] == 'v' && line[4] == '$') {
      // Number of network nodes specified as: "cc$v$[node_count]"
      *node_count = strtoul(line+5,NULL,10);
      *sorted_node_names = (char**) malloc(*node_count * sizeof(char*));
      *sorted_node_indicators =
          (SddLiteral**) malloc(*node_count * sizeof(SddLiteral*));      
      *sorted_node_num_indicators =
          (SddSize*) malloc(*node_count * sizeof(SddSize));
    } else if (line[3] == 'V' && line[4] == '$') {
      // Each BN node specified as: "cc$V$[node_name]$[node_num_indicators]"
      if (*sorted_node_names != NULL && *sorted_node_num_indicators != NULL) {
        (*sorted_node_names)[node] = (char*) calloc(read - 5, sizeof(char));
        int i = 5;
        while (i < read) {
          if (line[i] == '$') {
            (*sorted_node_names)[node][i-5] = '\0';
            i++;
            break;
          }
          (*sorted_node_names)[node][i-5] = line[i];
          i++;
        }
        (*sorted_node_num_indicators)[node] = strtol(line+i,NULL,10);
        (*sorted_node_indicators)[node] =
            (SddLiteral*) calloc((*sorted_node_num_indicators)[node],
                                 sizeof(SddLiteral));
        node++;
      }
    } else if (line[3] == 'I' && line[4] == '$') {
      // Each indicator variable for some value of BN node specified as:
      //  "cc$I$[var_id]$[weight]$+$[node_name]$[value]"
      int start = 5;
      while (line[start++] != '$');
      (*weights)[var++] = strtof(line+start,NULL);
      while (line[start++] != '+'); start++;
      int end = start;
      while (line[end++] != '$'); line[end-1] = '\0';
      int pos = bsearch_locate(*sorted_node_names, *node_count, line+start);
      start = 0;
      // Find the next empty slot for indicator of this node
      while ((*sorted_node_indicators)[pos][start] != 0) start++;
      (*sorted_node_indicators)[pos][start] = strtol(line+5,NULL,10);
    } else if (line[3] == 'C' && line[4] == '$') {
      // Each parameter variable specified as: "cc$C$[var_id]$[weight]$+$"
      if (line[5] == '-') continue; // Ignore negative literals
      int start = 5;
      while (line[start++] != '$');
      (*weights)[var++] = strtof(line+start,NULL);
    }
  }

  free(line);
  fclose(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r13
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
leaq 0x1f29a(%rip), %rsi     # 0x23111
callq 0x2220
testq %rax, %rax
je 0x41a0
movq %rax, %rbp
leaq 0x8(%rsp), %rax
andq $0x0, (%rax)
leaq 0x40(%rsp), %rbx
andq $0x0, (%rbx)
xorl %r15d, %r15d
movq $0x0, 0x20(%rsp)
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2270
cmpq $-0x1, %rax
je 0x417f
movq %rax, %r12
cmpq $0x5, %rax
jl 0x3ea6
movq 0x8(%rsp), %rdi
movzbl 0x3(%rdi), %eax
movslq %r15d, %r14
cmpl $0x43, %eax
jne 0x3f1a
cmpb $0x24, 0x4(%rdi)
jne 0x3ea6
cmpb $0x2d, 0x5(%rdi)
je 0x3ea6
pushq $0x5
popq %rax
cmpb $0x24, (%rdi,%rax)
leaq 0x1(%rax), %rax
jne 0x3ee9
movl %eax, %eax
addq %rax, %rdi
xorl %esi, %esi
callq 0x2100
cvtss2sd %xmm0, %xmm0
movq 0x80(%rsp), %rax
movq (%rax), %rax
leal 0x1(%r14), %r15d
movsd %xmm0, (%rax,%r14,8)
jmp 0x3ea6
cmpl $0x49, %eax
je 0x4050
cmpl $0x76, %eax
je 0x3f77
cmpl $0x56, %eax
je 0x3fd0
cmpl $0x4e, %eax
jne 0x3ea6
cmpb $0x24, 0x4(%rdi)
jne 0x3ea6
addq $0x5, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2250
movq 0x38(%rsp), %rcx
movq %rax, (%rcx)
leaq (,%rax,8), %rdi
callq 0x21b0
movq 0x80(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x3ea6
cmpb $0x24, 0x4(%rdi)
jne 0x3ea6
addq $0x5, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2250
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
leaq (,%rax,8), %r12
movq %r12, %rdi
callq 0x21b0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x21b0
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x21b0
movq %rax, (%r13)
jmp 0x3ea6
cmpb $0x24, 0x4(%rdi)
jne 0x3ea6
movq 0x10(%rsp), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x3ea6
cmpq $0x0, (%r13)
je 0x3ea6
leaq -0x5(%r12), %rdi
pushq $0x1
popq %rsi
callq 0x2130
movq 0x20(%rsp), %rcx
movslq %ecx, %rcx
movq %rax, (%r14,%rcx,8)
movq %rcx, %r14
pushq $0x5
popq %rcx
subq %r12, %rcx
pushq $0x6
popq %rax
movq 0x10(%rsp), %rdi
leaq (%rcx,%rax), %rdx
cmpq $0x6, %rdx
je 0x4137
movq 0x8(%rsp), %rdx
movb -0x1(%rdx,%rax), %dl
movq (%rdi), %rsi
movq (%rsi,%r14,8), %rsi
cmpb $0x24, %dl
je 0x413c
movb %dl, -0x6(%rsi,%rax)
incq %rax
jmp 0x4020
cmpb $0x24, 0x4(%rdi)
jne 0x3ea6
movq %r13, 0x30(%rsp)
pushq $0x8
popq %rax
pushq $0x7
popq %rdx
pushq $0x5
popq %rcx
movq %rdx, %r12
cmpb $0x24, (%rdi,%rcx)
movl %eax, %r13d
leaq 0x1(%rcx), %rcx
leaq 0x1(%rdx), %rdx
leal 0x1(%r13), %eax
jne 0x4068
movl %ecx, %eax
addq %rax, %rdi
xorl %esi, %esi
callq 0x2100
cvtss2sd %xmm0, %xmm0
movq 0x80(%rsp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x8(%rsp), %rax
movl %r13d, %ecx
cmpb $0x2b, -0x1(%rax,%r12)
leaq 0x1(%r12), %r12
leal 0x1(%rcx), %r13d
jne 0x40a6
movl %ecx, %ecx
addq %rcx, %rax
movq 0x10(%rsp), %rcx
cmpb $0x24, (%rax)
leaq 0x1(%rax), %rax
jne 0x40c4
movb $0x0, -0x1(%rax)
movq (%rcx), %rdi
movq 0x28(%rsp), %rax
movl (%rax), %esi
movq 0x8(%rsp), %r14
movl %r12d, %edx
addq %r14, %rdx
callq 0x3dd0
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movslq %eax, %r12
movq (%rcx,%r12,8), %rax
xorl %r13d, %r13d
cmpq $0x0, (%rax,%r13)
leaq 0x8(%r13), %r13
jne 0x40fd
incl %r15d
addq $0x5, %r14
movq %r14, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2170
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq %rax, -0x8(%rcx,%r13)
movq 0x30(%rsp), %r13
jmp 0x3ea6
movq %r12, %rax
jmp 0x4141
movb $0x0, -0x6(%rsi,%rax)
movslq %eax, %rdi
addq 0x8(%rsp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x2170
movq (%r13), %rcx
movq %rax, (%rcx,%r14,8)
movq %rax, %rdi
pushq $0x8
popq %rsi
callq 0x2130
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, (%rcx,%r14,8)
incl %r14d
movq %r14, 0x20(%rsp)
jmp 0x3ea6
movq 0x8(%rsp), %rdi
callq 0x2030
movq %rbp, %rdi
callq 0x20a0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x26e51(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x1f101(%rip), %rsi     # 0x232b2
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2150
pushq $0x1
popq %rdi
callq 0x2280