// File: /workspace/github_c_makefile_star2_O1/jondurbin[P]dre2/src/dre2_backtracking.c
// Function: dre2_backtrack_recursive
int dre2_backtrack_recursive( struct dre2 *graph, unsigned char *input, int pos, int id, int **state, int **group_open, int **group_close )
{
  int i, j;
  int offset;
  int g_open, g_close;
  unsigned char *tp;
  int *last_state;
  int assertion, single_match;
  struct dre2_node *node;
  int length = strlen( input );

  tp = input + pos;

  // If we are at the regex close node, then we've matched as much as possible, return.
  if ( id == graph->count - 1 )
    return true;

  // If we are past the end of input, we can't match, return false.
  if ( *tp == '\0' && ( graph->v[id].c != DRE2_GROUP_OPEN && graph->v[id].c != DRE2_GROUP_CLOSE ) &&
         ( graph->v[id].c != DRE2_EOF && graph->v[id].c != DRE2_EOL && graph->v[id].c != DRE2_BOL ) )
    return false;

  // Make sure we haven't already tried this node.
  if ( ( *state )[id] )
    return false;

  last_state = ( int * )malloc( sizeof( int ) * graph->count );
  for ( i = 0; i < graph->count; i++ )
    last_state[i] = ( *state )[i];

  ( *state )[id] = true;

  // Match the input character if it's not a group.
  if ( graph->v[id].c != DRE2_GROUP_OPEN && graph->v[id].c != DRE2_GROUP_CLOSE )
  {
    single_match = false;
    node = &graph->v[id];
    switch ( node->c )
    {
      case DRE2_BOL:
      case DRE2_EOF:
      case DRE2_EOL:
        assertion = true;
        break;
      default:
        assertion = false;
        break;
    }

    if ( node->c == DRE2_BOL && pos == 0 )
    {
      single_match = true;
    } else if ( node->c == DRE2_EOL && ( *tp == '\n' || *tp == '\0' ) )
    {
      single_match = true;
    } else if ( node->c == DRE2_EOF && *tp == '\0' )
    {
      single_match = true;
    } else if ( node->c == DRE2_EOF && *tp == '\n' && tp - input + 1 == length )
    {
      single_match = true;
    } else if ( node->c == DRE2_EOF && *tp == ' ' && tp - input + 1 == length - 1 )
    {
      single_match = true;
    } else if ( dre2_char_matches( graph, node, *tp ) )
    {
      single_match = true;
    }

    if ( single_match )
    {
      if ( !assertion )
      {
        offset = 1;
        pos++;
      }
      for ( i = 0; i < graph->count; i++ )
        ( *state )[i] = false;
    } else
    {
      free( last_state );
      last_state = NULL;
      return false;
    }
  } else
  {
    offset = 0;
  }

  // Set the group open and close positions, but also track where it was so we can revert later if needed.
  if ( graph->v[id].c == DRE2_GROUP_OPEN && graph->v[id].group_id > 0 )
  {
    g_open = ( *group_open )[graph->v[id].group_id - 1];
    ( *group_open )[graph->v[id].group_id - 1] = pos;
  } else if ( graph->v[id].c == DRE2_GROUP_CLOSE && graph->v[id].group_id > 0 )
  {
    g_close = ( *group_close )[graph->v[id].group_id - 1];
    ( *group_close )[graph->v[id].group_id - 1] = pos;
  }

  // Check each of this nodes neighbors.
  for ( i = 0; i < graph->v[id].n_count; i++ )
  {
    // If the recursive function returns true, the regex matched.
    if ( dre2_backtrack_recursive( graph, input, pos, graph->v[id].n[i], state, group_open, group_close ) )
    {
      free( last_state );
      last_state = NULL;
      return true;
    }
  }

  // Revert state table on failure as well.
  for ( i = 0; i < graph->count; i++ )
    ( *state )[i] = last_state[i]; //false;

  // Revert group open/close on failure.
  if ( graph->v[id].c == DRE2_GROUP_OPEN && graph->v[id].group_id > 0 )
  {
    ( *group_open )[graph->v[id].group_id - 1] = g_open;
  } else if ( graph->v[id].c == DRE2_GROUP_CLOSE && graph->v[id].group_id > 0 )
  {
    ( *group_close )[graph->v[id].group_id - 1] = g_close;
  }
  free( last_state );
  last_state = NULL;
  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movl %ecx, %ebx
movl %edx, 0xc(%rsp)
movq %rsi, %r13
movq %rdi, %r15
movq %rsi, %rdi
callq 0x2120
movq %rax, %rdx
movslq 0x8(%r15), %r12
leal -0x1(%r12), %eax
movl $0x1, %r14d
cmpl %ebx, %eax
je 0x742b
movslq 0xc(%rsp), %rsi
cmpb $0x0, (%r13,%rsi)
movslq %ebx, %rbp
jne 0x71e8
movq (%r15), %rax
movq %rbp, %rcx
shlq $0x6, %rcx
movl (%rax,%rcx), %eax
xorl %r14d, %r14d
addl $0x12, %eax
cmpl $0x11, %eax
ja 0x742b
movl $0x34003, %ecx          # imm = 0x34003
btl %eax, %ecx
jae 0x742b
movq 0x28(%rsp), %rax
movq (%rax), %rbx
xorl %r14d, %r14d
cmpl $0x0, (%rbx,%rbp,4)
jne 0x742b
movq %rsi, 0x18(%rsp)
movq %rdx, 0x30(%rsp)
movl %r12d, 0x10(%rsp)
shlq $0x2, %r12
movq %r12, %rdi
callq 0x22a0
movq %rax, %r14
cmpl $0x0, 0x10(%rsp)
jle 0x7230
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2230
movq %r14, 0x10(%rsp)
movl $0x1, (%rbx,%rbp,4)
movq (%r15), %r12
shlq $0x6, %rbp
movl (%r12,%rbp), %eax
addq %rbp, %r12
leal 0x12(%rax), %ecx
cmpl $0x11, %ecx
movq 0x18(%rsp), %rsi
ja 0x7272
xorl %r14d, %r14d
movl $0x4003, %edx           # imm = 0x4003
btl %ecx, %edx
jb 0x7278
movl $0x30000, %edx          # imm = 0x30000
btl %ecx, %edx
jb 0x7319
movl $0x1, %r14d
movl %eax, %ecx
xorl $-0x12, %ecx
orl 0xc(%rsp), %ecx
je 0x72f8
cmpl $-0x11, %eax
jne 0x7297
movzbl (%r13,%rsi), %ecx
testl %ecx, %ecx
je 0x72f8
cmpl $0xa, %ecx
je 0x72f8
cmpl $-0x4, %eax
jne 0x72df
cmpb $0x0, (%r13,%rsi)
je 0x72f8
cmpl $-0x4, %eax
jne 0x72df
cmpb $0xa, (%r13,%rsi)
jne 0x72bf
leaq 0x1(%rsi), %rcx
movslq 0x30(%rsp), %rdx
cmpq %rdx, %rcx
je 0x72f8
cmpl $-0x4, %eax
jne 0x72df
cmpb $0x20, (%r13,%rsi)
jne 0x72df
leaq 0x1(%rsi), %rax
movq 0x30(%rsp), %rcx
decl %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x72f8
movzbl (%r13,%rsi), %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2100
testl %eax, %eax
je 0x743d
addl %r14d, 0xc(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x7319
xorl %eax, %eax
movl $0x0, (%rbx,%rax,4)
incq %rax
movslq 0x8(%r15), %rcx
cmpq %rcx, %rax
jl 0x7306
movq 0x70(%rsp), %rbx
movl (%r12), %eax
cmpl $-0x2, %eax
je 0x734d
cmpl $-0x1, %eax
jne 0x7370
movslq 0x3c(%r12), %rcx
testq %rcx, %rcx
jle 0x7348
movq 0x20(%rsp), %rax
movq (%rax), %rax
leaq (%rax,%rcx,4), %rax
addq $-0x4, %rax
jmp 0x7362
cmpl $-0x2, %eax
jne 0x7370
movslq 0x3c(%r12), %rax
testq %rax, %rax
jle 0x7370
movq (%rbx), %rcx
leaq (%rcx,%rax,4), %rax
addq $-0x4, %rax
movl (%rax), %ecx
movl %ecx, 0x18(%rsp)
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x7370
movq (%r15), %rax
cmpl $0x0, 0x10(%rax,%rbp)
jle 0x73b7
xorl %r14d, %r14d
movq 0x8(%rax,%rbp), %rax
movl (%rax,%r14,4), %ecx
movq %rbx, (%rsp)
movq %r15, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %edx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x2080
testl %eax, %eax
jne 0x741b
incq %r14
movq (%r15), %rax
movslq 0x10(%rax,%rbp), %rcx
cmpq %rcx, %r14
jl 0x737d
addq %rbp, %rax
cmpl $0x0, 0x8(%r15)
movq 0x10(%rsp), %rdi
jle 0x73e2
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
movl (%rdi,%rdx,4), %esi
movl %esi, (%rcx,%rdx,4)
incq %rdx
movslq 0x8(%r15), %rsi
cmpq %rsi, %rdx
jl 0x73d0
movl (%rax), %edx
xorl %r14d, %r14d
cmpl $-0x2, %edx
je 0x7402
cmpl $-0x1, %edx
jne 0x7426
movl 0x3c(%rax), %ecx
testl %ecx, %ecx
movq 0x20(%rsp), %rsi
jg 0x740c
cmpl $-0x2, %edx
jne 0x7426
movl 0x3c(%rax), %ecx
movq %rbx, %rsi
testl %ecx, %ecx
jle 0x7426
movq (%rsi), %rax
movl %ecx, %ecx
movl 0x18(%rsp), %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x7426
movl $0x1, %r14d
movq 0x10(%rsp), %rdi
callq 0x2070
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
jmp 0x7421