// File: /workspace/github_c_makefile_star2_O3/jondurbin[P]dre2/src/dre2_parse.c
// Function: dre2_first_or_last_cost
struct dre2_fl_cost dre2_first_or_last_cost( struct dre2 *graph, int *minimal )
{
  int i, j, k;
  int f_n_count, f_c_count, f_frequency;
  int l_n_count, l_c_count, l_frequency;
  int next_count, *next_ptr;
  struct dre2_node *node;
  struct dre2_fl_cost cost;
  unsigned char *temp;
  int *tp, *original;

  f_n_count = graph->v[0].n_count;
  f_c_count = 0;
  f_frequency = 0;

  l_n_count = graph->v[graph->count - 1].p_count;
  l_c_count = 0;
  l_frequency = 0;

  for ( i = 0; i < graph->v[0].n_count; i++ )
  {
    if ( !minimal[graph->v[0].n[i]] && !( graph->options & DRE2_GREEDY ) && graph->count > 3 )
      f_n_count--;
  }
  for ( i = 0; i < graph->v[graph->count - 1].p_count; i++ )
  {
    if ( !minimal[graph->v[graph->count - 1].p[i]] && !( graph->options & DRE2_GREEDY ) && graph->count > 3 )
      l_n_count--;
  }

  // Get number of possible chars in the first reachable nodes.
  tp = ( int * )malloc( sizeof( int ) * RANGE );
  temp = ( unsigned char * )malloc( sizeof( unsigned char ) * RANGE );

  original = graph->starting_chars;
  for ( k = 0; k < 2; k++ )
  {
    graph->starting_chars = tp;
    for ( i = 0; i < RANGE; i++ )
    {
      tp[i] = false;
      temp[i] = '\0';
    }

    if ( k == 0 )
    {
      next_count = graph->v[0].n_count;
      next_ptr = graph->v[0].n;
    } else
    {
      next_count = graph->v[graph->count - 1].p_count;
      next_ptr = graph->v[graph->count - 1].p;
    }
    for ( i = 0; i < next_count; i++ )
    {
      if ( !minimal[next_ptr[i]] && !( graph->options & DRE2_GREEDY ) && graph->count > 3 )
        continue;
      dre2_set_chars( graph, next_ptr[i] );
      for ( j = 1; j < RANGE; j++ )
      {
        if ( tp[j] && !dre2_contains_char( temp, j ) )
        {
          if ( k == 0 )
          {
            f_c_count++;
            temp[f_c_count - 1] = j;
            f_frequency += dre2_frequency[j];
          } else
          {
            l_c_count++;
            temp[l_c_count - 1] = j;
            l_frequency += dre2_frequency[j];
          }
        } else
        {
        }
      }
    }
  }
  free( temp ); temp = NULL;
  free( tp ); tp = NULL;
  graph->starting_chars = original;

  cost.f_n_count = f_n_count;
  cost.f_c_count = f_c_count;
  cost.f_frequency = f_frequency;
  cost.l_n_count = l_n_count;
  cost.l_c_count = l_c_count;
  cost.l_frequency = l_frequency;

