// File: /workspace/github_c_makefile_star2_O3/jondurbin[P]dre2/src/dre2_parse.c
// Function: dre2_parse_recursive
struct dre2_parse_return dre2_parse_recursive( struct dre2_node **v, int *node_count, unsigned char *re, int length, int pos, int **minimal, int *group_count )
{
  int i, j, k;
  int last_node, current_group;
  int *option_end, option_count;
  int mod;
  struct dre2_parse_return ret_val, res;
  struct dre2_range_return range;
  unsigned char c;

  // Add the group open node.
  dre2_add_node( v, node_count, DRE2_GROUP_OPEN, minimal, false );

  *group_count = *group_count + 1;
  current_group = *group_count - 1;
  ( *v )[*node_count - 1].group_id = current_group;

  ret_val.open = *node_count - 1;

  // Which node was the preceeding node.
  last_node = ret_val.open;

  // End of option nodes.
  option_count = 0;
  option_end = ( int * )malloc( sizeof( int ) );

  // Loop through regex string.
  mod = false;
  while ( pos < length )
  {
    c = re[pos];
    if ( c == '(' )
    {
      res = dre2_parse_recursive( v, node_count, re, length, pos + 1, minimal, group_count );
      if ( res.pos == -1 )
      {
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }

      // Add a link from the previous node to the group open node.
      dre2_add_neighbor( v, last_node, res.open );

      // Set the regex input pos.
      pos = res.pos;

      // Update the last node.
      last_node = res.close;

      mod = true;
    } else if ( c == ')' )
    {
      if ( ret_val.open == 0 )
      {
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }

      // Add the group close node.
      dre2_add_node( v, node_count, DRE2_GROUP_CLOSE, minimal, false );
      ret_val.close = *node_count - 1;
      ret_val.pos = pos;
      for ( i = 0; i < option_count; i++ )
        dre2_add_neighbor( v, option_end[i], ret_val.close );
      dre2_add_neighbor( v, last_node, ret_val.close );
      free( option_end );
      option_end = NULL;
      ( *v )[*node_count - 1].group_id = current_group;
      return ret_val;
    } else if ( c == '|' )
    {
      // Store the last node so we can setup end of group links.
      option_count++;
      option_end = ( int * )realloc( option_end, sizeof( int ) * option_count );
      option_end[option_count - 1] = last_node;

      // Reset the last node to the group open.
      last_node = ret_val.open;
      mod = false;
    } else if ( c == '[' )
    {
      // Character class.
      dre2_add_node( v, node_count, DRE2_CHAR_CLASS, minimal, false );

      // Add it to the previous node's neighbor list.
      dre2_add_neighbor( v, last_node, *node_count - 1 );

      last_node = *node_count - 1;
      pos = dre2_character_class( &( *v )[last_node], re, pos + 1 );
      if ( pos == false )
      {
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }
      mod = true;
    } else if ( c == ']' || c == '}' )
    {
      free( option_end );
      option_end = NULL;
      ret_val.pos = -1;
      return ret_val;
    } else if ( c == '\\' )
    {
      // Character class.
      dre2_add_node( v, node_count, DRE2_CHAR_CLASS, minimal, false );

      // Add it to the previous node's neighbor list.
      dre2_add_neighbor( v, last_node, *node_count - 1 );

      last_node = *node_count - 1;
      dre2_predefined_class( &( *v )[last_node], &re[pos + 1], true, false );
      pos++;
      mod = true;
    } else if ( c == '.' )
    {
      // Character class, all chars welcome.
      dre2_add_node( v, node_count, DRE2_DOT, minimal, false );

      // Add it to the previous node's neighbor list.
      dre2_add_neighbor( v, last_node, *node_count - 1 );

      last_node = *node_count - 1;
      mod = true;
    } else if ( c == '*' )
    {
      if ( mod == false )
      {
        printf( "Invalid modifier.\n" );
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }
      mod = false;
      if ( ( *v )[last_node].c == DRE2_GROUP_CLOSE )
      {
        // Add link from group open to close and vice versa.
        dre2_add_neighbor( v, res.open, res.close );
        dre2_add_neighbor( v, res.close, res.open );
        for ( i = res.open; i <= res.close; i++ )
          dre2_remove_minimal( minimal, i );
      } else
      {
        dre2_add_node( v, node_count, ( *v )[last_node].c, minimal, false );
        if ( ( *v )[last_node].c == DRE2_CHAR_CLASS )
        {
          for ( i = 0; i < RANGE; i++ )
            ( *v )[*node_count - 1].possible[i] = ( *v )[last_node].possible[i];
        }

        ( *v )[last_node].c = DRE2_GROUP_OPEN;

        dre2_add_node( v, node_count, DRE2_GROUP_CLOSE, minimal, false );

        // Add a link from group open to node.
        dre2_add_neighbor( v, last_node, last_node + 1 );

        // Add a link from group open to group close.
        dre2_add_neighbor( v, last_node, *node_count - 1 );

        // Add a link from the node to itself.
        dre2_add_neighbor( v, last_node + 1, last_node + 1 );

        // Add a link from the node to the group close.
        dre2_add_neighbor( v, last_node + 1, *node_count - 1 );

        // Update the minimal bits.
        dre2_remove_minimal( minimal, last_node );
        dre2_remove_minimal( minimal, last_node + 1 );
        dre2_remove_minimal( minimal, *node_count - 1 );

        last_node = *node_count - 1;
      }
    } else if ( c == '?' )
    {
      if ( mod == false )
      {
        printf( "Invalid modifier.\n" );
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }
      mod = false;
      // If it's a group, add a link from group open to group close.
      if ( ( *v )[last_node].c == DRE2_GROUP_CLOSE )
      {
        dre2_add_neighbor( v, res.open, res.close );
        for ( i = res.open; i <= res.close; i++ )
          dre2_remove_minimal( minimal, i );
      } else
      {
        dre2_add_node( v, node_count, ( *v )[last_node].c, minimal, false );
        if ( ( *v )[last_node].c == DRE2_CHAR_CLASS )
        {
          for ( i = 0; i < RANGE; i++ )
            ( *v )[*node_count - 1].possible[i] = ( *v )[last_node].possible[i];
        }

        ( *v )[last_node].c = DRE2_GROUP_OPEN;
        dre2_add_node( v, node_count, DRE2_GROUP_CLOSE, minimal, false );

        // Add a link from group open to node.
        dre2_add_neighbor( v, last_node, last_node + 1 );

        // Add a link from group open to group close.
        dre2_add_neighbor( v, last_node, *node_count - 1 );

        // Add a link from the node to the group close.
        dre2_add_neighbor( v, last_node + 1, *node_count - 1 );

        // Update the minimal bits.
        dre2_remove_minimal( minimal, last_node );
        dre2_remove_minimal( minimal, last_node + 1 );
        dre2_remove_minimal( minimal, *node_count - 1 );

        last_node = *node_count - 1;
      }
    } else if ( c == '+' )
    {
      if ( mod == false )
      {
        printf( "Invalid modifier.\n" );
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }
      mod = false;

      if ( ( *v )[last_node].c == DRE2_GROUP_CLOSE )
        dre2_add_neighbor( v, res.close, res.open );
      else
        dre2_add_neighbor( v, last_node, last_node );
    } else if ( c == '{' )
    {
      if ( mod == false )
      {
        printf( "Invalid modifier.\n" );
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }
      mod = false;

      // Get the size range.
      range = dre2_range( re, length, pos );

      // Make sure it was a valid range.
      if ( !range.valid )
      {
        free( option_end );
        option_end = NULL;
        ret_val.pos = -1;
        return ret_val;
      }

      // Setup the range.
      dre2_make_range( v, node_count, &last_node, &res, range.min, range.max, minimal );
      pos = range.pos;
    } else if ( c == '^' || c == '$' )
    {
      // Add the node.
      if ( c == '^' )
        dre2_add_node( v, node_count, DRE2_BOL, minimal, false );
      else
        dre2_add_node( v, node_count, DRE2_EOL, minimal, false );

      // Add it to the previous node's neighbor list.
      dre2_add_neighbor( v, last_node, *node_count - 1 );

      // Update last node.
      last_node = *node_count - 1;
      mod = false;
    } else
    {
      // Add the node.
      dre2_add_node( v, node_count, c, minimal, false );

      // Add it to the previous node's neighbor list.
      dre2_add_neighbor( v, last_node, *node_count - 1 );

      // Update last node.
      last_node = *node_count - 1;
      mod = true;
    }
    pos++;
  }

