// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movq %r8, %r14
movq %rcx, %r15
movq %rdx, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
leaq 0x1eb5a(%rip), %rsi     # 0x23190
callq 0x2220
testq %rax, %rax
je 0x49e3
movq %rax, %rbp
xorl %eax, %eax
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x40(%rsp), %rsi
movq $0x0, 0x20(%rsp)
movq %rax, (%rsi)
movq %rbp, %rdx
callq 0x2270
cmpq $-0x1, %rax
je 0x49c2
movq %rax, %r12
leaq 0x8(%rsp), %r13
leaq 0x40(%rsp), %rbx
movl $0x0, 0x4(%rsp)
cmpq $0x4, %r12
jle 0x4709
movq 0x8(%rsp), %rdi
movzbl 0x3(%rdi), %eax
cmpl $0x4d, %eax
jle 0x46e8
cmpl $0x76, %eax
je 0x46c5
cmpl $0x56, %eax
je 0x46de
cmpl $0x4e, %eax
jne 0x4709
cmpb $0x24, 0x4(%rdi)
je 0x4937
cmpl $0x55, %eax
jle 0x46e8
cmpl $0x56, %eax
je 0x46de
cmpl $0x76, %eax
jne 0x4709
cmpb $0x24, 0x4(%rdi)
je 0x48f2
cmpl $0x43, %eax
je 0x46fd
cmpl $0x49, %eax
je 0x46f2
cmpl $0x56, %eax
jne 0x4709
cmpb $0x24, 0x4(%rdi)
je 0x484f
cmpl $0x43, %eax
je 0x46fd
cmpl $0x49, %eax
jne 0x4709
cmpb $0x24, 0x4(%rdi)
je 0x4767
cmpl $0x43, %eax
jne 0x4709
cmpb $0x24, 0x4(%rdi)
jne 0x4709
cmpb $0x2d, 0x5(%rdi)
jne 0x4729
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x2270
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x4689
jmp 0x49c2
movl $0x5, %eax
cmpb $0x24, (%rdi,%rax)
leaq 0x1(%rax), %rax
jne 0x472e
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 0x4709
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 0x477b
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 0x47be
movl %ecx, %ecx
addq %rcx, %rax
cmpb $0x24, (%rax)
leaq 0x1(%rax), %rax
jne 0x47d6
movb $0x0, -0x1(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movl (%rax), %esi
movq 0x8(%rsp), %r12
movl %r15d, %edx
addq %r12, %rdx
callq 0x4594
movq %r14, 0x30(%rsp)
movq (%r14), %rcx
movslq %eax, %r15
movq (%rcx,%r15,8), %rax
xorl %r14d, %r14d
cmpq $0x0, (%rax,%r14)
leaq 0x8(%r14), %r14
jne 0x4814
incl 0x4(%rsp)
addq $0x5, %r12
movq %r12, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2170
movq 0x30(%rsp), %rdx
movq (%rdx), %rcx
movq (%rcx,%r15,8), %rcx
movq %rax, -0x8(%rcx,%r14)
movq %rdx, %r14
jmp 0x496f
movq %r15, %rax
movq %r14, %r15
movq %rax, 0x10(%rsp)
movq (%rax), %r14
testq %r14, %r14
je 0x496c
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
je 0x496c
leaq -0x5(%r12), %rdi
movl $0x1, %esi
callq 0x2130
movq 0x20(%rsp), %rdi
movslq %edi, %rdi
movq %rax, (%r14,%rdi,8)
movl $0x5, %eax
cmpq $0x5, %r12
movq %r15, %r14
movq 0x10(%rsp), %r15
je 0x497e
cmpq $0x7, %r12
movl $0x6, %edx
cmovlq %rdx, %r12
movl $0x5, %ecx
subq %r12, %rcx
movq %rdx, %rax
movq 0x8(%rsp), %rdx
movb -0x1(%rdx,%rax), %dl
movq (%r15), %rsi
movq (%rsi,%rdi,8), %rsi
cmpb $0x24, %dl
je 0x4979
movb %dl, -0x6(%rsi,%rax)
leaq 0x1(%rax), %rdx
leaq (%rcx,%rax), %rsi
incq %rsi
cmpq $0x6, %rsi
jne 0x48bc
jmp 0x497e
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 %rax, (%r15)
movq %r12, %rdi
callq 0x21b0
movq %rax, (%r14)
movq %r12, %rdi
callq 0x21b0
movq 0x18(%rsp), %rcx
jmp 0x4964
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 0x4709
movq %r15, %r14
movq 0x10(%rsp), %r15
jmp 0x4709
movb $0x0, -0x6(%rsi,%rax)
movq %rdi, %r12
movslq %eax, %rdi
addq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2170
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, (%rcx,%r12,8)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2130
movq (%r14), %rcx
movq %rax, (%rcx,%r12,8)
incl %r12d
movq %r12, 0x20(%rsp)
jmp 0x4709
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