// File: /workspace/github_c_makefile_star2_O3/jondurbin[P]dre2/src/dre2_parse.c
// Function: dre2_parse
struct dre2 *dre2_parse( unsigned char *re, int options )
{
  int i, j;
  struct dre2_node *v, *min_v;
  int node_count, group_count;
  int length;
  int all_minimal;
  int *minimal, *new_minimal, minimal_count, *minimal_id;
  struct dre2_parse_return ret;
  struct dre2 *min_graph, *graph;
  int bol, eol, eof;

  // Setup the return graph and temp, initial graph.
  graph = ( struct dre2 * )malloc( sizeof( struct dre2 ) );
  min_graph = ( struct dre2 * )malloc( sizeof( struct dre2 ) );

  // Make sure options are set correctly.
  if ( options & DRE2_FULL_MATCH || options & DRE2_SUBMATCH )
    options = options | DRE2_GREEDY;

  // Call the recursive parse function.
  graph->options = options;
  graph->starting_points = NULL;
  graph->starting_chars = NULL;
  graph->original = NULL;
  graph->r_temp = NULL;
  graph->reachable = NULL;
  graph->state = NULL;

  v = NULL;

  minimal = ( int * )malloc( sizeof( int ) );
  node_count = 0;
  length = strlen( re );
  group_count = 0;
  ret = dre2_parse_recursive( &v, &node_count, re, length, 0, &minimal, &group_count );
  graph->group_count = group_count;
  if ( ret.pos == -1 )
  {
    cleanup_nodes( &v, node_count );
    free( minimal );
    free( v );
    minimal = NULL;
    v = NULL;
    graph->v = v;
    free( graph );
    graph = NULL;
    return graph;
  }

  graph->v = v;
  graph->count = node_count;

  dre2_add_parents( graph );

  min_graph->options = options;
  min_graph->starting_points = NULL;
  min_graph->starting_chars = ( int * )calloc( RANGE, sizeof( int ) );

  new_minimal = ( int * )calloc( graph->count, sizeof( int ) );
  minimal_id = ( int * )malloc( sizeof( int ) * graph->count );
  minimal_count = 0;

  // Strip out the group nodes and make it an epsilon-free graph.
  min_v = ( struct dre2_node * )malloc( sizeof( struct dre2 ) * graph->count );
  dre2_strip_groups( graph, min_graph, &min_v, minimal, &new_minimal, &minimal_count, &minimal_id );
  min_graph->v = min_v;

  // Setup the reverse graph.
  dre2_add_parents( min_graph );

  // Setup the skip table and find impossible chars.
  dre2_skip_table( min_graph );
  dre2_impossible_chars( min_graph );

  // Clean up the original graph's memory if we don't need it.
  if ( !( options & DRE2_SUBMATCH ) )
  {
    cleanup_dre2( graph );
    min_graph->original = NULL;
  } else
  {
    min_graph->original = graph;
  }

  // Find the best starting point and chars.
  min_graph->starting_point = dre2_starting_point( min_graph, new_minimal, minimal_id, minimal_count );
  dre2_starting_chars( min_graph, new_minimal );

  if ( min_graph->single )
  {
    for ( i = 0; i < RANGE; i++ )
    {
      if ( min_graph->starting_chars[i] )
        min_graph->c = i;
    }
  }

  if ( min_graph->starting_point == -1 )
  {
    min_graph->match_method = DRE2_MN;
  } else if ( min_graph->starting_point == 0 || min_graph->starting_point != min_graph->count - 1 )
  {
    if ( min_graph->single )
      min_graph->match_method = DRE2_SN_SC;
    else
      min_graph->match_method = DRE2_SN_MC;
  } else
  {
    if ( min_graph->single )
      min_graph->match_method = DRE2_SN_SC_H;
    else
      min_graph->match_method = DRE2_SN_MC_H;
  }

  // Make it case-insensitive.
  if ( options & DRE2_NO_CASE )
  {
    for ( i = 0; i < min_graph->count; i++ )
    {
      if ( min_graph->v[i].c == DRE2_CHAR_CLASS )
      {
        for ( j = 'a'; j <= 'z'; j++ )
        {
          if ( min_graph->v[i].possible[j] )
            min_graph->v[i].possible[j - ('a' - 'A')] = true;
        }
        for ( j = 'A'; j <= 'Z'; j++ )
        {
          if ( min_graph->v[i].possible[j] )
            min_graph->v[i].possible[j + ('a' - 'A')] = true;
        }
      }
    }
  }

