// File: /workspace/github_c_makefile_star2_O1/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, %r14
movl $0x870, %edi            # imm = 0x870
callq 0x22a0
movq %rax, %rbx
xorl %r12d, %r12d
testb $0xa, %bpl
setne %r12b
movl %ebp, 0x28(%rsp)
orl %ebp, %r12d
movl %r12d, 0x43c(%r14)
xorl %r15d, %r15d
movq %r15, 0x430(%r14)
movq %r15, 0x418(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x440(%r14)
movups %xmm0, 0x450(%r14)
leaq 0x20(%rsp), %rax
movq %r15, (%rax)
movl $0x4, %edi
callq 0x22a0
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x14(%rsp), %rax
movl %r15d, (%rax)
movq %r13, %rdi
callq 0x2120
leaq 0x5c(%rsp), %rbp
movl %r15d, (%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(%r14)
cmpl $-0x1, %edx
je 0x5a3c
movq 0x20(%rsp), %rax
movq %rax, (%r14)
movl 0x14(%rsp), %eax
movl %eax, 0x8(%r14)
movq %r14, %rdi
callq 0x2240
movl %r12d, 0x43c(%rbx)
movq $0x0, 0x430(%rbx)
movl $0x100, %edi            # imm = 0x100
movl $0x4, %esi
callq 0x2200
movq %rax, 0x418(%rbx)
movslq 0x8(%r14), %r13
movl $0x4, %esi
movq %r13, %rdi
callq 0x2200
leaq 0x50(%rsp), %rbp
movq %rax, 0x40(%rsp)
movq %rax, (%rbp)
leaq (,%r13,4), %rdi
callq 0x22a0
leaq 0x48(%rsp), %r15
movq %rax, 0x38(%rsp)
movq %rax, (%r15)
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 %r15, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rcx, 0x30(%rsp)
movq %rbp, %r8
movq %r12, %r9
callq 0x2340
movq (%r13), %rax
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x2240
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2170
movq %r14, %rax
movl 0x28(%rsp), %r12d
testb $0x8, %r12b
jne 0x59c2
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 0x38(%rsp), %rbp
movq %rbp, %rdx
callq 0x21f0
movl %eax, 0x410(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2110
movl 0x420(%rbx), %eax
testl %eax, %eax
je 0x5a21
movq 0x418(%rbx), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rdx,4)
je 0x5a15
movb %dl, 0x428(%rbx)
incq %rdx
cmpq $0x100, %rdx            # imm = 0x100
jne 0x5a09
movl 0x410(%rbx), %ecx
testl %ecx, %ecx
je 0x5a92
cmpl $-0x1, %ecx
jne 0x5a79
movl $0x4, 0x424(%rbx)
jmp 0x5ab8
movl 0x14(%rsp), %esi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x2190
movq 0x18(%rsp), %rdi
callq 0x2070
movq (%r15), %rdi
callq 0x2070
xorl %ebx, %ebx
movq %rbx, 0x18(%rsp)
movq %rbx, (%r15)
movq %rbx, (%r14)
movq %r14, %rdi
callq 0x2070
jmp 0x5c32
movl 0x8(%rbx), %edx
decl %edx
cmpl %edx, %ecx
jne 0x5a92
testl %eax, %eax
je 0x5aae
movl $0x2, 0x424(%rbx)
jmp 0x5ab8
testl %eax, %eax
je 0x5aa2
movl $0x0, 0x424(%rbx)
jmp 0x5ab8
movl $0x1, 0x424(%rbx)
jmp 0x5ab8
movl $0x3, 0x424(%rbx)
testb $0x4, %r12b
je 0x5b2e
cmpl $0x0, 0x8(%rbx)
jle 0x5b2e
movq (%rbx), %rax
xorl %ecx, %ecx
movq %rcx, %rdx
shlq $0x6, %rdx
cmpl $-0x3, (%rax,%rdx)
jne 0x5b22
movq 0x28(%rax,%rdx), %rsi
movq $-0x1a, %rdi
cmpl $0x0, 0x1ec(%rsi,%rdi,4)
je 0x5af7
movl $0x1, 0x16c(%rsi,%rdi,4)
incq %rdi
jne 0x5ae2
movq 0x28(%rax,%rdx), %rdx
movq $-0x1a, %rsi
cmpl $0x0, 0x16c(%rdx,%rsi,4)
je 0x5b1d
movl $0x1, 0x1ec(%rdx,%rsi,4)
incq %rsi
jne 0x5b08
incq %rcx
movslq 0x8(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x5ac9
testb $0x10, %r12b
jne 0x5b74
movslq 0x8(%rbx), %r12
leaq (,%r12,4), %r13
movq %r13, %rdi
callq 0x22a0
movq %rax, 0x440(%rbx)
movq %r13, %rdi
callq 0x22a0
movq %rax, 0x448(%rbx)
movl $0x4, %esi
movq %r12, %rdi
callq 0x2200
movq %rax, 0x450(%rbx)
jmp 0x5b89
xorps %xmm0, %xmm0
movups %xmm0, 0x440(%rbx)
movq $0x0, 0x450(%rbx)
movl 0x410(%rbx), %eax
testl %eax, %eax
jle 0x5bb4
movl 0x8(%r14), %ecx
decl %ecx
cmpl %ecx, %eax
jge 0x5bb4
cmpl $0x1, 0x424(%rbx)
ja 0x5bb4
movq %rbx, %rdi
callq 0x20f0
movl %eax, 0x864(%rbx)
movl $0x0, 0x868(%rbx)
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x5c07
movq (%rbx), %rcx
xorl %edx, %edx
movslq %edx, %rdi
shlq $0x6, %rdi
movslq 0x10(%rcx,%rdi), %rsi
testq %rsi, %rsi
jle 0x5c01
addq %rcx, %rdi
movq 0x8(%rdi), %rdi
xorl %r8d, %r8d
cmpl %edx, (%rdi,%r8,4)
jle 0x5bf5
incq %r8
cmpq %r8, %rsi
jne 0x5be5
jmp 0x5c01
movl $0x1, 0x868(%rbx)
movl %eax, %edx
incl %edx
cmpl %eax, %edx
jl 0x5bca
movq %r15, %rdi
callq 0x2070
movq %rbp, %rdi
callq 0x2070
movq 0x30(%rsp), %rdi
callq 0x2070
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq %rax, 0x18(%rsp)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq