// File: /workspace/github_c_makefile_star2_O3/jondurbin[P]dre2/src/dre2_parse.c
// Function: dre2_skip_table
void dre2_skip_table( struct dre2 *graph )
{
  int i, j, r;
  int iter, l_iter;
  int matched, complete, total;
  int *swapper, *visited;
  int *reachable, *state, *r_temp;
  struct dre2_node *node;

  // Allocate some memory.
  state = ( int * )malloc( sizeof( int ) * graph->count );
  reachable = ( int * )malloc( sizeof( int ) * graph->count );
  r_temp = ( int * )malloc( sizeof( int ) * graph->count );

  // Initialize some variables.
  i = 0;
  iter = 0;
  total = 0;
  complete = false;

  // Initialize the reachable states to be neighbors of the first node.
  for ( i = 0; i < graph->v[graph->count - 1].p_count; i++ )
  {
    reachable[i] = graph->v[graph->count - 1].p[i];
    if ( reachable[i] == 0 )
    {
      for ( j = 0; j < RANGE; j++ )
        graph->skip_table[j] = 1;
      graph->min_length = 0;
      return;
    }
  }
  l_iter = i;

  visited = ( int * )malloc( sizeof( int ) * graph->count );
  for ( j = 0; j < RANGE; j++ )
    graph->skip_table[j] = 0;

  // Outer loop, runs until the initial node is reachable.
  while ( true )
  {
    if ( l_iter == 0 )
      break;

    total++;
    iter = 0;

    // Iterate through all of the reachable nodes.
    for ( j = 0; j < graph->count; j++ )
    {
      visited[j] = false;
      state[j] = false;
    }
    for ( i = 0; i < l_iter; i++ )
    {
      r = reachable[i];
      node = &graph->v[r];
      if ( total > 1 )
      {

        // Setup the skip table for the current node's chars.
        if ( node->c == DRE2_CHAR_CLASS )
        {
          for ( j = 0; j < RANGE; j++ )
          {
            if ( node->possible[j] && graph->skip_table[j] > total - 1 || graph->skip_table[j] == 0 )
              graph->skip_table[j] = total - 1;
          }
        } else if ( node->c >= 0 )
        {
          if ( graph->skip_table[node->c] > total - 1 || graph->skip_table[node->c] == 0 )
            graph->skip_table[node->c] = total - 1;
        }
      }
      if ( node->c == DRE2_BOL || node->c == DRE2_EOF || node->c == DRE2_EOL )
          total--;


      // Add the current node's parents to the next reachable nodes.
      for ( j = 0; j < node->p_count; j++ )
      {
        if ( !state[node->p[j]] && !visited[node->p[j]] )
          r_temp[iter++] = node->p[j];

        visited[node->p[j]] = true;
        state[node->p[j]] = true;
      }

      // Check if the initial state was reached.
      if ( state[0] == true )
      {
        complete = true;
      }
    }
    
    l_iter = iter;

    // Change the current reachable nodes to the next reachable nodes from above.
    swapper = reachable;
    reachable = r_temp;
    r_temp = swapper;
    if ( complete )
      break;
  }

  // Set remaining character's skip table value.
  for ( i = 0; i < RANGE; i++ )
  {
    if ( graph->skip_table[i] == 0 )
      graph->skip_table[i] = total;
  }

  // Set the min length of the regular expression.
  graph->min_length = total;

  free( reachable );
  free( r_temp );
  free( state );
  free( visited );
  reachable = NULL;
  r_temp = NULL;
  state = NULL;
  visited = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbp
movslq 0x8(%rdi), %r13
leaq (,%r13,4), %r12
movq %r12, %rdi
callq 0x22a0
movq %rax, %r14
movq %r12, %rdi
callq 0x22a0
movq %rax, (%rsp)
movq %r12, %rdi
callq 0x22a0
movq %rax, 0x8(%rsp)
movq (%rbp), %rax
shlq $0x6, %r13
movq %rax, 0x20(%rsp)
movslq -0x20(%rax,%r13), %rax
xorl %r15d, %r15d
movl $0x0, %ebx
testq %rax, %rax
jle 0x488d
addq 0x20(%rsp), %r13
movq -0x28(%r13), %rcx
xorl %edx, %edx
movq (%rsp), %rdi
movl (%rcx,%rdx,4), %esi
movl %esi, (%rdi,%rdx,4)
testl %esi, %esi
je 0x4ac5
incq %rdx
cmpq %rdx, %rax
jne 0x4875
movl %eax, %ebx
movq %r12, %rdi
callq 0x22a0
movq %rax, %r12
movq %rbp, 0x18(%rsp)
leaq 0xc(%rbp), %r13
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
xorl %esi, %esi
callq 0x2180
testl %ebx, %ebx
je 0x4a30
xorl %r15d, %r15d
movl $0x1, %ebp
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rax, (%rsp)
movq %rcx, 0x8(%rsp)
movq 0x18(%rsp), %rax
movslq 0x8(%rax), %rax
testq %rax, %rax
jle 0x4905
shlq $0x2, %rax
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
movq %rax, 0x10(%rsp)
callq 0x2180
movq %r14, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x2180
incl %r15d
testl %ebx, %ebx
jle 0x4a30
movl %ebx, %eax
movq %rax, 0x10(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movslq (%rax,%rcx,4), %rsi
shlq $0x6, %rsi
addq 0x20(%rsp), %rsi
cmpl $0x2, %r15d
jl 0x49a0
movl (%rsi), %edi
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
cmpq %rax, %rdi
jne 0x497d
movq 0x28(%rsi), %rdi
leal -0x1(%r15), %r8d
xorl %r9d, %r9d
cmpl $0x0, (%rdi,%r9,4)
setne %r11b
movl (%r13,%r9,4), %r10d
cmpl %r15d, %r10d
setge %al
testb %al, %r11b
jne 0x496a
testl %r10d, %r10d
jne 0x496f
movl %r8d, (%r13,%r9,4)
incq %r9
cmpq $0x100, %r9             # imm = 0x100
jne 0x494c
jmp 0x49a0
testl %edi, %edi
js 0x49a2
movl (%r13,%rdi,4), %eax
cmpl %r15d, %eax
setl %r8b
testl %eax, %eax
setne %al
testb %al, %r8b
jne 0x49a0
leal -0x1(%r15), %eax
movl %eax, (%r13,%rdi,4)
movl (%rsi), %edi
addl $0x12, %edi
cmpl $0xe, %edi
ja 0x49b7
movl $0x4003, %eax           # imm = 0x4003
btl %edi, %eax
jae 0x49b7
decl %r15d
movl 0x20(%rsi), %r8d
testl %r8d, %r8d
jle 0x4a04
movq 0x18(%rsi), %rdi
xorl %r9d, %r9d
movl (%rdi,%r9,4), %r11d
movslq %r11d, %r10
cmpl $0x0, (%r14,%r10,4)
jne 0x49f1
cmpl $0x0, (%r12,%r10,4)
jne 0x49f1
movslq %ebx, %rax
incl %ebx
movq (%rsp), %r8
movl %r11d, (%r8,%rax,4)
movslq (%rdi,%r9,4), %r10
movl 0x20(%rsi), %r8d
movl %ebp, (%r12,%r10,4)
movl %ebp, (%r14,%r10,4)
incq %r9
movslq %r8d, %rax
cmpq %rax, %r9
jl 0x49c7
movl (%r14), %eax
cmpl $0x1, %eax
cmovel %eax, %edx
incq %rcx
cmpq 0x10(%rsp), %rcx
jne 0x491d
testl %edx, %edx
jne 0x4a30
movq (%rsp), %rdi
movq %rdi, %rcx
testl %ebx, %ebx
jne 0x48c4
jmp 0x4a34
movq (%rsp), %rdi
xorl %eax, %eax
pxor %xmm0, %xmm0
movq 0x18(%rsp), %rdx
movdqu (%r13,%rax,4), %xmm1
pcmpeqd %xmm0, %xmm1
movd %xmm1, %ecx
testb $0x1, %cl
je 0x4a58
movl %r15d, (%r13,%rax,4)
pextrw $0x2, %xmm1, %ecx
testb $0x1, %cl
je 0x4a67
movl %r15d, 0x4(%r13,%rax,4)
pextrw $0x4, %xmm1, %ecx
testb $0x1, %cl
je 0x4a76
movl %r15d, 0x8(%r13,%rax,4)
pextrw $0x6, %xmm1, %ecx
testb $0x1, %cl
je 0x4a85
movl %r15d, 0xc(%r13,%rax,4)
addq $0x4, %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x4a3f
movl %r15d, 0x40c(%rdx)
callq 0x2070
movq 0x8(%rsp), %rdi
callq 0x2070
movq %r14, %rdi
callq 0x2070
movq %r12, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2070
xorl %eax, %eax
movaps 0x6672(%rip), %xmm0     # 0xb140
movups %xmm0, 0xc(%rbp,%rax,4)
addq $0x4, %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x4ace
movl $0x0, 0x40c(%rbp)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq