// File: /workspace/github_c_makefile_star2_O0/UCLA-StarAI[P]TrimBN/src/fnf/io.c
// Function: parse_fnf_file
Fnf* parse_fnf_file(char* buffer) {
  const char* whitespace = " \t\n\v\f\r";

  //id
  SddSize id=0;
  
  // initialize Fnf
  Fnf* cnf = (Fnf*)malloc(sizeof(Fnf));
  cnf->var_count = 0;
  cnf->litset_count = 0;
  cnf->litsets = NULL;

  // 1st token is "p" then check "cnf"
  char* token = strtok(buffer,whitespace);
  test_parse_fnf_file(token == NULL || strcmp(token,"p") != 0,
                      "Expected header \"p cnf\".");
  token = strtok(NULL,whitespace);
  test_parse_fnf_file(token == NULL || strcmp(token,"cnf") != 0,
                      "Expected header \"p cnf\".");

  // read variable & clause count
  cnf->var_count = cnf_int_strtok();
  cnf->litset_count = cnf_int_strtok();
  cnf->litsets = (LitSet*)calloc(cnf->litset_count,sizeof(LitSet));

  // read in clauses
  // assume longest possible clause is #-vars * 2
  LitSet* clause;
  SddLiteral* temp_clause = (SddLiteral*)calloc(cnf->var_count*2,sizeof(SddLiteral));
  SddLiteral lit;
  SddLiteral lit_index;
  for(SddSize clause_index = 0; clause_index < cnf->litset_count; clause_index++) {
    lit_index = 0;
    while (1) { // read a clause
      lit = cnf_int_strtok();
      if (lit == 0) break;
      test_parse_fnf_file(lit_index >= cnf->var_count*2,
                          "Unexpected long clause.");
      temp_clause[lit_index] = lit;
      lit_index++;
    }
    clause = &(cnf->litsets[clause_index]);
    clause->id = id++;
    clause->bit = 0;
    clause->literal_count = lit_index;
    clause->literals = (SddLiteral*)calloc(clause->literal_count,sizeof(SddLiteral));
    for(lit_index = 0; lit_index < clause->literal_count; lit_index++)
      clause->literals[lit_index] = temp_clause[lit_index];
  }

  free(temp_clause);
  return cnf;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
leaq 0x20d56(%rip), %rax     # 0x24196
movq %rax, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x20, %edi
callq 0x2190
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq $0x0, (%rax)
movq 0x38(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x38(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2210
movq %rax, 0x30(%rsp)
movb $0x1, %al
cmpq $0x0, 0x30(%rsp)
movb %al, 0x7(%rsp)
je 0x34c0
movq 0x30(%rsp), %rdi
leaq 0x21ea1(%rip), %rsi     # 0x25352
callq 0x2120
cmpl $0x0, %eax
setne %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x20ccd(%rip), %rsi     # 0x2419d
callq 0x37d0
movq 0x48(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x2210
movq %rax, 0x30(%rsp)
movb $0x1, %al
cmpq $0x0, 0x30(%rsp)
movb %al, 0x6(%rsp)
je 0x3511
movq 0x30(%rsp), %rdi
leaq 0x20c51(%rip), %rsi     # 0x24153
callq 0x2120
cmpl $0x0, %eax
setne %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x20c7c(%rip), %rsi     # 0x2419d
callq 0x37d0
callq 0x3810
movslq %eax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
callq 0x3810
movslq %eax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rdi
movl $0x30, %esi
callq 0x2110
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x38(%rsp), %rax
movq (%rax), %rdi
shlq %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x36d2
movq $0x0, 0x10(%rsp)
callq 0x3810
cltq
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x35bc
jmp 0x3606
movq 0x10(%rsp), %rax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
setge %al
andb $0x1, %al
movzbl %al, %edi
leaq 0x20bd8(%rip), %rsi     # 0x241b6
callq 0x37d0
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x35a6
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x30, 0x8(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rcx
movq %rcx, %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rax
movq %rcx, (%rax)
movq 0x28(%rsp), %rax
movb 0x28(%rax), %cl
andb $-0x2, %cl
orb $0x0, %cl
movb %cl, 0x28(%rax)
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
movl $0x8, %esi
callq 0x2110
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rax)
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x36bd
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x367d
jmp 0x36bf
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x3589
movq 0x20(%rsp), %rdi
callq 0x2030
movq 0x38(%rsp), %rax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)