// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movl %ecx, %ebp
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r12
movq 0x90(%rsp), %r14
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r9, 0x10(%rsp)
movq %r9, %rcx
xorl %r8d, %r8d
callq 0x20b0
movl (%r14), %edx
leal 0x1(%rdx), %eax
movl %eax, (%r14)
movq (%r12), %rax
movslq (%r15), %rcx
shlq $0x6, %rcx
movq %rdx, 0x50(%rsp)
movl %edx, -0x4(%rax,%rcx)
movl (%r15), %eax
decl %eax
movl %eax, 0x20(%rsp)
movl %eax, 0xc(%rsp)
movl $0x4, %edi
callq 0x22a0
movq %rax, 0x18(%rsp)
movl %ebp, 0x24(%rsp)
cmpl %ebp, %ebx
jge 0x56b4
movq $0x0, 0x38(%rsp)
xorl %ebp, %ebp
movslq %ebx, %r14
movzbl (%r13,%r14), %edx
cmpl $0x5a, %edx
jle 0x51e2
cmpl $0x5b, %edx
je 0x5241
cmpl $0x7c, %edx
jne 0x52b1
movq 0x38(%rsp), %rax
movslq %eax, %r14
incl %eax
movq %rax, 0x38(%rsp)
leaq 0x4(,%r14,4), %rsi
movq 0x18(%rsp), %rdi
callq 0x22d0
movl 0xc(%rsp), %ecx
movq %rax, 0x18(%rsp)
movl %ecx, (%rax,%r14,4)
movl 0x20(%rsp), %eax
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
jmp 0x5297
cmpl $0x28, %edx
jne 0x52a8
incl %ebx
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl 0x24(%rsp), %ecx
movl %ebx, %r8d
movq 0x10(%rsp), %r9
callq 0x22b0
movq %rax, 0x28(%rsp)
movl %edx, 0x30(%rsp)
cmpl $-0x1, %edx
je 0x56c4
movl %edx, %ebx
movl 0xc(%rsp), %esi
movl 0x28(%rsp), %edx
movq %r12, %rdi
callq 0x21d0
movl 0x2c(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x5292
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0xc(%rsp), %esi
movl (%r15), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movslq (%r15), %rdi
decq %rdi
movl %edi, 0xc(%rsp)
shlq $0x6, %rdi
addq (%r12), %rdi
incl %ebx
movq %r13, %rsi
movl %ebx, %edx
callq 0x2040
testl %eax, %eax
je 0x56c4
movl %eax, %ebx
movl $0x1, %ebp
incl %ebx
cmpl 0x24(%rsp), %ebx
jl 0x5185
jmp 0x56bd
cmpl $0x29, %edx
je 0x5765
movl %edx, %eax
andb $-0x21, %al
cmpb $0x5d, %al
je 0x56c4
cmpl $0x3e, %edx
jg 0x5325
cmpl $0x2a, %edx
jg 0x539a
cmpl $0x24, %edx
je 0x5421
cmpl $0x2a, %edx
jne 0x54ee
testl %ebp, %ebp
je 0x57ed
movq (%r12), %rax
movslq 0xc(%rsp), %rcx
movq %rcx, %r14
shlq $0x6, %r14
movl (%rax,%r14), %edx
cmpl $-0x2, %edx
jne 0x55d5
movl 0x28(%rsp), %ebp
movl 0x2c(%rsp), %r14d
movq %r12, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x21d0
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
jmp 0x5489
cmpl $0x5d, %edx
jg 0x53bc
cmpl $0x3f, %edx
je 0x5453
cmpl $0x5c, %edx
jne 0x54ee
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0xc(%rsp), %esi
movl (%r15), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movslq (%r15), %rdi
decq %rdi
movl %edi, 0xc(%rsp)
shlq $0x6, %rdi
addq (%r12), %rdi
incl %ebx
leaq (%r14,%r13), %rsi
incq %rsi
movl $0x1, %ebp
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x2270
jmp 0x5297
cmpl $0x2b, %edx
je 0x54bc
cmpl $0x2e, %edx
jne 0x54ee
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffff0, %edx       # imm = 0xFFFFFFF0
jmp 0x54f4
movl $0xffffffee, %eax       # imm = 0xFFFFFFEE
cmpl $0x5e, %edx
je 0x5426
cmpl $0x7b, %edx
jne 0x54ee
testl %ebp, %ebp
je 0x57ed
movq %r13, %rdi
movl 0x24(%rsp), %esi
movl %ebx, %edx
callq 0x2090
movq %rdx, %rbx
testl %ebx, %ebx
je 0x56c4
movq %rax, %r9
shrq $0x20, %r9
movq 0x10(%rsp), %rcx
movq %rcx, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movl %eax, %r8d
callq 0x2050
shrq $0x20, %rbx
jmp 0x51db
movl $0xffffffef, %eax       # imm = 0xFFFFFFEF
xorl %ebp, %ebp
movq %r12, %rdi
movq %r15, %rsi
movl %eax, %edx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0xc(%rsp), %esi
movl (%r15), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
jmp 0x56a6
testl %ebp, %ebp
je 0x57ed
movq (%r12), %rax
movslq 0xc(%rsp), %rcx
movq %rcx, %r14
shlq $0x6, %r14
movl (%rax,%r14), %edx
cmpl $-0x2, %edx
jne 0x551c
movl 0x28(%rsp), %ebp
movl 0x2c(%rsp), %r14d
movq %r12, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x21d0
subl %ebp, %r14d
jl 0x51db
movslq %ebp, %rdi
shlq $0x2, %rdi
movq 0x10(%rsp), %rax
addq (%rax), %rdi
leaq 0x4(,%r14,4), %rdx
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x2180
jmp 0x5297
testl %ebp, %ebp
je 0x57ed
movq (%r12), %rax
movslq 0xc(%rsp), %rdx
movq %rdx, %rcx
shlq $0x6, %rcx
cmpl $-0x2, (%rax,%rcx)
jne 0x55c6
movl 0x28(%rsp), %edx
movl 0x2c(%rsp), %esi
movq %r12, %rdi
jmp 0x55cb
movq %r12, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl 0xc(%rsp), %esi
movl (%r15), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movl (%r15), %eax
decl %eax
jmp 0x523b
movq %rcx, 0x40(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq (%r12), %rax
cmpl $-0x3, (%rax,%r14)
jne 0x5568
movq 0x28(%rax,%r14), %rcx
leaq -0x18(%rax), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movslq (%r15), %r8
shlq $0x6, %r8
movq (%rdx,%r8), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
cmpq $0x100, %rsi            # imm = 0x100
jne 0x554a
addq %r14, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq 0x40(%rsp), %r14
leaq 0x1(%r14), %rdx
movq %rdx, 0x48(%rsp)
movq %r12, %rdi
movl %r14d, %esi
callq 0x21d0
movl (%r15), %edx
decl %edx
movq %r12, %rdi
movl %r14d, %esi
callq 0x21d0
movl (%r15), %edx
decl %edx
movq %r12, %rdi
movq 0x48(%rsp), %rsi
jmp 0x5689
movq %r12, %rdi
movl %edx, %esi
callq 0x21d0
jmp 0x51db
movq %rcx, 0x48(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq (%r12), %rax
cmpl $-0x3, (%rax,%r14)
jne 0x5621
movq 0x28(%rax,%r14), %rcx
leaq -0x18(%rax), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movslq (%r15), %r8
shlq $0x6, %r8
movq (%rdx,%r8), %r8
movl %edi, (%r8,%rsi,4)
incq %rsi
cmpq $0x100, %rsi            # imm = 0x100
jne 0x5603
addq %r14, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq 0x48(%rsp), %r14
leaq 0x1(%r14), %rdx
movq %rdx, 0x40(%rsp)
movq %r12, %rdi
movl %r14d, %esi
callq 0x21d0
movl (%r15), %edx
decl %edx
movq %r12, %rdi
movl %r14d, %esi
callq 0x21d0
movq %r12, %rdi
movq 0x40(%rsp), %rdx
movl %edx, %esi
callq 0x21d0
movl (%r15), %edx
decl %edx
movq %r12, %rdi
movq 0x40(%rsp), %rsi
callq 0x21d0
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl %ebp, (%rax,%r14,4)
movl %ebp, 0x4(%rax,%r14,4)
movslq (%r15), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
movl (%r15), %eax
decl %eax
movl %eax, 0xc(%rsp)
jmp 0x5297
movq $0x0, 0x38(%rsp)
cmpl $0x0, 0x20(%rsp)
je 0x56d8
movq 0x18(%rsp), %rdi
callq 0x2070
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
jmp 0x574d
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movq 0x38(%rsp), %rax
testl %eax, %eax
movq 0x18(%rsp), %r13
jle 0x571d
movl %eax, %ebx
xorl %r14d, %r14d
movl (%r13,%r14,4), %esi
movl (%r15), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
incq %r14
cmpq %r14, %rbx
jne 0x5703
movl 0xc(%rsp), %esi
movl (%r15), %edx
decl %edx
movq %r12, %rdi
callq 0x21d0
movq (%r12), %rax
movslq (%r15), %rcx
shlq $0x6, %rcx
movq 0x50(%rsp), %rdx
movl %edx, -0x4(%rax,%rcx)
movq %r13, %rdi
callq 0x2070
xorl %r13d, %r13d
movl 0x20(%rsp), %eax
orq %r13, %rax
movl %ebx, %edx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x20(%rsp)
je 0x56c4
movq %r12, %rdi
movq %r15, %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x20b0
movl (%r15), %r13d
decl %r13d
movq 0x38(%rsp), %rax
testl %eax, %eax
jle 0x57b7
movl %eax, %r14d
xorl %ebp, %ebp
movq 0x18(%rsp), %rax
movl (%rax,%rbp,4), %esi
movq %r12, %rdi
movl %r13d, %edx
callq 0x21d0
incq %rbp
cmpq %rbp, %r14
jne 0x579c
movl 0xc(%rsp), %esi
movq %r12, %rdi
movl %r13d, %edx
callq 0x21d0
movq 0x18(%rsp), %rdi
callq 0x2070
movq (%r12), %rax
movslq (%r15), %rcx
shlq $0x6, %rcx
movq 0x50(%rsp), %rdx
movl %edx, -0x4(%rax,%rcx)
shlq $0x20, %r13
jmp 0x574d
leaq 0x5a34(%rip), %rdi      # 0xb228
callq 0x20d0
jmp 0x56c4