  if ( ret_val.open != 0 )
  {
    free( option_end );
    option_end = NULL;
    ret_val.pos = -1;
    return ret_val;
  }

  // Add links from the last node in each option group to the group close node.
  dre2_add_node( v, node_count, DRE2_GROUP_CLOSE, minimal, false );
  for ( i = 0; i < option_count; i++ )
    dre2_add_neighbor( v, option_end[i], *node_count - 1 );

  dre2_add_neighbor( v, last_node, *node_count - 1 );

  ( *v )[*node_count - 1].group_id = current_group;

  free( option_end );
  option_end = NULL;

  return ret_val;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r15d
movl %ecx, %ebp
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
movq 0x90(%rsp), %rbx
movq $0x0, 0x48(%rsp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r9, 0x18(%rsp)
movq %r9, %rcx
xorl %r8d, %r8d
callq 0x20b0
movl (%rbx), %edx
leal 0x1(%rdx), %eax
movl %eax, (%rbx)
movq (%r12), %rax
movslq (%r14), %rcx
shlq $0x6, %rcx
movq %rdx, 0x50(%rsp)
movl %edx, -0x4(%rax,%rcx)
movl (%r14), %ebx
decl %ebx
movl %ebx, 0x10(%rsp)
movl $0x4, %edi
callq 0x22a0
movq %rax, 0x20(%rsp)
movl %ebp, 0x2c(%rsp)
cmpl %ebp, %r15d
jge 0x5666
movl %ebx, 0x14(%rsp)
xorl %ebx, %ebx
movslq %r15d, %rbp
movzbl (%r13,%rbp), %edx
cmpl $0x5a, %edx
jle 0x51b7
cmpl $0x5b, %edx
je 0x5219
cmpl $0x7c, %edx
jne 0x528e
movq 0x48(%rsp), %rax
movslq %eax, %rbx
incl %eax
movq %rax, 0x48(%rsp)
leaq 0x4(,%rbx,4), %rsi
movq 0x20(%rsp), %rdi
callq 0x22d0
movl 0x10(%rsp), %ecx
movq %rax, 0x20(%rsp)
movl %ecx, (%rax,%rbx,4)
movl 0x14(%rsp), %eax
movl %eax, 0x10(%rsp)
xorl %ebx, %ebx
jmp 0x5272
cmpl $0x28, %edx
jne 0x5285
incl %r15d
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl 0x2c(%rsp), %ecx
movl %r15d, %r8d
movq 0x18(%rsp), %r9
callq 0x22b0
movq %rax, 0x30(%rsp)
movl %edx, 0x38(%rsp)
cmpl $-0x1, %edx
je 0x57b8
movl %edx, %r15d
movq %rax, %rbx
shrq $0x20, %rbx
movl 0x10(%rsp), %esi
movq %r12, %rdi
movl %eax, %edx
callq 0x21d0
movl %ebx, 0x10(%rsp)
jmp 0x526d
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0x10(%rsp), %esi
movl (%r14), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movslq (%r14), %rdi
decq %rdi
movl %edi, 0x10(%rsp)
shlq $0x6, %rdi
addq (%r12), %rdi
incl %r15d
movq %r13, %rsi
movl %r15d, %edx
callq 0x2040
testl %eax, %eax
je 0x57b8
movl %eax, %r15d
movl $0x1, %ebx
incl %r15d
cmpl 0x2c(%rsp), %r15d
jl 0x515b
jmp 0x5689
cmpl $0x29, %edx
je 0x5727
movl %edx, %eax
andb $-0x21, %al
cmpb $0x5d, %al
je 0x57b8
cmpl $0x3e, %edx
jg 0x52fe
cmpl $0x2a, %edx
jg 0x5378
cmpl $0x24, %edx
je 0x5401
cmpl $0x2a, %edx
jne 0x54ca
testl %ebx, %ebx
je 0x57ac
movq (%r12), %rax
movslq 0x10(%rsp), %rbp
movq %rbp, %rbx
shlq $0x6, %rbx
movl (%rax,%rbx), %edx
cmpl $-0x2, %edx
jne 0x5596
movl 0x30(%rsp), %ebp
movl 0x34(%rsp), %ebx
movq %r12, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x21d0
movq %r12, %rdi
movl %ebx, %esi
movl %ebp, %edx
jmp 0x5466
cmpl $0x5d, %edx
jg 0x539a
cmpl $0x3f, %edx
je 0x5433
cmpl $0x5c, %edx
jne 0x54ca
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0x10(%rsp), %esi
movl (%r14), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movslq (%r14), %rdi
decq %rdi
movl %edi, 0x10(%rsp)
shlq $0x6, %rdi
addq (%r12), %rdi
incl %r15d
leaq 0x1(,%r13), %rsi
addq %rbp, %rsi
movl $0x1, %ebx
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x2270
jmp 0x5272
cmpl $0x2b, %edx
je 0x5498
cmpl $0x2e, %edx
jne 0x54ca
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffff0, %edx       # imm = 0xFFFFFFF0
jmp 0x54d0
movl $0xffffffee, %eax       # imm = 0xFFFFFFEE
cmpl $0x5e, %edx
je 0x5406
cmpl $0x7b, %edx
jne 0x54ca
testl %ebx, %ebx
je 0x57ac
movq %r13, %rdi
movl 0x2c(%rsp), %esi
movl %r15d, %edx
callq 0x2090
movq %rdx, %r15
testl %r15d, %r15d
je 0x57b8
movq %rax, %r9
shrq $0x20, %r9
movq 0x18(%rsp), %rcx
movq %rcx, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl %eax, %r8d
callq 0x2050
shrq $0x20, %r15
jmp 0x51b0
movl $0xffffffef, %eax       # imm = 0xFFFFFFEF
xorl %ebx, %ebx
movq %r12, %rdi
movq %r14, %rsi
movl %eax, %edx
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0x10(%rsp), %esi
movl (%r14), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
jmp 0x5658
testl %ebx, %ebx
je 0x57ac
movq (%r12), %rax
movslq 0x10(%rsp), %rbp
movq %rbp, %rbx
shlq $0x6, %rbx
movl (%rax,%rbx), %edx
cmpl $-0x2, %edx
jne 0x54fc
movl 0x30(%rsp), %ebp
movl 0x34(%rsp), %ebx
movq %r12, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x21d0
subl %ebp, %ebx
jl 0x51b0
movslq %ebp, %rdi
shlq $0x2, %rdi
movq 0x18(%rsp), %rax
addq (%rax), %rdi
leaq 0x4(,%rbx,4), %rdx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x2180
jmp 0x5272
testl %ebx, %ebx
je 0x57ac
movq (%r12), %rax
movslq 0x10(%rsp), %rdx
movq %rdx, %rcx
shlq $0x6, %rcx
cmpl $-0x2, (%rax,%rcx)
jne 0x5587
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %esi
movq %r12, %rdi
jmp 0x558c
movq %r12, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0x10(%rsp), %esi
movl (%r14), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movl (%r14), %eax
decl %eax
movl %eax, 0x10(%rsp)
jmp 0x526d
movq %r12, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq (%r12), %rax
cmpl $-0x3, (%rax,%rbx)
jne 0x5542
movq 0x28(%rax,%rbx), %rcx
leaq -0x18(%rax), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movslq (%r14), %r8
shlq $0x6, %r8
movq (%rdx,%r8), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
cmpq $0x100, %rsi            # imm = 0x100
jne 0x5524
addq %rbx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
leaq 0x1(%rbp), %rdx
movq %rdx, 0x40(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x21d0
movl (%r14), %edx
decl %edx
movq %r12, %rdi
movl %ebp, %esi
jmp 0x562b
movq %r12, %rdi
movl %edx, %esi
callq 0x21d0
jmp 0x51b0
movq %r12, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq (%r12), %rax
cmpl $-0x3, (%rax,%rbx)
jne 0x55dc
movq 0x28(%rax,%rbx), %rcx
leaq -0x18(%rax), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movslq (%r14), %r8
shlq $0x6, %r8
movq (%rdx,%r8), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
cmpq $0x100, %rsi            # imm = 0x100
jne 0x55be
addq %rbx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
leaq 0x1(%rbp), %rdx
movq %rdx, 0x40(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x21d0
movl (%r14), %edx
decl %edx
movq %r12, %rdi
movl %ebp, %esi
callq 0x21d0
movq %r12, %rdi
movq 0x40(%rsp), %rdx
movl %edx, %esi
callq 0x21d0
movl (%r14), %edx
decl %edx
movq %r12, %rdi
movq 0x40(%rsp), %rsi
callq 0x21d0
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl %ebx, (%rax,%rbp,4)
movl %ebx, 0x4(%rax,%rbp,4)
movslq (%r14), %rcx
movl %ebx, -0x4(%rax,%rcx,4)
movl (%r14), %eax
decl %eax
movl %eax, 0x10(%rsp)
jmp 0x5272
testl %ebx, %ebx
jne 0x5691
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq 0x20(%rsp), %r13
jmp 0x56f2
movl 0x14(%rsp), %ebx
testl %ebx, %ebx
je 0x56a9
movq 0x20(%rsp), %rdi
callq 0x2070
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
jmp 0x57cf
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq 0x48(%rsp), %rax
testl %eax, %eax
movq 0x20(%rsp), %r13
jle 0x56f2
movl %eax, %ebx
xorl %r15d, %r15d
movl (%r13,%r15,4), %esi
movl (%r14), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
incq %r15
cmpq %r15, %rbx
jne 0x56d4
movl 0x14(%rsp), %ebx
movl 0x10(%rsp), %esi
movl (%r14), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movq (%r12), %rax
movslq (%r14), %rcx
shlq $0x6, %rcx
movq 0x50(%rsp), %rdx
movl %edx, -0x4(%rax,%rcx)
movq %r13, %rdi
callq 0x2070
xorl %r13d, %r13d
jmp 0x57cf
movl 0x14(%rsp), %ebx
testl %ebx, %ebx
je 0x5691
movq %r12, %rdi
movq %r14, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl (%r14), %r13d
decl %r13d
movq 0x48(%rsp), %rax
testl %eax, %eax
jle 0x5779
movl %eax, %ebx
xorl %ebp, %ebp
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %esi
movq %r12, %rdi
movl %r13d, %edx
callq 0x21d0
incq %rbp
cmpq %rbp, %rbx
jne 0x575e
movl 0x10(%rsp), %esi
movq %r12, %rdi
movl %r13d, %edx
callq 0x21d0
movq 0x20(%rsp), %rdi
callq 0x2070
movq (%r12), %rax
movslq (%r14), %rcx
shlq $0x6, %rcx
movq 0x50(%rsp), %rdx
movl %edx, -0x4(%rax,%rcx)
shlq $0x20, %r13
jmp 0x57cb
leaq 0x5a75(%rip), %rdi      # 0xb228
callq 0x20d0
movq 0x20(%rsp), %rdi
callq 0x2070
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movl 0x14(%rsp), %ebx
movl %ebx, %eax
orq %r13, %rax
movl %r15d, %edx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq