Skip to content

Regular expressions

A pattern compiled from a bytes literal is matched natively over a byte buffer. The compiler reads the pattern with CPython's own parser and writes a matcher function for it, and a function that searches with it lowers like any other.

Run it

The input is 50,000 generated lines of name = value pairs, words, and hex numbers. The line starting with # is a timing; it differs between machines and between the two runs below.

python  patterns.ppy
ppy run patterns.ppy
ppy emit ir patterns.ppy
ppy emit c patterns.ppy

What it prints

python patterns.ppy

989986 bytes
119454 7 25007528 7143
True False
# four passes: 75.5 ms

ppy run patterns.ppy

989986 bytes
119454 7 25007528 7143
True False
# four passes: 9.7 ms

ppy emit ir patterns.ppy

1520 lines
ppyir 1
module @patterns
dialect core 1
dialect regex 1

func @patterns_count_words(%text: buffer<u8> {ownership = "borrowed", ppy.kind = "view"}) -> i64 attrs {effects = ["alloc", "may_raise", "read_memory"], ppy.abi = "ppy", ppy.qualname = "patterns.count_words", ppy.releases_gil = true, ppy.symbol = "ppy_patterns_count_words"} loc("examples/43_regex/patterns.ppy":15:0) {
^entry:
    %0 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":16:4)
    %n_addr = core.alloca : ptr<i64, stack>
    core.store %0, %n_addr
    %1 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":17:4)
    %pos_addr = core.alloca : ptr<i64, stack>
    core.store %1, %pos_addr
    %m_found_addr = core.alloca : ptr<bool, stack>
    %m_spans = core.alloca {count = 2} : ptr<i64, stack>
    %2 = core.const 0 : i64
    %3 = core.const 1 : i64
    %4 = core.const 1 : i64
    %5 = core.const 1 : i64
    core.br ^while.head1 loc("examples/43_regex/patterns.ppy":18:4)
^while.head1:
    %6 = core.load %pos_addr : i64 loc("examples/43_regex/patterns.ppy":19:8)
    %7 = core.buffer_len %text : index
    %8 = core.cast %7 : i64
    %9, %10, %11 = core.call %text, %6, %8 {callee = @ppy.regex.0} : bool, i64, i64
    core.store %9, %m_found_addr loc("examples/43_regex/patterns.ppy":19:8)
    %12 = core.ptr_offset %m_spans, %2 : ptr<i64, stack>
    core.store %10, %12
    %13 = core.ptr_offset %m_spans, %3 : ptr<i64, stack>
    core.store %11, %13
    %14 = core.load %m_found_addr : bool loc("examples/43_regex/patterns.ppy":20:8)
    %15 = core.const false : bool
    %16 = core.cmp.eq %14, %15 : bool
    core.cond_br %16, ^then4, ^else5
^then4:
    %17 = core.load %n_addr : i64 loc("examples/43_regex/patterns.ppy":21:12)
    core.ret %17
^else5:
    %18 = core.load %n_addr : i64 loc("examples/43_regex/patterns.ppy":22:8)
    %19 = core.add %18, %4 {overflow = "python"} : i64
    core.store %19, %n_addr
    %20 = core.ptr_offset %m_spans, %5 : ptr<i64, stack> loc("examples/43_regex/patterns.ppy":23:8)
    %21 = core.load %20 : i64
    core.store %21, %pos_addr
    core.br ^while.head1
}

func @patterns_longest_word(%text: buffer<u8> {ownership = "borrowed", ppy.kind = "view"}) -> i64 attrs {effects = ["alloc", "may_raise", "read_memory"], ppy.abi = "ppy", ppy.qualname = "patterns.longest_word", ppy.releases_gil = true, ppy.symbol = "ppy_patterns_longest_word"} loc("examples/43_regex/patterns.ppy":26:0) {
^entry:
    %0 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":27:4)
    %best_addr = core.alloca : ptr<i64, stack>
    core.store %0, %best_addr
    %1 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":28:4)
    %pos_addr = core.alloca : ptr<i64, stack>
    core.store %1, %pos_addr
    %m_found_addr = core.alloca : ptr<bool, stack>
    %m_spans = core.alloca {count = 2} : ptr<i64, stack>
    %2 = core.const 0 : i64
    %3 = core.const 1 : i64
    %4 = core.const 0 : i64
    %5 = core.const 1 : i64
    %start_addr = core.alloca : ptr<i64, stack>
    %end_addr = core.alloca : ptr<i64, stack>
    core.br ^while.head1 loc("examples/43_regex/patterns.ppy":29:4)
^while.head1:
    %6 = core.load %pos_addr : i64 loc("examples/43_regex/patterns.ppy":29:4)
    %7 = core.buffer_len %text : index
    %8 = core.cast %7 : i64
    %9 = core.cmp.lt %6, %8 : bool
    core.cond_br %9, ^while.body2, ^while.end3
^while.body2:
    %10 = core.load %pos_addr : i64 loc("examples/43_regex/patterns.ppy":30:8)
    %11 = core.buffer_len %text : index
    %12 = core.cast %11 : i64
    %13, %14, %15 = core.call %text, %10, %12 {callee = @ppy.regex.0} : bool, i64, i64
    core.store %13, %m_found_addr loc("examples/43_regex/patterns.ppy":30:8)
    %16 = core.ptr_offset %m_spans, %2 : ptr<i64, stack>
    core.store %14, %16
    %17 = core.ptr_offset %m_spans, %3 : ptr<i64, stack>
    core.store %15, %17
    %18 = core.load %m_found_addr : bool loc("examples/43_regex/patterns.ppy":31:8)
    %19 = core.const true : bool
    %20 = core.xor %18, %19 : bool
    core.cond_br %20, ^then4, ^else5
^while.end3:
    %21 = core.load %best_addr : i64 loc("examples/43_regex/patterns.ppy":36:4)
    core.ret %21
^then4:
    core.br ^while.end3 loc("examples/43_regex/patterns.ppy":32:12)
^else5:
    %22 = core.ptr_offset %m_spans, %4 : ptr<i64, stack> loc("examples/43_regex/patterns.ppy":33:8)
    %23 = core.load %22 : i64
    %24 = core.ptr_offset %m_spans, %5 : ptr<i64, stack>
    %25 = core.load %24 : i64
    core.store %23, %start_addr
    core.store %25, %end_addr
    %26 = core.load %best_addr : i64 loc("examples/43_regex/patterns.ppy":34:8)
    %27 = core.load %end_addr : i64
    %28 = core.load %start_addr : i64
    %29 = core.sub %27, %28 {overflow = "python"} : i64
    %30 = core.cmp.gt %29, %26 : bool
    %31 = core.select %30, %29, %26 : i64
    core.store %31, %best_addr
    %32 = core.load %end_addr : i64 loc("examples/43_regex/patterns.ppy":35:8)
    core.store %32, %pos_addr
    core.br ^while.head1
}

func @patterns_sum_values(%text: buffer<u8> {ownership = "borrowed", ppy.kind = "view"}) -> i64 attrs {effects = ["alloc", "may_raise", "read_memory"], ppy.abi = "ppy", ppy.qualname = "patterns.sum_values", ppy.releases_gil = true, ppy.symbol = "ppy_patterns_sum_values"} loc("examples/43_regex/patterns.ppy":39:0) {
^entry:
    %0 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":40:4)
    %total_addr = core.alloca : ptr<i64, stack>
    core.store %0, %total_addr
    %1 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":41:4)
    %pos_addr = core.alloca : ptr<i64, stack>
    core.store %1, %pos_addr
    %m_found_addr = core.alloca : ptr<bool, stack>
    %m_spans = core.alloca {count = 6} : ptr<i64, stack>
    %2 = core.const 0 : i64
    %3 = core.const 1 : i64
    %4 = core.const 2 : i64
    %5 = core.const 3 : i64
    %6 = core.const 4 : i64
    %7 = core.const 5 : i64
    %8 = core.const 0 : i64
    %value_addr = core.alloca : ptr<i64, stack>
    %9 = core.const 4 : i64
    %10 = core.const 5 : i64
    %11 = core.const 1 : i64
    %i_addr = core.alloca : ptr<i64, stack>
    %12 = core.const 10 : i64
    %13 = core.const 48 : i64
    %14 = core.const 1 : i64
    core.br ^while.head1 loc("examples/43_regex/patterns.ppy":42:4)
^while.head1:
    %15 = core.load %pos_addr : i64 loc("examples/43_regex/patterns.ppy":43:8)
    %16 = core.buffer_len %text : index
    %17 = core.cast %16 : i64
    %18, %19, %20, %21, %22, %23, %24 = core.call %text, %15, %17 {callee = @ppy.regex.1} : bool, i64, i64, i64, i64, i64, i64
    core.store %18, %m_found_addr loc("examples/43_regex/patterns.ppy":43:8)
    %25 = core.ptr_offset %m_spans, %2 : ptr<i64, stack>
    core.store %19, %25
    %26 = core.ptr_offset %m_spans, %3 : ptr<i64, stack>
    core.store %20, %26
    %27 = core.ptr_offset %m_spans, %4 : ptr<i64, stack>
    core.store %21, %27
    %28 = core.ptr_offset %m_spans, %5 : ptr<i64, stack>
    core.store %22, %28
    %29 = core.ptr_offset %m_spans, %6 : ptr<i64, stack>
    core.store %23, %29
    %30 = core.ptr_offset %m_spans, %7 : ptr<i64, stack>
    core.store %24, %30
    %31 = core.load %m_found_addr : bool loc("examples/43_regex/patterns.ppy":44:8)
    %32 = core.const false : bool
    %33 = core.cmp.eq %31, %32 : bool
    core.cond_br %33, ^then4, ^else5
^then4:
    %34 = core.load %total_addr : i64 loc("examples/43_regex/patterns.ppy":45:12)
    core.ret %34
^else5:
    core.store %8, %value_addr loc("examples/43_regex/patterns.ppy":46:8)
    %35 = core.ptr_offset %m_spans, %9 : ptr<i64, stack> loc("examples/43_regex/patterns.ppy":47:8)
    %36 = core.load %35 : i64
    %37 = core.ptr_offset %m_spans, %10 : ptr<i64, stack>
    %38 = core.load %37 : i64
    %39 = core.const 1 : i64
    %40 = core.sub %38, %39 {overflow = "wrap"} : i64
    %41 = core.const 0 : i64
    %42 = core.buffer_len %text : index
    %43 = core.cast %42 : i64
    %44 = core.cmp.ge %36, %41 : bool
    %45 = core.cmp.lt %40, %43 : bool
    %46 = core.and %44, %45 : bool
    core.guard %46 {kind = "bounds", message = "hoisted bounds check", raises = "IndexError: array index out of range"}
    core.store %36, %i_addr loc("examples/43_regex/patterns.ppy":47:8)
    core.br ^for.head9
^for.head9:
    %47 = core.load %i_addr : i64 loc("examples/43_regex/patterns.ppy":47:8)
    %48 = core.cmp.lt %47, %38 : bool
    core.cond_br %48, ^for.body10, ^for.end12
^for.body10:
    %49 = core.load %value_addr : i64 loc("examples/43_regex/patterns.ppy":48:12)
    %50 = core.mul %49, %12 {overflow = "python"} : i64
    %51 = core.load %i_addr : i64
    %52 = core.buffer_load %text, %51 : u8
    %53 = core.cast %52 : i64
    %54 = core.sub %53, %13 {overflow = "python"} : i64
    %55 = core.add %50, %54 {overflow = "python"} : i64
    core.store %55, %value_addr
    %56 = core.load %i_addr : i64
    %57 = core.add %56, %11 {overflow = "proven"} : i64
    core.store %57, %i_addr
    core.br ^for.head9
^for.end12:
    %58 = core.load %total_addr : i64 loc("examples/43_regex/patterns.ppy":49:8)
    %59 = core.load %value_addr : i64
    %60 = core.add %58, %59 {overflow = "python"} : i64
    core.store %60, %total_addr
    %61 = core.ptr_offset %m_spans, %14 : ptr<i64, stack> loc("examples/43_regex/patterns.ppy":50:8)
    %62 = core.load %61 : i64
    core.store %62, %pos_addr
    core.br ^while.head1
}

func @patterns_count_hex(%text: buffer<u8> {ownership = "borrowed", ppy.kind = "view"}) -> i64 attrs {effects = ["alloc", "may_raise", "read_memory"], ppy.abi = "ppy", ppy.qualname = "patterns.count_hex", ppy.releases_gil = true, ppy.symbol = "ppy_patterns_count_hex"} loc("examples/43_regex/patterns.ppy":53:0) {
^entry:
    %0 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":54:4)
    %n_addr = core.alloca : ptr<i64, stack>
    core.store %0, %n_addr
    %1 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":55:4)
    %pos_addr = core.alloca : ptr<i64, stack>
    core.store %1, %pos_addr
    %m_found_addr = core.alloca : ptr<bool, stack>
    %m_spans = core.alloca {count = 2} : ptr<i64, stack>
    %2 = core.const 0 : i64
    %3 = core.const 1 : i64
    %4 = core.const 1 : i64
    %5 = core.const 1 : i64
    core.br ^while.head1 loc("examples/43_regex/patterns.ppy":56:4)
^while.head1:
    %6 = core.load %pos_addr : i64 loc("examples/43_regex/patterns.ppy":57:8)
    %7 = core.buffer_len %text : index
    %8 = core.cast %7 : i64
    %9, %10, %11 = core.call %text, %6, %8 {callee = @ppy.regex.2} : bool, i64, i64
    core.store %9, %m_found_addr loc("examples/43_regex/patterns.ppy":57:8)
    %12 = core.ptr_offset %m_spans, %2 : ptr<i64, stack>
    core.store %10, %12
    %13 = core.ptr_offset %m_spans, %3 : ptr<i64, stack>
    core.store %11, %13
    %14 = core.load %m_found_addr : bool loc("examples/43_regex/patterns.ppy":58:8)
    %15 = core.const false : bool
    %16 = core.cmp.eq %14, %15 : bool
    core.cond_br %16, ^then4, ^else5
^then4:
    %17 = core.load %n_addr : i64 loc("examples/43_regex/patterns.ppy":59:12)
    core.ret %17
^else5:
    %18 = core.load %n_addr : i64 loc("examples/43_regex/patterns.ppy":60:8)
    %19 = core.add %18, %4 {overflow = "python"} : i64
    core.store %19, %n_addr
    %20 = core.ptr_offset %m_spans, %5 : ptr<i64, stack> loc("examples/43_regex/patterns.ppy":61:8)
    %21 = core.load %20 : i64
    core.store %21, %pos_addr
    core.br ^while.head1
}

func @patterns_is_identifier(%text: buffer<u8> {ownership = "borrowed", ppy.kind = "view"}) -> bool attrs {effects = ["alloc", "may_raise", "read_memory"], ppy.abi = "ppy", ppy.qualname = "patterns.is_identifier", ppy.releases_gil = true, ppy.symbol = "ppy_patterns_is_identifier"} loc("examples/43_regex/patterns.ppy":64:0) {
^entry:
    %0 = core.const 0 : i64 loc("examples/43_regex/patterns.ppy":65:4)
    %1 = core.buffer_len %text : index
    %2 = core.cast %1 : i64
    %3, %4, %5 = core.call %text, %0, %2 {callee = @ppy.regex.3} : bool, i64, i64
    core.ret %3 loc("examples/43_regex/patterns.ppy":65:4)
}

private func @ppy.regex.0(%buf: buffer<u8>, %pos: i64, %endpos: i64) -> (bool, i64, i64) attrs {ppy.regex = "[A-Za-z]+", ppy.synthesized = "regex.search"} {
^entry:
    %1 = core.buffer_len %buf : index
    %2 = core.cast %1 : i64
    %3 = core.const 0 : i64
    %4 = core.cmp.lt %endpos, %3 : bool
    %5 = core.select %4, %3, %endpos : i64
    %6 = core.cmp.gt %5, %2 : bool
    %7 = core.select %6, %2, %5 : i64
    %8 = core.cmp.lt %pos, %3 : bool
    %9 = core.select %8, %3, %pos : i64
    %10 = core.cmp.gt %9, %2 : bool
    %11 = core.select %10, %2, %9 : i64
    %p = core.alloca : ptr<i64, stack>
    %sp = core.alloca : ptr<i64, stack>
    %start = core.alloca : ptr<i64, stack>
    %stack = core.alloca {count = 12288} : ptr<i64, stack>
    %caps = core.alloca {count = 2} : ptr<i64, stack>
    %12 = core.const 0 : i64
    %13 = core.cmp.gt %11, %7 : bool
    %14 = core.const 0 : i64
    %15 = core.const 0 : i64
    %16 = core.ptr_offset %stack, %15 : ptr<i64, stack>
    core.store %14, %16
    %17 = core.const 1 : i64
    %18 = core.ptr_offset %stack, %17 : ptr<i64, stack>
    core.store %14, %18
    %19 = core.const 2 : i64
    %20 = core.ptr_offset %stack, %19 : ptr<i64, stack>
    core.store %14, %20
    core.cond_br %13, ^notfound4, ^attempt1(%11)
^attempt1(%s: i64):
    core.store %s, %p
    core.store %s, %start
    core.store %12, %sp
    %21 = core.const -1 : i64
    %22 = core.const 0 : i64
    %23 = core.ptr_offset %caps, %22 : ptr<i64, stack>
    core.store %21, %23
    %24 = core.const 1 : i64
    %25 = core.ptr_offset %caps, %24 : ptr<i64, stack>
    core.store %21, %25
    %26 = core.const 0 : i64
    %27 = core.ptr_offset %caps, %26 : ptr<i64, stack>
    core.store %s, %27
    %28 = core.load %p : i64
    %29 = core.const 1 : i64
    %30 = core.add %28, %29 {overflow = "proven"} : i64
    %31 = core.cmp.le %30, %7 : bool
    core.cond_br %31, ^fits7, ^fail2
^fail2:
    %32 = core.load %sp : i64
    %33 = core.const 0 : i64
    %34 = core.cmp.eq %32, %33 : bool
    core.cond_br %34, ^exhausted3, ^pop17
^exhausted3:
    %35 = core.load %start : i64
    %36 = core.const 1 : i64
    %37 = core.add %35, %36 {overflow = "proven"} : i64
    %38 = core.cmp.gt %37, %7 : bool
    core.cond_br %38, ^notfound4, ^attempt1(%37)
^notfound4:
    %39 = core.const false : bool
    %40 = core.const -1 : i64
    %41 = core.const -1 : i64
    core.ret %39, %40, %41
^fits7:
    core.br ^scan8(%28)
^scan8(%q: i64):
    %42 = core.cmp.lt %q, %7 : bool
    core.cond_br %42, ^bound9, ^scanned11(%q)
^bound9:
    %43 = core.buffer_load %buf, %q : u8
    %44 = core.cast %43 : i64
    %45 = core.const 65 : i64
    %46 = core.cmp.ge %44, %45 : bool
    %47 = core.const 90 : i64
    %48 = core.cmp.le %44, %47 : bool
    %49 = core.and %46, %48 : bool
    %50 = core.const 97 : i64
    %51 = core.cmp.ge %44, %50 : bool
    %52 = core.const 122 : i64
    %53 = core.cmp.le %44, %52 : bool
    %54 = core.and %51, %53 : bool
    %55 = core.or %49, %54 : bool
    %56 = core.const 1 : i64
    %57 = core.add %q, %56 {overflow = "proven"} : i64
    core.cond_br %55, ^scan8(%57), ^scanned11(%q)
^scanned11(%0: i64):
    %58 = core.const 1 : i64
    %59 = core.add %28, %58 {overflow = "proven"} : i64
    %60 = core.cmp.lt %0, %59 : bool
    core.cond_br %60, ^fail2, ^enough13
^exit12:
    %61 = core.load %p : i64
    %62 = core.const 1 : i64
    %63 = core.ptr_offset %caps, %62 : ptr<i64, stack>
    core.store %61, %63
    %64 = core.const true : bool
    %65 = core.const 0 : i64
    %66 = core.ptr_offset %caps, %65 : ptr<i64, stack>
    %67 = core.load %66 : i64
    %68 = core.const 1 : i64
    %69 = core.ptr_offset %caps, %68 : ptr<i64, stack>
    %70 = core.load %69 : i64
    core.ret %64, %67, %70
^enough13:
    core.store %0, %p
    %71 = core.cmp.gt %0, %59 : bool
    core.cond_br %71, ^keep15, ^exit12
^giveback14(%held: i64):
    %72 = core.load %p : i64
    %73 = core.const 1 : i64
    %74 = core.sub %72, %73 {overflow = "proven"} : i64
    core.store %74, %p
    %75 = core.cmp.gt %74, %held : bool
    core.cond_br %75, ^keep16, ^exit12
^keep15:
    %76 = core.load %sp : i64
    %77 = core.const 4096 : i64
    %78 = core.cmp.lt %76, %77 : bool
    core.guard %78 {kind = "range", label = "regex.stack.ok"}
    %79 = core.const 3 : i64
    %80 = core.mul %76, %79 {overflow = "proven"} : i64
    %81 = core.ptr_offset %stack, %80 : ptr<i64, stack>
    %82 = core.const 0 : i64
    %83 = core.load %p : i64
    %84 = core.const 0 : i64
    %85 = core.ptr_offset %81, %84 : ptr<i64, stack>
    core.store %82, %85
    %86 = core.const 1 : i64
    %87 = core.ptr_offset %81, %86 : ptr<i64, stack>
    core.store %83, %87
    %88 = core.const 2 : i64
    %89 = core.ptr_offset %81, %88 : ptr<i64, stack>
    core.store %59, %89
    %90 = core.const 1 : i64
    %91 = core.add %76, %90 {overflow = "proven"} : i64
    core.store %91, %sp
    core.br ^exit12
^keep16:
    %92 = core.load %sp : i64
    %93 = core.const 4096 : i64
    %94 = core.cmp.lt %92, %93 : bool
    core.guard %94 {kind = "range", label = "regex.stack.ok"}
    %95 = core.const 3 : i64
    %96 = core.mul %92, %95 {overflow = "proven"} : i64
    %97 = core.ptr_offset %stack, %96 : ptr<i64, stack>
    %98 = core.const 0 : i64
    %99 = core.load %p : i64
    %100 = core.const 0 : i64
    %101 = core.ptr_offset %97, %100 : ptr<i64, stack>
    core.store %98, %101
    %102 = core.const 1 : i64
    %103 = core.ptr_offset %97, %102 : ptr<i64, stack>
    core.store %99, %103
    %104 = core.const 2 : i64
    %105 = core.ptr_offset %97, %104 : ptr<i64, stack>
    core.store %held, %105
    %106 = core.const 1 : i64
    %107 = core.add %92, %106 {overflow = "proven"} : i64
    core.store %107, %sp
    core.br ^exit12
^pop17:
    %108 = core.const 1 : i64
    %109 = core.sub %32, %108 {overflow = "proven"} : i64
    core.store %109, %sp
    %110 = core.const 3 : i64
    %111 = core.mul %109, %110 {overflow = "proven"} : i64
    %112 = core.ptr_offset %stack, %111 : ptr<i64, stack>
    %113 = core.const 0 : i64
    %114 = core.ptr_offset %112, %113 : ptr<i64, stack>
    %115 = core.load %114 : i64
    %116 = core.const 1 : i64
    %117 = core.ptr_offset %112, %116 : ptr<i64, stack>
    %118 = core.load %117 : i64
    core.store %118, %p
    %119 = core.const 2 : i64
    %120 = core.ptr_offset %112, %119 : ptr<i64, stack>
    %121 = core.load %120 : i64
    %122 = core.const 0 : i64
    %123 = core.cmp.eq %115, %122 : bool
    core.cond_br %123, ^giveback14(%121), ^dispatch18
^dispatch18:
    core.unreachable
}

private func @ppy.regex.1(%buf: buffer<u8>, %pos: i64, %endpos: i64) -> (bool, i64, i64, i64, i64, i64, i64) attrs {ppy.regex = "(?P<key>\\w+)\\s*=\\s*(\\d+)", ppy.synthesized = "regex.search"} {
^entry:
    %14 = core.buffer_len %buf : index
    %15 = core.cast %14 : i64
    %16 = core.const 0 : i64
    %17 = core.cmp.lt %endpos, %16 : bool
    %18 = core.select %17, %16, %endpos : i64
    %19 = core.cmp.gt %18, %15 : bool
    %20 = core.select %19, %15, %18 : i64
    %21 = core.cmp.lt %pos, %16 : bool
    %22 = core.select %21, %16, %pos : i64
    %23 = core.cmp.gt %22, %15 : bool
    %24 = core.select %23, %15, %22 : i64
    %p = core.alloca : ptr<i64, stack>
    %sp = core.alloca : ptr<i64, stack>
    %start = core.alloca : ptr<i64, stack>
    %stack = core.alloca {count = 12288} : ptr<i64, stack>
    %caps = core.alloca {count = 6} : ptr<i64, stack>
    %25 = core.const 0 : i64
    %26 = core.cmp.gt %24, %20 : bool
    %27 = core.const 0 : i64
    %28 = core.const 0 : i64
    %29 = core.ptr_offset %stack, %28 : ptr<i64, stack>
    core.store %27, %29
    %30 = core.const 1 : i64
    %31 = core.ptr_offset %stack, %30 : ptr<i64, stack>
    core.store %27, %31
    %32 = core.const 2 : i64
    %33 = core.ptr_offset %stack, %32 : ptr<i64, stack>
    core.store %27, %33
    core.cond_br %26, ^notfound4, ^attempt1(%24)
^attempt1(%s: i64):
    core.store %s, %p
    core.store %s, %start
    core.store %25, %sp
    %34 = core.const -1 : i64
    %35 = core.const 0 : i64
    %36 = core.ptr_offset %caps, %35 : ptr<i64, stack>
    core.store %34, %36
    %37 = core.const 1 : i64
    %38 = core.ptr_offset %caps, %37 : ptr<i64, stack>
    core.store %34, %38
    %39 = core.const 2 : i64
    %40 = core.ptr_offset %caps, %39 : ptr<i64, stack>
    core.store %34, %40
    %41 = core.const 3 : i64
    %42 = core.ptr_offset %caps, %41 : ptr<i64, stack>
    core.store %34, %42
    %43 = core.const 4 : i64
    %44 = core.ptr_offset %caps, %43 : ptr<i64, stack>
    core.store %34, %44
    %45 = core.const 5 : i64
    %46 = core.ptr_offset %caps, %45 : ptr<i64, stack>
    core.store %34, %46
    %47 = core.const 0 : i64
    %48 = core.ptr_offset %caps, %47 : ptr<i64, stack>
    core.store %s, %48
    %49 = core.const 2 : i64
    %50 = core.ptr_offset %caps, %49 : ptr<i64, stack>
    %51 = core.load %50 : i64
    %52 = core.load %sp : i64
    %53 = core.const 4096 : i64
    %54 = core.cmp.lt %52, %53 : bool
    core.guard %54 {kind = "range", label = "regex.stack.ok"}
    %55 = core.const 3 : i64
    %56 = core.mul %52, %55 {overflow = "proven"} : i64
    %57 = core.ptr_offset %stack, %56 : ptr<i64, stack>
    %58 = core.const 0 : i64
    %59 = core.load %p : i64
    %60 = core.const 0 : i64
    %61 = core.ptr_offset %57, %60 : ptr<i64, stack>
    core.store %58, %61
    %62 = core.const 1 : i64
    %63 = core.ptr_offset %57, %62 : ptr<i64, stack>
    core.store %59, %63
    %64 = core.const 2 : i64
    %65 = core.ptr_offset %57, %64 : ptr<i64, stack>
    core.store %51, %65
    %66 = core.const 1 : i64
    %67 = core.add %52, %66 {overflow = "proven"} : i64
    core.store %67, %sp
    %68 = core.load %p : i64
    %69 = core.const 2 : i64
    %70 = core.ptr_offset %caps, %69 : ptr<i64, stack>
    core.store %68, %70
    %71 = core.load %p : i64
    %72 = core.const 1 : i64
    %73 = core.add %71, %72 {overflow = "proven"} : i64
    %74 = core.cmp.le %73, %20 : bool
    core.cond_br %74, ^fits8, ^fail2
^fail2:
    %75 = core.load %sp : i64
    %76 = core.const 0 : i64
    %77 = core.cmp.eq %75, %76 : bool
    core.cond_br %77, ^exhausted3, ^pop53
^exhausted3:
    %78 = core.load %start : i64
    %79 = core.const 1 : i64
    %80 = core.add %78, %79 {overflow = "proven"} : i64
    %81 = core.cmp.gt %80, %20 : bool
    core.cond_br %81, ^notfound4, ^attempt1(%80)
^notfound4:
    %82 = core.const false : bool
    %83 = core.const -1 : i64
    %84 = core.const -1 : i64
    %85 = core.const -1 : i64
    %86 = core.const -1 : i64
    %87 = core.const -1 : i64
    %88 = core.const -1 : i64
    core.ret %82, %83, %84, %85, %86, %87, %88
^restore7(%held: i64):
    %89 = core.const 2 : i64
    %90 = core.ptr_offset %caps, %89 : ptr<i64, stack>
    core.store %held, %90
    core.br ^fail2
^fits8:
    core.br ^scan9(%71)
^scan9(%q: i64):
    %91 = core.cmp.lt %q, %20 : bool
    core.cond_br %91, ^bound10, ^scanned12(%q)
^bound10:
    %92 = core.buffer_load %buf, %q : u8
    %93 = core.cast %92 : i64
    %94 = core.const 0 : i64
    %95 = core.const 192 : i64
    %96 = core.cmp.lt %93, %95 : bool
    %97 = core.const 0 : i64
    %98 = core.select %96, %97, %94 : i64
    %99 = core.const 128 : i64
    %100 = core.cmp.lt %93, %99 : bool
    %101 = core.const 576460745995190270 : i64
    %102 = core.select %100, %101, %98 : i64
    %103 = core.const 64 : i64
    %104 = core.cmp.lt %93, %103 : bool
    %105 = core.const 287948901175001088 : i64
    %106 = core.select %104, %105, %102 : i64
    %107 = core.const 63 : i64
    %108 = core.and %93, %107 : i64
    %109 = core.shr %106, %108 : i64
    %110 = core.const 1 : i64
    %111 = core.and %109, %110 : i64
    %112 = core.const 0 : i64
    %113 = core.cmp.ne %111, %112 : bool
    %114 = core.const 1 : i64
    %115 = core.add %q, %114 {overflow = "proven"} : i64
    core.cond_br %113, ^scan9(%115), ^scanned12(%q)
^scanned12(%0: i64):
    %116 = core.const 1 : i64
    %117 = core.add %71, %116 {overflow = "proven"} : i64
    %118 = core.cmp.lt %0, %117 : bool
    core.cond_br %118, ^fail2, ^enough14
^exit13:
    %119 = core.const 3 : i64
    %120 = core.ptr_offset %caps, %119 : ptr<i64, stack>
    %121 = core.load %120 : i64
    %122 = core.load %sp : i64
    %123 = core.const 4096 : i64
    %124 = core.cmp.lt %122, %123 : bool
    core.guard %124 {kind = "range", label = "regex.stack.ok"}
    %125 = core.const 3 : i64
    %126 = core.mul %122, %125 {overflow = "proven"} : i64
    %127 = core.ptr_offset %stack, %126 : ptr<i64, stack>
    %128 = core.const 2 : i64
    %129 = core.load %p : i64
    %130 = core.const 0 : i64
    %131 = core.ptr_offset %127, %130 : ptr<i64, stack>
    core.store %128, %131
    %132 = core.const 1 : i64
    %133 = core.ptr_offset %127, %132 : ptr<i64, stack>
    core.store %129, %133
    %134 = core.const 2 : i64
    %135 = core.ptr_offset %127, %134 : ptr<i64, stack>
    core.store %121, %135
    %136 = core.const 1 : i64
    %137 = core.add %122, %136 {overflow = "proven"} : i64
    core.store %137, %sp
    %138 = core.load %p : i64
    %139 = core.const 3 : i64
    %140 = core.ptr_offset %caps, %139 : ptr<i64, stack>
    core.store %138, %140
    %141 = core.load %p : i64
    %142 = core.cmp.le %141, %20 : bool
    core.cond_br %142, ^fits19, ^fail2
^enough14:
    core.store %0, %p
    %143 = core.cmp.gt %0, %117 : bool
    core.cond_br %143, ^keep16, ^exit13
^giveback15(%1: i64):
    %144 = core.load %p : i64
    %145 = core.const 1 : i64
    %146 = core.sub %144, %145 {overflow = "proven"} : i64
    core.store %146, %p
    %147 = core.cmp.gt %146, %1 : bool
    core.cond_br %147, ^keep17, ^exit13
^keep16:
    %148 = core.load %sp : i64
    %149 = core.const 4096 : i64
    %150 = core.cmp.lt %148, %149 : bool
    core.guard %150 {kind = "range", label = "regex.stack.ok"}
    %151 = core.const 3 : i64
    %152 = core.mul %148, %151 {overflow = "proven"} : i64
    %153 = core.ptr_offset %stack, %152 : ptr<i64, stack>
    %154 = core.const 1 : i64
    %155 = core.load %p : i64
    %156 = core.const 0 : i64
    %157 = core.ptr_offset %153, %156 : ptr<i64, stack>
    core.store %154, %157
    %158 = core.const 1 : i64
    %159 = core.ptr_offset %153, %158 : ptr<i64, stack>
    core.store %155, %159
    %160 = core.const 2 : i64
    %161 = core.ptr_offset %153, %160 : ptr<i64, stack>
    core.store %117, %161
    %162 = core.const 1 : i64
    %163 = core.add %148, %162 {overflow = "proven"} : i64
    core.store %163, %sp
    core.br ^exit13
^keep17:
    %164 = core.load %sp : i64
    %165 = core.const 4096 : i64
    %166 = core.cmp.lt %164, %165 : bool
    core.guard %166 {kind = "range", label = "regex.stack.ok"}
    %167 = core.const 3 : i64
    %168 = core.mul %164, %167 {overflow = "proven"} : i64
    %169 = core.ptr_offset %stack, %168 : ptr<i64, stack>
    %170 = core.const 1 : i64
    %171 = core.load %p : i64
    %172 = core.const 0 : i64
    %173 = core.ptr_offset %169, %172 : ptr<i64, stack>
    core.store %170, %173
    %174 = core.const 1 : i64
    %175 = core.ptr_offset %169, %174 : ptr<i64, stack>
    core.store %171, %175
    %176 = core.const 2 : i64
    %177 = core.ptr_offset %169, %176 : ptr<i64, stack>
    core.store %1, %177
    %178 = core.const 1 : i64
    %179 = core.add %164, %178 {overflow = "proven"} : i64
    core.store %179, %sp
    core.br ^exit13
^restore18(%2: i64):
    %180 = core.const 3 : i64
    %181 = core.ptr_offset %caps, %180 : ptr<i64, stack>
    core.store %2, %181
    core.br ^fail2
^fits19:
    core.br ^scan20(%141)
^scan20(%3: i64):
    %182 = core.cmp.lt %3, %20 : bool
    core.cond_br %182, ^bound21, ^scanned23(%3)
^bound21:
    %183 = core.buffer_load %buf, %3 : u8
    %184 = core.cast %183 : i64
    %185 = core.const 9 : i64
    %186 = core.cmp.ge %184, %185 : bool
    %187 = core.const 13 : i64
    %188 = core.cmp.le %184, %187 : bool
    %189 = core.and %186, %188 : bool
    %190 = core.const 32 : i64
    %191 = core.cmp.eq %184, %190 : bool
    %192 = core.or %189, %191 : bool
    %193 = core.const 1 : i64
    %194 = core.add %3, %193 {overflow = "proven"} : i64
    core.cond_br %192, ^scan20(%194), ^scanned23(%3)
^scanned23(%4: i64):
    %195 = core.cmp.lt %4, %141 : bool
    core.cond_br %195, ^fail2, ^enough25
^exit24:
    %196 = core.load %p : i64
    %197 = core.cmp.lt %196, %20 : bool
    core.cond_br %197, ^read29, ^fail2
^enough25:
    core.store %4, %p
    %198 = core.cmp.gt %4, %141 : bool
    core.cond_br %198, ^keep27, ^exit24
^giveback26(%5: i64):
    %199 = core.load %p : i64
    %200 = core.const 1 : i64
    %201 = core.sub %199, %200 {overflow = "proven"} : i64
    core.store %201, %p
    %202 = core.cmp.gt %201, %5 : bool
    core.cond_br %202, ^keep28, ^exit24
^keep27:
    %203 = core.load %sp : i64
    %204 = core.const 4096 : i64
    %205 = core.cmp.lt %203, %204 : bool
    core.guard %205 {kind = "range", label = "regex.stack.ok"}
    %206 = core.const 3 : i64
    %207 = core.mul %203, %206 {overflow = "proven"} : i64
    %208 = core.ptr_offset %stack, %207 : ptr<i64, stack>
    %209 = core.const 3 : i64
    %210 = core.load %p : i64
    %211 = core.const 0 : i64
    %212 = core.ptr_offset %208, %211 : ptr<i64, stack>
    core.store %209, %212
    %213 = core.const 1 : i64
    %214 = core.ptr_offset %208, %213 : ptr<i64, stack>
    core.store %210, %214
    %215 = core.const 2 : i64
    %216 = core.ptr_offset %208, %215 : ptr<i64, stack>
    core.store %141, %216
    %217 = core.const 1 : i64
    %218 = core.add %203, %217 {overflow = "proven"} : i64
    core.store %218, %sp
    core.br ^exit24
^keep28:
    %219 = core.load %sp : i64
    %220 = core.const 4096 : i64
    %221 = core.cmp.lt %219, %220 : bool
    core.guard %221 {kind = "range", label = "regex.stack.ok"}
    %222 = core.const 3 : i64
    %223 = core.mul %219, %222 {overflow = "proven"} : i64
    %224 = core.ptr_offset %stack, %223 : ptr<i64, stack>
    %225 = core.const 3 : i64
    %226 = core.load %p : i64
    %227 = core.const 0 : i64
    %228 = core.ptr_offset %224, %227 : ptr<i64, stack>
    core.store %225, %228
    %229 = core.const 1 : i64
    %230 = core.ptr_offset %224, %229 : ptr<i64, stack>
    core.store %226, %230
    %231 = core.const 2 : i64
    %232 = core.ptr_offset %224, %231 : ptr<i64, stack>
    core.store %5, %232
    %233 = core.const 1 : i64
    %234 = core.add %219, %233 {overflow = "proven"} : i64
    core.store %234, %sp
    core.br ^exit24
^read29:
    %235 = core.buffer_load %buf, %196 : u8
    %236 = core.cast %235 : i64
    %237 = core.const 61 : i64
    %238 = core.cmp.eq %236, %237 : bool
    core.cond_br %238, ^advance30, ^fail2
^advance30:
    %239 = core.const 1 : i64
    %240 = core.add %196, %239 {overflow = "proven"} : i64
    core.store %240, %p
    %241 = core.load %p : i64
    %242 = core.cmp.le %241, %20 : bool
    core.cond_br %242, ^fits31, ^fail2
^fits31:
    core.br ^scan32(%241)
^scan32(%6: i64):
    %243 = core.cmp.lt %6, %20 : bool
    core.cond_br %243, ^bound33, ^scanned35(%6)
^bound33:
    %244 = core.buffer_load %buf, %6 : u8
    %245 = core.cast %244 : i64
    %246 = core.const 9 : i64
    %247 = core.cmp.ge %245, %246 : bool
    %248 = core.const 13 : i64
    %249 = core.cmp.le %245, %248 : bool
    %250 = core.and %247, %249 : bool
    %251 = core.const 32 : i64
    %252 = core.cmp.eq %245, %251 : bool
    %253 = core.or %250, %252 : bool
    %254 = core.const 1 : i64
    %255 = core.add %6, %254 {overflow = "proven"} : i64
    core.cond_br %253, ^scan32(%255), ^scanned35(%6)
^scanned35(%7: i64):
    %256 = core.cmp.lt %7, %241 : bool
    core.cond_br %256, ^fail2, ^enough37
^exit36:
    %257 = core.const 4 : i64
    %258 = core.ptr_offset %caps, %257 : ptr<i64, stack>
    %259 = core.load %258 : i64
    %260 = core.load %sp : i64
    %261 = core.const 4096 : i64
    %262 = core.cmp.lt %260, %261 : bool
    core.guard %262 {kind = "range", label = "regex.stack.ok"}
    %263 = core.const 3 : i64
    %264 = core.mul %260, %263 {overflow = "proven"} : i64
    %265 = core.ptr_offset %stack, %264 : ptr<i64, stack>
    %266 = core.const 5 : i64
    %267 = core.load %p : i64
    %268 = core.const 0 : i64
    %269 = core.ptr_offset %265, %268 : ptr<i64, stack>
    core.store %266, %269
    %270 = core.const 1 : i64
    %271 = core.ptr_offset %265, %270 : ptr<i64, stack>
    core.store %267, %271
    %272 = core.const 2 : i64
    %273 = core.ptr_offset %265, %272 : ptr<i64, stack>
    core.store %259, %273
    %274 = core.const 1 : i64
    %275 = core.add %260, %274 {overflow = "proven"} : i64
    core.store %275, %sp
    %276 = core.load %p : i64
    %277 = core.const 4 : i64
    %278 = core.ptr_offset %caps, %277 : ptr<i64, stack>
    core.store %276, %278
    %279 = core.load %p : i64
    %280 = core.const 1 : i64
    %281 = core.add %279, %280 {overflow = "proven"} : i64
    %282 = core.cmp.le %281, %20 : bool
    core.cond_br %282, ^fits42, ^fail2
^enough37:
    core.store %7, %p
    %283 = core.cmp.gt %7, %241 : bool
    core.cond_br %283, ^keep39, ^exit36
^giveback38(%8: i64):
    %284 = core.load %p : i64
    %285 = core.const 1 : i64
    %286 = core.sub %284, %285 {overflow = "proven"} : i64
    core.store %286, %p
    %287 = core.cmp.gt %286, %8 : bool
    core.cond_br %287, ^keep40, ^exit36
^keep39:
    %288 = core.load %sp : i64
    %289 = core.const 4096 : i64
    %290 = core.cmp.lt %288, %289 : bool
    core.guard %290 {kind = "range", label = "regex.stack.ok"}
    %291 = core.const 3 : i64
    %292 = core.mul %288, %291 {overflow = "proven"} : i64
    %293 = core.ptr_offset %stack, %292 : ptr<i64, stack>
    %294 = core.const 4 : i64
    %295 = core.load %p : i64
    %296 = core.const 0 : i64
    %297 = core.ptr_offset %293, %296 : ptr<i64, stack>
    core.store %294, %297
    %298 = core.const 1 : i64
    %299 = core.ptr_offset %293, %298 : ptr<i64, stack>
    core.store %295, %299
    %300 = core.const 2 : i64
    %301 = core.ptr_offset %293, %300 : ptr<i64, stack>
    core.store %241, %301
    %302 = core.const 1 : i64
    %303 = core.add %288, %302 {overflow = "proven"} : i64
    core.store %303, %sp
    core.br ^exit36
^keep40:
    %304 = core.load %sp : i64
    %305 = core.const 4096 : i64
    %306 = core.cmp.lt %304, %305 : bool
    core.guard %306 {kind = "range", label = "regex.stack.ok"}
    %307 = core.const 3 : i64
    %308 = core.mul %304, %307 {overflow = "proven"} : i64
    %309 = core.ptr_offset %stack, %308 : ptr<i64, stack>
    %310 = core.const 4 : i64
    %311 = core.load %p : i64
    %312 = core.const 0 : i64
    %313 = core.ptr_offset %309, %312 : ptr<i64, stack>
    core.store %310, %313
    %314 = core.const 1 : i64
    %315 = core.ptr_offset %309, %314 : ptr<i64, stack>
    core.store %311, %315
    %316 = core.const 2 : i64
    %317 = core.ptr_offset %309, %316 : ptr<i64, stack>
    core.store %8, %317
    %318 = core.const 1 : i64
    %319 = core.add %304, %318 {overflow = "proven"} : i64
    core.store %319, %sp
    core.br ^exit36
^restore41(%9: i64):
    %320 = core.const 4 : i64
    %321 = core.ptr_offset %caps, %320 : ptr<i64, stack>
    core.store %9, %321
    core.br ^fail2
^fits42:
    core.br ^scan43(%279)
^scan43(%10: i64):
    %322 = core.cmp.lt %10, %20 : bool
    core.cond_br %322, ^bound44, ^scanned46(%10)
^bound44:
    %323 = core.buffer_load %buf, %10 : u8
    %324 = core.cast %323 : i64
    %325 = core.const 48 : i64
    %326 = core.cmp.ge %324, %325 : bool
    %327 = core.const 57 : i64
    %328 = core.cmp.le %324, %327 : bool
    %329 = core.and %326, %328 : bool
    %330 = core.const 1 : i64
    %331 = core.add %10, %330 {overflow = "proven"} : i64
    core.cond_br %329, ^scan43(%331), ^scanned46(%10)
^scanned46(%11: i64):
    %332 = core.const 1 : i64
    %333 = core.add %279, %332 {overflow = "proven"} : i64
    %334 = core.cmp.lt %11, %333 : bool
    core.cond_br %334, ^fail2, ^enough48
^exit47:
    %335 = core.const 5 : i64
    %336 = core.ptr_offset %caps, %335 : ptr<i64, stack>
    %337 = core.load %336 : i64
    %338 = core.load %sp : i64
    %339 = core.const 4096 : i64
    %340 = core.cmp.lt %338, %339 : bool
    core.guard %340 {kind = "range", label = "regex.stack.ok"}
    %341 = core.const 3 : i64
    %342 = core.mul %338, %341 {overflow = "proven"} : i64
    %343 = core.ptr_offset %stack, %342 : ptr<i64, stack>
    %344 = core.const 7 : i64
    %345 = core.load %p : i64
    %346 = core.const 0 : i64
    %347 = core.ptr_offset %343, %346 : ptr<i64, stack>
    core.store %344, %347
    %348 = core.const 1 : i64
    %349 = core.ptr_offset %343, %348 : ptr<i64, stack>
    core.store %345, %349
    %350 = core.const 2 : i64
    %351 = core.ptr_offset %343, %350 : ptr<i64, stack>
    core.store %337, %351
    %352 = core.const 1 : i64
    %353 = core.add %338, %352 {overflow = "proven"} : i64
    core.store %353, %sp
    %354 = core.load %p : i64
    %355 = core.const 5 : i64
    %356 = core.ptr_offset %caps, %355 : ptr<i64, stack>
    core.store %354, %356
    %357 = core.load %p : i64
    %358 = core.const 1 : i64
    %359 = core.ptr_offset %caps, %358 : ptr<i64, stack>
    core.store %357, %359
    %360 = core.const true : bool
    %361 = core.const 0 : i64
    %362 = core.ptr_offset %caps, %361 : ptr<i64, stack>
    %363 = core.load %362 : i64
    %364 = core.const 1 : i64
    %365 = core.ptr_offset %caps, %364 : ptr<i64, stack>
    %366 = core.load %365 : i64
    %367 = core.const 2 : i64
    %368 = core.ptr_offset %caps, %367 : ptr<i64, stack>
    %369 = core.load %368 : i64
    %370 = core.const 3 : i64
    %371 = core.ptr_offset %caps, %370 : ptr<i64, stack>
    %372 = core.load %371 : i64
    %373 = core.const 4 : i64
    %374 = core.ptr_offset %caps, %373 : ptr<i64, stack>
    %375 = core.load %374 : i64
    %376 = core.const 5 : i64
    %377 = core.ptr_offset %caps, %376 : ptr<i64, stack>
    %378 = core.load %377 : i64
    core.ret %360, %363, %366, %369, %372, %375, %378
^enough48:
    core.store %11, %p
    %379 = core.cmp.gt %11, %333 : bool
    core.cond_br %379, ^keep50, ^exit47
^giveback49(%12: i64):
    %380 = core.load %p : i64
    %381 = core.const 1 : i64
    %382 = core.sub %380, %381 {overflow = "proven"} : i64
    core.store %382, %p
    %383 = core.cmp.gt %382, %12 : bool
    core.cond_br %383, ^keep51, ^exit47
^keep50:
    %384 = core.load %sp : i64
    %385 = core.const 4096 : i64
    %386 = core.cmp.lt %384, %385 : bool
    core.guard %386 {kind = "range", label = "regex.stack.ok"}
    %387 = core.const 3 : i64
    %388 = core.mul %384, %387 {overflow = "proven"} : i64
    %389 = core.ptr_offset %stack, %388 : ptr<i64, stack>
    %390 = core.const 6 : i64
    %391 = core.load %p : i64
    %392 = core.const 0 : i64
    %393 = core.ptr_offset %389, %392 : ptr<i64, stack>
    core.store %390, %393
    %394 = core.const 1 : i64
    %395 = core.ptr_offset %389, %394 : ptr<i64, stack>
    core.store %391, %395
    %396 = core.const 2 : i64
    %397 = core.ptr_offset %389, %396 : ptr<i64, stack>
    core.store %333, %397
    %398 = core.const 1 : i64
    %399 = core.add %384, %398 {overflow = "proven"} : i64
    core.store %399, %sp
    core.br ^exit47
^keep51:
    %400 = core.load %sp : i64
    %401 = core.const 4096 : i64
    %402 = core.cmp.lt %400, %401 : bool
    core.guard %402 {kind = "range", label = "regex.stack.ok"}
    %403 = core.const 3 : i64
    %404 = core.mul %400, %403 {overflow = "proven"} : i64
    %405 = core.ptr_offset %stack, %404 : ptr<i64, stack>
    %406 = core.const 6 : i64
    %407 = core.load %p : i64
    %408 = core.const 0 : i64
    %409 = core.ptr_offset %405, %408 : ptr<i64, stack>
    core.store %406, %409
    %410 = core.const 1 : i64
    %411 = core.ptr_offset %405, %410 : ptr<i64, stack>
    core.store %407, %411
    %412 = core.const 2 : i64
    %413 = core.ptr_offset %405, %412 : ptr<i64, stack>
    core.store %12, %413
    %414 = core.const 1 : i64
    %415 = core.add %400, %414 {overflow = "proven"} : i64
    core.store %415, %sp
    core.br ^exit47
^restore52(%13: i64):
    %416 = core.const 5 : i64
    %417 = core.ptr_offset %caps, %416 : ptr<i64, stack>
    core.store %13, %417
    core.br ^fail2
^pop53:
    %418 = core.const 1 : i64
    %419 = core.sub %75, %418 {overflow = "proven"} : i64
    core.store %419, %sp
    %420 = core.const 3 : i64
    %421 = core.mul %419, %420 {overflow = "proven"} : i64
    %422 = core.ptr_offset %stack, %421 : ptr<i64, stack>
    %423 = core.const 0 : i64
    %424 = core.ptr_offset %422, %423 : ptr<i64, stack>
    %425 = core.load %424 : i64
    %426 = core.const 1 : i64
    %427 = core.ptr_offset %422, %426 : ptr<i64, stack>
    %428 = core.load %427 : i64
    core.store %428, %p
    %429 = core.const 2 : i64
    %430 = core.ptr_offset %422, %429 : ptr<i64, stack>
    %431 = core.load %430 : i64
    %432 = core.const 0 : i64
    %433 = core.cmp.eq %425, %432 : bool
    core.cond_br %433, ^restore7(%431), ^dispatch54
^dispatch54:
    %434 = core.const 1 : i64
    %435 = core.cmp.eq %425, %434 : bool
    core.cond_br %435, ^giveback15(%431), ^dispatch55
^dispatch55:
    %436 = core.const 2 : i64
    %437 = core.cmp.eq %425, %436 : bool
    core.cond_br %437, ^restore18(%431), ^dispatch56
^dispatch56:
    %438 = core.const 3 : i64
    %439 = core.cmp.eq %425, %438 : bool
    core.cond_br %439, ^giveback26(%431), ^dispatch57
^dispatch57:
    %440 = core.const 4 : i64
    %441 = core.cmp.eq %425, %440 : bool
    core.cond_br %441, ^giveback38(%431), ^dispatch58
^dispatch58:
    %442 = core.const 5 : i64
    %443 = core.cmp.eq %425, %442 : bool
    core.cond_br %443, ^restore41(%431), ^dispatch59
^dispatch59:
    %444 = core.const 6 : i64
    %445 = core.cmp.eq %425, %444 : bool
    core.cond_br %445, ^giveback49(%431), ^dispatch60
^dispatch60:
    %446 = core.const 7 : i64
    %447 = core.cmp.eq %425, %446 : bool
    core.cond_br %447, ^restore52(%431), ^dispatch61
^dispatch61:
    core.unreachable
}

private func @ppy.regex.2(%buf: buffer<u8>, %pos: i64, %endpos: i64) -> (bool, i64, i64) attrs {ppy.regex = "0x[0-9a-f]+", ppy.synthesized = "regex.search"} {
^entry:
    %1 = core.buffer_len %buf : index
    %2 = core.cast %1 : i64
    %3 = core.const 0 : i64
    %4 = core.cmp.lt %endpos, %3 : bool
    %5 = core.select %4, %3, %endpos : i64
    %6 = core.cmp.gt %5, %2 : bool
    %7 = core.select %6, %2, %5 : i64
    %8 = core.cmp.lt %pos, %3 : bool
    %9 = core.select %8, %3, %pos : i64
    %10 = core.cmp.gt %9, %2 : bool
    %11 = core.select %10, %2, %9 : i64
    %p = core.alloca : ptr<i64, stack>
    %sp = core.alloca : ptr<i64, stack>
    %start = core.alloca : ptr<i64, stack>
    %stack = core.alloca {count = 12288} : ptr<i64, stack>
    %caps = core.alloca {count = 2} : ptr<i64, stack>
    %12 = core.const 0 : i64
    %13 = core.cmp.gt %11, %7 : bool
    %14 = core.const 0 : i64
    %15 = core.const 0 : i64
    %16 = core.ptr_offset %stack, %15 : ptr<i64, stack>
    core.store %14, %16
    %17 = core.const 1 : i64
    %18 = core.ptr_offset %stack, %17 : ptr<i64, stack>
    core.store %14, %18
    %19 = core.const 2 : i64
    %20 = core.ptr_offset %stack, %19 : ptr<i64, stack>
    core.store %14, %20
    core.cond_br %13, ^notfound4, ^attempt1(%11)
^attempt1(%s: i64):
    core.store %s, %p
    core.store %s, %start
    core.store %12, %sp
    %21 = core.const -1 : i64
    %22 = core.const 0 : i64
    %23 = core.ptr_offset %caps, %22 : ptr<i64, stack>
    core.store %21, %23
    %24 = core.const 1 : i64
    %25 = core.ptr_offset %caps, %24 : ptr<i64, stack>
    core.store %21, %25
    %26 = core.const 0 : i64
    %27 = core.ptr_offset %caps, %26 : ptr<i64, stack>
    core.store %s, %27
    %28 = core.load %p : i64
    %29 = core.cmp.lt %28, %7 : bool
    core.cond_br %29, ^read7, ^fail2
^fail2:
    %30 = core.load %sp : i64
    %31 = core.const 0 : i64
    %32 = core.cmp.eq %30, %31 : bool
    core.cond_br %32, ^exhausted3, ^pop21
^exhausted3:
    %33 = core.load %start : i64
    %34 = core.const 1 : i64
    %35 = core.add %33, %34 {overflow = "proven"} : i64
    %36 = core.cmp.gt %35, %7 : bool
    core.cond_br %36, ^notfound4, ^attempt1(%35)
^notfound4:
    %37 = core.const false : bool
    %38 = core.const -1 : i64
    %39 = core.const -1 : i64
    core.ret %37, %38, %39
^read7:
    %40 = core.buffer_load %buf, %28 : u8
    %41 = core.cast %40 : i64
    %42 = core.const 48 : i64
    %43 = core.cmp.eq %41, %42 : bool
    core.cond_br %43, ^advance8, ^fail2
^advance8:
    %44 = core.const 1 : i64
    %45 = core.add %28, %44 {overflow = "proven"} : i64
    core.store %45, %p
    %46 = core.load %p : i64
    %47 = core.cmp.lt %46, %7 : bool
    core.cond_br %47, ^read9, ^fail2
^read9:
    %48 = core.buffer_load %buf, %46 : u8
    %49 = core.cast %48 : i64
    %50 = core.const 88 : i64
    %51 = core.cmp.eq %49, %50 : bool
    %52 = core.const 120 : i64
    %53 = core.cmp.eq %49, %52 : bool
    %54 = core.or %51, %53 : bool
    core.cond_br %54, ^advance10, ^fail2
^advance10:
    %55 = core.const 1 : i64
    %56 = core.add %46, %55 {overflow = "proven"} : i64
    core.store %56, %p
    %57 = core.load %p : i64
    %58 = core.const 1 : i64
    %59 = core.add %57, %58 {overflow = "proven"} : i64
    %60 = core.cmp.le %59, %7 : bool
    core.cond_br %60, ^fits11, ^fail2
^fits11:
    core.br ^scan12(%57)
^scan12(%q: i64):
    %61 = core.cmp.lt %q, %7 : bool
    core.cond_br %61, ^bound13, ^scanned15(%q)
^bound13:
    %62 = core.buffer_load %buf, %q : u8
    %63 = core.cast %62 : i64
    %64 = core.const 48 : i64
    %65 = core.cmp.ge %63, %64 : bool
    %66 = core.const 57 : i64
    %67 = core.cmp.le %63, %66 : bool
    %68 = core.and %65, %67 : bool
    %69 = core.const 65 : i64
    %70 = core.cmp.ge %63, %69 : bool
    %71 = core.const 70 : i64
    %72 = core.cmp.le %63, %71 : bool
    %73 = core.and %70, %72 : bool
    %74 = core.const 97 : i64
    %75 = core.cmp.ge %63, %74 : bool
    %76 = core.const 102 : i64
    %77 = core.cmp.le %63, %76 : bool
    %78 = core.and %75, %77 : bool
    %79 = core.or %68, %73 : bool
    %80 = core.or %79, %78 : bool
    %81 = core.const 1 : i64
    %82 = core.add %q, %81 {overflow = "proven"} : i64
    core.cond_br %80, ^scan12(%82), ^scanned15(%q)
^scanned15(%0: i64):
    %83 = core.const 1 : i64
    %84 = core.add %57, %83 {overflow = "proven"} : i64
    %85 = core.cmp.lt %0, %84 : bool
    core.cond_br %85, ^fail2, ^enough17
^exit16:
    %86 = core.load %p : i64
    %87 = core.const 1 : i64
    %88 = core.ptr_offset %caps, %87 : ptr<i64, stack>
    core.store %86, %88
    %89 = core.const true : bool
    %90 = core.const 0 : i64
    %91 = core.ptr_offset %caps, %90 : ptr<i64, stack>
    %92 = core.load %91 : i64
    %93 = core.const 1 : i64
    %94 = core.ptr_offset %caps, %93 : ptr<i64, stack>
    %95 = core.load %94 : i64
    core.ret %89, %92, %95
^enough17:
    core.store %0, %p
    %96 = core.cmp.gt %0, %84 : bool
    core.cond_br %96, ^keep19, ^exit16
^giveback18(%held: i64):
    %97 = core.load %p : i64
    %98 = core.const 1 : i64
    %99 = core.sub %97, %98 {overflow = "proven"} : i64
    core.store %99, %p
    %100 = core.cmp.gt %99, %held : bool
    core.cond_br %100, ^keep20, ^exit16
^keep19:
    %101 = core.load %sp : i64
    %102 = core.const 4096 : i64
    %103 = core.cmp.lt %101, %102 : bool
    core.guard %103 {kind = "range", label = "regex.stack.ok"}
    %104 = core.const 3 : i64
    %105 = core.mul %101, %104 {overflow = "proven"} : i64
    %106 = core.ptr_offset %stack, %105 : ptr<i64, stack>
    %107 = core.const 0 : i64
    %108 = core.load %p : i64
    %109 = core.const 0 : i64
    %110 = core.ptr_offset %106, %109 : ptr<i64, stack>
    core.store %107, %110
    %111 = core.const 1 : i64
    %112 = core.ptr_offset %106, %111 : ptr<i64, stack>
    core.store %108, %112
    %113 = core.const 2 : i64
    %114 = core.ptr_offset %106, %113 : ptr<i64, stack>
    core.store %84, %114
    %115 = core.const 1 : i64
    %116 = core.add %101, %115 {overflow = "proven"} : i64
    core.store %116, %sp
    core.br ^exit16
^keep20:
    %117 = core.load %sp : i64
    %118 = core.const 4096 : i64
    %119 = core.cmp.lt %117, %118 : bool
    core.guard %119 {kind = "range", label = "regex.stack.ok"}
    %120 = core.const 3 : i64
    %121 = core.mul %117, %120 {overflow = "proven"} : i64
    %122 = core.ptr_offset %stack, %121 : ptr<i64, stack>
    %123 = core.const 0 : i64
    %124 = core.load %p : i64
    %125 = core.const 0 : i64
    %126 = core.ptr_offset %122, %125 : ptr<i64, stack>
    core.store %123, %126
    %127 = core.const 1 : i64
    %128 = core.ptr_offset %122, %127 : ptr<i64, stack>
    core.store %124, %128
    %129 = core.const 2 : i64
    %130 = core.ptr_offset %122, %129 : ptr<i64, stack>
    core.store %held, %130
    %131 = core.const 1 : i64
    %132 = core.add %117, %131 {overflow = "proven"} : i64
    core.store %132, %sp
    core.br ^exit16
^pop21:
    %133 = core.const 1 : i64
    %134 = core.sub %30, %133 {overflow = "proven"} : i64
    core.store %134, %sp
    %135 = core.const 3 : i64
    %136 = core.mul %134, %135 {overflow = "proven"} : i64
    %137 = core.ptr_offset %stack, %136 : ptr<i64, stack>
    %138 = core.const 0 : i64
    %139 = core.ptr_offset %137, %138 : ptr<i64, stack>
    %140 = core.load %139 : i64
    %141 = core.const 1 : i64
    %142 = core.ptr_offset %137, %141 : ptr<i64, stack>
    %143 = core.load %142 : i64
    core.store %143, %p
    %144 = core.const 2 : i64
    %145 = core.ptr_offset %137, %144 : ptr<i64, stack>
    %146 = core.load %145 : i64
    %147 = core.const 0 : i64
    %148 = core.cmp.eq %140, %147 : bool
    core.cond_br %148, ^giveback18(%146), ^dispatch22
^dispatch22:
    core.unreachable
}

private func @ppy.regex.3(%buf: buffer<u8>, %pos: i64, %endpos: i64) -> (bool, i64, i64) attrs {ppy.regex = "[A-Za-z_]\\w*", ppy.synthesized = "regex.fullmatch"} {
^entry:
    %1 = core.buffer_len %buf : index
    %2 = core.cast %1 : i64
    %3 = core.const 0 : i64
    %4 = core.cmp.lt %endpos, %3 : bool
    %5 = core.select %4, %3, %endpos : i64
    %6 = core.cmp.gt %5, %2 : bool
    %7 = core.select %6, %2, %5 : i64
    %8 = core.cmp.lt %pos, %3 : bool
    %9 = core.select %8, %3, %pos : i64
    %10 = core.cmp.gt %9, %2 : bool
    %11 = core.select %10, %2, %9 : i64
    %p = core.alloca : ptr<i64, stack>
    %sp = core.alloca : ptr<i64, stack>
    %stack = core.alloca {count = 12288} : ptr<i64, stack>
    %caps = core.alloca {count = 2} : ptr<i64, stack>
    %12 = core.const 0 : i64
    %13 = core.const 0 : i64
    %14 = core.const 0 : i64
    %15 = core.ptr_offset %stack, %14 : ptr<i64, stack>
    core.store %13, %15
    %16 = core.const 1 : i64
    %17 = core.ptr_offset %stack, %16 : ptr<i64, stack>
    core.store %13, %17
    %18 = core.const 2 : i64
    %19 = core.ptr_offset %stack, %18 : ptr<i64, stack>
    core.store %13, %19
    core.store %11, %p
    core.store %12, %sp
    %20 = core.const -1 : i64
    %21 = core.const 0 : i64
    %22 = core.ptr_offset %caps, %21 : ptr<i64, stack>
    core.store %20, %22
    %23 = core.const 1 : i64
    %24 = core.ptr_offset %caps, %23 : ptr<i64, stack>
    core.store %20, %24
    %25 = core.const 0 : i64
    %26 = core.ptr_offset %caps, %25 : ptr<i64, stack>
    core.store %11, %26
    %27 = core.load %p : i64
    %28 = core.cmp.lt %27, %7 : bool
    core.cond_br %28, ^read7, ^fail2
^fail2:
    %29 = core.load %sp : i64
    %30 = core.const 0 : i64
    %31 = core.cmp.eq %29, %30 : bool
    core.cond_br %31, ^exhausted3, ^pop19
^exhausted3:
    %32 = core.const false : bool
    %33 = core.const -1 : i64
    %34 = core.const -1 : i64
    core.ret %32, %33, %34
^success5:
    %35 = core.load %p : i64
    %36 = core.const 1 : i64
    %37 = core.ptr_offset %caps, %36 : ptr<i64, stack>
    core.store %35, %37
    %38 = core.const true : bool
    %39 = core.const 0 : i64
    %40 = core.ptr_offset %caps, %39 : ptr<i64, stack>
    %41 = core.load %40 : i64
    %42 = core.const 1 : i64
    %43 = core.ptr_offset %caps, %42 : ptr<i64, stack>
    %44 = core.load %43 : i64
    core.ret %38, %41, %44
^read7:
    %45 = core.buffer_load %buf, %27 : u8
    %46 = core.cast %45 : i64
    %47 = core.const 65 : i64
    %48 = core.cmp.ge %46, %47 : bool
    %49 = core.const 90 : i64
    %50 = core.cmp.le %46, %49 : bool
    %51 = core.and %48, %50 : bool
    %52 = core.const 95 : i64
    %53 = core.cmp.eq %46, %52 : bool
    %54 = core.const 97 : i64
    %55 = core.cmp.ge %46, %54 : bool
    %56 = core.const 122 : i64
    %57 = core.cmp.le %46, %56 : bool
    %58 = core.and %55, %57 : bool
    %59 = core.or %51, %53 : bool
    %60 = core.or %59, %58 : bool
    core.cond_br %60, ^advance8, ^fail2
^advance8:
    %61 = core.const 1 : i64
    %62 = core.add %27, %61 {overflow = "proven"} : i64
    core.store %62, %p
    %63 = core.load %p : i64
    %64 = core.cmp.le %63, %7 : bool
    core.cond_br %64, ^fits9, ^fail2
^fits9:
    core.br ^scan10(%63)
^scan10(%q: i64):
    %65 = core.cmp.lt %q, %7 : bool
    core.cond_br %65, ^bound11, ^scanned13(%q)
^bound11:
    %66 = core.buffer_load %buf, %q : u8
    %67 = core.cast %66 : i64
    %68 = core.const 0 : i64
    %69 = core.const 192 : i64
    %70 = core.cmp.lt %67, %69 : bool
    %71 = core.const 0 : i64
    %72 = core.select %70, %71, %68 : i64
    %73 = core.const 128 : i64
    %74 = core.cmp.lt %67, %73 : bool
    %75 = core.const 576460745995190270 : i64
    %76 = core.select %74, %75, %72 : i64
    %77 = core.const 64 : i64
    %78 = core.cmp.lt %67, %77 : bool
    %79 = core.const 287948901175001088 : i64
    %80 = core.select %78, %79, %76 : i64
    %81 = core.const 63 : i64
    %82 = core.and %67, %81 : i64
    %83 = core.shr %80, %82 : i64
    %84 = core.const 1 : i64
    %85 = core.and %83, %84 : i64
    %86 = core.const 0 : i64
    %87 = core.cmp.ne %85, %86 : bool
    %88 = core.const 1 : i64
    %89 = core.add %q, %88 {overflow = "proven"} : i64
    core.cond_br %87, ^scan10(%89), ^scanned13(%q)
^scanned13(%0: i64):
    %90 = core.cmp.lt %0, %63 : bool
    core.cond_br %90, ^fail2, ^enough15
^exit14:
    %91 = core.load %p : i64
    %92 = core.cmp.eq %91, %7 : bool
    core.cond_br %92, ^success5, ^fail2
^enough15:
    core.store %0, %p
    %93 = core.cmp.gt %0, %63 : bool
    core.cond_br %93, ^keep17, ^exit14
^giveback16(%held: i64):
    %94 = core.load %p : i64
    %95 = core.const 1 : i64
    %96 = core.sub %94, %95 {overflow = "proven"} : i64
    core.store %96, %p
    %97 = core.cmp.gt %96, %held : bool
    core.cond_br %97, ^keep18, ^exit14
^keep17:
    %98 = core.load %sp : i64
    %99 = core.const 4096 : i64
    %100 = core.cmp.lt %98, %99 : bool
    core.guard %100 {kind = "range", label = "regex.stack.ok"}
    %101 = core.const 3 : i64
    %102 = core.mul %98, %101 {overflow = "proven"} : i64
    %103 = core.ptr_offset %stack, %102 : ptr<i64, stack>
    %104 = core.const 0 : i64
    %105 = core.load %p : i64
    %106 = core.const 0 : i64
    %107 = core.ptr_offset %103, %106 : ptr<i64, stack>
    core.store %104, %107
    %108 = core.const 1 : i64
    %109 = core.ptr_offset %103, %108 : ptr<i64, stack>
    core.store %105, %109
    %110 = core.const 2 : i64
    %111 = core.ptr_offset %103, %110 : ptr<i64, stack>
    core.store %63, %111
    %112 = core.const 1 : i64
    %113 = core.add %98, %112 {overflow = "proven"} : i64
    core.store %113, %sp
    core.br ^exit14
^keep18:
    %114 = core.load %sp : i64
    %115 = core.const 4096 : i64
    %116 = core.cmp.lt %114, %115 : bool
    core.guard %116 {kind = "range", label = "regex.stack.ok"}
    %117 = core.const 3 : i64
    %118 = core.mul %114, %117 {overflow = "proven"} : i64
    %119 = core.ptr_offset %stack, %118 : ptr<i64, stack>
    %120 = core.const 0 : i64
    %121 = core.load %p : i64
    %122 = core.const 0 : i64
    %123 = core.ptr_offset %119, %122 : ptr<i64, stack>
    core.store %120, %123
    %124 = core.const 1 : i64
    %125 = core.ptr_offset %119, %124 : ptr<i64, stack>
    core.store %121, %125
    %126 = core.const 2 : i64
    %127 = core.ptr_offset %119, %126 : ptr<i64, stack>
    core.store %held, %127
    %128 = core.const 1 : i64
    %129 = core.add %114, %128 {overflow = "proven"} : i64
    core.store %129, %sp
    core.br ^exit14
^pop19:
    %130 = core.const 1 : i64
    %131 = core.sub %29, %130 {overflow = "proven"} : i64
    core.store %131, %sp
    %132 = core.const 3 : i64
    %133 = core.mul %131, %132 {overflow = "proven"} : i64
    %134 = core.ptr_offset %stack, %133 : ptr<i64, stack>
    %135 = core.const 0 : i64
    %136 = core.ptr_offset %134, %135 : ptr<i64, stack>
    %137 = core.load %136 : i64
    %138 = core.const 1 : i64
    %139 = core.ptr_offset %134, %138 : ptr<i64, stack>
    %140 = core.load %139 : i64
    core.store %140, %p
    %141 = core.const 2 : i64
    %142 = core.ptr_offset %134, %141 : ptr<i64, stack>
    %143 = core.load %142 : i64
    %144 = core.const 0 : i64
    %145 = core.cmp.eq %137, %144 : bool
    core.cond_br %145, ^giveback16(%143), ^dispatch20
^dispatch20:
    core.unreachable
}

ppy emit c patterns.ppy

1547 lines
/* patterns: generated by ppy, C11 */
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#if defined(__GNUC__) || defined(__clang__)
#define PPY_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
#define PPY_UNREACHABLE() __assume(0)
#else
#define PPY_UNREACHABLE() abort()
#endif

static inline int ppy_ovf_add_i64(int64_t a, int64_t b, int64_t *out) {
#if defined(__GNUC__) || defined(__clang__)
    return __builtin_add_overflow(a, b, out);
#else
    if ((b > 0 && a > INT64_MAX - b) || (b < 0 && a < INT64_MIN - b)) {
        return 1;
    }
    *out = a + b;
    return 0;
#endif
}

static inline int ppy_ovf_sub_i64(int64_t a, int64_t b, int64_t *out) {
#if defined(__GNUC__) || defined(__clang__)
    return __builtin_sub_overflow(a, b, out);
#else
    if ((b < 0 && a > INT64_MAX + b) || (b > 0 && a < INT64_MIN + b)) {
        return 1;
    }
    *out = a - b;
    return 0;
#endif
}

static inline int ppy_ovf_mul_i64(int64_t a, int64_t b, int64_t *out) {
#if defined(__GNUC__) || defined(__clang__)
    return __builtin_mul_overflow(a, b, out);
#else
    if (a > 0) {
        if (b > 0) { if (a > INT64_MAX / b) return 1; }
        else if (b < INT64_MIN / a) return 1;
    } else if (b > 0) {
        if (a < INT64_MIN / b) return 1;
    } else if (a != 0 && b < INT64_MAX / a) return 1;
    *out = a * b;
    return 0;
#endif
}

int32_t ppy_patterns_count_words(uint8_t *text, int64_t text_len, int64_t *out);
int32_t ppy_patterns_longest_word(uint8_t *text, int64_t text_len, int64_t *out);
int32_t ppy_patterns_sum_values(uint8_t *text, int64_t text_len, int64_t *out);
int32_t ppy_patterns_count_hex(uint8_t *text, int64_t text_len, int64_t *out);
int32_t ppy_patterns_is_identifier(uint8_t *text, int64_t text_len, int8_t *out);
int32_t ppy_regex_0(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2);
int32_t ppy_regex_1(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2, int64_t *out3, int64_t *out4, int64_t *out5, int64_t *out6);
int32_t ppy_regex_2(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2);
int32_t ppy_regex_3(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2);

int32_t ppy_patterns_count_words(uint8_t *text, int64_t text_len, int64_t *out) {
    int64_t m_spans[2];

    int64_t n = 0;
    int64_t pos = 0;
    while (1) {
        int8_t res;
        int64_t t1;
        int64_t t2;
        if (ppy_regex_0(text, text_len, pos, text_len, &res, &t1, &t2) != 0) return 1; /* call.ok */
        bool m_found = res != 0;
        m_spans[0] = t1;
        m_spans[1] = t2;
        if (m_found == false) break;
        if (ppy_ovf_add_i64(n, 1, &n)) return 1; /* arith.ok */
        pos = m_spans[1];
    }
    *out = n;
    return 0;
}

int32_t ppy_patterns_longest_word(uint8_t *text, int64_t text_len, int64_t *out) {
    int64_t m_spans[2];

    int64_t best = 0;
    int64_t pos = 0;
    while (pos < text_len) {
        int8_t res;
        int64_t t1;
        int64_t t2;
        if (ppy_regex_0(text, text_len, pos, text_len, &res, &t1, &t2) != 0) return 1; /* call.ok */
        bool m_found = res != 0;
        m_spans[0] = t1;
        m_spans[1] = t2;
        if (m_found ^ true) break;
        int64_t start = m_spans[0];
        int64_t end = m_spans[1];
        int64_t t3;
        if (ppy_ovf_sub_i64(end, start, &t3)) return 1; /* arith.ok */
        best = t3 > best ? t3 : best;
        pos = end;
    }
    *out = best;
    return 0;
}

int32_t ppy_patterns_sum_values(uint8_t *text, int64_t text_len, int64_t *out) {
    int64_t m_spans[6];

    int64_t total = 0;
    int64_t pos = 0;
    while (1) {
        int8_t res;
        int64_t t1;
        int64_t t2;
        int64_t t3;
        int64_t t4;
        int64_t t5;
        int64_t t6;
        if (ppy_regex_1(text, text_len, pos, text_len, &res, &t1, &t2, &t3, &t4, &t5, &t6) != 0) return 1; /* call.ok */
        bool m_found = res != 0;
        m_spans[0] = t1;
        m_spans[1] = t2;
        m_spans[2] = t3;
        m_spans[3] = t4;
        m_spans[4] = t5;
        m_spans[5] = t6;
        if (m_found == false) break;
        int64_t value = 0;
        int64_t t7 = m_spans[4];
        int64_t t8 = m_spans[5];
        if (t7 < 0 || (int64_t)((uint64_t)t8 - 1u) >= text_len) return 1; /* bounds.ok */
        int64_t i = t7;
        while (i < t8) {
            int64_t t9;
            if (ppy_ovf_mul_i64(value, 10, &t9)) return 1; /* arith.ok */
            int64_t t10;
            if (ppy_ovf_sub_i64((int64_t)text[i], 48, &t10)) return 1; /* arith.ok */
            if (ppy_ovf_add_i64(t9, t10, &value)) return 1; /* arith.ok */
            i += 1;
        }
        if (ppy_ovf_add_i64(total, value, &total)) return 1; /* arith.ok */
        pos = m_spans[1];
    }
    *out = total;
    return 0;
}

int32_t ppy_patterns_count_hex(uint8_t *text, int64_t text_len, int64_t *out) {
    int64_t m_spans[2];

    int64_t n = 0;
    int64_t pos = 0;
    while (1) {
        int8_t res;
        int64_t t1;
        int64_t t2;
        if (ppy_regex_2(text, text_len, pos, text_len, &res, &t1, &t2) != 0) return 1; /* call.ok */
        bool m_found = res != 0;
        m_spans[0] = t1;
        m_spans[1] = t2;
        if (m_found == false) break;
        if (ppy_ovf_add_i64(n, 1, &n)) return 1; /* arith.ok */
        pos = m_spans[1];
    }
    *out = n;
    return 0;
}

int32_t ppy_patterns_is_identifier(uint8_t *text, int64_t text_len, int8_t *out) {
    int8_t res;
    int64_t res_2;
    int64_t res_3;
    if (ppy_regex_3(text, text_len, 0, text_len, &res, &res_2, &res_3) != 0) return 1; /* call.ok */
    bool t1 = res != 0;
    *out = (int8_t)t1;
    return 0;
}

int32_t ppy_regex_0(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2) {
    int64_t p;
    int64_t sp;
    int64_t q_2;
    int64_t held;
    int64_t stack[12288];
    int64_t caps[2];
    int64_t t7;
    int64_t t8;

    int64_t t1 = endpos < 0 ? 0 : endpos;
    int64_t t2 = t1 > buf_len ? buf_len : t1;
    int64_t t3 = pos < 0 ? 0 : pos;
    int64_t t4 = t3 > buf_len ? buf_len : t3;
    stack[0] = 0;
    stack[1] = 0;
    stack[2] = 0;
    if (t4 <= t2) {
        int64_t s = t4;
        while (1) {
            p = s;
            int64_t start = s;
            sp = 0;
            caps[0] = -1;
            caps[1] = -1;
            caps[0] = s;
            int64_t t5 = p;
            if (t5 + 1 <= t2) {
                int64_t q = t5;
                while (1) {
                    if (q >= t2) {
                        q_2 = q;
                        break;
                    }
                    int64_t t6 = (int64_t)buf[q];
                    if (((t6 >= 65) & (t6 <= 90)) | ((t6 >= 97) & (t6 <= 122))) {
                        int64_t pass = q + 1;
                        q = pass;
                        continue;
                    }
                    q_2 = q;
                    break;
                }
                t7 = t5 + 1;
                if (q_2 >= t7) break;
                t8 = sp;
                if (t8 == 0) {
                    int64_t t9 = start + 1;
                    if (t9 <= t2) {
                        s = t9;
                        continue;
                    }
                    *out0 = (int8_t)false;
                    *out1 = -1;
                    *out2 = -1;
                    return 0;
                }
                int64_t t10 = t8 - 1;
                sp = t10;
                int64_t *t11 = stack + t10 * 3;
                p = t11[1];
                if (t11[0] == 0) {
                    held = t11[2];
                    int64_t t12 = p - 1;
                    p = t12;
                    if (t12 > held) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t13 = stack + sp * 3;
                        t13[0] = 0;
                        t13[1] = p;
                        t13[2] = held;
                        sp += 1;
                    }
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    return 0;
                }
                PPY_UNREACHABLE();
            } else {
                t8 = sp;
                if (t8 == 0) {
                    int64_t t14 = start + 1;
                    if (t14 <= t2) {
                        s = t14;
                        continue;
                    }
                    *out0 = (int8_t)false;
                    *out1 = -1;
                    *out2 = -1;
                    return 0;
                }
                int64_t t15 = t8 - 1;
                sp = t15;
                int64_t *t16 = stack + t15 * 3;
                p = t16[1];
                if (t16[0] == 0) {
                    held = t16[2];
                    int64_t t17 = p - 1;
                    p = t17;
                    if (t17 > held) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t18 = stack + sp * 3;
                        t18[0] = 0;
                        t18[1] = p;
                        t18[2] = held;
                        sp += 1;
                    }
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    return 0;
                }
                PPY_UNREACHABLE();
            }
        }
        p = q_2;
        if (q_2 > t7) {
            if (sp >= 4096) return 1; /* regex.stack.ok */
            int64_t *t19 = stack + sp * 3;
            t19[0] = 0;
            t19[1] = p;
            t19[2] = t7;
            sp += 1;
        }
        caps[1] = p;
        *out0 = (int8_t)true;
        *out1 = caps[0];
        *out2 = caps[1];
        return 0;
    }
    *out0 = (int8_t)false;
    *out1 = -1;
    *out2 = -1;
    return 0;
}

int32_t ppy_regex_1(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2, int64_t *out3, int64_t *out4, int64_t *out5, int64_t *out6) {
    int64_t p;
    int64_t sp;
    int64_t held;
    int64_t q_2;
    int64_t held_2;
    int64_t held_3;
    int64_t q_3;
    int64_t q_4;
    int64_t held_4;
    int64_t q_5;
    int64_t q_6;
    int64_t held_5;
    int64_t held_6;
    int64_t q_7;
    int64_t q_8;
    int64_t held_7;
    int64_t held_8;
    int64_t q_10;
    int64_t q_12;
    int64_t q_14;
    int64_t stack[12288];
    int64_t caps[6];
    int64_t t48;

    int64_t t1 = endpos < 0 ? 0 : endpos;
    int64_t t2 = t1 > buf_len ? buf_len : t1;
    int64_t t3 = pos < 0 ? 0 : pos;
    int64_t t4 = t3 > buf_len ? buf_len : t3;
    stack[0] = 0;
    stack[1] = 0;
    stack[2] = 0;
    if (t4 <= t2) {
        int64_t s = t4;
        while (1) {
            p = s;
            int64_t start = s;
            sp = 0;
            caps[0] = -1;
            caps[1] = -1;
            caps[2] = -1;
            caps[3] = -1;
            caps[4] = -1;
            caps[5] = -1;
            caps[0] = s;
            int64_t t5 = caps[2];
            if (sp >= 4096) return 1; /* regex.stack.ok */
            int64_t *t6 = stack + sp * 3;
            t6[0] = 0;
            t6[1] = p;
            t6[2] = t5;
            sp += 1;
            caps[2] = p;
            int64_t t7 = p;
            if (t7 + 1 <= t2) {
                int64_t q = t7;
                while (1) {
                    if (q >= t2) {
                        q_2 = q;
                        break;
                    }
                    int64_t t8 = (int64_t)buf[q];
                    if ((((t8 < 64 ? INT64_C(287948901175001088) : (t8 < 128 ? INT64_C(576460745995190270) : (t8 < 192 ? 0 : 0))) >> (t8 & 63)) & 1) != 0) {
                        int64_t pass = q + 1;
                        q = pass;
                        continue;
                    }
                    q_2 = q;
                    break;
                }
                int64_t t9 = t7 + 1;
                if (q_2 >= t9) {
                    p = q_2;
                    if (q_2 > t9) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t10 = stack + sp * 3;
                        t10[0] = 1;
                        t10[1] = p;
                        t10[2] = t9;
                        sp += 1;
                    }
                    int64_t t11 = caps[3];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t12 = stack + sp * 3;
                    t12[0] = 2;
                    t12[1] = p;
                    t12[2] = t11;
                    sp += 1;
                    caps[3] = p;
                    int64_t t13 = p;
                    if (t13 <= t2) {
                        int64_t q_9 = t13;
                        while (1) {
                            if (q_9 >= t2) {
                                q_10 = q_9;
                                break;
                            }
                            int64_t t14 = (int64_t)buf[q_9];
                            if (((t14 >= 9) & (t14 <= 13)) | (t14 == 32)) {
                                int64_t pass_2 = q_9 + 1;
                                q_9 = pass_2;
                                continue;
                            }
                            q_10 = q_9;
                            break;
                        }
                        if (q_10 >= t13) {
                            p = q_10;
                            if (q_10 > t13) {
                                if (sp >= 4096) return 1; /* regex.stack.ok */
                                int64_t *t15 = stack + sp * 3;
                                t15[0] = 3;
                                t15[1] = p;
                                t15[2] = t13;
                                sp += 1;
                            }
                            int64_t t16 = p;
                            if (t16 < t2 && (int64_t)buf[t16] == 61) {
                                p = t16 + 1;
                                int64_t t17 = p;
                                if (t17 <= t2) {
                                    int64_t q_11 = t17;
                                    while (1) {
                                        if (q_11 >= t2) {
                                            q_12 = q_11;
                                            break;
                                        }
                                        int64_t t18 = (int64_t)buf[q_11];
                                        if (((t18 >= 9) & (t18 <= 13)) | (t18 == 32)) {
                                            int64_t pass_3 = q_11 + 1;
                                            q_11 = pass_3;
                                            continue;
                                        }
                                        q_12 = q_11;
                                        break;
                                    }
                                    if (q_12 >= t17) {
                                        p = q_12;
                                        if (q_12 > t17) {
                                            if (sp >= 4096) return 1; /* regex.stack.ok */
                                            int64_t *t19 = stack + sp * 3;
                                            t19[0] = 4;
                                            t19[1] = p;
                                            t19[2] = t17;
                                            sp += 1;
                                        }
                                        int64_t t20 = caps[4];
                                        if (sp >= 4096) return 1; /* regex.stack.ok */
                                        int64_t *t21 = stack + sp * 3;
                                        t21[0] = 5;
                                        t21[1] = p;
                                        t21[2] = t20;
                                        sp += 1;
                                        caps[4] = p;
                                        int64_t t22 = p;
                                        if (t22 + 1 <= t2) {
                                            int64_t q_13 = t22;
                                            while (1) {
                                                if (q_13 >= t2) {
                                                    q_14 = q_13;
                                                    break;
                                                }
                                                int64_t t23 = (int64_t)buf[q_13];
                                                if ((t23 >= 48) & (t23 <= 57)) {
                                                    int64_t pass_4 = q_13 + 1;
                                                    q_13 = pass_4;
                                                    continue;
                                                }
                                                q_14 = q_13;
                                                break;
                                            }
                                            int64_t t24 = t22 + 1;
                                            if (q_14 >= t24) {
                                                p = q_14;
                                                if (q_14 > t24) {
                                                    if (sp >= 4096) return 1; /* regex.stack.ok */
                                                    int64_t *t25 = stack + sp * 3;
                                                    t25[0] = 6;
                                                    t25[1] = p;
                                                    t25[2] = t24;
                                                    sp += 1;
                                                }
                                                int64_t t26 = caps[5];
                                                if (sp >= 4096) return 1; /* regex.stack.ok */
                                                int64_t *t27 = stack + sp * 3;
                                                t27[0] = 7;
                                                t27[1] = p;
                                                t27[2] = t26;
                                                sp += 1;
                                                caps[5] = p;
                                                caps[1] = p;
                                                *out0 = (int8_t)true;
                                                *out1 = caps[0];
                                                *out2 = caps[1];
                                                *out3 = caps[2];
                                                *out4 = caps[3];
                                                *out5 = caps[4];
                                                *out6 = caps[5];
                                                return 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                while (1) {
                    int64_t t28 = sp;
                    if (t28 == 0) break;
                    int64_t t29 = t28 - 1;
                    sp = t29;
                    int64_t *t30 = stack + t29 * 3;
                    int64_t t31 = t30[0];
                    p = t30[1];
                    int64_t t32 = t30[2];
                    if (t31 == 0) {
                        held = t32;
                        caps[2] = held;
                        continue;
                    }
                    if (t31 == 1) {
                        held_2 = t32;
                        int64_t t33 = p - 1;
                        p = t33;
                        if (t33 > held_2) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t34 = stack + sp * 3;
                            t34[0] = 1;
                            t34[1] = p;
                            t34[2] = held_2;
                            sp += 1;
                        }
                        int64_t t35 = caps[3];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t36 = stack + sp * 3;
                        t36[0] = 2;
                        t36[1] = p;
                        t36[2] = t35;
                        sp += 1;
                        caps[3] = p;
                        int64_t t37 = p;
                        if (t37 > t2) continue;
                        q_3 = t37;
                        while (1) {
                            if (q_3 >= t2) {
                                q_4 = q_3;
                                break;
                            }
                            int64_t t38 = (int64_t)buf[q_3];
                            if (((t38 >= 9) & (t38 <= 13)) | (t38 == 32)) {
                                int64_t pass_5 = q_3 + 1;
                                q_3 = pass_5;
                                continue;
                            }
                            q_4 = q_3;
                            break;
                        }
                        if (q_4 < t37) continue;
                        p = q_4;
                        if (q_4 > t37) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t39 = stack + sp * 3;
                            t39[0] = 3;
                            t39[1] = p;
                            t39[2] = t37;
                            sp += 1;
                        }
                        int64_t t40 = p;
                        if (t40 >= t2 || (int64_t)buf[t40] != 61) continue;
                        p = t40 + 1;
                        int64_t t41 = p;
                        if (t41 > t2) continue;
                        q_5 = t41;
                        while (1) {
                            if (q_5 >= t2) {
                                q_6 = q_5;
                                break;
                            }
                            int64_t t42 = (int64_t)buf[q_5];
                            if (((t42 >= 9) & (t42 <= 13)) | (t42 == 32)) {
                                int64_t pass_6 = q_5 + 1;
                                q_5 = pass_6;
                                continue;
                            }
                            q_6 = q_5;
                            break;
                        }
                        if (q_6 < t41) continue;
                        p = q_6;
                        if (q_6 > t41) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t43 = stack + sp * 3;
                            t43[0] = 4;
                            t43[1] = p;
                            t43[2] = t41;
                            sp += 1;
                        }
                        int64_t t44 = caps[4];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t45 = stack + sp * 3;
                        t45[0] = 5;
                        t45[1] = p;
                        t45[2] = t44;
                        sp += 1;
                        caps[4] = p;
                        int64_t t46 = p;
                        if (t46 + 1 > t2) continue;
                        q_7 = t46;
                        while (1) {
                            if (q_7 >= t2) {
                                q_8 = q_7;
                                break;
                            }
                            int64_t t47 = (int64_t)buf[q_7];
                            if ((t47 >= 48) & (t47 <= 57)) {
                                int64_t pass_7 = q_7 + 1;
                                q_7 = pass_7;
                                continue;
                            }
                            q_8 = q_7;
                            break;
                        }
                        t48 = t46 + 1;
                        if (q_8 < t48) continue;
                        p = q_8;
                        if (q_8 > t48) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t49 = stack + sp * 3;
                            t49[0] = 6;
                            t49[1] = p;
                            t49[2] = t48;
                            sp += 1;
                        }
                        int64_t t50 = caps[5];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t51 = stack + sp * 3;
                        t51[0] = 7;
                        t51[1] = p;
                        t51[2] = t50;
                        sp += 1;
                        caps[5] = p;
                        caps[1] = p;
                        *out0 = (int8_t)true;
                        *out1 = caps[0];
                        *out2 = caps[1];
                        *out3 = caps[2];
                        *out4 = caps[3];
                        *out5 = caps[4];
                        *out6 = caps[5];
                        return 0;
                    }
                    if (t31 == 2) {
                        held_3 = t32;
                        caps[3] = held_3;
                        continue;
                    }
                    if (t31 == 3) {
                        held_4 = t32;
                        int64_t t52 = p - 1;
                        p = t52;
                        if (t52 > held_4) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t53 = stack + sp * 3;
                            t53[0] = 3;
                            t53[1] = p;
                            t53[2] = held_4;
                            sp += 1;
                        }
                        int64_t t54 = p;
                        if (t54 >= t2) continue;
                        if ((int64_t)buf[t54] != 61) continue;
                        p = t54 + 1;
                        int64_t t55 = p;
                        if (t55 > t2) continue;
                        q_5 = t55;
                        while (1) {
                            if (q_5 >= t2) {
                                q_6 = q_5;
                                break;
                            }
                            int64_t t56 = (int64_t)buf[q_5];
                            if (((t56 >= 9) & (t56 <= 13)) | (t56 == 32)) {
                                int64_t pass_8 = q_5 + 1;
                                q_5 = pass_8;
                                continue;
                            }
                            q_6 = q_5;
                            break;
                        }
                        if (q_6 < t55) continue;
                        p = q_6;
                        if (q_6 > t55) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t57 = stack + sp * 3;
                            t57[0] = 4;
                            t57[1] = p;
                            t57[2] = t55;
                            sp += 1;
                        }
                        int64_t t58 = caps[4];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t59 = stack + sp * 3;
                        t59[0] = 5;
                        t59[1] = p;
                        t59[2] = t58;
                        sp += 1;
                        caps[4] = p;
                        int64_t t60 = p;
                        if (t60 + 1 > t2) continue;
                        q_7 = t60;
                        while (1) {
                            if (q_7 >= t2) {
                                q_8 = q_7;
                                break;
                            }
                            int64_t t61 = (int64_t)buf[q_7];
                            if ((t61 >= 48) & (t61 <= 57)) {
                                int64_t pass_9 = q_7 + 1;
                                q_7 = pass_9;
                                continue;
                            }
                            q_8 = q_7;
                            break;
                        }
                        t48 = t60 + 1;
                        if (q_8 < t48) continue;
                        p = q_8;
                        if (q_8 > t48) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t62 = stack + sp * 3;
                            t62[0] = 6;
                            t62[1] = p;
                            t62[2] = t48;
                            sp += 1;
                        }
                        int64_t t63 = caps[5];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t64 = stack + sp * 3;
                        t64[0] = 7;
                        t64[1] = p;
                        t64[2] = t63;
                        sp += 1;
                        caps[5] = p;
                        caps[1] = p;
                        *out0 = (int8_t)true;
                        *out1 = caps[0];
                        *out2 = caps[1];
                        *out3 = caps[2];
                        *out4 = caps[3];
                        *out5 = caps[4];
                        *out6 = caps[5];
                        return 0;
                    }
                    if (t31 == 4) {
                        held_5 = t32;
                        int64_t t65 = p - 1;
                        p = t65;
                        if (t65 > held_5) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t66 = stack + sp * 3;
                            t66[0] = 4;
                            t66[1] = p;
                            t66[2] = held_5;
                            sp += 1;
                        }
                        int64_t t67 = caps[4];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t68 = stack + sp * 3;
                        t68[0] = 5;
                        t68[1] = p;
                        t68[2] = t67;
                        sp += 1;
                        caps[4] = p;
                        int64_t t69 = p;
                        if (t69 + 1 > t2) continue;
                        q_7 = t69;
                        while (1) {
                            if (q_7 >= t2) {
                                q_8 = q_7;
                                break;
                            }
                            int64_t t70 = (int64_t)buf[q_7];
                            if ((t70 >= 48) & (t70 <= 57)) {
                                int64_t pass_10 = q_7 + 1;
                                q_7 = pass_10;
                                continue;
                            }
                            q_8 = q_7;
                            break;
                        }
                        t48 = t69 + 1;
                        if (q_8 < t48) continue;
                        p = q_8;
                        if (q_8 > t48) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t71 = stack + sp * 3;
                            t71[0] = 6;
                            t71[1] = p;
                            t71[2] = t48;
                            sp += 1;
                        }
                        int64_t t72 = caps[5];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t73 = stack + sp * 3;
                        t73[0] = 7;
                        t73[1] = p;
                        t73[2] = t72;
                        sp += 1;
                        caps[5] = p;
                        caps[1] = p;
                        *out0 = (int8_t)true;
                        *out1 = caps[0];
                        *out2 = caps[1];
                        *out3 = caps[2];
                        *out4 = caps[3];
                        *out5 = caps[4];
                        *out6 = caps[5];
                        return 0;
                    }
                    if (t31 == 5) {
                        held_6 = t32;
                        caps[4] = held_6;
                        continue;
                    }
                    if (t31 == 6) {
                        held_7 = t32;
                        int64_t t74 = p - 1;
                        p = t74;
                        if (t74 > held_7) {
                            if (sp >= 4096) return 1; /* regex.stack.ok */
                            int64_t *t75 = stack + sp * 3;
                            t75[0] = 6;
                            t75[1] = p;
                            t75[2] = held_7;
                            sp += 1;
                        }
                        int64_t t76 = caps[5];
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t77 = stack + sp * 3;
                        t77[0] = 7;
                        t77[1] = p;
                        t77[2] = t76;
                        sp += 1;
                        caps[5] = p;
                        caps[1] = p;
                        *out0 = (int8_t)true;
                        *out1 = caps[0];
                        *out2 = caps[1];
                        *out3 = caps[2];
                        *out4 = caps[3];
                        *out5 = caps[4];
                        *out6 = caps[5];
                        return 0;
                    }
                    if (t31 == 7) {
                        held_8 = t32;
                        caps[5] = held_8;
                        continue;
                    }
                    PPY_UNREACHABLE();
                }
                int64_t t78 = start + 1;
                if (t78 <= t2) {
                    s = t78;
                    continue;
                }
                *out0 = (int8_t)false;
                *out1 = -1;
                *out2 = -1;
                *out3 = -1;
                *out4 = -1;
                *out5 = -1;
                *out6 = -1;
                return 0;
            }
            while (1) {
                int64_t t79 = sp;
                if (t79 == 0) break;
                int64_t t80 = t79 - 1;
                sp = t80;
                int64_t *t81 = stack + t80 * 3;
                int64_t t82 = t81[0];
                p = t81[1];
                int64_t t83 = t81[2];
                if (t82 == 0) {
                    held = t83;
                    caps[2] = held;
                    continue;
                }
                if (t82 == 1) {
                    held_2 = t83;
                    int64_t t84 = p - 1;
                    p = t84;
                    if (t84 > held_2) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t85 = stack + sp * 3;
                        t85[0] = 1;
                        t85[1] = p;
                        t85[2] = held_2;
                        sp += 1;
                    }
                    int64_t t86 = caps[3];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t87 = stack + sp * 3;
                    t87[0] = 2;
                    t87[1] = p;
                    t87[2] = t86;
                    sp += 1;
                    caps[3] = p;
                    int64_t t88 = p;
                    if (t88 > t2) continue;
                    q_3 = t88;
                    while (1) {
                        if (q_3 >= t2) {
                            q_4 = q_3;
                            break;
                        }
                        int64_t t89 = (int64_t)buf[q_3];
                        if (((t89 >= 9) & (t89 <= 13)) | (t89 == 32)) {
                            int64_t pass_11 = q_3 + 1;
                            q_3 = pass_11;
                            continue;
                        }
                        q_4 = q_3;
                        break;
                    }
                    if (q_4 < t88) continue;
                    p = q_4;
                    if (q_4 > t88) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t90 = stack + sp * 3;
                        t90[0] = 3;
                        t90[1] = p;
                        t90[2] = t88;
                        sp += 1;
                    }
                    int64_t t91 = p;
                    if (t91 >= t2) continue;
                    if ((int64_t)buf[t91] != 61) continue;
                    p = t91 + 1;
                    int64_t t92 = p;
                    if (t92 > t2) continue;
                    q_5 = t92;
                    while (1) {
                        if (q_5 >= t2) {
                            q_6 = q_5;
                            break;
                        }
                        int64_t t93 = (int64_t)buf[q_5];
                        if (((t93 >= 9) & (t93 <= 13)) | (t93 == 32)) {
                            int64_t pass_12 = q_5 + 1;
                            q_5 = pass_12;
                            continue;
                        }
                        q_6 = q_5;
                        break;
                    }
                    if (q_6 < t92) continue;
                    p = q_6;
                    if (q_6 > t92) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t94 = stack + sp * 3;
                        t94[0] = 4;
                        t94[1] = p;
                        t94[2] = t92;
                        sp += 1;
                    }
                    int64_t t95 = caps[4];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t96 = stack + sp * 3;
                    t96[0] = 5;
                    t96[1] = p;
                    t96[2] = t95;
                    sp += 1;
                    caps[4] = p;
                    int64_t t97 = p;
                    if (t97 + 1 > t2) continue;
                    q_7 = t97;
                    while (1) {
                        if (q_7 >= t2) {
                            q_8 = q_7;
                            break;
                        }
                        int64_t t98 = (int64_t)buf[q_7];
                        if ((t98 >= 48) & (t98 <= 57)) {
                            int64_t pass_13 = q_7 + 1;
                            q_7 = pass_13;
                            continue;
                        }
                        q_8 = q_7;
                        break;
                    }
                    t48 = t97 + 1;
                    if (q_8 < t48) continue;
                    p = q_8;
                    if (q_8 > t48) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t99 = stack + sp * 3;
                        t99[0] = 6;
                        t99[1] = p;
                        t99[2] = t48;
                        sp += 1;
                    }
                    int64_t t100 = caps[5];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t101 = stack + sp * 3;
                    t101[0] = 7;
                    t101[1] = p;
                    t101[2] = t100;
                    sp += 1;
                    caps[5] = p;
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    *out3 = caps[2];
                    *out4 = caps[3];
                    *out5 = caps[4];
                    *out6 = caps[5];
                    return 0;
                }
                if (t82 == 2) {
                    held_3 = t83;
                    caps[3] = held_3;
                    continue;
                }
                if (t82 == 3) {
                    held_4 = t83;
                    int64_t t102 = p - 1;
                    p = t102;
                    if (t102 > held_4) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t103 = stack + sp * 3;
                        t103[0] = 3;
                        t103[1] = p;
                        t103[2] = held_4;
                        sp += 1;
                    }
                    int64_t t104 = p;
                    if (t104 >= t2) continue;
                    if ((int64_t)buf[t104] != 61) continue;
                    p = t104 + 1;
                    int64_t t105 = p;
                    if (t105 > t2) continue;
                    q_5 = t105;
                    while (1) {
                        if (q_5 >= t2) {
                            q_6 = q_5;
                            break;
                        }
                        int64_t t106 = (int64_t)buf[q_5];
                        if (((t106 >= 9) & (t106 <= 13)) | (t106 == 32)) {
                            int64_t pass_14 = q_5 + 1;
                            q_5 = pass_14;
                            continue;
                        }
                        q_6 = q_5;
                        break;
                    }
                    if (q_6 < t105) continue;
                    p = q_6;
                    if (q_6 > t105) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t107 = stack + sp * 3;
                        t107[0] = 4;
                        t107[1] = p;
                        t107[2] = t105;
                        sp += 1;
                    }
                    int64_t t108 = caps[4];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t109 = stack + sp * 3;
                    t109[0] = 5;
                    t109[1] = p;
                    t109[2] = t108;
                    sp += 1;
                    caps[4] = p;
                    int64_t t110 = p;
                    if (t110 + 1 > t2) continue;
                    q_7 = t110;
                    while (1) {
                        if (q_7 >= t2) {
                            q_8 = q_7;
                            break;
                        }
                        int64_t t111 = (int64_t)buf[q_7];
                        if ((t111 >= 48) & (t111 <= 57)) {
                            int64_t pass_15 = q_7 + 1;
                            q_7 = pass_15;
                            continue;
                        }
                        q_8 = q_7;
                        break;
                    }
                    t48 = t110 + 1;
                    if (q_8 < t48) continue;
                    p = q_8;
                    if (q_8 > t48) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t112 = stack + sp * 3;
                        t112[0] = 6;
                        t112[1] = p;
                        t112[2] = t48;
                        sp += 1;
                    }
                    int64_t t113 = caps[5];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t114 = stack + sp * 3;
                    t114[0] = 7;
                    t114[1] = p;
                    t114[2] = t113;
                    sp += 1;
                    caps[5] = p;
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    *out3 = caps[2];
                    *out4 = caps[3];
                    *out5 = caps[4];
                    *out6 = caps[5];
                    return 0;
                }
                if (t82 == 4) {
                    held_5 = t83;
                    int64_t t115 = p - 1;
                    p = t115;
                    if (t115 > held_5) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t116 = stack + sp * 3;
                        t116[0] = 4;
                        t116[1] = p;
                        t116[2] = held_5;
                        sp += 1;
                    }
                    int64_t t117 = caps[4];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t118 = stack + sp * 3;
                    t118[0] = 5;
                    t118[1] = p;
                    t118[2] = t117;
                    sp += 1;
                    caps[4] = p;
                    int64_t t119 = p;
                    if (t119 + 1 > t2) continue;
                    q_7 = t119;
                    while (1) {
                        if (q_7 >= t2) {
                            q_8 = q_7;
                            break;
                        }
                        int64_t t120 = (int64_t)buf[q_7];
                        if ((t120 >= 48) & (t120 <= 57)) {
                            int64_t pass_16 = q_7 + 1;
                            q_7 = pass_16;
                            continue;
                        }
                        q_8 = q_7;
                        break;
                    }
                    t48 = t119 + 1;
                    if (q_8 < t48) continue;
                    p = q_8;
                    if (q_8 > t48) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t121 = stack + sp * 3;
                        t121[0] = 6;
                        t121[1] = p;
                        t121[2] = t48;
                        sp += 1;
                    }
                    int64_t t122 = caps[5];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t123 = stack + sp * 3;
                    t123[0] = 7;
                    t123[1] = p;
                    t123[2] = t122;
                    sp += 1;
                    caps[5] = p;
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    *out3 = caps[2];
                    *out4 = caps[3];
                    *out5 = caps[4];
                    *out6 = caps[5];
                    return 0;
                }
                if (t82 == 5) {
                    held_6 = t83;
                    caps[4] = held_6;
                    continue;
                }
                if (t82 == 6) {
                    held_7 = t83;
                    int64_t t124 = p - 1;
                    p = t124;
                    if (t124 > held_7) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t125 = stack + sp * 3;
                        t125[0] = 6;
                        t125[1] = p;
                        t125[2] = held_7;
                        sp += 1;
                    }
                    int64_t t126 = caps[5];
                    if (sp >= 4096) return 1; /* regex.stack.ok */
                    int64_t *t127 = stack + sp * 3;
                    t127[0] = 7;
                    t127[1] = p;
                    t127[2] = t126;
                    sp += 1;
                    caps[5] = p;
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    *out3 = caps[2];
                    *out4 = caps[3];
                    *out5 = caps[4];
                    *out6 = caps[5];
                    return 0;
                }
                if (t82 == 7) {
                    held_8 = t83;
                    caps[5] = held_8;
                    continue;
                }
                PPY_UNREACHABLE();
            }
            int64_t t128 = start + 1;
            if (t128 <= t2) {
                s = t128;
                continue;
            }
            *out0 = (int8_t)false;
            *out1 = -1;
            *out2 = -1;
            *out3 = -1;
            *out4 = -1;
            *out5 = -1;
            *out6 = -1;
            return 0;
        }
        p = q_8;
        if (q_8 > t48) {
            if (sp >= 4096) return 1; /* regex.stack.ok */
            int64_t *t129 = stack + sp * 3;
            t129[0] = 6;
            t129[1] = p;
            t129[2] = t48;
            sp += 1;
        }
        int64_t t130 = caps[5];
        if (sp >= 4096) return 1; /* regex.stack.ok */
        int64_t *t131 = stack + sp * 3;
        t131[0] = 7;
        t131[1] = p;
        t131[2] = t130;
        sp += 1;
        caps[5] = p;
        caps[1] = p;
        *out0 = (int8_t)true;
        *out1 = caps[0];
        *out2 = caps[1];
        *out3 = caps[2];
        *out4 = caps[3];
        *out5 = caps[4];
        *out6 = caps[5];
        return 0;
    }
    *out0 = (int8_t)false;
    *out1 = -1;
    *out2 = -1;
    *out3 = -1;
    *out4 = -1;
    *out5 = -1;
    *out6 = -1;
    return 0;
}

