// File: /workspace/github_c_makefile_star2_O3/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, %r15d
movl %edx, 0xc(%rsp)
movq %rsi, %r13
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2120
movq %rax, %rdx
movslq 0x8(%r14), %rbx
leal -0x1(%rbx), %eax
movl $0x1, %ebp
cmpl %r15d, %eax
je 0x7477
movslq 0xc(%rsp), %rsi
cmpb $0x0, (%r13,%rsi)
je 0x71fd
movslq %r15d, %r12
jmp 0x7229
movq (%r14), %rax
movslq %r15d, %r12
movq %r12, %rcx
shlq $0x6, %rcx
movl (%rax,%rcx), %eax
xorl %ebp, %ebp
addl $0x12, %eax
cmpl $0x11, %eax
ja 0x7477
movl $0x34003, %ecx          # imm = 0x34003
btl %eax, %ecx
jae 0x7477
movq 0x28(%rsp), %rax
movq (%rax), %r15
xorl %ebp, %ebp
cmpl $0x0, (%r15,%r12,4)
jne 0x7477
movq %rsi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movl %ebx, %ebp
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x22a0
movq %rax, %rdi
testl %ebp, %ebp
jle 0x726e
movq %rdi, %rbp
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2230
movq %rbp, %rdi
movl $0x1, (%r15,%r12,4)
movq (%r14), %rbx
shlq $0x6, %r12
leaq (%rbx,%r12), %rsi
movl (%rbx,%r12), %edx
leal 0x12(%rdx), %eax
cmpl $0x11, %eax
ja 0x72ac
xorl %ebp, %ebp
movl $0x4003, %ecx           # imm = 0x4003
btl %eax, %ecx
jb 0x72b1
movl $0x30000, %ecx          # imm = 0x30000
btl %eax, %ecx
jae 0x72ac
movl 0xc(%rsp), %ebp
jmp 0x7373
movl $0x1, %ebp
movl %edx, %eax
xorl $-0x12, %eax
orl 0xc(%rsp), %eax
je 0x7350
movq 0x10(%rsp), %rax
movb (%r13,%rax), %al
movzbl %al, %ecx
cmpl $-0x4, %edx
je 0x72e2
cmpl $-0x11, %edx
jne 0x731a
testl %ecx, %ecx
je 0x7350
cmpl $0xa, %ecx
je 0x7350
jmp 0x731a
testl %ecx, %ecx
je 0x7350
cmpl $0x20, %ecx
je 0x7301
cmpl $0xa, %ecx
jne 0x731a
movq 0x10(%rsp), %r8
incq %r8
movslq 0x18(%rsp), %rcx
movb $0xa, %al
jmp 0x7315
movq 0x10(%rsp), %r8
incq %r8
movq 0x18(%rsp), %rax
decl %eax
movslq %eax, %rcx
movb $0x20, %al
cmpq %rcx, %r8
je 0x7350
movq %rdx, 0x30(%rsp)
movzbl %al, %edx
movq %rdi, 0x10(%rsp)
movq %r14, %rdi
movq %rsi, 0x18(%rsp)
movq 0x18(%rsp), %rsi
callq 0x2100
movq 0x30(%rsp), %rdx
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
testl %eax, %eax
je 0x7470
addl 0xc(%rsp), %ebp
cmpl $0x0, 0x8(%r14)
jle 0x7373
xorl %eax, %eax
movl $0x0, (%r15,%rax,4)
incq %rax
movslq 0x8(%r14), %rcx
cmpq %rcx, %rax
jl 0x735d
movl (%rsi), %edx
cmpl $-0x2, %edx
je 0x738d
cmpl $-0x1, %edx
jne 0x73b0
movslq 0x3c(%rsi), %rax
testq %rax, %rax
jle 0x73b0
movq 0x20(%rsp), %rcx
jmp 0x739b
movslq 0x3c(%rsi), %rax
testq %rax, %rax
jle 0x73b0
movq 0x70(%rsp), %rcx
movq (%rcx), %rcx
leaq (%rcx,%rax,4), %rax
addq $-0x4, %rax
movl (%rax), %ecx
movl %ecx, 0xc(%rsp)
movl %ebp, (%rax)
jmp 0x73b0
cmpl $0x0, 0x10(%rsi)
jle 0x7406
movq %rdi, 0x10(%rsp)
xorl %r15d, %r15d
movq 0x8(%rbx,%r12), %rax
movl (%rax,%r15,4), %ecx
movq 0x70(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x2080
testl %eax, %eax
jne 0x7449
incq %r15
movq (%r14), %rbx
movslq 0x10(%rbx,%r12), %rax
cmpq %rax, %r15
jl 0x73be
addq %r12, %rbx
movq %rbx, %rsi
movq 0x10(%rsp), %rdi
cmpl $0x0, 0x8(%r14)
jle 0x7429
movq 0x28(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movl (%rdi,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
movslq 0x8(%r14), %rdx
cmpq %rdx, %rcx
jl 0x7417
movl (%rsi), %eax
xorl %ebp, %ebp
cmpl $-0x2, %eax
je 0x7455
cmpl $-0x1, %eax
movl 0xc(%rsp), %edx
jne 0x7472
movl 0x3c(%rsi), %eax
testl %eax, %eax
movq 0x20(%rsp), %rcx
jg 0x7465
jmp 0x7472
movl $0x1, %ebp
movq 0x10(%rsp), %rdi
jmp 0x7472
movl 0x3c(%rsi), %eax
movq 0x70(%rsp), %rcx
testl %eax, %eax
movl 0xc(%rsp), %edx
jle 0x7472
movq (%rcx), %rcx
movl %eax, %eax
movl %edx, -0x4(%rcx,%rax,4)
jmp 0x7472
xorl %ebp, %ebp
callq 0x2070
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq