// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
leaq 0x1eb32(%rip), %rsi     # 0x23190
callq 0x2220
testq %rax, %rax
je 0x49d6
movq %rax, %rbp
xorl %r14d, %r14d
leaq 0x8(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x40(%rsp), %rsi
movq %r14, (%rsi)
movq %rax, %rdx
callq 0x2270
cmpq $-0x1, %rax
je 0x49b5
movq %rax, %r12
leaq 0x8(%rsp), %r13
leaq 0x40(%rsp), %rbx
movl $0x0, 0x4(%rsp)
cmpq $0x4, %r12
jle 0x46cd
movq 0x8(%rsp), %rdi
movzbl 0x3(%rdi), %eax
cmpl $0x43, %eax
jne 0x46e9
cmpb $0x24, 0x4(%rdi)
jne 0x46cd
cmpb $0x2d, 0x5(%rdi)
jne 0x4929
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2270
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x46a9
jmp 0x49b5
cmpl $0x55, %eax
jg 0x4734
cmpl $0x49, %eax
je 0x4798
cmpl $0x4e, %eax
jne 0x46cd
cmpb $0x24, 0x4(%rdi)
jne 0x46cd
addq $0x5, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2250
movq 0x38(%rsp), %rcx
movq %rax, (%rcx)
leaq (,%rax,8), %rdi
callq 0x21b0
movq 0x80(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x46cd
cmpl $0x56, %eax
je 0x4890
cmpl $0x76, %eax
jne 0x46cd
cmpb $0x24, 0x4(%rdi)
jne 0x46cd
addq $0x5, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2250
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
leaq (,%rax,8), %r12
movq %r12, %rdi
callq 0x21b0
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x21b0
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x21b0
movq %rax, (%r15)
jmp 0x46cd
cmpb $0x24, 0x4(%rdi)
jne 0x46cd
movq %r15, 0x10(%rsp)
movl $0x5, %ecx
movl $0x7, %edx
movl $0x8, %eax
movq %rdx, %r15
cmpb $0x24, (%rdi,%rcx)
movl %eax, %r12d
leaq 0x1(%rcx), %rcx
leaq 0x1(%rdx), %rdx
leal 0x1(%r12), %eax
jne 0x47b6
movl %ecx, %eax
addq %rax, %rdi
xorl %esi, %esi
callq 0x2100
cvtss2sd %xmm0, %xmm0
movq 0x80(%rsp), %rax
movq (%rax), %rax
movslq 0x4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
movl %r12d, %ecx
cmpb $0x2b, -0x1(%rax,%r15)
leaq 0x1(%r15), %r15
leal 0x1(%rcx), %r12d
jne 0x47f9
movl %ecx, %ecx
addq %rcx, %rax
cmpb $0x24, (%rax)
leaq 0x1(%rax), %rax
jne 0x4811
movb $0x0, -0x1(%rax)
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movl (%rax), %esi
movq 0x8(%rsp), %r12
movl %r15d, %edx
addq %r12, %rdx
callq 0x45c0
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
cltq
movq %rax, 0x30(%rsp)
movq (%rcx,%rax,8), %rax
xorl %r15d, %r15d
cmpq $0x0, (%rax,%r15)
leaq 0x8(%r15), %r15
jne 0x4853
incl 0x4(%rsp)
addq $0x5, %r12
movq %r12, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2170
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq 0x30(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rax, -0x8(%rcx,%r15)
jmp 0x496a
cmpb $0x24, 0x4(%rdi)
jne 0x46cd
movq %r15, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x496a
movq 0x10(%rsp), %rax
cmpq $0x0, (%rax)
je 0x496a
leaq -0x5(%r12), %rdi
movl $0x1, %esi
callq 0x2130
movslq %r14d, %r14
movq %rax, (%r15,%r14,8)
movl $0x5, %eax
cmpq $0x5, %r12
movq 0x10(%rsp), %r15
je 0x4979
movl $0x5, %ecx
subq %r12, %rcx
movl $0x6, %eax
movq 0x8(%rsp), %rdx
movb -0x1(%rdx,%rax), %dl
movq 0x20(%rsp), %rsi
movq (%rsi), %rsi
movq (%rsi,%r14,8), %rsi
cmpb $0x24, %dl
je 0x4974
movb %dl, -0x6(%rsi,%rax)
leaq (%rcx,%rax), %rdx
incq %rdx
incq %rax
cmpq $0x6, %rdx
jne 0x48f6
movq %r12, %rax
jmp 0x4979
movl $0x5, %eax
cmpb $0x24, (%rdi,%rax)
leaq 0x1(%rax), %rax
jne 0x492e
movl %eax, %eax
addq %rax, %rdi
xorl %esi, %esi
callq 0x2100
cvtss2sd %xmm0, %xmm0
movq 0x80(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rsp), %edx
movslq %edx, %rcx
incl %edx
movl %edx, 0x4(%rsp)
movsd %xmm0, (%rax,%rcx,8)
jmp 0x46cd
movq 0x10(%rsp), %r15
jmp 0x46cd
movb $0x0, -0x6(%rsi,%rax)
movslq %eax, %rdi
addq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2170
movq (%r15), %rcx
movq %rax, (%rcx,%r14,8)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2130
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, (%rcx,%r14,8)
incl %r14d
jmp 0x46cd
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 %rbx, %rdi
callq 0x2329