int32_t ppy_regex_2(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2) {
    int64_t p;
    int64_t sp;
    int64_t q_2;
    int64_t held;
    int64_t stack[12288];
    int64_t caps[2];
    int64_t t10;
    int64_t t11;

    int64_t t1 = endpos < 0 ? 0 : endpos;
    int64_t t2 = t1 > buf_len ? buf_len : t1;
    int64_t t3 = pos < 0 ? 0 : pos;
    int64_t t4 = t3 > buf_len ? buf_len : t3;
    stack[0] = 0;
    stack[1] = 0;
    stack[2] = 0;
    if (t4 <= t2) {
        int64_t s = t4;
        while (1) {
            p = s;
            int64_t start = s;
            sp = 0;
            caps[0] = -1;
            caps[1] = -1;
            caps[0] = s;
            int64_t t5 = p;
            if (t5 < t2 && (int64_t)buf[t5] == 48) {
                p = t5 + 1;
                int64_t t6 = p;
                if (t6 < t2) {
                    int64_t t7 = (int64_t)buf[t6];
                    if ((t7 == 88) | (t7 == 120)) {
                        p = t6 + 1;
                        int64_t t8 = p;
                        if (t8 + 1 <= t2) {
                            int64_t q = t8;
                            while (1) {
                                if (q >= t2) {
                                    q_2 = q;
                                    break;
                                }
                                int64_t t9 = (int64_t)buf[q];
                                if ((((t9 >= 48) & (t9 <= 57)) | ((t9 >= 65) & (t9 <= 70))) | ((t9 >= 97) & (t9 <= 102))) {
                                    int64_t pass = q + 1;
                                    q = pass;
                                    continue;
                                }
                                q_2 = q;
                                break;
                            }
                            t10 = t8 + 1;
                            if (q_2 >= t10) break;
                        }
                    }
                }
                t11 = sp;
                if (t11 == 0) {
                    int64_t t12 = start + 1;
                    if (t12 <= t2) {
                        s = t12;
                        continue;
                    }
                    *out0 = (int8_t)false;
                    *out1 = -1;
                    *out2 = -1;
                    return 0;
                }
                int64_t t13 = t11 - 1;
                sp = t13;
                int64_t *t14 = stack + t13 * 3;
                p = t14[1];
                if (t14[0] == 0) {
                    held = t14[2];
                    int64_t t15 = p - 1;
                    p = t15;
                    if (t15 > held) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t16 = stack + sp * 3;
                        t16[0] = 0;
                        t16[1] = p;
                        t16[2] = held;
                        sp += 1;
                    }
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    return 0;
                }
                PPY_UNREACHABLE();
            } else {
                t11 = sp;
                if (t11 == 0) {
                    int64_t t17 = start + 1;
                    if (t17 <= t2) {
                        s = t17;
                        continue;
                    }
                    *out0 = (int8_t)false;
                    *out1 = -1;
                    *out2 = -1;
                    return 0;
                }
                int64_t t18 = t11 - 1;
                sp = t18;
                int64_t *t19 = stack + t18 * 3;
                p = t19[1];
                if (t19[0] == 0) {
                    held = t19[2];
                    int64_t t20 = p - 1;
                    p = t20;
                    if (t20 > held) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t21 = stack + sp * 3;
                        t21[0] = 0;
                        t21[1] = p;
                        t21[2] = held;
                        sp += 1;
                    }
                    caps[1] = p;
                    *out0 = (int8_t)true;
                    *out1 = caps[0];
                    *out2 = caps[1];
                    return 0;
                }
                PPY_UNREACHABLE();
            }
        }
        p = q_2;
        if (q_2 > t10) {
            if (sp >= 4096) return 1; /* regex.stack.ok */
            int64_t *t22 = stack + sp * 3;
            t22[0] = 0;
            t22[1] = p;
            t22[2] = t10;
            sp += 1;
        }
        caps[1] = p;
        *out0 = (int8_t)true;
        *out1 = caps[0];
        *out2 = caps[1];
        return 0;
    }
    *out0 = (int8_t)false;
    *out1 = -1;
    *out2 = -1;
    return 0;
}

int32_t ppy_regex_3(uint8_t *buf, int64_t buf_len, int64_t pos, int64_t endpos, int8_t *out0, int64_t *out1, int64_t *out2) {
    int64_t q_2;
    int64_t stack[12288];
    int64_t caps[2];

    int64_t t1 = endpos < 0 ? 0 : endpos;
    int64_t t2 = t1 > buf_len ? buf_len : t1;
    int64_t t3 = pos < 0 ? 0 : pos;
    int64_t t4 = t3 > buf_len ? buf_len : t3;
    stack[0] = 0;
    stack[1] = 0;
    stack[2] = 0;
    int64_t p = t4;
    int64_t sp = 0;
    caps[0] = -1;
    caps[1] = -1;
    caps[0] = t4;
    int64_t t5 = p;
    if (t5 < t2) {
        int64_t t6 = (int64_t)buf[t5];
        if ((((t6 >= 65) & (t6 <= 90)) | (t6 == 95)) | ((t6 >= 97) & (t6 <= 122))) {
            p = t5 + 1;
            int64_t t7 = p;
            if (t7 <= t2) {
                int64_t q = t7;
                while (1) {
                    if (q >= t2) {
                        q_2 = q;
                        break;
                    }
                    int64_t t8 = (int64_t)buf[q];
                    if ((((t8 < 64 ? INT64_C(287948901175001088) : (t8 < 128 ? INT64_C(576460745995190270) : (t8 < 192 ? 0 : 0))) >> (t8 & 63)) & 1) != 0) {
                        int64_t pass = q + 1;
                        q = pass;
                        continue;
                    }
                    q_2 = q;
                    break;
                }
                if (q_2 >= t7) {
                    p = q_2;
                    if (q_2 > t7) {
                        if (sp >= 4096) return 1; /* regex.stack.ok */
                        int64_t *t9 = stack + sp * 3;
                        t9[0] = 0;
                        t9[1] = p;
                        t9[2] = t7;
                        sp += 1;
                    }
                    if (p == t2) {
                        caps[1] = p;
                        *out0 = (int8_t)true;
                        *out1 = caps[0];
                        *out2 = caps[1];
                        return 0;
                    }
                }
            }
        }
    }
    while (1) {
        int64_t t10 = sp;
        if (t10 == 0) break;
        int64_t t11 = t10 - 1;
        sp = t11;
        int64_t *t12 = stack + t11 * 3;
        p = t12[1];
        if (t12[0] == 0) {
            int64_t held = t12[2];
            int64_t t13 = p - 1;
            p = t13;
            if (t13 > held) {
                if (sp >= 4096) return 1; /* regex.stack.ok */
                int64_t *t14 = stack + sp * 3;
                t14[0] = 0;
                t14[1] = p;
                t14[2] = held;
                sp += 1;
            }
            if (p != t2) continue;
            caps[1] = p;
            *out0 = (int8_t)true;
            *out1 = caps[0];
            *out2 = caps[1];
            return 0;
        }
        PPY_UNREACHABLE();
    }
    *out0 = (int8_t)false;
    *out1 = -1;
    *out2 = -1;
    return 0;
}

