On 64-bit hosts (where size_t is 64 bits and int is 32 bits), the
[pspp] / lib / regexec.c
index cb6234128b4c73b7a55f8b05fa226420960275a7..230f10d427b3b31fa0fc1e81235e50aa28e784e3 100644 (file)
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 
 static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
-                                    int n) internal_function;
+                                    Idx n) internal_function;
 static void match_ctx_clean (re_match_context_t *mctx) internal_function;
 static void match_ctx_free (re_match_context_t *cache) internal_function;
-static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node,
-                                         int str_idx, int from, int to)
+static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, Idx node,
+                                         Idx str_idx, Idx from, Idx to)
      internal_function;
-static int search_cur_bkref_entry (re_match_context_t *mctx, int str_idx)
+static Idx search_cur_bkref_entry (re_match_context_t *mctx, Idx str_idx)
      internal_function;
-static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
-                                          int str_idx) internal_function;
+static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, Idx node,
+                                          Idx str_idx) internal_function;
 static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
-                                                  int node, int str_idx)
+                                                   Idx node, Idx str_idx)
      internal_function;
 static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
-                          re_dfastate_t **limited_sts, int last_node,
-                          int last_str_idx)
+                          re_dfastate_t **limited_sts, Idx last_node,
+                          Idx last_str_idx)
      internal_function;
 static reg_errcode_t re_search_internal (const regex_t *preg,
-                                        const char *string, int length,
-                                        int start, int range, int stop,
+                                        const char *string, Idx length,
+                                        Idx start, regoff_t range, Idx stop,
                                         size_t nmatch, regmatch_t pmatch[],
                                         int eflags) internal_function;
-static int re_search_2_stub (struct re_pattern_buffer *bufp,
-                            const char *string1, int length1,
-                            const char *string2, int length2,
-                            int start, int range, struct re_registers *regs,
-                            int stop, int ret_len) internal_function;
-static int re_search_stub (struct re_pattern_buffer *bufp,
-                          const char *string, int length, int start,
-                          int range, int stop, struct re_registers *regs,
-                          int ret_len) internal_function;
+static regoff_t re_search_2_stub (struct re_pattern_buffer *bufp,
+                                 const char *string1, Idx length1,
+                                 const char *string2, Idx length2,
+                                 Idx start, regoff_t range,
+                                 struct re_registers *regs,
+                                 Idx stop, int ret_len) internal_function;
+static regoff_t re_search_stub (struct re_pattern_buffer *bufp,
+                               const char *string, Idx length, Idx start,
+                               regoff_t range, Idx stop,
+                               struct re_registers *regs,
+                               int ret_len) internal_function;
 static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
-                             int nregs, int regs_allocated) internal_function;
-static inline re_dfastate_t *acquire_init_state_context
-     (reg_errcode_t *err, const re_match_context_t *mctx, int idx)
-     __attribute ((always_inline)) internal_function;
+                             Idx nregs, int regs_allocated) internal_function;
 static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
      internal_function;
-static int check_matching (re_match_context_t *mctx, int fl_longest_match,
-                          int *p_match_first)
+static Idx check_matching (re_match_context_t *mctx, int fl_longest_match,
+                          Idx *p_match_first)
      internal_function;
-static int check_halt_node_context (const re_dfa_t *dfa, int node,
-                                   unsigned int context) internal_function;
-static int check_halt_state_context (const re_match_context_t *mctx,
-                                    const re_dfastate_t *state, int idx)
+static Idx check_halt_state_context (const re_match_context_t *mctx,
+                                    const re_dfastate_t *state, Idx idx)
      internal_function;
 static void update_regs (re_dfa_t *dfa, regmatch_t *pmatch,
-                        regmatch_t *prev_idx_match, int cur_node,
-                        int cur_idx, int nmatch) internal_function;
-static int proceed_next_node (const re_match_context_t *mctx,
-                             int nregs, regmatch_t *regs,
-                             int *pidx, int node, re_node_set *eps_via_nodes,
-                             struct re_fail_stack_t *fs) internal_function;
+                        regmatch_t *prev_idx_match, Idx cur_node,
+                        Idx cur_idx, Idx nmatch) internal_function;
 static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
-                                     int str_idx, int dest_node, int nregs,
+                                     Idx str_idx, Idx dest_node, Idx nregs,
                                      regmatch_t *regs,
                                      re_node_set *eps_via_nodes) internal_function;
-static int pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
-                          regmatch_t *regs, re_node_set *eps_via_nodes) internal_function;
 static reg_errcode_t set_regs (const regex_t *preg,
                               const re_match_context_t *mctx,
                               size_t nmatch, regmatch_t *pmatch,
@@ -86,46 +77,41 @@ static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) interna
 #ifdef RE_ENABLE_I18N
 static int sift_states_iter_mb (const re_match_context_t *mctx,
                                re_sift_context_t *sctx,
-                               int node_idx, int str_idx, int max_str_idx) internal_function;
+                               Idx node_idx, Idx str_idx, Idx max_str_idx) internal_function;
 #endif /* RE_ENABLE_I18N */
 static reg_errcode_t sift_states_backward (re_match_context_t *mctx,
                                           re_sift_context_t *sctx) internal_function;
 static reg_errcode_t build_sifted_states (re_match_context_t *mctx,
-                                         re_sift_context_t *sctx, int str_idx,
+                                         re_sift_context_t *sctx, Idx str_idx,
                                          re_node_set *cur_dest) internal_function;
 static reg_errcode_t update_cur_sifted_state (re_match_context_t *mctx,
                                              re_sift_context_t *sctx,
-                                             int str_idx,
+                                             Idx str_idx,
                                              re_node_set *dest_nodes) internal_function;
 static reg_errcode_t add_epsilon_src_nodes (re_dfa_t *dfa,
                                            re_node_set *dest_nodes,
                                            const re_node_set *candidates) internal_function;
-static reg_errcode_t sub_epsilon_src_nodes (re_dfa_t *dfa, int node,
-                                           re_node_set *dest_nodes,
-                                           const re_node_set *and_nodes) internal_function;
 static int check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
-                            int dst_node, int dst_idx, int src_node,
-                            int src_idx) internal_function;
+                            Idx dst_node, Idx dst_idx, Idx src_node,
+                            Idx src_idx) internal_function;
 static int check_dst_limits_calc_pos_1 (re_match_context_t *mctx,
-                                       int boundaries, int subexp_idx,
-                                       int from_node, int bkref_idx) internal_function;
+                                       int boundaries, Idx subexp_idx,
+                                       Idx from_node, Idx bkref_idx) internal_function;
 static int check_dst_limits_calc_pos (re_match_context_t *mctx,
-                                     int limit, int subexp_idx,
-                                     int node, int str_idx,
-                                     int bkref_idx) internal_function;
+                                     Idx limit, Idx subexp_idx,
+                                     Idx node, Idx str_idx,
+                                     Idx bkref_idx) internal_function;
 static reg_errcode_t check_subexp_limits (re_dfa_t *dfa,
                                          re_node_set *dest_nodes,
                                          const re_node_set *candidates,
                                          re_node_set *limits,
                                          struct re_backref_cache_entry *bkref_ents,
-                                         int str_idx) internal_function;
+                                         Idx str_idx) internal_function;
 static reg_errcode_t sift_states_bkref (re_match_context_t *mctx,
                                        re_sift_context_t *sctx,
-                                       int str_idx, const re_node_set *candidates) internal_function;
-static reg_errcode_t clean_state_log_if_needed (re_match_context_t *mctx,
-                                               int next_state_log_idx) internal_function;
+                                       Idx str_idx, const re_node_set *candidates) internal_function;
 static reg_errcode_t merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst,
-                                       re_dfastate_t **src, int num) internal_function;
+                                       re_dfastate_t **src, Idx num) internal_function;
 static re_dfastate_t *find_recover_state (reg_errcode_t *err,
                                         re_match_context_t *mctx) internal_function;
 static re_dfastate_t *transit_state (reg_errcode_t *err,
@@ -136,7 +122,7 @@ static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
                                            re_dfastate_t *next_state) internal_function;
 static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
                                                re_node_set *cur_nodes,
-                                               int str_idx) internal_function;
+                                               Idx str_idx) internal_function;
 #if 0
 static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
                                        re_match_context_t *mctx,
@@ -149,47 +135,47 @@ static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
 static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
                                          const re_node_set *nodes) internal_function;
 static reg_errcode_t get_subexp (re_match_context_t *mctx,
-                                int bkref_node, int bkref_str_idx) internal_function;
+                                Idx bkref_node, Idx bkref_str_idx) internal_function;
 static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
                                     const re_sub_match_top_t *sub_top,
                                     re_sub_match_last_t *sub_last,
-                                    int bkref_node, int bkref_str) internal_function;
-static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
-                            int subexp_idx, int type) internal_function;
+                                    Idx bkref_node, Idx bkref_str) internal_function;
+static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
+                            Idx subexp_idx, int type) internal_function;
 static reg_errcode_t check_arrival (re_match_context_t *mctx,
-                                   state_array_t *path, int top_node,
-                                   int top_str, int last_node, int last_str,
+                                   state_array_t *path, Idx top_node,
+                                   Idx top_str, Idx last_node, Idx last_str,
                                    int type) internal_function;
 static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
-                                                  int str_idx,
+                                                  Idx str_idx,
                                                   re_node_set *cur_nodes,
                                                   re_node_set *next_nodes) internal_function;
 static reg_errcode_t check_arrival_expand_ecl (re_dfa_t *dfa,
                                               re_node_set *cur_nodes,
-                                              int ex_subexp, int type) internal_function;
+                                              Idx ex_subexp, int type) internal_function;
 static reg_errcode_t check_arrival_expand_ecl_sub (re_dfa_t *dfa,
                                                   re_node_set *dst_nodes,
-                                                  int target, int ex_subexp,
+                                                  Idx target, Idx ex_subexp,
                                                   int type) internal_function;
 static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
-                                        re_node_set *cur_nodes, int cur_str,
-                                        int subexp_num, int type) internal_function;
+                                        re_node_set *cur_nodes, Idx cur_str,
+                                        Idx subexp_num, int type) internal_function;
 static int build_trtable (re_dfa_t *dfa,
                          re_dfastate_t *state) internal_function;
 #ifdef RE_ENABLE_I18N
-static int check_node_accept_bytes (re_dfa_t *dfa, int node_idx,
-                                   const re_string_t *input, int idx) internal_function;
+static int check_node_accept_bytes (re_dfa_t *dfa, Idx node_idx,
+                                   const re_string_t *input, Idx idx) internal_function;
 # ifdef _LIBC
 static unsigned int find_collation_sequence_value (const unsigned char *mbs,
                                                   size_t name_len) internal_function;
 # endif /* _LIBC */
 #endif /* RE_ENABLE_I18N */
-static int group_nodes_into_DFAstates (re_dfa_t *dfa,
+static Idx group_nodes_into_DFAstates (re_dfa_t *dfa,
                                       const re_dfastate_t *state,
                                       re_node_set *states_node,
                                       bitset *states_ch) internal_function;
 static int check_node_accept (const re_match_context_t *mctx,
-                             const re_token_t *node, int idx) internal_function;
+                             const re_token_t *node, Idx idx) internal_function;
 static reg_errcode_t extend_buffers (re_match_context_t *mctx) internal_function;
 \f
 /* Entry point for POSIX code.  */
@@ -209,16 +195,14 @@ static reg_errcode_t extend_buffers (re_match_context_t *mctx) internal_function
    We return 0 if we find a match and REG_NOMATCH if not.  */
 
 int
-regexec (preg, string, nmatch, pmatch, eflags)
-    const regex_t *__restrict preg;
-    const char *__restrict string;
-    size_t nmatch;
-    regmatch_t pmatch[];
-    int eflags;
+regexec (const regex_t *__restrict preg, const char *__restrict string,
+        size_t nmatch, regmatch_t pmatch[], int eflags)
 {
   reg_errcode_t err;
-  int start, length;
-  re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
+  Idx start, length;
+#ifdef _LIBC
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+#endif
 
   if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
     return REG_BADPAT;
@@ -235,7 +219,7 @@ regexec (preg, string, nmatch, pmatch, eflags)
     }
 
   __libc_lock_lock (dfa->lock);
-  if (preg->no_sub)
+  if (preg->re_no_sub)
     err = re_search_internal (preg, string, length, start, length - start,
                              length, 0, NULL, eflags);
   else
@@ -285,7 +269,7 @@ compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
    the first STOP characters of the concatenation of the strings should be
    concerned.
 
-   If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
+   If REGS is not NULL, and BUFP->re_no_sub is not set, the offsets of the match
    and all groups is stroed in REGS.  (For the "_2" variants, the offsets are
    computed relative to the concatenation, not relative to the individual
    strings.)
@@ -294,12 +278,9 @@ compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
    return the position of the start of the match.  Return value -1 means no
    match was found and -2 indicates an internal error.  */
 
-int
-re_match (bufp, string, length, start, regs)
-    struct re_pattern_buffer *bufp;
-    const char *string;
-    int length, start;
-    struct re_registers *regs;
+regoff_t
+re_match (struct re_pattern_buffer *bufp, const char *string,
+         Idx length, Idx start, struct re_registers *regs)
 {
   return re_search_stub (bufp, string, length, start, 0, length, regs, 1);
 }
@@ -307,12 +288,9 @@ re_match (bufp, string, length, start, regs)
 weak_alias (__re_match, re_match)
 #endif
 
-int
-re_search (bufp, string, length, start, range, regs)
-    struct re_pattern_buffer *bufp;
-    const char *string;
-    int length, start, range;
-    struct re_registers *regs;
+regoff_t
+re_search (struct re_pattern_buffer *bufp, const char *string,
+          Idx length, Idx start, regoff_t range, struct re_registers *regs)
 {
   return re_search_stub (bufp, string, length, start, range, length, regs, 0);
 }
@@ -320,12 +298,11 @@ re_search (bufp, string, length, start, range, regs)
 weak_alias (__re_search, re_search)
 #endif
 
-int
-re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
-    struct re_pattern_buffer *bufp;
-    const char *string1, *string2;
-    int length1, length2, start, stop;
-    struct re_registers *regs;
+regoff_t
+re_match_2 (struct re_pattern_buffer *bufp,
+           const char *string1, Idx length1,
+           const char *string2, Idx length2,
+           Idx start, struct re_registers *regs, Idx stop)
 {
   return re_search_2_stub (bufp, string1, length1, string2, length2,
                           start, 0, regs, stop, 1);
@@ -334,12 +311,11 @@ re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
 weak_alias (__re_match_2, re_match_2)
 #endif
 
-int
-re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, stop)
-    struct re_pattern_buffer *bufp;
-    const char *string1, *string2;
-    int length1, length2, start, range, stop;
-    struct re_registers *regs;
+regoff_t
+re_search_2 (struct re_pattern_buffer *bufp,
+            const char *string1, Idx length1,
+            const char *string2, Idx length2,
+            Idx start, regoff_t range, struct re_registers *regs, Idx stop)
 {
   return re_search_2_stub (bufp, string1, length1, string2, length2,
                           start, range, regs, stop, 0);
@@ -348,17 +324,17 @@ re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, stop)
 weak_alias (__re_search_2, re_search_2)
 #endif
 
-static int
-re_search_2_stub (bufp, string1, length1, string2, length2, start, range, regs,
-                 stop, ret_len)
-    struct re_pattern_buffer *bufp;
-    const char *string1, *string2;
-    int length1, length2, start, range, stop, ret_len;
-    struct re_registers *regs;
+static regoff_t
+internal_function
+re_search_2_stub (struct re_pattern_buffer *bufp,
+                 const char *string1, Idx length1,
+                 const char *string2, Idx length2,
+                 Idx start, regoff_t range, struct re_registers *regs,
+                 Idx stop, int ret_len)
 {
   const char *str;
-  int rval;
-  int len = length1 + length2;
+  regoff_t rval;
+  Idx len = length1 + length2;
   int free_str = 0;
 
   if (BE (length1 < 0 || length2 < 0 || stop < 0, 0))
@@ -394,46 +370,61 @@ re_search_2_stub (bufp, string1, length1, string2, length2, start, range, regs,
    If RET_LEN is nonzero the length of the match is returned (re_match style);
    otherwise the position of the match is returned.  */
 
-static int
-re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
-    struct re_pattern_buffer *bufp;
-    const char *string;
-    int length, start, range, stop, ret_len;
-    struct re_registers *regs;
+static regoff_t
+internal_function
+re_search_stub (struct re_pattern_buffer *bufp,
+               const char *string, Idx length,
+               Idx start, regoff_t range, Idx stop, struct re_registers *regs,
+               int ret_len)
 {
   reg_errcode_t result;
   regmatch_t *pmatch;
-  int nregs, rval;
+  Idx nregs;
+  regoff_t rval;
   int eflags = 0;
-  re_dfa_t *dfa = (re_dfa_t *)bufp->buffer;
+#ifdef _LIBC
+  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+#endif
 
   /* Check for out-of-range.  */
   if (BE (start < 0 || start > length, 0))
     return -1;
-  if (BE (start + range > length, 0))
-    range = length - start;
-  else if (BE (start + range < 0, 0))
-    range = -start;
+  if (sizeof start < sizeof range)
+    {
+      regoff_t length_offset = length;
+      regoff_t start_offset = start;
+      if (BE (length_offset - start_offset < range, 0))
+       range = length_offset - start_offset;
+      else if (BE (range < - start_offset, 0))
+       range = -start_offset;
+    }
+  else
+    {
+      if (BE (start + range > length, 0))
+       range = length - start;
+      else if (BE (start + range < 0, 0))
+       range = -start;
+    }
 
   __libc_lock_lock (dfa->lock);
 
-  eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
-  eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
+  eflags |= (bufp->re_not_bol) ? REG_NOTBOL : 0;
+  eflags |= (bufp->re_not_eol) ? REG_NOTEOL : 0;
 
   /* Compile fastmap if we haven't yet.  */
-  if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate)
+  if (range > 0 && bufp->re_fastmap != NULL && !bufp->re_fastmap_accurate)
     re_compile_fastmap (bufp);
 
-  if (BE (bufp->no_sub, 0))
+  if (BE (bufp->re_no_sub, 0))
     regs = NULL;
 
   /* We need at least 1 register.  */
   if (regs == NULL)
     nregs = 1;
-  else if (BE (bufp->regs_allocated == REGS_FIXED &&
-              regs->num_regs < bufp->re_nsub + 1, 0))
+  else if (BE (bufp->re_regs_allocated == REG_FIXED
+              && regs->rm_num_regs < bufp->re_nsub + 1, 0))
     {
-      nregs = regs->num_regs;
+      nregs = regs->rm_num_regs;
       if (BE (nregs < 1, 0))
        {
          /* Nothing can be copied to regs.  */
@@ -461,9 +452,9 @@ re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
   else if (regs != NULL)
     {
       /* If caller wants register contents data back, copy them.  */
-      bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
-                                          bufp->regs_allocated);
-      if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
+      bufp->re_regs_allocated = re_copy_regs (regs, pmatch, nregs,
+                                             bufp->re_regs_allocated);
+      if (BE (bufp->re_regs_allocated == REG_UNALLOCATED, 0))
        rval = -2;
     }
 
@@ -484,57 +475,57 @@ re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
 }
 
 static unsigned
-re_copy_regs (regs, pmatch, nregs, regs_allocated)
-    struct re_registers *regs;
-    regmatch_t *pmatch;
-    int nregs, regs_allocated;
+internal_function
+re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
+             int regs_allocated)
 {
-  int rval = REGS_REALLOCATE;
-  int i;
-  int need_regs = nregs + 1;
-  /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
+  int rval = REG_REALLOCATE;
+  Idx i;
+  Idx need_regs = nregs + 1;
+  /* We need one extra element beyond `rm_num_regs' for the `-1' marker GNU code
      uses.  */
 
   /* Have the register data arrays been allocated?  */
-  if (regs_allocated == REGS_UNALLOCATED)
+  if (regs_allocated == REG_UNALLOCATED)
     { /* No.  So allocate them with malloc.  */
-      regs->start = re_malloc (regoff_t, need_regs);
-      regs->end = re_malloc (regoff_t, need_regs);
-      if (BE (regs->start == NULL, 0) || BE (regs->end == NULL, 0))
-       return REGS_UNALLOCATED;
-      regs->num_regs = need_regs;
+      regs->rm_start = re_malloc (regoff_t, need_regs);
+      regs->rm_end = re_malloc (regoff_t, need_regs);
+      if (BE (regs->rm_start == NULL, 0) || BE (regs->rm_end == NULL, 0))
+       return REG_UNALLOCATED;
+      regs->rm_num_regs = need_regs;
     }
-  else if (regs_allocated == REGS_REALLOCATE)
+  else if (regs_allocated == REG_REALLOCATE)
     { /* Yes.  If we need more elements than were already
         allocated, reallocate them.  If we need fewer, just
         leave it alone.  */
-      if (BE (need_regs > regs->num_regs, 0))
+      if (BE (need_regs > regs->rm_num_regs, 0))
        {
-         regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
-         regoff_t *new_end = re_realloc (regs->end, regoff_t, need_regs);
+         regoff_t *new_start =
+           re_realloc (regs->rm_start, regoff_t, need_regs);
+         regoff_t *new_end = re_realloc (regs->rm_end, regoff_t, need_regs);
          if (BE (new_start == NULL, 0) || BE (new_end == NULL, 0))
-           return REGS_UNALLOCATED;
-         regs->start = new_start;
-         regs->end = new_end;
-         regs->num_regs = need_regs;
+           return REG_UNALLOCATED;
+         regs->rm_start = new_start;
+         regs->rm_end = new_end;
+         regs->rm_num_regs = need_regs;
        }
     }
   else
     {
-      assert (regs_allocated == REGS_FIXED);
-      /* This function may not be called with REGS_FIXED and nregs too big.  */
-      assert (regs->num_regs >= nregs);
-      rval = REGS_FIXED;
+      assert (regs_allocated == REG_FIXED);
+      /* This function may not be called with REG_FIXED and nregs too big.  */
+      assert (regs->rm_num_regs >= nregs);
+      rval = REG_FIXED;
     }
 
   /* Copy the regs.  */
   for (i = 0; i < nregs; ++i)
     {
-      regs->start[i] = pmatch[i].rm_so;
-      regs->end[i] = pmatch[i].rm_eo;
+      regs->rm_start[i] = pmatch[i].rm_so;
+      regs->rm_end[i] = pmatch[i].rm_eo;
     }
-  for ( ; i < regs->num_regs; ++i)
-    regs->start[i] = regs->end[i] = -1;
+  for ( ; i < regs->rm_num_regs; ++i)
+    regs->rm_start[i] = regs->rm_end[i] = -1;
 
   return rval;
 }
@@ -553,24 +544,21 @@ re_copy_regs (regs, pmatch, nregs, regs_allocated)
    freeing the old data.  */
 
 void
-re_set_registers (bufp, regs, num_regs, starts, ends)
-    struct re_pattern_buffer *bufp;
-    struct re_registers *regs;
-    unsigned num_regs;
-    regoff_t *starts, *ends;
+re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
+                 __re_size_t num_regs, regoff_t *starts, regoff_t *ends)
 {
   if (num_regs)
     {
-      bufp->regs_allocated = REGS_REALLOCATE;
-      regs->num_regs = num_regs;
-      regs->start = starts;
-      regs->end = ends;
+      bufp->re_regs_allocated = REG_REALLOCATE;
+      regs->rm_num_regs = num_regs;
+      regs->rm_start = starts;
+      regs->rm_end = ends;
     }
   else
     {
-      bufp->regs_allocated = REGS_UNALLOCATED;
-      regs->num_regs = 0;
-      regs->start = regs->end = (regoff_t *) 0;
+      bufp->re_regs_allocated = REG_UNALLOCATED;
+      regs->rm_num_regs = 0;
+      regs->rm_start = regs->rm_end = NULL;
     }
 }
 #ifdef _LIBC
@@ -585,8 +573,7 @@ int
 # ifdef _LIBC
 weak_function
 # endif
-re_exec (s)
-     const char *s;
+re_exec (const char *s)
 {
   return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
 }
@@ -604,28 +591,30 @@ re_exec (s)
    (START + RANGE >= 0 && START + RANGE <= LENGTH)  */
 
 static reg_errcode_t
-re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
-                   eflags)
-    const regex_t *preg;
-    const char *string;
-    int length, start, range, stop, eflags;
-    size_t nmatch;
-    regmatch_t pmatch[];
+internal_function
+re_search_internal (const regex_t *preg,
+                   const char *string, Idx length,
+                   Idx start, regoff_t range, Idx stop,
+                   size_t nmatch, regmatch_t pmatch[],
+                   int eflags)
 {
   reg_errcode_t err;
-  re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
-  int left_lim, right_lim, incr;
-  int fl_longest_match, match_first, match_kind, match_last = -1;
-  int extra_nmatch;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  Idx left_lim, right_lim;
+  int incr;
+  int fl_longest_match, match_kind;
+  Idx match_first, match_last = REG_MISSING;
+  Idx extra_nmatch;
   int sb, ch;
 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
   re_match_context_t mctx = { .dfa = dfa };
 #else
   re_match_context_t mctx;
 #endif
-  char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
-                  && range && !preg->can_be_null) ? preg->fastmap : NULL;
-  unsigned RE_TRANSLATE_TYPE t = (unsigned RE_TRANSLATE_TYPE) preg->translate;
+  char *fastmap = (preg->re_fastmap != NULL && preg->re_fastmap_accurate
+                  && range && !preg->re_can_be_null) ? preg->re_fastmap : NULL;
+  unsigned REG_TRANSLATE_TYPE t =
+    (unsigned REG_TRANSLATE_TYPE) preg->re_translate;
 
 #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
   memset (&mctx, '\0', sizeof (re_match_context_t));
@@ -636,7 +625,7 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
   nmatch -= extra_nmatch;
 
   /* Check if the DFA haven't been compiled.  */
-  if (BE (preg->used == 0 || dfa->init_state == NULL
+  if (BE (preg->re_used == 0 || dfa->init_state == NULL
          || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
          || dfa->init_state_begbuf == NULL, 0))
     return REG_NOMATCH;
@@ -647,12 +636,12 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
 #endif
 
   /* If initial states with non-begbuf contexts have no elements,
-     the regex must be anchored.  If preg->newline_anchor is set,
+     the regex must be anchored.  If preg->re_newline_anchor is set,
      we'll never use init_state_nl, so do not check it.  */
   if (dfa->init_state->nodes.nelem == 0
       && dfa->init_state_word->nodes.nelem == 0
       && (dfa->init_state_nl->nodes.nelem == 0
-         || !preg->newline_anchor))
+         || !preg->re_newline_anchor))
     {
       if (start != 0 && start + range != 0)
         return REG_NOMATCH;
@@ -663,12 +652,13 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
   fl_longest_match = (nmatch != 0 || dfa->nbackref);
 
   err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
-                           preg->translate, preg->syntax & RE_ICASE, dfa);
+                           preg->re_translate,
+                           preg->re_syntax & REG_IGNORE_CASE, dfa);
   if (BE (err != REG_NOERROR, 0))
     goto free_return;
   mctx.input.stop = stop;
   mctx.input.raw_stop = stop;
-  mctx.input.newline_anchor = preg->newline_anchor;
+  mctx.input.newline_anchor = preg->re_newline_anchor;
 
   err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
   if (BE (err != REG_NOERROR, 0))
@@ -701,7 +691,7 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
   sb = dfa->mb_cur_max == 1;
   match_kind =
     (fastmap
-     ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
+     ? ((sb || !(preg->re_syntax & REG_IGNORE_CASE || t) ? 4 : 0)
        | (range >= 0 ? 2 : 0)
        | (t != NULL ? 1 : 0))
      : 8);
@@ -769,8 +759,8 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
            {
              /* If MATCH_FIRST is out of the valid range, reconstruct the
                 buffers.  */
-             unsigned int offset = match_first - mctx.input.raw_mbs_idx;
-             if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0))
+             __re_size_t offset = match_first - mctx.input.raw_mbs_idx;
+             if (BE (offset >= (__re_size_t) mctx.input.valid_raw_len, 0))
                {
                  err = re_string_reconstruct (&mctx.input, match_first,
                                               eflags);
@@ -813,9 +803,9 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
       mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
       match_last = check_matching (&mctx, fl_longest_match,
                                   range >= 0 ? &match_first : NULL);
-      if (match_last != -1)
+      if (match_last != REG_MISSING)
        {
-         if (BE (match_last == -2, 0))
+         if (BE (match_last == REG_ERROR, 0))
            {
              err = REG_ESPACE;
              goto free_return;
@@ -823,13 +813,13 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
          else
            {
              mctx.match_last = match_last;
-             if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
+             if ((!preg->re_no_sub && nmatch > 1) || dfa->nbackref)
                {
                  re_dfastate_t *pstate = mctx.state_log[match_last];
                  mctx.last_node = check_halt_state_context (&mctx, pstate,
                                                             match_last);
                }
-             if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
+             if ((!preg->re_no_sub && nmatch > 1 && dfa->has_plural_match)
                  || dfa->nbackref)
                {
                  err = prune_impossible_nodes (&mctx);
@@ -837,7 +827,7 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
                    break;
                  if (BE (err != REG_NOMATCH, 0))
                    goto free_return;
-                 match_last = -1;
+                 match_last = REG_MISSING;
                }
              else
                break; /* We found a match.  */
@@ -848,14 +838,14 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
     }
 
 #ifdef DEBUG
-  assert (match_last != -1);
+  assert (match_last != REG_MISSING);
   assert (err == REG_NOERROR);
 #endif
 
   /* Set pmatch[] if we need.  */
   if (nmatch > 0)
     {
-      int reg_idx;
+      Idx reg_idx;
 
       /* Initialize registers.  */
       for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
@@ -864,8 +854,11 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
       /* Set the points where matching start/end.  */
       pmatch[0].rm_so = 0;
       pmatch[0].rm_eo = mctx.match_last;
+      /* FIXME: This function should fail if mctx.match_last exceeds
+        the maximum possible regoff_t value.  We need a new error
+        code REG_OVERFLOW.  */
 
-      if (!preg->no_sub && nmatch > 1)
+      if (!preg->re_no_sub && nmatch > 1)
        {
          err = set_regs (preg, &mctx, nmatch, pmatch,
                          dfa->has_plural_match && dfa->nbackref > 0);
@@ -882,14 +875,14 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
 #ifdef RE_ENABLE_I18N
            if (BE (mctx.input.offsets_needed != 0, 0))
              {
-               if (pmatch[reg_idx].rm_so == mctx.input.valid_len)
-                 pmatch[reg_idx].rm_so += mctx.input.valid_raw_len - mctx.input.valid_len;
-               else
-                 pmatch[reg_idx].rm_so = mctx.input.offsets[pmatch[reg_idx].rm_so];
-               if (pmatch[reg_idx].rm_eo == mctx.input.valid_len)
-                 pmatch[reg_idx].rm_eo += mctx.input.valid_raw_len - mctx.input.valid_len;
-               else
-                 pmatch[reg_idx].rm_eo = mctx.input.offsets[pmatch[reg_idx].rm_eo];
+               pmatch[reg_idx].rm_so =
+                 (pmatch[reg_idx].rm_so == mctx.input.valid_len
+                  ? mctx.input.valid_raw_len
+                  : mctx.input.offsets[pmatch[reg_idx].rm_so]);
+               pmatch[reg_idx].rm_eo =
+                 (pmatch[reg_idx].rm_eo == mctx.input.valid_len
+                  ? mctx.input.valid_raw_len
+                  : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
              }
 #else
            assert (mctx.input.offsets_needed == 0);
@@ -923,11 +916,11 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
 }
 
 static reg_errcode_t
-prune_impossible_nodes (mctx)
-     re_match_context_t *mctx;
+internal_function
+prune_impossible_nodes (re_match_context_t *mctx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int halt_node, match_last;
+  Idx halt_node, match_last;
   reg_errcode_t ret;
   re_dfastate_t **sifted_states;
   re_dfastate_t **lim_states = NULL;
@@ -966,7 +959,7 @@ prune_impossible_nodes (mctx)
          do
            {
              --match_last;
-             if (match_last < 0)
+             if (! REG_VALID_INDEX (match_last))
                {
                  ret = REG_NOMATCH;
                  goto free_return;
@@ -1009,10 +1002,9 @@ prune_impossible_nodes (mctx)
    since initial states may have constraints like "\<", "^", etc..  */
 
 static inline re_dfastate_t *
-acquire_init_state_context (err, mctx, idx)
-     reg_errcode_t *err;
-     const re_match_context_t *mctx;
-     int idx;
+__attribute ((always_inline)) internal_function
+acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
+                           Idx idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
   if (dfa->init_state->has_constraint)
@@ -1043,28 +1035,27 @@ acquire_init_state_context (err, mctx, idx)
 }
 
 /* Check whether the regular expression match input string INPUT or not,
-   and return the index where the matching end, return -1 if not match,
-   or return -2 in case of an error.
+   and return the index where the matching end.  Return REG_MISSING if
+   there is no match, and return REG_ERROR in case of an error.
    FL_LONGEST_MATCH means we want the POSIX longest matching.
    If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
    next place where we may want to try matching.
    Note that the matcher assume that the maching starts from the current
    index of the buffer.  */
 
-static int
-check_matching (mctx, fl_longest_match, p_match_first)
-    re_match_context_t *mctx;
-    int fl_longest_match;
-    int *p_match_first;
+static Idx
+internal_function
+check_matching (re_match_context_t *mctx, int fl_longest_match,
+               Idx *p_match_first)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int match = 0;
-  int match_last = -1;
-  int cur_str_idx = re_string_cur_idx (&mctx->input);
+  Idx match = 0;
+  Idx match_last = REG_MISSING;
+  Idx cur_str_idx = re_string_cur_idx (&mctx->input);
   re_dfastate_t *cur_state;
   int at_init_state = p_match_first != NULL;
-  int next_start_idx = cur_str_idx;
+  Idx next_start_idx = cur_str_idx;
 
   err = REG_NOERROR;
   cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
@@ -1072,7 +1063,7 @@ check_matching (mctx, fl_longest_match, p_match_first)
   if (BE (cur_state == NULL, 0))
     {
       assert (err == REG_ESPACE);
-      return -2;
+      return REG_ERROR;
     }
 
   if (mctx->state_log != NULL)
@@ -1116,7 +1107,7 @@ check_matching (mctx, fl_longest_match, p_match_first)
   while (!re_string_eoi (&mctx->input))
     {
       re_dfastate_t *old_state = cur_state;
-      int next_char_idx = re_string_cur_idx (&mctx->input) + 1;
+      Idx next_char_idx = re_string_cur_idx (&mctx->input) + 1;
 
       if (BE (next_char_idx >= mctx->input.bufs_len, 0)
           || (BE (next_char_idx >= mctx->input.valid_len, 0)
@@ -1126,7 +1117,7 @@ check_matching (mctx, fl_longest_match, p_match_first)
           if (BE (err != REG_NOERROR, 0))
            {
              assert (err == REG_ESPACE);
-             return -2;
+             return REG_ERROR;
            }
         }
 
@@ -1140,7 +1131,7 @@ check_matching (mctx, fl_longest_match, p_match_first)
             state using the state log, if available and if we have not
             already found a valid (even if not the longest) match.  */
          if (BE (err != REG_NOERROR, 0))
-           return -2;
+           return REG_ERROR;
 
          if (mctx->state_log == NULL
              || (match && !fl_longest_match)
@@ -1184,10 +1175,9 @@ check_matching (mctx, fl_longest_match, p_match_first)
 
 /* Check NODE match the current context.  */
 
-static int check_halt_node_context (dfa, node, context)
-    const re_dfa_t *dfa;
-    int node;
-    unsigned int context;
+static int
+internal_function
+check_halt_node_context (const re_dfa_t *dfa, Idx node, unsigned int context)
 {
   re_token_type_t type = dfa->nodes[node].type;
   unsigned int constraint = dfa->nodes[node].constraint;
@@ -1204,13 +1194,12 @@ static int check_halt_node_context (dfa, node, context)
    Return 0 if not match, if the node, STATE has, is a halt node and
    match the context, return the node.  */
 
-static int
-check_halt_state_context (mctx, state, idx)
-    const re_match_context_t *mctx;
-    const re_dfastate_t *state;
-    int idx;
+static Idx
+internal_function
+check_halt_state_context (const re_match_context_t *mctx,
+                         const re_dfastate_t *state, Idx idx)
 {
-  int i;
+  Idx i;
   unsigned int context;
 #ifdef DEBUG
   assert (state->halt);
@@ -1224,35 +1213,34 @@ check_halt_state_context (mctx, state, idx)
 
 /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
    corresponding to the DFA).
-   Return the destination node, and update EPS_VIA_NODES, return -1 in case
-   of errors.  */
-
-static int
-proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
-    const re_match_context_t *mctx;
-    regmatch_t *regs;
-    int nregs, *pidx, node;
-    re_node_set *eps_via_nodes;
-    struct re_fail_stack_t *fs;
+   Return the destination node, and update EPS_VIA_NODES;
+   return REG_MISSING in case of errors.  */
+
+static Idx
+internal_function
+proceed_next_node (const re_match_context_t *mctx,
+                  Idx nregs, regmatch_t *regs, Idx *pidx, Idx node,
+                  re_node_set *eps_via_nodes, struct re_fail_stack_t *fs)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int i, err, dest_node;
-  dest_node = -1;
+  Idx i;
+  int err;
   if (IS_EPSILON_NODE (dfa->nodes[node].type))
     {
       re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
       re_node_set *edests = &dfa->edests[node];
-      int dest_node;
+      Idx dest_node;
       err = re_node_set_insert (eps_via_nodes, node);
       if (BE (err < 0, 0))
-       return -2;
-      /* Pick up a valid destination, or return -1 if none is found.  */
-      for (dest_node = -1, i = 0; i < edests->nelem; ++i)
+       return REG_ERROR;
+      /* Pick up a valid destination, or return REG_MISSING if none
+        is found.  */
+      for (dest_node = REG_MISSING, i = 0; i < edests->nelem; ++i)
        {
-         int candidate = edests->elems[i];
+         Idx candidate = edests->elems[i];
          if (!re_node_set_contains (cur_nodes, candidate))
            continue;
-          if (dest_node == -1)
+          if (dest_node == REG_MISSING)
            dest_node = candidate;
 
           else
@@ -1266,7 +1254,7 @@ proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
              else if (fs != NULL
                       && push_fail_stack (fs, *pidx, candidate, nregs, regs,
                                           eps_via_nodes))
-               return -2;
+               return REG_ERROR;
 
              /* We know we are going to exit.  */
              break;
@@ -1276,7 +1264,7 @@ proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
     }
   else
     {
-      int naccepted = 0;
+      Idx naccepted = 0;
       re_token_type_t type = dfa->nodes[node].type;
 
 #ifdef RE_ENABLE_I18N
@@ -1286,26 +1274,27 @@ proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
 #endif /* RE_ENABLE_I18N */
       if (type == OP_BACK_REF)
        {
-         int subexp_idx = dfa->nodes[node].opr.idx + 1;
+         Idx subexp_idx = dfa->nodes[node].opr.idx + 1;
          naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
          if (fs != NULL)
            {
              if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
-               return -1;
+               return REG_MISSING;
              else if (naccepted)
                {
                  char *buf = (char *) re_string_get_buffer (&mctx->input);
                  if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
                              naccepted) != 0)
-                   return -1;
+                   return REG_MISSING;
                }
            }
 
          if (naccepted == 0)
            {
+             Idx dest_node;
              err = re_node_set_insert (eps_via_nodes, node);
              if (BE (err < 0, 0))
-               return -2;
+               return REG_ERROR;
              dest_node = dfa->edests[node].elems[0];
              if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
                                        dest_node))
@@ -1316,33 +1305,30 @@ proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
       if (naccepted != 0
          || check_node_accept (mctx, dfa->nodes + node, *pidx))
        {
-         dest_node = dfa->nexts[node];
+         Idx dest_node = dfa->nexts[node];
          *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
          if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
                     || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
                                               dest_node)))
-           return -1;
+           return REG_MISSING;
          re_node_set_empty (eps_via_nodes);
          return dest_node;
        }
     }
-  return -1;
+  return REG_MISSING;
 }
 
 static reg_errcode_t
-push_fail_stack (fs, str_idx, dest_node, nregs, regs, eps_via_nodes)
-     struct re_fail_stack_t *fs;
-     int str_idx, dest_node, nregs;
-     regmatch_t *regs;
-     re_node_set *eps_via_nodes;
+internal_function
+push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
+                Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
 {
   reg_errcode_t err;
-  int num = fs->num++;
+  Idx num = fs->num++;
   if (fs->num == fs->alloc)
     {
-      struct re_fail_stack_ent_t *new_array;
-      new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
-                                      * fs->alloc * 2));
+      struct re_fail_stack_ent_t *new_array =
+       re_realloc (fs->stack, struct re_fail_stack_ent_t, fs->alloc * 2);
       if (new_array == NULL)
        return REG_ESPACE;
       fs->alloc *= 2;
@@ -1358,15 +1344,13 @@ push_fail_stack (fs, str_idx, dest_node, nregs, regs, eps_via_nodes)
   return err;
 }
 
-static int
-pop_fail_stack (fs, pidx, nregs, regs, eps_via_nodes)
-     struct re_fail_stack_t *fs;
-     int *pidx, nregs;
-     regmatch_t *regs;
-     re_node_set *eps_via_nodes;
+static Idx
+internal_function
+pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx,
+               Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
 {
-  int num = --fs->num;
-  assert (num >= 0);
+  Idx num = --fs->num;
+  assert (REG_VALID_INDEX (num));
   *pidx = fs->stack[num].idx;
   memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
   re_node_set_free (eps_via_nodes);
@@ -1381,19 +1365,17 @@ pop_fail_stack (fs, pidx, nregs, regs, eps_via_nodes)
    pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch.  */
 
 static reg_errcode_t
-set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
-     const regex_t *preg;
-     const re_match_context_t *mctx;
-     size_t nmatch;
-     regmatch_t *pmatch;
-     int fl_backtrack;
+internal_function
+set_regs (const regex_t *preg, const re_match_context_t *mctx,
+         size_t nmatch, regmatch_t *pmatch, int fl_backtrack)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
-  int idx, cur_node;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  Idx idx, cur_node;
   re_node_set eps_via_nodes;
   struct re_fail_stack_t *fs;
   struct re_fail_stack_t fs_body = { 0, 2, NULL };
   regmatch_t *prev_idx_match;
+  int prev_idx_match_malloced = 0;
 
 #ifdef DEBUG
   assert (nmatch > 1);
@@ -1412,7 +1394,18 @@ set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
   cur_node = dfa->init_node;
   re_node_set_init_empty (&eps_via_nodes);
 
-  prev_idx_match = (regmatch_t *) alloca (sizeof (regmatch_t) * nmatch);
+  if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
+    prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
+  else
+    {
+      prev_idx_match = re_malloc (regmatch_t, nmatch);
+      if (prev_idx_match == NULL)
+       {
+         free_fail_stack_return (fs);
+         return REG_ESPACE;
+       }
+      prev_idx_match_malloced = 1;
+    }
   memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
 
   for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
@@ -1421,7 +1414,7 @@ set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
 
       if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
        {
-         int reg_idx;
+         Idx reg_idx;
          if (fs)
            {
              for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
@@ -1430,6 +1423,8 @@ set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
              if (reg_idx == nmatch)
                {
                  re_node_set_free (&eps_via_nodes);
+                 if (prev_idx_match_malloced)
+                   re_free (prev_idx_match);
                  return free_fail_stack_return (fs);
                }
              cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
@@ -1438,6 +1433,8 @@ set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
          else
            {
              re_node_set_free (&eps_via_nodes);
+             if (prev_idx_match_malloced)
+               re_free (prev_idx_match);
              return REG_NOERROR;
            }
        }
@@ -1446,11 +1443,13 @@ set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
       cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
                                    &eps_via_nodes, fs);
 
-      if (BE (cur_node < 0, 0))
+      if (BE (! REG_VALID_INDEX (cur_node), 0))
        {
-         if (BE (cur_node == -2, 0))
+         if (BE (cur_node == REG_ERROR, 0))
            {
              re_node_set_free (&eps_via_nodes);
+             if (prev_idx_match_malloced)
+               re_free (prev_idx_match);
              free_fail_stack_return (fs);
              return REG_ESPACE;
            }
@@ -1460,21 +1459,25 @@ set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
          else
            {
              re_node_set_free (&eps_via_nodes);
+             if (prev_idx_match_malloced)
+               re_free (prev_idx_match);
              return REG_NOMATCH;
            }
        }
     }
   re_node_set_free (&eps_via_nodes);
+  if (prev_idx_match_malloced)
+    re_free (prev_idx_match);
   return free_fail_stack_return (fs);
 }
 
 static reg_errcode_t
-free_fail_stack_return (fs)
-     struct re_fail_stack_t *fs;
+internal_function
+free_fail_stack_return (struct re_fail_stack_t *fs)
 {
   if (fs)
     {
-      int fs_idx;
+      Idx fs_idx;
       for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
        {
          re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
@@ -1486,15 +1489,14 @@ free_fail_stack_return (fs)
 }
 
 static void
-update_regs (dfa, pmatch, prev_idx_match, cur_node, cur_idx, nmatch)
-     re_dfa_t *dfa;
-     regmatch_t *pmatch, *prev_idx_match;
-     int cur_node, cur_idx, nmatch;
+internal_function
+update_regs (re_dfa_t *dfa, regmatch_t *pmatch, regmatch_t *prev_idx_match,
+            Idx cur_node, Idx cur_idx, Idx nmatch)
 {
   int type = dfa->nodes[cur_node].type;
   if (type == OP_OPEN_SUBEXP)
     {
-      int reg_num = dfa->nodes[cur_node].opr.idx + 1;
+      Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
 
       /* We are at the first node of this sub expression.  */
       if (reg_num < nmatch)
@@ -1505,7 +1507,7 @@ update_regs (dfa, pmatch, prev_idx_match, cur_node, cur_idx, nmatch)
     }
   else if (type == OP_CLOSE_SUBEXP)
     {
-      int reg_num = dfa->nodes[cur_node].opr.idx + 1;
+      Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
       if (reg_num < nmatch)
        {
          /* We are at the last node of this sub expression.  */
@@ -1559,13 +1561,12 @@ update_regs (dfa, pmatch, prev_idx_match, cur_node, cur_idx, nmatch)
   ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
 
 static reg_errcode_t
-sift_states_backward (mctx, sctx)
-     re_match_context_t *mctx;
-     re_sift_context_t *sctx;
+internal_function
+sift_states_backward (re_match_context_t *mctx, re_sift_context_t *sctx)
 {
   reg_errcode_t err;
   int null_cnt = 0;
-  int str_idx = sctx->last_str_idx;
+  Idx str_idx = sctx->last_str_idx;
   re_node_set cur_dest;
 
 #ifdef DEBUG
@@ -1618,15 +1619,13 @@ sift_states_backward (mctx, sctx)
 }
 
 static reg_errcode_t
-build_sifted_states (mctx, sctx, str_idx, cur_dest)
-     re_match_context_t *mctx;
-     re_sift_context_t *sctx;
-     int str_idx;
-     re_node_set *cur_dest;
+internal_function
+build_sifted_states (re_match_context_t *mctx, re_sift_context_t *sctx,
+                    Idx str_idx, re_node_set *cur_dest)
 {
   re_dfa_t *const dfa = mctx->dfa;
   re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
-  int i;
+  Idx i;
 
   /* Then build the next sifted state.
      We build the next sifted state on `cur_dest', and update
@@ -1637,7 +1636,7 @@ build_sifted_states (mctx, sctx, str_idx, cur_dest)
      (with the epsilon nodes pre-filtered out).  */
   for (i = 0; i < cur_src->nelem; i++)
     {
-      int prev_node = cur_src->elems[i];
+      Idx prev_node = cur_src->elems[i];
       int naccepted = 0;
       int ret;
 
@@ -1665,7 +1664,7 @@ build_sifted_states (mctx, sctx, str_idx, cur_dest)
 
       if (sctx->limits.nelem)
        {
-         int to_idx = str_idx + naccepted;
+         Idx to_idx = str_idx + naccepted;
          if (check_dst_limits (mctx, &sctx->limits,
                                dfa->nexts[prev_node], to_idx,
                                prev_node, str_idx))
@@ -1682,11 +1681,10 @@ build_sifted_states (mctx, sctx, str_idx, cur_dest)
 /* Helper functions.  */
 
 static reg_errcode_t
-clean_state_log_if_needed (mctx, next_state_log_idx)
-    re_match_context_t *mctx;
-    int next_state_log_idx;
+internal_function
+clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx)
 {
-  int top = mctx->state_log_top;
+  Idx top = mctx->state_log_top;
 
   if (next_state_log_idx >= mctx->input.bufs_len
       || (next_state_log_idx >= mctx->input.valid_len
@@ -1708,13 +1706,11 @@ clean_state_log_if_needed (mctx, next_state_log_idx)
 }
 
 static reg_errcode_t
-merge_state_array (dfa, dst, src, num)
-     re_dfa_t *dfa;
-     re_dfastate_t **dst;
-     re_dfastate_t **src;
-     int num;
+internal_function
+merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst, re_dfastate_t **src,
+                  Idx num)
 {
-  int st_idx;
+  Idx st_idx;
   reg_errcode_t err;
   for (st_idx = 0; st_idx < num; ++st_idx)
     {
@@ -1737,11 +1733,9 @@ merge_state_array (dfa, dst, src, num)
 }
 
 static reg_errcode_t
-update_cur_sifted_state (mctx, sctx, str_idx, dest_nodes)
-     re_match_context_t *mctx;
-     re_sift_context_t *sctx;
-     int str_idx;
-     re_node_set *dest_nodes;
+internal_function
+update_cur_sifted_state (re_match_context_t *mctx, re_sift_context_t *sctx,
+                        Idx str_idx, re_node_set *dest_nodes)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
@@ -1786,13 +1780,12 @@ update_cur_sifted_state (mctx, sctx, str_idx, dest_nodes)
 }
 
 static reg_errcode_t
-add_epsilon_src_nodes (dfa, dest_nodes, candidates)
-     re_dfa_t *dfa;
-     re_node_set *dest_nodes;
-     const re_node_set *candidates;
+internal_function
+add_epsilon_src_nodes (re_dfa_t *dfa, re_node_set *dest_nodes,
+                      const re_node_set *candidates)
 {
   reg_errcode_t err = REG_NOERROR;
-  int i;
+  Idx i;
 
   re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
   if (BE (err != REG_NOERROR, 0))
@@ -1812,30 +1805,28 @@ add_epsilon_src_nodes (dfa, dest_nodes, candidates)
 }
 
 static reg_errcode_t
-sub_epsilon_src_nodes (dfa, node, dest_nodes, candidates)
-     re_dfa_t *dfa;
-     int node;
-     re_node_set *dest_nodes;
-     const re_node_set *candidates;
+internal_function
+sub_epsilon_src_nodes (re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
+                      const re_node_set *candidates)
 {
-    int ecl_idx;
+    Idx ecl_idx;
     reg_errcode_t err;
     re_node_set *inv_eclosure = dfa->inveclosures + node;
     re_node_set except_nodes;
     re_node_set_init_empty (&except_nodes);
     for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
       {
-       int cur_node = inv_eclosure->elems[ecl_idx];
+       Idx cur_node = inv_eclosure->elems[ecl_idx];
        if (cur_node == node)
          continue;
        if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
          {
-           int edst1 = dfa->edests[cur_node].elems[0];
-           int edst2 = ((dfa->edests[cur_node].nelem > 1)
-                        ? dfa->edests[cur_node].elems[1] : -1);
+           Idx edst1 = dfa->edests[cur_node].elems[0];
+           Idx edst2 = ((dfa->edests[cur_node].nelem > 1)
+                        ? dfa->edests[cur_node].elems[1] : REG_MISSING);
            if ((!re_node_set_contains (inv_eclosure, edst1)
                 && re_node_set_contains (dest_nodes, edst1))
-               || (edst2 > 0
+               || (REG_VALID_NONZERO_INDEX (edst2)
                    && !re_node_set_contains (inv_eclosure, edst2)
                    && re_node_set_contains (dest_nodes, edst2)))
              {
@@ -1851,10 +1842,10 @@ sub_epsilon_src_nodes (dfa, node, dest_nodes, candidates)
       }
     for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
       {
-       int cur_node = inv_eclosure->elems[ecl_idx];
+       Idx cur_node = inv_eclosure->elems[ecl_idx];
        if (!re_node_set_contains (&except_nodes, cur_node))
          {
-           int idx = re_node_set_contains (dest_nodes, cur_node) - 1;
+           Idx idx = re_node_set_contains (dest_nodes, cur_node) - 1;
            re_node_set_remove_at (dest_nodes, idx);
          }
       }
@@ -1863,19 +1854,18 @@ sub_epsilon_src_nodes (dfa, node, dest_nodes, candidates)
 }
 
 static int
-check_dst_limits (mctx, limits, dst_node, dst_idx, src_node, src_idx)
-     re_match_context_t *mctx;
-     re_node_set *limits;
-     int dst_node, dst_idx, src_node, src_idx;
+internal_function
+check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
+                 Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int lim_idx, src_pos, dst_pos;
+  Idx lim_idx, src_pos, dst_pos;
 
-  int dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
-  int src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
+  Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
+  Idx src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
   for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
     {
-      int subexp_idx;
+      Idx subexp_idx;
       struct re_backref_cache_entry *ent;
       ent = mctx->bkref_ents + limits->elems[lim_idx];
       subexp_idx = dfa->nodes[ent->node].opr.idx;
@@ -1900,34 +1890,36 @@ check_dst_limits (mctx, limits, dst_node, dst_idx, src_node, src_idx)
 }
 
 static int
-check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, from_node, bkref_idx)
-     re_match_context_t *mctx;
-     int boundaries, subexp_idx, from_node, bkref_idx;
+internal_function
+check_dst_limits_calc_pos_1 (re_match_context_t *mctx, int boundaries,
+                            Idx subexp_idx, Idx from_node, Idx bkref_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
   re_node_set *eclosures = dfa->eclosures + from_node;
-  int node_idx;
+  Idx node_idx;
 
   /* Else, we are on the boundary: examine the nodes on the epsilon
      closure.  */
   for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
     {
-      int node = eclosures->elems[node_idx];
+      Idx node = eclosures->elems[node_idx];
       switch (dfa->nodes[node].type)
        {
        case OP_BACK_REF:
-         if (bkref_idx != -1)
+         if (bkref_idx != REG_MISSING)
            {
              struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
              do
                {
-                 int dst, cpos;
+                 Idx dst;
+                 int cpos;
 
                  if (ent->node != node)
                    continue;
 
-                 if (subexp_idx <= 8 * sizeof (ent->eps_reachable_subexps_map)
-                     && !(ent->eps_reachable_subexps_map & (1 << subexp_idx)))
+                 if (subexp_idx
+                     < CHAR_BIT * sizeof ent->eps_reachable_subexps_map
+                     && !(ent->eps_reachable_subexps_map & (1u << subexp_idx)))
                    continue;
 
                  /* Recurse trying to reach the OP_OPEN_SUBEXP and
@@ -1953,7 +1945,9 @@ check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, from_node, bkref_idx)
                  if (cpos == 0 && (boundaries & 2))
                    return 0;
 
-                 ent->eps_reachable_subexps_map &= ~(1 << subexp_idx);
+                 if (subexp_idx
+                     < CHAR_BIT * sizeof ent->eps_reachable_subexps_map)
+                   ent->eps_reachable_subexps_map &= ~(1u << subexp_idx);
                }
              while (ent++->more);
            }
@@ -1978,9 +1972,9 @@ check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, from_node, bkref_idx)
 }
 
 static int
-check_dst_limits_calc_pos (mctx, limit, subexp_idx, from_node, str_idx, bkref_idx)
-     re_match_context_t *mctx;
-     int limit, subexp_idx, from_node, str_idx, bkref_idx;
+internal_function
+check_dst_limits_calc_pos (re_match_context_t *mctx, Idx limit, Idx subexp_idx,
+                          Idx from_node, Idx str_idx, Idx bkref_idx)
 {
   struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
   int boundaries;
@@ -2007,20 +2001,17 @@ check_dst_limits_calc_pos (mctx, limit, subexp_idx, from_node, str_idx, bkref_id
    which are against limitations from DEST_NODES. */
 
 static reg_errcode_t
-check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
-     re_dfa_t *dfa;
-     re_node_set *dest_nodes;
-     const re_node_set *candidates;
-     re_node_set *limits;
-     struct re_backref_cache_entry *bkref_ents;
-     int str_idx;
+internal_function
+check_subexp_limits (re_dfa_t *dfa, re_node_set *dest_nodes,
+                    const re_node_set *candidates, re_node_set *limits,
+                    struct re_backref_cache_entry *bkref_ents, Idx str_idx)
 {
   reg_errcode_t err;
-  int node_idx, lim_idx;
+  Idx node_idx, lim_idx;
 
   for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
     {
-      int subexp_idx;
+      Idx subexp_idx;
       struct re_backref_cache_entry *ent;
       ent = bkref_ents + limits->elems[lim_idx];
 
@@ -2030,11 +2021,11 @@ check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
       subexp_idx = dfa->nodes[ent->node].opr.idx;
       if (ent->subexp_to == str_idx)
        {
-         int ops_node = -1;
-         int cls_node = -1;
+         Idx ops_node = REG_MISSING;
+         Idx cls_node = REG_MISSING;
          for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
            {
-             int node = dest_nodes->elems[node_idx];
+             Idx node = dest_nodes->elems[node_idx];
              re_token_type_t type = dfa->nodes[node].type;
              if (type == OP_OPEN_SUBEXP
                  && subexp_idx == dfa->nodes[node].opr.idx)
@@ -2046,7 +2037,7 @@ check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
 
          /* Check the limitation of the open subexpression.  */
          /* Note that (ent->subexp_to = str_idx != ent->subexp_from).  */
-         if (ops_node >= 0)
+         if (REG_VALID_INDEX (ops_node))
            {
              err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
                                           candidates);
@@ -2055,10 +2046,10 @@ check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
            }
 
          /* Check the limitation of the close subexpression.  */
-         if (cls_node >= 0)
+         if (REG_VALID_INDEX (cls_node))
            for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
              {
-               int node = dest_nodes->elems[node_idx];
+               Idx node = dest_nodes->elems[node_idx];
                if (!re_node_set_contains (dfa->inveclosures + node,
                                           cls_node)
                    && !re_node_set_contains (dfa->eclosures + node,
@@ -2078,7 +2069,7 @@ check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
        {
          for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
            {
-             int node = dest_nodes->elems[node_idx];
+             Idx node = dest_nodes->elems[node_idx];
              re_token_type_t type = dfa->nodes[node].type;
              if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
                {
@@ -2098,26 +2089,24 @@ check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
 }
 
 static reg_errcode_t
-sift_states_bkref (mctx, sctx, str_idx, candidates)
-     re_match_context_t *mctx;
-     re_sift_context_t *sctx;
-     int str_idx;
-     const re_node_set *candidates;
+internal_function
+sift_states_bkref (re_match_context_t *mctx, re_sift_context_t *sctx,
+                  Idx str_idx, const re_node_set *candidates)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int node_idx, node;
+  Idx node_idx, node;
   re_sift_context_t local_sctx;
-  int first_idx = search_cur_bkref_entry (mctx, str_idx);
+  Idx first_idx = search_cur_bkref_entry (mctx, str_idx);
 
-  if (first_idx == -1)
+  if (first_idx == REG_MISSING)
     return REG_NOERROR;
 
   local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized.  */
 
   for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
     {
-      int enabled_idx;
+      Idx enabled_idx;
       re_token_type_t type;
       struct re_backref_cache_entry *entry;
       node = candidates->elems[node_idx];
@@ -2132,7 +2121,7 @@ sift_states_bkref (mctx, sctx, str_idx, candidates)
       enabled_idx = first_idx;
       do
        {
-         int subexp_len, to_idx, dst_node;
+         Idx subexp_len, to_idx, dst_node, ret;
          re_dfastate_t *cur_state;
 
          if (entry->node != node)
@@ -2158,8 +2147,8 @@ sift_states_bkref (mctx, sctx, str_idx, candidates)
            }
          local_sctx.last_node = node;
          local_sctx.last_str_idx = str_idx;
-         err = re_node_set_insert (&local_sctx.limits, enabled_idx);
-         if (BE (err < 0, 0))
+         ret = re_node_set_insert (&local_sctx.limits, enabled_idx);
+         if (BE (ret < 0, 0))
            {
              err = REG_ESPACE;
              goto free_return;
@@ -2197,10 +2186,9 @@ sift_states_bkref (mctx, sctx, str_idx, candidates)
 
 #ifdef RE_ENABLE_I18N
 static int
-sift_states_iter_mb (mctx, sctx, node_idx, str_idx, max_str_idx)
-    const re_match_context_t *mctx;
-    re_sift_context_t *sctx;
-    int node_idx, str_idx, max_str_idx;
+internal_function
+sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
+                    Idx node_idx, Idx str_idx, Idx max_str_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
   int naccepted;
@@ -2228,10 +2216,9 @@ sift_states_iter_mb (mctx, sctx, node_idx, str_idx, max_str_idx)
    update the destination of STATE_LOG.  */
 
 static re_dfastate_t *
-transit_state (err, mctx, state)
-     reg_errcode_t *err;
-     re_match_context_t *mctx;
-     re_dfastate_t *state;
+internal_function
+transit_state (reg_errcode_t *err, re_match_context_t *mctx,
+              re_dfastate_t *state)
 {
   re_dfastate_t **trtable;
   unsigned char ch;
@@ -2287,13 +2274,12 @@ transit_state (err, mctx, state)
 
 /* Update the state_log if we need */
 re_dfastate_t *
-merge_state_with_log (err, mctx, next_state)
-     reg_errcode_t *err;
-     re_match_context_t *mctx;
-     re_dfastate_t *next_state;
+internal_function
+merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
+                     re_dfastate_t *next_state)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int cur_idx = re_string_cur_idx (&mctx->input);
+  Idx cur_idx = re_string_cur_idx (&mctx->input);
 
   if (cur_idx > mctx->state_log_top)
     {
@@ -2366,16 +2352,15 @@ merge_state_with_log (err, mctx, next_state)
 /* Skip bytes in the input that correspond to part of a
    multi-byte match, then look in the log for a state
    from which to restart matching.  */
-re_dfastate_t *
-find_recover_state (err, mctx)
-     reg_errcode_t *err;
-     re_match_context_t *mctx;
+static re_dfastate_t *
+internal_function
+find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
 {
   re_dfastate_t *cur_state = NULL;
   do
     {
-      int max = mctx->state_log_top;
-      int cur_str_idx = re_string_cur_idx (&mctx->input);
+      Idx max = mctx->state_log_top;
+      Idx cur_str_idx = re_string_cur_idx (&mctx->input);
 
       do
        {
@@ -2387,7 +2372,7 @@ find_recover_state (err, mctx)
 
       cur_state = merge_state_with_log (err, mctx, NULL);
     }
-  while (err == REG_NOERROR && cur_state == NULL);
+  while (*err == REG_NOERROR && cur_state == NULL);
   return cur_state;
 }
 
@@ -2399,13 +2384,12 @@ find_recover_state (err, mctx)
    correspoding back references.  */
 
 static reg_errcode_t
-check_subexp_matching_top (mctx, cur_nodes, str_idx)
-     re_match_context_t *mctx;
-     re_node_set *cur_nodes;
-     int str_idx;
+internal_function
+check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
+                          Idx str_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int node_idx;
+  Idx node_idx;
   reg_errcode_t err;
 
   /* TODO: This isn't efficient.
@@ -2415,10 +2399,10 @@ check_subexp_matching_top (mctx, cur_nodes, str_idx)
           E.g. RE: (a){2}  */
   for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
     {
-      int node = cur_nodes->elems[node_idx];
+      Idx node = cur_nodes->elems[node_idx];
       if (dfa->nodes[node].type == OP_OPEN_SUBEXP
-         && dfa->nodes[node].opr.idx < (8 * sizeof (dfa->used_bkref_map))
-         && dfa->used_bkref_map & (1 << dfa->nodes[node].opr.idx))
+         && dfa->nodes[node].opr.idx < CHAR_BIT * sizeof dfa->used_bkref_map
+         && dfa->used_bkref_map & (1u << dfa->nodes[node].opr.idx))
        {
          err = match_ctx_add_subtop (mctx, node, str_idx);
          if (BE (err != REG_NOERROR, 0))
@@ -2433,15 +2417,13 @@ check_subexp_matching_top (mctx, cur_nodes, str_idx)
    accepting the current input byte.  */
 
 static re_dfastate_t *
-transit_state_sb (err, mctx, state)
-     reg_errcode_t *err;
-     re_match_context_t *mctx;
-     re_dfastate_t *state;
+transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
+                 re_dfastate_t *state)
 {
   re_dfa_t *const dfa = mctx->dfa;
   re_node_set next_nodes;
   re_dfastate_t *next_state;
-  int node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
+  Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
   unsigned int context;
 
   *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
@@ -2449,7 +2431,7 @@ transit_state_sb (err, mctx, state)
     return NULL;
   for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
     {
-      int cur_node = state->nodes.elems[node_cnt];
+      Idx cur_node = state->nodes.elems[node_cnt];
       if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
        {
          *err = re_node_set_merge (&next_nodes,
@@ -2474,19 +2456,19 @@ transit_state_sb (err, mctx, state)
 
 #ifdef RE_ENABLE_I18N
 static reg_errcode_t
-transit_state_mb (mctx, pstate)
-    re_match_context_t *mctx;
-    re_dfastate_t *pstate;
+internal_function
+transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int i;
+  Idx i;
 
   for (i = 0; i < pstate->nodes.nelem; ++i)
     {
       re_node_set dest_nodes, *new_nodes;
-      int cur_node_idx = pstate->nodes.elems[i];
-      int naccepted, dest_idx;
+      Idx cur_node_idx = pstate->nodes.elems[i];
+      int naccepted;
+      Idx dest_idx;
       unsigned int context;
       re_dfastate_t *dest_state;
 
@@ -2517,7 +2499,7 @@ transit_state_mb (mctx, pstate)
       if (BE (err != REG_NOERROR, 0))
        return err;
 #ifdef DEBUG
-      assert (dfa->nexts[cur_node_idx] != -1);
+      assert (dfa->nexts[cur_node_idx] != REG_MISSING);
 #endif
       new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
 
@@ -2544,19 +2526,18 @@ transit_state_mb (mctx, pstate)
 #endif /* RE_ENABLE_I18N */
 
 static reg_errcode_t
-transit_state_bkref (mctx, nodes)
-    re_match_context_t *mctx;
-    const re_node_set *nodes;
+internal_function
+transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int i;
-  int cur_str_idx = re_string_cur_idx (&mctx->input);
+  Idx i;
+  Idx cur_str_idx = re_string_cur_idx (&mctx->input);
 
   for (i = 0; i < nodes->nelem; ++i)
     {
-      int dest_str_idx, prev_nelem, bkc_idx;
-      int node_idx = nodes->elems[i];
+      Idx dest_str_idx, prev_nelem, bkc_idx;
+      Idx node_idx = nodes->elems[i];
       unsigned int context;
       const re_token_t *node = dfa->nodes + node_idx;
       re_node_set *new_dest_nodes;
@@ -2583,11 +2564,11 @@ transit_state_bkref (mctx, nodes)
       /* And add the epsilon closures (which is `new_dest_nodes') of
         the backreference to appropriate state_log.  */
 #ifdef DEBUG
-      assert (dfa->nexts[node_idx] != -1);
+      assert (dfa->nexts[node_idx] != REG_MISSING);
 #endif
       for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
        {
-         int subexp_len;
+         Idx subexp_len;
          re_dfastate_t *dest_state;
          struct re_backref_cache_entry *bkref_ent;
          bkref_ent = mctx->bkref_ents + bkc_idx;
@@ -2659,16 +2640,15 @@ transit_state_bkref (mctx, nodes)
    delay these checking for prune_impossible_nodes().  */
 
 static reg_errcode_t
-get_subexp (mctx, bkref_node, bkref_str_idx)
-     re_match_context_t *mctx;
-     int bkref_node, bkref_str_idx;
+internal_function
+get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int subexp_num, sub_top_idx;
+  Idx subexp_num, sub_top_idx;
   const char *buf = (const char *) re_string_get_buffer (&mctx->input);
   /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX.  */
-  int cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
-  if (cache_idx != -1)
+  Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
+  if (cache_idx != REG_MISSING)
     {
       const struct re_backref_cache_entry *entry = mctx->bkref_ents + cache_idx;
       do
@@ -2685,7 +2665,7 @@ get_subexp (mctx, bkref_node, bkref_str_idx)
       reg_errcode_t err;
       re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
       re_sub_match_last_t *sub_last;
-      int sub_last_idx, sl_str, bkref_str_off;
+      Idx sub_last_idx, sl_str, bkref_str_off;
 
       if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
        continue; /* It isn't related.  */
@@ -2696,7 +2676,7 @@ get_subexp (mctx, bkref_node, bkref_str_idx)
         evaluated.  */
       for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
        {
-         int sl_str_diff;
+         regoff_t sl_str_diff;
          sub_last = sub_top->lasts[sub_last_idx];
          sl_str_diff = sub_last->str_idx - sl_str;
          /* The matched string by the sub expression match with the substring
@@ -2741,7 +2721,8 @@ get_subexp (mctx, bkref_node, bkref_str_idx)
       /* Then, search for the other last nodes of the sub expression.  */
       for (; sl_str <= bkref_str_idx; ++sl_str)
        {
-         int cls_node, sl_str_off;
+         Idx cls_node;
+         regoff_t sl_str_off;
          const re_node_set *nodes;
          sl_str_off = sl_str - sub_top->str_idx;
          /* The matched string by the sub expression match with the substring
@@ -2769,12 +2750,12 @@ get_subexp (mctx, bkref_node, bkref_str_idx)
          /* Does this state have a ')' of the sub expression?  */
          nodes = &mctx->state_log[sl_str]->nodes;
          cls_node = find_subexp_node (dfa, nodes, subexp_num, OP_CLOSE_SUBEXP);
-         if (cls_node == -1)
+         if (cls_node == REG_MISSING)
            continue; /* No.  */
          if (sub_top->path == NULL)
            {
-             sub_top->path = calloc (sizeof (state_array_t),
-                                     sl_str - sub_top->str_idx + 1);
+             sub_top->path = re_calloc (state_array_t,
+                                        sl_str - sub_top->str_idx + 1);
              if (sub_top->path == NULL)
                return REG_ESPACE;
            }
@@ -2805,14 +2786,12 @@ get_subexp (mctx, bkref_node, bkref_str_idx)
    and SUB_LAST.  */
 
 static reg_errcode_t
-get_subexp_sub (mctx, sub_top, sub_last, bkref_node, bkref_str)
-     re_match_context_t *mctx;
-     const re_sub_match_top_t *sub_top;
-     re_sub_match_last_t *sub_last;
-     int bkref_node, bkref_str;
+internal_function
+get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
+               re_sub_match_last_t *sub_last, Idx bkref_node, Idx bkref_str)
 {
   reg_errcode_t err;
-  int to_idx;
+  Idx to_idx;
   /* Can the subexpression arrive the back reference?  */
   err = check_arrival (mctx, &sub_last->path, sub_last->node,
                       sub_last->str_idx, bkref_node, bkref_str, OP_OPEN_SUBEXP);
@@ -2834,22 +2813,21 @@ get_subexp_sub (mctx, sub_top, sub_last, bkref_node, bkref_str)
         nodes.
         E.g. RE: (a){2}  */
 
-static int
-find_subexp_node (dfa, nodes, subexp_idx, type)
-     const re_dfa_t *dfa;
-     const re_node_set *nodes;
-     int subexp_idx, type;
+static Idx
+internal_function
+find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
+                 Idx subexp_idx, int type)
 {
-  int cls_idx;
+  Idx cls_idx;
   for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
     {
-      int cls_node = nodes->elems[cls_idx];
+      Idx cls_node = nodes->elems[cls_idx];
       const re_token_t *node = dfa->nodes + cls_node;
       if (node->type == type
          && node->opr.idx == subexp_idx)
        return cls_node;
     }
-  return -1;
+  return REG_MISSING;
 }
 
 /* Check whether the node TOP_NODE at TOP_STR can arrive to the node
@@ -2858,15 +2836,14 @@ find_subexp_node (dfa, nodes, subexp_idx, type)
    Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise.  */
 
 static reg_errcode_t
-check_arrival (mctx, path, top_node, top_str, last_node, last_str,
-              type)
-     re_match_context_t *mctx;
-     state_array_t *path;
-     int top_node, top_str, last_node, last_str, type;
+internal_function
+check_arrival (re_match_context_t *mctx, state_array_t *path,
+              Idx top_node, Idx top_str, Idx last_node, Idx last_str,
+              int type)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int subexp_num, backup_cur_idx, str_idx, null_cnt;
+  Idx subexp_num, backup_cur_idx, str_idx, null_cnt;
   re_dfastate_t *cur_state = NULL;
   re_node_set *cur_nodes, next_nodes;
   re_dfastate_t **backup_state_log;
@@ -2877,7 +2854,7 @@ check_arrival (mctx, path, top_node, top_str, last_node, last_str,
   if (BE (path->alloc < last_str + mctx->max_mb_elem_len + 1, 0))
     {
       re_dfastate_t **new_array;
-      int old_alloc = path->alloc;
+      Idx old_alloc = path->alloc;
       path->alloc += last_str + mctx->max_mb_elem_len + 1;
       new_array = re_realloc (path->array, re_dfastate_t *, path->alloc);
       if (new_array == NULL)
@@ -3020,21 +2997,21 @@ check_arrival (mctx, path, top_node, top_str, last_node, last_str,
         Can't we unify them?  */
 
 static reg_errcode_t
-check_arrival_add_next_nodes (mctx, str_idx, cur_nodes, next_nodes)
-     re_match_context_t *mctx;
-     int str_idx;
-     re_node_set *cur_nodes, *next_nodes;
+internal_function
+check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
+                             re_node_set *cur_nodes,
+                             re_node_set *next_nodes)
 {
   re_dfa_t *const dfa = mctx->dfa;
   int result;
-  int cur_idx;
+  Idx cur_idx;
   reg_errcode_t err;
   re_node_set union_set;
   re_node_set_init_empty (&union_set);
   for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
     {
       int naccepted = 0;
-      int cur_node = cur_nodes->elems[cur_idx];
+      Idx cur_node = cur_nodes->elems[cur_idx];
 #ifdef DEBUG
       re_token_type_t type = dfa->nodes[cur_node].type;
       assert (!IS_EPSILON_NODE (type));
@@ -3048,8 +3025,8 @@ check_arrival_add_next_nodes (mctx, str_idx, cur_nodes, next_nodes)
          if (naccepted > 1)
            {
              re_dfastate_t *dest_state;
-             int next_node = dfa->nexts[cur_node];
-             int next_idx = str_idx + naccepted;
+             Idx next_node = dfa->nexts[cur_node];
+             Idx next_idx = str_idx + naccepted;
              dest_state = mctx->state_log[next_idx];
              re_node_set_empty (&union_set);
              if (dest_state)
@@ -3100,13 +3077,12 @@ check_arrival_add_next_nodes (mctx, str_idx, cur_nodes, next_nodes)
 */
 
 static reg_errcode_t
-check_arrival_expand_ecl (dfa, cur_nodes, ex_subexp, type)
-     re_dfa_t *dfa;
-     re_node_set *cur_nodes;
-     int ex_subexp, type;
+internal_function
+check_arrival_expand_ecl (re_dfa_t *dfa, re_node_set *cur_nodes,
+                         Idx ex_subexp, int type)
 {
   reg_errcode_t err;
-  int idx, outside_node;
+  Idx idx, outside_node;
   re_node_set new_nodes;
 #ifdef DEBUG
   assert (cur_nodes->nelem);
@@ -3119,10 +3095,10 @@ check_arrival_expand_ecl (dfa, cur_nodes, ex_subexp, type)
 
   for (idx = 0; idx < cur_nodes->nelem; ++idx)
     {
-      int cur_node = cur_nodes->elems[idx];
+      Idx cur_node = cur_nodes->elems[idx];
       re_node_set *eclosure = dfa->eclosures + cur_node;
       outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
-      if (outside_node == -1)
+      if (outside_node == REG_MISSING)
        {
          /* There are no problematic nodes, just merge them.  */
          err = re_node_set_merge (&new_nodes, eclosure);
@@ -3154,12 +3130,11 @@ check_arrival_expand_ecl (dfa, cur_nodes, ex_subexp, type)
    problematic append it to DST_NODES.  */
 
 static reg_errcode_t
-check_arrival_expand_ecl_sub (dfa, dst_nodes, target, ex_subexp, type)
-     re_dfa_t *dfa;
-     int target, ex_subexp, type;
-     re_node_set *dst_nodes;
+internal_function
+check_arrival_expand_ecl_sub (re_dfa_t *dfa, re_node_set *dst_nodes,
+                             Idx target, Idx ex_subexp, int type)
 {
-  int cur_node;
+  Idx cur_node;
   for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
     {
       int err;
@@ -3182,11 +3157,12 @@ check_arrival_expand_ecl_sub (dfa, dst_nodes, target, ex_subexp, type)
        break;
       if (dfa->edests[cur_node].nelem == 2)
        {
-         err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
-                                             dfa->edests[cur_node].elems[1],
-                                             ex_subexp, type);
-         if (BE (err != REG_NOERROR, 0))
-           return err;
+         reg_errcode_t ret =
+           check_arrival_expand_ecl_sub (dfa, dst_nodes,
+                                         dfa->edests[cur_node].elems[1],
+                                         ex_subexp, type);
+         if (BE (ret != REG_NOERROR, 0))
+           return ret;
        }
       cur_node = dfa->edests[cur_node].elems[0];
     }
@@ -3199,25 +3175,23 @@ check_arrival_expand_ecl_sub (dfa, dst_nodes, target, ex_subexp, type)
    in MCTX->BKREF_ENTS.  */
 
 static reg_errcode_t
-expand_bkref_cache (mctx, cur_nodes, cur_str, subexp_num,
-                   type)
-     re_match_context_t *mctx;
-     int cur_str, subexp_num, type;
-     re_node_set *cur_nodes;
+internal_function
+expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
+                   Idx cur_str, Idx subexp_num, int type)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
+  Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
   struct re_backref_cache_entry *ent;
 
-  if (cache_idx_start == -1)
+  if (cache_idx_start == REG_MISSING)
     return REG_NOERROR;
 
  restart:
   ent = mctx->bkref_ents + cache_idx_start;
   do
     {
-      int to_idx, next_node;
+      Idx to_idx, next_node;
 
       /* Is this entry ENT is appropriate?  */
       if (!re_node_set_contains (cur_nodes, ent->node))
@@ -3290,15 +3264,15 @@ expand_bkref_cache (mctx, cur_nodes, cur_str, subexp_num,
    Return 1 if succeeded, otherwise return NULL.  */
 
 static int
-build_trtable (dfa, state)
-    re_dfa_t *dfa;
-    re_dfastate_t *state;
+internal_function
+build_trtable (re_dfa_t *dfa, re_dfastate_t *state)
 {
   reg_errcode_t err;
-  int i, j, ch, need_word_trtable = 0;
+  Idx i, j;
+  int ch, need_word_trtable = 0;
   unsigned int elem, mask;
   int dests_node_malloced = 0, dest_states_malloced = 0;
-  int ndests; /* Number of the destination states from `state'.  */
+  Idx ndests; /* Number of the destination states from `state'.  */
   re_dfastate_t **trtable;
   re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
   re_node_set follows, *dests_node;
@@ -3309,12 +3283,10 @@ build_trtable (dfa, state)
      from `state'.  `dests_node[i]' represents the nodes which i-th
      destination state contains, and `dests_ch[i]' represents the
      characters which i-th destination state accepts.  */
-#ifdef _LIBC
   if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX))
     dests_node = (re_node_set *)
       alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
   else
-#endif
     {
       dests_node = (re_node_set *)
        malloc ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
@@ -3330,15 +3302,14 @@ build_trtable (dfa, state)
   /* At first, group all nodes belonging to `state' into several
      destinations.  */
   ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
-  if (BE (ndests <= 0, 0))
+  if (BE (! REG_VALID_NONZERO_INDEX (ndests), 0))
     {
       if (dests_node_malloced)
        free (dests_node);
       /* Return 0 in case of an error, 1 otherwise.  */
       if (ndests == 0)
        {
-         state->trtable = (re_dfastate_t **)
-           calloc (sizeof (re_dfastate_t *), SBC_MAX);
+         state->trtable = re_calloc (re_dfastate_t *, SBC_MAX);
          return 1;
        }
       return 0;
@@ -3348,13 +3319,11 @@ build_trtable (dfa, state)
   if (BE (err != REG_NOERROR, 0))
     goto out_free;
 
-#ifdef _LIBC
   if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX
                         + ndests * 3 * sizeof (re_dfastate_t *)))
     dest_states = (re_dfastate_t **)
       alloca (ndests * 3 * sizeof (re_dfastate_t *));
   else
-#endif
     {
       dest_states = (re_dfastate_t **)
        malloc (ndests * 3 * sizeof (re_dfastate_t *));
@@ -3379,13 +3348,13 @@ out_free:
   /* Then build the states for all destinations.  */
   for (i = 0; i < ndests; ++i)
     {
-      int next_node;
+      Idx next_node;
       re_node_set_empty (&follows);
       /* Merge the follows of this destination states.  */
       for (j = 0; j < dests_node[i].nelem; ++j)
        {
          next_node = dfa->nexts[dests_node[i].elems[j]];
-         if (next_node != -1)
+         if (next_node != REG_MISSING)
            {
              err = re_node_set_merge (&follows, dfa->eclosures + next_node);
              if (BE (err != REG_NOERROR, 0))
@@ -3426,8 +3395,7 @@ out_free:
         character, or we are in a single-byte character set so we can
         discern by looking at the character code: allocate a
         256-entry transition table.  */
-      trtable = state->trtable =
-       (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
+      trtable = state->trtable = re_calloc (re_dfastate_t *, SBC_MAX);
       if (BE (trtable == NULL, 0))
        goto out_free;
 
@@ -3457,8 +3425,7 @@ out_free:
         by looking at the character code: build two 256-entry
         transition tables, one starting at trtable[0] and one
         starting at trtable[SBC_MAX].  */
-      trtable = state->word_trtable =
-       (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
+      trtable = state->word_trtable = re_calloc (re_dfastate_t *, 2 * SBC_MAX);
       if (BE (trtable == NULL, 0))
        goto out_free;
 
@@ -3515,17 +3482,15 @@ out_free:
    to DESTS_NODE[i] and set the characters accepted by the destination
    to DEST_CH[i].  This function return the number of destinations.  */
 
-static int
-group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch)
-    re_dfa_t *dfa;
-    const re_dfastate_t *state;
-    re_node_set *dests_node;
-    bitset *dests_ch;
+static Idx
+internal_function
+group_nodes_into_DFAstates (re_dfa_t *dfa, const re_dfastate_t *state,
+                           re_node_set *dests_node, bitset *dests_ch)
 {
   reg_errcode_t err;
   int result;
-  int i, j, k;
-  int ndests; /* Number of the destinations from `state'.  */
+  Idx i, j, k;
+  Idx ndests; /* Number of the destinations from `state'.  */
   bitset accepts; /* Characters a node can accept.  */
   const re_node_set *cur_nodes = &state->nodes;
   bitset_empty (accepts);
@@ -3553,18 +3518,18 @@ group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch)
          else
 #endif
            bitset_set_all (accepts);
-         if (!(dfa->syntax & RE_DOT_NEWLINE))
+         if (!(dfa->syntax & REG_DOT_NEWLINE))
            bitset_clear (accepts, '\n');
-         if (dfa->syntax & RE_DOT_NOT_NULL)
+         if (dfa->syntax & REG_DOT_NOT_NULL)
            bitset_clear (accepts, '\0');
        }
 #ifdef RE_ENABLE_I18N
       else if (type == OP_UTF8_PERIOD)
         {
          memset (accepts, 255, sizeof (unsigned int) * BITSET_UINTS / 2);
-         if (!(dfa->syntax & RE_DOT_NEWLINE))
+         if (!(dfa->syntax & REG_DOT_NEWLINE))
            bitset_clear (accepts, '\n');
-         if (dfa->syntax & RE_DOT_NOT_NULL)
+         if (dfa->syntax & REG_DOT_NOT_NULL)
            bitset_clear (accepts, '\0');
         }
 #endif
@@ -3695,7 +3660,7 @@ group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch)
  error_return:
   for (j = 0; j < ndests; ++j)
     re_node_set_free (dests_node + j);
-  return -1;
+  return REG_MISSING;
 }
 
 #ifdef RE_ENABLE_I18N
@@ -3708,14 +3673,13 @@ group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch)
    can only accept one byte.  */
 
 static int
-check_node_accept_bytes (dfa, node_idx, input, str_idx)
-    re_dfa_t *dfa;
-    int node_idx, str_idx;
-    const re_string_t *input;
+internal_function
+check_node_accept_bytes (re_dfa_t *dfa, Idx node_idx,
+                        const re_string_t *input, Idx str_idx)
 {
   const re_token_t *node = dfa->nodes + node_idx;
   int char_len, elem_len;
-  int i;
+  Idx i;
 
   if (BE (node->type == OP_UTF8_PERIOD, 0))
     {
@@ -3776,9 +3740,9 @@ check_node_accept_bytes (dfa, node_idx, input, str_idx)
       /* FIXME: I don't think this if is needed, as both '\n'
         and '\0' are char_len == 1.  */
       /* '.' accepts any one character except the following two cases.  */
-      if ((!(dfa->syntax & RE_DOT_NEWLINE) &&
+      if ((!(dfa->syntax & REG_DOT_NEWLINE) &&
           re_string_byte_at (input, str_idx) == '\n') ||
-         ((dfa->syntax & RE_DOT_NOT_NULL) &&
+         ((dfa->syntax & REG_DOT_NOT_NULL) &&
           re_string_byte_at (input, str_idx) == '\0'))
        return 0;
       return char_len;
@@ -3794,7 +3758,7 @@ check_node_accept_bytes (dfa, node_idx, input, str_idx)
 # ifdef _LIBC
       const unsigned char *pin
        = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
-      int j;
+      Idx j;
       uint32_t nrules;
 # endif /* _LIBC */
       int match_len = 0;
@@ -3893,7 +3857,7 @@ check_node_accept_bytes (dfa, node_idx, input, str_idx)
                    size_t weight_len = weights[idx];
                    if (weight_len == weights[equiv_class_idx])
                      {
-                       int cnt = 0;
+                       Idx cnt = 0;
                        while (cnt <= weight_len
                               && (weights[equiv_class_idx + 1 + cnt]
                                   == weights[idx + 1 + cnt]))
@@ -3945,9 +3909,7 @@ check_node_accept_bytes (dfa, node_idx, input, str_idx)
 
 # ifdef _LIBC
 static unsigned int
-find_collation_sequence_value (mbs, mbs_len)
-    const unsigned char *mbs;
-    size_t mbs_len;
+find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
 {
   uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
   if (nrules == 0)
@@ -4009,10 +3971,9 @@ find_collation_sequence_value (mbs, mbs_len)
    byte of the INPUT.  */
 
 static int
-check_node_accept (mctx, node, idx)
-    const re_match_context_t *mctx;
-    const re_token_t *node;
-    int idx;
+internal_function
+check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
+                  Idx idx)
 {
   unsigned char ch;
   ch = re_string_byte_at (&mctx->input, idx);
@@ -4035,8 +3996,8 @@ check_node_accept (mctx, node, idx)
       /* FALLTHROUGH */
 #endif
     case OP_PERIOD:
-      if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
-         || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
+      if ((ch == '\n' && !(mctx->dfa->syntax & REG_DOT_NEWLINE))
+         || (ch == '\0' && (mctx->dfa->syntax & REG_DOT_NOT_NULL)))
        return 0;
       break;
 
@@ -4060,8 +4021,8 @@ check_node_accept (mctx, node, idx)
 /* Extend the buffers, if the buffers have run out.  */
 
 static reg_errcode_t
-extend_buffers (mctx)
-     re_match_context_t *mctx;
+internal_function
+extend_buffers (re_match_context_t *mctx)
 {
   reg_errcode_t ret;
   re_string_t *pstr = &mctx->input;
@@ -4119,12 +4080,11 @@ extend_buffers (mctx)
 /* Initialize MCTX.  */
 
 static reg_errcode_t
-match_ctx_init (mctx, eflags, n)
-    re_match_context_t *mctx;
-    int eflags, n;
+internal_function
+match_ctx_init (re_match_context_t *mctx, int eflags, Idx n)
 {
   mctx->eflags = eflags;
-  mctx->match_last = -1;
+  mctx->match_last = REG_MISSING;
   if (n > 0)
     {
       mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
@@ -4148,13 +4108,13 @@ match_ctx_init (mctx, eflags, n)
    of the input, or changes the input string.  */
 
 static void
-match_ctx_clean (mctx)
-    re_match_context_t *mctx;
+internal_function
+match_ctx_clean (re_match_context_t *mctx)
 {
-  int st_idx;
+  Idx st_idx;
   for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
     {
-      int sl_idx;
+      Idx sl_idx;
       re_sub_match_top_t *top = mctx->sub_tops[st_idx];
       for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
        {
@@ -4178,8 +4138,8 @@ match_ctx_clean (mctx)
 /* Free all the memory associated with MCTX.  */
 
 static void
-match_ctx_free (mctx)
-    re_match_context_t *mctx;
+internal_function
+match_ctx_free (re_match_context_t *mctx)
 {
   /* First, free all the memory associated with MCTX->SUB_TOPS.  */
   match_ctx_clean (mctx);
@@ -4193,9 +4153,9 @@ match_ctx_free (mctx)
 */
 
 static reg_errcode_t
-match_ctx_add_entry (mctx, node, str_idx, from, to)
-     re_match_context_t *mctx;
-     int node, str_idx, from, to;
+internal_function
+match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx,
+                    Idx from, Idx to)
 {
   if (mctx->nbkref_ents >= mctx->abkref_ents)
     {
@@ -4230,7 +4190,7 @@ match_ctx_add_entry (mctx, node, str_idx, from, to)
      A backreference does not epsilon-transition unless it is empty, so set
      to all zeros if FROM != TO.  */
   mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
-    = (from == to ? ~0 : 0);
+    = (from == to ? -1 : 0);
 
   mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
   if (mctx->max_mb_elem_len < to - from)
@@ -4238,15 +4198,14 @@ match_ctx_add_entry (mctx, node, str_idx, from, to)
   return REG_NOERROR;
 }
 
-/* Search for the first entry which has the same str_idx, or -1 if none is
+/* Return the first entry with the same str_idx, or REG_MISSING if none is
    found.  Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX.  */
 
-static int
-search_cur_bkref_entry (mctx, str_idx)
-     re_match_context_t *mctx;
-     int str_idx;
+static Idx
+internal_function
+search_cur_bkref_entry (re_match_context_t *mctx, Idx str_idx)
 {
-  int left, right, mid, last;
+  Idx left, right, mid, last;
   last = right = mctx->nbkref_ents;
   for (left = 0; left < right;)
     {
@@ -4259,16 +4218,15 @@ search_cur_bkref_entry (mctx, str_idx)
   if (left < last && mctx->bkref_ents[left].str_idx == str_idx)
     return left;
   else
-    return -1;
+    return REG_MISSING;
 }
 
 /* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
    at STR_IDX.  */
 
 static reg_errcode_t
-match_ctx_add_subtop (mctx, node, str_idx)
-     re_match_context_t *mctx;
-     int node, str_idx;
+internal_function
+match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
 {
 #ifdef DEBUG
   assert (mctx->sub_tops != NULL);
@@ -4276,7 +4234,7 @@ match_ctx_add_subtop (mctx, node, str_idx)
 #endif
   if (BE (mctx->nsub_tops == mctx->asub_tops, 0))
     {
-      int new_asub_tops = mctx->asub_tops * 2;
+      Idx new_asub_tops = mctx->asub_tops * 2;
       re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
                                                   re_sub_match_top_t *,
                                                   new_asub_tops);
@@ -4285,7 +4243,7 @@ match_ctx_add_subtop (mctx, node, str_idx)
       mctx->sub_tops = new_array;
       mctx->asub_tops = new_asub_tops;
     }
-  mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
+  mctx->sub_tops[mctx->nsub_tops] = re_calloc (re_sub_match_top_t, 1);
   if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0))
     return REG_ESPACE;
   mctx->sub_tops[mctx->nsub_tops]->node = node;
@@ -4297,14 +4255,13 @@ match_ctx_add_subtop (mctx, node, str_idx)
    at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP.  */
 
 static re_sub_match_last_t *
-match_ctx_add_sublast (subtop, node, str_idx)
-     re_sub_match_top_t *subtop;
-     int node, str_idx;
+internal_function
+match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx)
 {
   re_sub_match_last_t *new_entry;
   if (BE (subtop->nlasts == subtop->alasts, 0))
     {
-      int new_alasts = 2 * subtop->alasts + 1;
+      Idx new_alasts = 2 * subtop->alasts + 1;
       re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
                                                    re_sub_match_last_t *,
                                                    new_alasts);
@@ -4313,7 +4270,7 @@ match_ctx_add_sublast (subtop, node, str_idx)
       subtop->lasts = new_array;
       subtop->alasts = new_alasts;
     }
-  new_entry = calloc (1, sizeof (re_sub_match_last_t));
+  new_entry = re_calloc (re_sub_match_last_t, 1);
   if (BE (new_entry != NULL, 1))
     {
       subtop->lasts[subtop->nlasts] = new_entry;
@@ -4325,10 +4282,11 @@ match_ctx_add_sublast (subtop, node, str_idx)
 }
 
 static void
-sift_ctx_init (sctx, sifted_sts, limited_sts, last_node, last_str_idx)
-    re_sift_context_t *sctx;
-    re_dfastate_t **sifted_sts, **limited_sts;
-    int last_node, last_str_idx;
+internal_function
+sift_ctx_init (re_sift_context_t *sctx,
+              re_dfastate_t **sifted_sts,
+              re_dfastate_t **limited_sts,
+              Idx last_node, Idx last_str_idx)
 {
   sctx->sifted_states = sifted_sts;
   sctx->limited_states = limited_sts;