  if ( options & DRE2_THREAD_SAFE )
  {
    min_graph->r_temp = NULL;
    min_graph->reachable = NULL;
    min_graph->state = NULL;
  } else
  {
    min_graph->r_temp = ( int * )malloc( sizeof( int ) * min_graph->count );
    min_graph->reachable = ( int * )malloc( sizeof( int ) * min_graph->count );
    min_graph->state = ( int * )calloc( min_graph->count, sizeof( int ) );
  }

  if ( min_graph->starting_point > 0 && min_graph->starting_point < graph->count - 1 )
  {
    if ( min_graph->match_method == DRE2_SN_SC || min_graph->match_method == DRE2_SN_MC )
      min_graph->initial_skip = dre2_initial_skip( min_graph );
  }

  min_graph->has_loop = false;
  for ( i = 0; i < min_graph->count; i++ )
  {
    for ( j = 0; j < min_graph->v[i].n_count; j++ )
    {
      if ( min_graph->v[i].n[j] <= i )
      {
        min_graph->has_loop = true;
        i = min_graph->count;
        break;
      }
    }
  }

  // Cleanup.
  free( new_minimal );
  free( minimal_id );
  free( minimal );
  new_minimal = NULL;
  minimal_id = NULL;
  minimal = NULL;

  return min_graph;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %ebp
movq %rdi, %r13
movl $0x870, %edi            # imm = 0x870
callq 0x22a0
movq %rax, %r15
movl $0x870, %edi            # imm = 0x870
callq 0x22a0
movq %rax, %rbx
xorl %r14d, %r14d
testb $0xa, %bpl
setne %r14b
movl %ebp, 0x28(%rsp)
orl %ebp, %r14d
movl %r14d, 0x43c(%r15)
xorl %r12d, %r12d
movq %r12, 0x430(%r15)
movq %r12, 0x418(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x440(%r15)
movups %xmm0, 0x450(%r15)
leaq 0x20(%rsp), %rax
movq %r12, (%rax)
movl $0x4, %edi
callq 0x22a0
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x14(%rsp), %rax
movl %r12d, (%rax)
movq %r13, %rdi
callq 0x2120
leaq 0x4c(%rsp), %rbp
movl %r12d, (%rbp)
movq %rbp, (%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movq %r13, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
leaq 0x18(%rsp), %r9
callq 0x22b0
movl (%rbp), %eax
movl %eax, 0x460(%r15)
cmpl $-0x1, %edx
je 0x5a23
movq 0x20(%rsp), %rax
movq %rax, (%r15)
movl 0x14(%rsp), %eax
movl %eax, 0x8(%r15)
movq %r15, %rdi
callq 0x2240
movl %r14d, 0x43c(%rbx)
movq $0x0, 0x430(%rbx)
movl $0x100, %edi            # imm = 0x100
movl $0x4, %esi
callq 0x2200
movq %rax, 0x418(%rbx)
movslq 0x8(%r15), %r13
movl $0x4, %esi
movq %r13, %rdi
callq 0x2200
leaq 0x58(%rsp), %rbp
movq %rax, 0x40(%rsp)
movq %rax, (%rbp)
leaq (,%r13,4), %rdi
callq 0x22a0
leaq 0x50(%rsp), %r14
movq %rax, 0x30(%rsp)
movq %rax, (%r14)
leaq 0x2c(%rsp), %r12
movl $0x0, (%r12)
imulq $0x870, %r13, %rdi      # imm = 0x870
callq 0x22a0
leaq 0x60(%rsp), %r13
movq %rax, (%r13)
movq 0x18(%rsp), %rcx
movq %r14, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rcx, 0x38(%rsp)
movq %rbp, %r8
movq %r12, %r9
callq 0x2330
movq (%r13), %rax
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x2240
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2170
movq %r15, %rax
movl 0x28(%rsp), %ebp
testb $0x8, %bpl
movq %r15, %r14
jne 0x59ac
movq %r14, %rdi
callq 0x22e0
xorl %eax, %eax
movq %rax, 0x458(%rbx)
movl 0x2c(%rsp), %ecx
movq %rbx, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
movq 0x30(%rsp), %rdx
callq 0x21f0
movl %eax, 0x410(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2110
movl 0x420(%rbx), %eax
testl %eax, %eax
je 0x5a08
movq 0x418(%rbx), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rdx,4)
je 0x59fc
movb %dl, 0x428(%rbx)
incq %rdx
cmpq $0x100, %rdx            # imm = 0x100
jne 0x59f0
movl 0x410(%rbx), %ecx
testl %ecx, %ecx
je 0x5a6e
cmpl $-0x1, %ecx
jne 0x5a55
movl $0x4, 0x424(%rbx)
jmp 0x5a94
movl 0x14(%rsp), %esi
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x2190
movq 0x18(%rsp), %rdi
callq 0x2070
movq (%rbx), %rdi
callq 0x2070
movq %r15, %rdi
callq 0x2070
xorl %ebx, %ebx
jmp 0x5bf5
movl 0x8(%rbx), %edx
decl %edx
cmpl %edx, %ecx
jne 0x5a6e
testl %eax, %eax
je 0x5a8a
movl $0x2, 0x424(%rbx)
jmp 0x5a94
testl %eax, %eax
je 0x5a7e
movl $0x0, 0x424(%rbx)
jmp 0x5a94
movl $0x1, 0x424(%rbx)
jmp 0x5a94
movl $0x3, 0x424(%rbx)
testb $0x4, %bpl
je 0x5b08
movl 0x8(%rbx), %edx
testl %edx, %edx
jle 0x5b08
movq (%rbx), %rax
xorl %ecx, %ecx
movq %rcx, %rsi
shlq $0x6, %rsi
cmpl $-0x3, (%rax,%rsi)
jne 0x5afd
movq 0x28(%rax,%rsi), %rdx
movq $-0x1a, %rsi
cmpl $0x0, 0x1ec(%rdx,%rsi,4)
je 0x5ad4
movl $0x1, 0x16c(%rdx,%rsi,4)
incq %rsi
jne 0x5abf
movq $-0x1a, %rsi
cmpl $0x0, 0x16c(%rdx,%rsi,4)
je 0x5af5
movl $0x1, 0x1ec(%rdx,%rsi,4)
incq %rsi
jne 0x5ae0
movl 0x8(%rbx), %edx
incq %rcx
movslq %edx, %rsi
cmpq %rsi, %rcx
jl 0x5aa6
testb $0x10, %bpl
jne 0x5b3f
movslq 0x8(%rbx), %r12
leaq (,%r12,4), %r13
movq %r13, %rdi
callq 0x22a0
movq %rax, %rbp
movq %r13, %rdi
callq 0x22a0
movq %rax, %r13
movl $0x4, %esi
movq %r12, %rdi
callq 0x2200
jmp 0x5b46
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %eax, %eax
movq %rbp, 0x440(%rbx)
movq %r13, 0x448(%rbx)
movq %rax, 0x450(%rbx)
movl 0x410(%rbx), %eax
testl %eax, %eax
jle 0x5b86
movl 0x8(%r14), %ecx
decl %ecx
cmpl %ecx, %eax
jge 0x5b86
cmpl $0x1, 0x424(%rbx)
ja 0x5b86
movq %rbx, %rdi
callq 0x20f0
movl %eax, 0x864(%rbx)
movl $0x0, 0x868(%rbx)
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x5bd9
movq (%rbx), %rcx
xorl %edx, %edx
movslq %edx, %rdi
shlq $0x6, %rdi
movslq 0x10(%rcx,%rdi), %rsi
testq %rsi, %rsi
jle 0x5bd3
addq %rcx, %rdi
movq 0x8(%rdi), %rdi
xorl %r8d, %r8d
cmpl %edx, (%rdi,%r8,4)
jle 0x5bc7
incq %r8
cmpq %r8, %rsi
jne 0x5bb7
jmp 0x5bd3
movl $0x1, 0x868(%rbx)
movl %eax, %edx
incl %edx
cmpl %eax, %edx
jl 0x5b9c
movq %r15, %rdi
callq 0x2070
movq 0x30(%rsp), %rdi
callq 0x2070
movq 0x38(%rsp), %rdi
callq 0x2070
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq