// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq 0x80(%rsp), %rax
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x48(%rsp)
movq 0x70(%rsp), %rdi
leaq 0x1ef95(%rip), %rsi     # 0x2415b
callq 0x2200
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x51ff
movq 0x25e19(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
movq 0x70(%rsp), %rdx
leaq 0x1f112(%rip), %rsi     # 0x24300
movb $0x0, %al
callq 0x2130
movl $0x1, %edi
callq 0x2260
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2250
movq %rax, 0x28(%rsp)
cmpq $-0x1, %rax
je 0x56d1
cmpq $0x5, 0x28(%rsp)
jge 0x524e
jmp 0x5221
movq 0x38(%rsp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x4e, %eax
jne 0x52b0
movq 0x38(%rsp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x24, %eax
jne 0x52b0
movq 0x38(%rsp), %rdi
addq $0x5, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x2230
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq %rcx, (%rax)
movq 0x68(%rsp), %rax
movq (%rax), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x80(%rsp), %rax
movq %rcx, (%rax)
jmp 0x56cc
movq 0x38(%rsp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x76, %eax
jne 0x534b
movq 0x38(%rsp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x24, %eax
jne 0x534b
movq 0x38(%rsp), %rdi
addq $0x5, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x2230
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, (%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rdi
shlq $0x3, %rdi
callq 0x2190
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, (%rax)
jmp 0x56ca
movq 0x38(%rsp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x56, %eax
jne 0x54b0
movq 0x38(%rsp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x24, %eax
jne 0x54b0
movq 0x58(%rsp), %rax
cmpq $0x0, (%rax)
je 0x54ab
movq 0x48(%rsp), %rax
cmpq $0x0, (%rax)
je 0x54ab
movq 0x28(%rsp), %rdi
subq $0x5, %rdi
movl $0x1, %esi
callq 0x2110
movq %rax, %rdx
movq 0x58(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x5, 0x1c(%rsp)
movslq 0x1c(%rsp), %rax
cmpq 0x28(%rsp), %rax
jge 0x5442
movq 0x38(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x24, %eax
jne 0x5407
movq 0x58(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x1c(%rsp), %ecx
subl $0x5, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x5442
movq 0x38(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x58(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x1c(%rsp), %ecx
subl $0x5, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x53bc
movq 0x38(%rsp), %rdi
movslq 0x1c(%rsp), %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x2150
movq %rax, %rdx
movq 0x48(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, %rdx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x56c8
movq 0x38(%rsp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x49, %eax
jne 0x562f
movq 0x38(%rsp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x24, %eax
jne 0x562f
movl $0x5, 0x18(%rsp)
movq 0x38(%rsp), %rax
movl 0x18(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x18(%rsp)
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x24, %eax
je 0x54fc
jmp 0x54dc
movq 0x38(%rsp), %rdi
movslq 0x18(%rsp), %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20e0
cvtss2sd %xmm0, %xmm0
movq 0x80(%rsp), %rax
movq (%rax), %rax
movl 0x20(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x20(%rsp)
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movl 0x18(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x18(%rsp)
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2b, %eax
je 0x5556
jmp 0x5536
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x38(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x14(%rsp)
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x24, %eax
je 0x5589
jmp 0x5569
movq 0x38(%rsp), %rax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq 0x58(%rsp), %rax
movq (%rax), %rdi
movq 0x60(%rsp), %rax
movq (%rax), %rax
movl %eax, %esi
movq 0x38(%rsp), %rdx
movslq 0x18(%rsp), %rax
addq %rax, %rdx
callq 0x50c0
movl %eax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x55f6
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x55cc
movq 0x38(%rsp), %rdi
addq $0x5, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x2150
movq %rax, %rdx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x56c6
movq 0x38(%rsp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x43, %eax
jne 0x56c4
movq 0x38(%rsp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x24, %eax
jne 0x56c4
movq 0x38(%rsp), %rax
movsbl 0x5(%rax), %eax
cmpl $0x2d, %eax
jne 0x5662
jmp 0x5221
movl $0x5, 0xc(%rsp)
movq 0x38(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0xc(%rsp)
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x24, %eax
je 0x568a
jmp 0x566a
movq 0x38(%rsp), %rdi
movslq 0xc(%rsp), %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20e0
cvtss2sd %xmm0, %xmm0
movq 0x80(%rsp), %rax
movq (%rax), %rax
movl 0x20(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x20(%rsp)
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x56c6
jmp 0x56c8
jmp 0x56ca
jmp 0x56cc
jmp 0x5221
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x40(%rsp), %rdi
callq 0x2080
addq $0x78, %rsp
retq
nopw (%rax,%rax)