  return cost;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %rbp
movq %rdi, 0x28(%rsp)
movq (%rsi), %rdx
movslq 0x10(%rdx), %rdi
movslq 0x8(%rsi), %rax
movq %rax, %rsi
shlq $0x6, %rsi
movl -0x20(%rdx,%rsi), %ecx
testq %rdi, %rdi
jle 0x38f4
movq 0x8(%rdx), %r8
xorl %r9d, %r9d
movl %edi, %ebx
movslq (%r8,%r9,4), %r10
movq 0x8(%rsp), %r11
cmpl $0x0, (%r11,%r10,4)
jne 0x38ea
cmpl $0x4, %eax
setge %r10b
testb $0x1, 0x43c(%rbp)
sete %r11b
andb %r10b, %r11b
movzbl %r11b, %r10d
subl %r10d, %ebx
incq %r9
cmpq %r9, %rdi
jne 0x38be
jmp 0x38f6
movl %edi, %ebx
testl %ecx, %ecx
jle 0x393c
addq %rsi, %rdx
movq -0x28(%rdx), %rdx
xorl %esi, %esi
movl %ecx, %r9d
movslq (%rdx,%rsi,4), %rdi
movq 0x8(%rsp), %r8
cmpl $0x0, (%r8,%rdi,4)
jne 0x3932
cmpl $0x4, %eax
setge %dil
testb $0x1, 0x43c(%rbp)
sete %r8b
andb %dil, %r8b
movzbl %r8b, %edi
subl %edi, %r9d
incq %rsi
cmpq %rsi, %rcx
jne 0x3906
jmp 0x393f
movl %ecx, %r9d
movl %r9d, 0x18(%rsp)
movl %ebx, 0x1c(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x22a0
movq %rax, %r12
movl $0x100, %edi            # imm = 0x100
callq 0x22a0
movq %rax, %r13
movq 0x418(%rbp), %rax
movq %rax, 0x20(%rsp)
movb $0x1, %r15b
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movl $0x0, (%rsp)
xorl %r14d, %r14d
movq %rbp, 0x30(%rsp)
movq %r12, 0x418(%rbp)
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x2180
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
xorl %esi, %esi
callq 0x2180
movq (%rbp), %rax
testb $0x1, %r15b
je 0x39c4
leaq 0x10(%rax), %rcx
addq $0x8, %rax
jmp 0x39db
movslq 0x8(%rbp), %rdx
shlq $0x6, %rdx
leaq (%rax,%rdx), %rcx
addq $-0x20, %rcx
addq %rdx, %rax
addq $-0x28, %rax
movl %r15d, 0x4(%rsp)
movslq (%rcx), %rcx
movq %rcx, 0x40(%rsp)
testq %rcx, %rcx
jle 0x3aa4
movq (%rax), %rax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
movq 0x38(%rsp), %rax
movslq (%rax,%r15,4), %rsi
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax,%rsi,4)
jne 0x3a1f
testb $0x1, 0x43c(%rbp)
jne 0x3a1f
cmpl $0x3, 0x8(%rbp)
jg 0x3a91
movq %rbp, %rdi
callq 0x2260
movl $0x1, %ebp
cmpl $0x0, (%r12,%rbp,4)
je 0x3a85
movzbl %bpl, %esi
movq %r13, %rdi
callq 0x2160
testl %eax, %eax
jne 0x3a85
testb $0x1, 0x4(%rsp)
je 0x3a67
movslq %r14d, %rax
incl %r14d
movb %bpl, (%r13,%rax)
movl (%rsp), %eax
movq 0xa589(%rip), %rcx      # 0xdfe8
addl (%rcx,%rbp,4), %eax
movl %eax, (%rsp)
jmp 0x3a85
movslq %ebx, %rax
incl %ebx
movb %bpl, (%r13,%rax)
movq 0x10(%rsp), %rax
movq 0xa56b(%rip), %rcx      # 0xdfe8
addl (%rcx,%rbp,4), %eax
movq %rax, 0x10(%rsp)
incq %rbp
cmpq $0x100, %rbp            # imm = 0x100
jne 0x3a2c
incq %r15
cmpq 0x40(%rsp), %r15
movq 0x30(%rsp), %rbp
jne 0x39fc
testb $0x1, 0x4(%rsp)
movl $0x0, %r15d
jne 0x398b
movq %r13, %rdi
callq 0x2070
movq %r12, %rdi
callq 0x2070
movq 0x20(%rsp), %rax
movq %rax, 0x418(%rbp)
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl %ecx, (%rax)
movl %r14d, 0x4(%rax)
movl (%rsp), %ecx
movl %ecx, 0x8(%rax)
movl 0x18(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl %ebx, 0x10(%rax)
movq 0x10(%rsp), %rcx
movl %ecx, 0x14(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq