// File: /workspace/github_c_makefile_star2_O1/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, %r14
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 0x3973
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 0x3969
cmpl $0x4, %eax
setge %r10b
testb $0x1, 0x43c(%r14)
sete %r11b
andb %r10b, %r11b
movzbl %r11b, %r10d
subl %r10d, %ebx
incq %r9
cmpq %r9, %rdi
jne 0x393c
jmp 0x3975
movl %edi, %ebx
testl %ecx, %ecx
jle 0x39bc
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 0x39b2
cmpl $0x4, %eax
setge %dil
testb $0x1, 0x43c(%r14)
sete %r8b
andb %dil, %r8b
movzbl %r8b, %edi
subl %edi, %r9d
incq %rsi
cmpq %rsi, %rcx
jne 0x3985
jmp 0x39bf
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(%r14), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
xorl %ebp, %ebp
movq %r14, 0x30(%rsp)
movq %r12, 0x418(%r14)
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 (%r14), %rax
testl %ebp, %ebp
je 0x3a54
movslq 0x8(%r14), %rdx
shlq $0x6, %rdx
leaq (%rax,%rdx), %rcx
addq $-0x20, %rcx
addq %rdx, %rax
addq $-0x28, %rax
jmp 0x3a5c
leaq 0x10(%rax), %rcx
addq $0x8, %rax
movslq (%rcx), %rcx
movq %rcx, 0x40(%rsp)
testq %rcx, %rcx
jle 0x3b29
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 0x3a9d
testb $0x1, 0x43c(%r14)
jne 0x3a9d
cmpl $0x3, 0x8(%r14)
jg 0x3b16
movq %r14, %rdi
callq 0x2260
movl $0x1, %r14d
cmpl $0x0, (%r12,%r14,4)
je 0x3b0a
movzbl %r14b, %esi
movq %r13, %rdi
callq 0x2160
testl %eax, %eax
jne 0x3b0a
testl %ebp, %ebp
je 0x3ae7
movslq %ebx, %rax
incl %ebx
movb %r14b, (%r13,%rax)
movq 0xa511(%rip), %rax      # 0xdfe8
movq 0x10(%rsp), %rcx
addl (%rax,%r14,4), %ecx
movq %rcx, 0x10(%rsp)
jmp 0x3b0a
movl (%rsp), %ecx
movslq %ecx, %rax
incl %ecx
movl %ecx, (%rsp)
movb %r14b, (%r13,%rax)
movl 0x4(%rsp), %eax
movq 0xa4e6(%rip), %rcx      # 0xdfe8
addl (%rcx,%r14,4), %eax
movl %eax, 0x4(%rsp)
incq %r14
cmpq $0x100, %r14            # imm = 0x100
jne 0x3aab
incq %r15
cmpq 0x40(%rsp), %r15
movq 0x30(%rsp), %r14
jne 0x3a78
leal 0x1(%rbp), %eax
testl %ebp, %ebp
movl %eax, %ebp
je 0x3a0f
movq %r13, %rdi
callq 0x2070
movq %r12, %rdi
callq 0x2070
movq 0x20(%rsp), %rax
movq %rax, 0x418(%r14)
movq 0x28(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl %ecx, (%rax)
movl (%rsp), %ecx
movl %ecx, 0x4(%rax)
movl 0x4(%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