One pattern, one matcher

WORD = re.compile(rb"[A-Za-z]+")
PAIR = re.compile(rb"(?P<key>\w+)\s*=\s*(\d+)")


def sum_values(text: Buffer[ppy.u8]) -> int:
    total = 0
    pos = 0
    while True:
        m = PAIR.search(text, pos)
        if m is None:
            return total
        value = 0
        for i in range(m.start(2), m.end(2)):
            value = value * 10 + (text[i] - 48)
        total += value
        pos = m.end()

PAIR.search(text, pos) becomes a regex.search operation in the IR. The lower-regex pass compiles the pattern into a private function of core operations, so the LLVM and C backends run it without a regex library.

The match is a local. m is None and if m: narrow it, and m.start(2), m.end(), and start, end = m.span() are loads.

The matcher backtracks the way re does (ordered alternation, greedy and lazy repeats, a group's last iteration), so the spans agree byte for byte. A repeat of one byte class such as [A-Za-z]+ scans its run rather than pushing per byte.

The same source runs unchanged on CPython, where re accepts the array.array("B") the buffer is.

Compared with CPython's re and Rust's regex

The benchmark makes four passes over 400,000 generated lines (7.9 MB): count the words, find the longest, sum the name = value pairs, count the hex numbers. The programs are in compare/: patterns_bench.ppy, patterns_re.py, and patterns.rs with its Cargo.toml. The site shows them whole. Times are in milliseconds, best of five passes, mean and spread over five processes.

PPy: a pattern compiled from a bytes literal, search from a position, m.end(). The same file runs on CPython.

def sum_values(text: Buffer[ppy.u8]) -> int:
    total = 0
    pos = 0
    while True:
        m = PAIR.search(text, pos)
        if m is None:
            return total
        value = 0
        for i in range(m.start(2), m.end(2)):
            value = value * 10 + (text[i] - 48)
        total += value
        pos = m.end()

CPython re: the idiomatic spelling, finditer and int(m.group(2)).

def sum_values(text: bytes) -> int:
    return sum(int(m.group(2)) for m in PAIR.finditer(text))

Rust regex: captures_iter over &[u8], the number parsed from the captured bytes, built with cargo build --release.

fn sum_values(pair: &Regex, text: &[u8]) -> u64 {
    pair.captures_iter(text)
        .map(|c| std::str::from_utf8(&c[2]).unwrap().parse::<u64>().unwrap())
        .sum()
}
PPy ppy run CPython re Rust regex
count_words 6.57 ± 0.05 107.07 ± 1.27 18.23 ± 0.13
longest_word 6.53 ± 0.05 133.05 ± 8.69 36.85 ± 2.99
sum_values 56.70 ± 0.33 198.20 ± 1.86 65.62 ± 0.79
count_hex 6.59 ± 0.03 10.22 ± 0.10 2.33 ± 0.10

PPy compiles each pattern into a function of core operations: a backtracker in which a byte-class repeat scans its run instead of stepping byte by byte. A match is a few locals rather than an object, so a search loop is a native loop. That is the whole distance to re, whose matcher is a bytecode interpreter allocating a match object per hit.

Against Rust, the pattern decides:

  • [A-Za-z]+ is a scan in both engines.
  • \w+\s*=\s*(\d+) makes regex fall back to its slower capturing engine.
  • 0x[0-9a-f]+ under (?i) is where its lazy DFA wins outright. A literal-prefix prefilter is the obvious next step for the PPy matcher there.

Intel Core Ultra 9 386H; rustc 1.95.0 with regex 1.x, CPython 3.12.13 for re, PPy on CPython 3.13.13, from a checkout on a native filesystem.

What stays with Python

m.group() returns bytes, which has no native form, so a function that calls it keeps running on Python. Read the buffer between start() and end() instead.

  • Backreferences, lookahead and lookbehind, atomic groups, and possessive repeats are refused when the pattern is analysed, and ppy explain names the construct.
  • A match that would need more than the matcher's stack of 4096 entries ((a|b)*c over a long input without a c) fails a guard, and the function falls back to re for that call.

Read on: Regular expressions · The IR: the regex dialect · Substring search

patterns.ppy is hand-written; there is no .py source and no conversion step.

43_regex/patterns.ppy

"""Regular expressions over a byte buffer, compiled to native matchers."""

import array
import re
import time

import ppy
from ppy import Buffer

WORD = re.compile(rb"[A-Za-z]+")
PAIR = re.compile(rb"(?P<key>\w+)\s*=\s*(\d+)")
HEX = re.compile(rb"0x[0-9a-f]+", re.IGNORECASE)


def count_words(text: Buffer[ppy.u8]) -> int:
    n = 0
    pos = 0
    while True:
        m = WORD.search(text, pos)
        if m is None:
            return n
        n += 1
        pos = m.end()


def longest_word(text: Buffer[ppy.u8]) -> int:
    best = 0
    pos = 0
    while pos < len(text):
        m = WORD.search(text, pos)
        if not m:
            break
        start, end = m.span()
        best = max(best, end - start)
        pos = end
    return best


def sum_values(text: Buffer[ppy.u8]) -> int:
    total = 0
    pos = 0
    while True:
        m = PAIR.search(text, pos)
        if m is None:
            return total
        value = 0
        for i in range(m.start(2), m.end(2)):
            value = value * 10 + (text[i] - 48)
        total += value
        pos = m.end()


def count_hex(text: Buffer[ppy.u8]) -> int:
    n = 0
    pos = 0
    while True:
        m = HEX.search(text, pos)
        if m is None:
            return n
        n += 1
        pos = m.end()


def is_identifier(text: Buffer[ppy.u8]) -> bool:
    return re.fullmatch(rb"[A-Za-z_]\w*", text) is not None


def make_text(lines: int) -> Buffer[ppy.u8]:
    """`lines` lines of `name = value` pairs, words, and hex numbers, the same every run."""
    words = [b"alpha", b"beta", b"gamma", b"delta", b"epsilon", b"zeta", b"eta", b"theta"]
    parts = []
    state = 12345
    for i in range(lines):
        state = (state * 1103515245 + 12345) % (1 << 31)
        word = words[state % len(words)]
        parts.append(word + b" = " + str(state % 1000).encode() + b"  " + word.upper())
        if i % 7 == 0:
            parts.append(b"0x" + format(state, "x").encode())
        parts.append(b"\n")
    return memoryview(array.array("B", b"".join(parts)))


def main() -> None:
    text = make_text(50_000)
    print(len(text), "bytes")
    started = time.perf_counter()
    print(count_words(text), longest_word(text), sum_values(text), count_hex(text))
    elapsed = time.perf_counter() - started
    snake = array.array("B", b"snake_case9")
    lives = array.array("B", b"9lives")
    print(is_identifier(snake), is_identifier(lives))
    print(f"# four passes: {elapsed * 1000:.1f} ms")


main()

Counterpart programs

The programs the comparison above measured, each written the way its tool expects. The PPy one is first.

patterns_bench.ppy (PPy)
"""The four passes of `patterns.ppy` over 400,000 lines, timed: the PPY side of the comparison."""

import array
import re
import time

import ppy
from ppy import Buffer

WORD = re.compile(rb"[A-Za-z]+")
PAIR = re.compile(rb"(?P<key>\w+)\s*=\s*(\d+)")
HEX = re.compile(rb"0x[0-9a-f]+", re.IGNORECASE)


def count_words(text: Buffer[ppy.u8]) -> int:
    n = 0
    pos = 0
    while True:
        m = WORD.search(text, pos)
        if m is None:
            return n
        n += 1
        pos = m.end()


def longest_word(text: Buffer[ppy.u8]) -> int:
    best = 0
    pos = 0
    while pos < len(text):
        m = WORD.search(text, pos)
        if not m:
            break
        start, end = m.span()
        best = max(best, end - start)
        pos = end
    return best


def sum_values(text: Buffer[ppy.u8]) -> int:
    total = 0
    pos = 0
    while True:
        m = PAIR.search(text, pos)
        if m is None:
            return total
        value = 0
        for i in range(m.start(2), m.end(2)):
            value = value * 10 + (text[i] - 48)
        total += value
        pos = m.end()


def count_hex(text: Buffer[ppy.u8]) -> int:
    n = 0
    pos = 0
    while True:
        m = HEX.search(text, pos)
        if m is None:
            return n
        n += 1
        pos = m.end()


def make_text(lines: int) -> Buffer[ppy.u8]:
    """`lines` lines of `name = value` pairs, words, and hex numbers, the same every run."""
    words = [b"alpha", b"beta", b"gamma", b"delta", b"epsilon", b"zeta", b"eta", b"theta"]
    parts = []
    state = 12345
    for i in range(lines):
        state = (state * 1103515245 + 12345) % (1 << 31)
        word = words[state % len(words)]
        parts.append(word + b" = " + str(state % 1000).encode() + b"  " + word.upper())
        if i % 7 == 0:
            parts.append(b"0x" + format(state, "x").encode())
        parts.append(b"\n")
    return memoryview(array.array("B", b"".join(parts)))


def main() -> None:
    text = make_text(400_000)
    for _ in range(2):
        count_words(text)
        longest_word(text)
        sum_values(text)
        count_hex(text)
    print(len(text))
    best = [1e9, 1e9, 1e9, 1e9]
    answers = [0, 0, 0, 0]
    for _ in range(5):
        started = time.perf_counter()
        answers[0] = count_words(text)
        best[0] = min(best[0], (time.perf_counter() - started) * 1000.0)
        started = time.perf_counter()
        answers[1] = longest_word(text)
        best[1] = min(best[1], (time.perf_counter() - started) * 1000.0)
        started = time.perf_counter()
        answers[2] = sum_values(text)
        best[2] = min(best[2], (time.perf_counter() - started) * 1000.0)
        started = time.perf_counter()
        answers[3] = count_hex(text)
        best[3] = min(best[3], (time.perf_counter() - started) * 1000.0)
    labels = ["count_words", "longest_word", "sum_values", "count_hex"]
    for label, answer, took in zip(labels, answers, best, strict=True):
        print(f"# {label}: {took:.2f} ms")
        print(answer)


main()
patterns.rs (Rust)
//! The same four passes with Rust's `regex` crate over bytes.

use regex::bytes::{Regex, RegexBuilder};
use std::time::Instant;

fn make_text(lines: u64) -> Vec<u8> {
    let words: [&[u8]; 8] = [
        b"alpha", b"beta", b"gamma", b"delta", b"epsilon", b"zeta", b"eta", b"theta",
    ];
    let mut out = Vec::new();
    let mut state: u64 = 12345;
    for i in 0..lines {
        state = (state * 1103515245 + 12345) % (1 << 31);
        let word = words[(state % 8) as usize];
        out.extend_from_slice(word);
        out.extend_from_slice(b" = ");
        out.extend_from_slice((state % 1000).to_string().as_bytes());
        out.extend_from_slice(b"  ");
        out.extend_from_slice(word.to_ascii_uppercase().as_slice());
        if i % 7 == 0 {
            out.extend_from_slice(format!("0x{:x}", state).as_bytes());
        }
        out.push(b'\n');
    }
    out
}

fn count_words(word: &Regex, text: &[u8]) -> u64 {
    word.find_iter(text).count() as u64
}

fn longest_word(word: &Regex, text: &[u8]) -> u64 {
    word.find_iter(text).map(|m| m.len() as u64).max().unwrap_or(0)
}

fn sum_values(pair: &Regex, text: &[u8]) -> u64 {
    pair.captures_iter(text)
        .map(|c| std::str::from_utf8(&c[2]).unwrap().parse::<u64>().unwrap())
        .sum()
}

fn count_hex(hex: &Regex, text: &[u8]) -> u64 {
    hex.find_iter(text).count() as u64
}

fn timed(label: &str, mut run: impl FnMut() -> u64) -> u64 {
    let mut best = f64::INFINITY;
    let mut answer = 0;
    for _ in 0..5 {
        let started = Instant::now();
        answer = run();
        best = best.min(started.elapsed().as_secs_f64());
    }
    println!("# {}: {:.2} ms", label, best * 1000.0);
    answer
}

fn main() {
    let word = Regex::new(r"[A-Za-z]+").unwrap();
    let pair = Regex::new(r"(?P<key>\w+)\s*=\s*(\d+)").unwrap();
    let hex = RegexBuilder::new(r"0x[0-9a-f]+").case_insensitive(true).build().unwrap();
    let text = make_text(400_000);
    println!("{}", text.len());
    println!("{}", timed("count_words", || count_words(&word, &text)));
    println!("{}", timed("longest_word", || longest_word(&word, &text)));
    println!("{}", timed("sum_values", || sum_values(&pair, &text)));
    println!("{}", timed("count_hex", || count_hex(&hex, &text)));
}
patterns_codon.py (Python)
"""The `re` program as Codon compiles it: the same source, Codon's own `re`."""

import re
import time

WORD = re.compile(rb"[A-Za-z]+")
PAIR = re.compile(rb"(?P<key>\w+)\s*=\s*(\d+)")
HEX = re.compile(rb"0x[0-9a-f]+", re.IGNORECASE)


def count_words(text: bytes) -> int:
    return sum(1 for _ in WORD.finditer(text))


def longest_word(text: bytes) -> int:
    return max((m.end() - m.start() for m in WORD.finditer(text)), default=0)


def sum_values(text: bytes) -> int:
    return sum(int(m.group(2)) for m in PAIR.finditer(text))


def count_hex(text: bytes) -> int:
    return sum(1 for _ in HEX.finditer(text))


def make_text(lines: int) -> bytes:
    words = [b"alpha", b"beta", b"gamma", b"delta", b"epsilon", b"zeta", b"eta", b"theta"]
    parts = []
    state = 12345
    for i in range(lines):
        state = (state * 1103515245 + 12345) % (1 << 31)
        word = words[state % len(words)]
        parts.append(word + b" = " + str(state % 1000).encode() + b"  " + word.upper())
        if i % 7 == 0:
            parts.append(b"0x" + format(state, "x").encode())
        parts.append(b"\n")
    return b"".join(parts)


def timed(label, run, text):
    best = 1e9
    answer = 0
    for _ in range(5):
        started = time.perf_counter()
        answer = run(text)
        best = min(best, time.perf_counter() - started)
    print(f"# {label}: {best * 1000:.2f} ms")
    return answer


def main():
    text = make_text(400_000)
    print(len(text))
    print(timed("count_words", count_words, text))
    print(timed("longest_word", longest_word, text))
    print(timed("sum_values", sum_values, text))
    print(timed("count_hex", count_hex, text))


main()
patterns_re.py (Python)
"""The same four passes with CPython's `re`, written the way `re` is written."""

import re
import time

WORD = re.compile(rb"[A-Za-z]+")
PAIR = re.compile(rb"(?P<key>\w+)\s*=\s*(\d+)")
HEX = re.compile(rb"0x[0-9a-f]+", re.IGNORECASE)


def count_words(text: bytes) -> int:
    return sum(1 for _ in WORD.finditer(text))


def longest_word(text: bytes) -> int:
    return max((m.end() - m.start() for m in WORD.finditer(text)), default=0)


def sum_values(text: bytes) -> int:
    return sum(int(m.group(2)) for m in PAIR.finditer(text))


def count_hex(text: bytes) -> int:
    return sum(1 for _ in HEX.finditer(text))


def make_text(lines: int) -> bytes:
    words = [b"alpha", b"beta", b"gamma", b"delta", b"epsilon", b"zeta", b"eta", b"theta"]
    parts = []
    state = 12345
    for i in range(lines):
        state = (state * 1103515245 + 12345) % (1 << 31)
        word = words[state % len(words)]
        parts.append(word + b" = " + str(state % 1000).encode() + b"  " + word.upper())
        if i % 7 == 0:
            parts.append(b"0x" + format(state, "x").encode())
        parts.append(b"\n")
    return b"".join(parts)


def timed(label, run, text):
    best = 1e9
    answer = 0
    for _ in range(5):
        started = time.perf_counter()
        answer = run(text)
        best = min(best, time.perf_counter() - started)
    print(f"# {label}: {best * 1000:.2f} ms")
    return answer


def main():
    text = make_text(400_000)
    print(len(text))
    print(timed("count_words", count_words, text))
    print(timed("longest_word", longest_word, text))
    print(timed("sum_values", sum_values, text))
    print(timed("count_hex", count_hex, text))


main()

Source: examples/43_regex.