#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ luaL_arg_check(int, int, char *);
char *luaL_check_string(int);
scalar_t__ luaL_opt_number(int, int);
scalar_t__ lua_lua2C(int);
int /*<<< orphan*/ lua_pushnumber(int);
char *match(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ push_captures();
int strlen(char *);
int /*<<< orphan*/ *strpbrk(char *, char *);
char *strstr(char *, char *);
void str_find(void) {
  char *s = luaL_check_string(1);
  char *p = luaL_check_string(2);
  long init = (long)luaL_opt_number(3, 1) - 1;
  luaL_arg_check(0 <= init && init <= strlen(s), 3, "out of range");
  if (lua_lua2C(4) != 0 || strpbrk(p, "^$*?.([%-") == NULL) {
    char *s2 = strstr(s + init, p);
    if (s2) {
      lua_pushnumber(s2 - s + 1);
      lua_pushnumber(s2 - s + strlen(p));
    }
  } else {
    int anchor = (*p == '^') ? (p++, 1) : 0;
    char *s1 = s + init;
    do {
      char *res;
      if ((res = match(s1, p, 0)) != NULL) {
        lua_pushnumber(s1 - s + 1);
        lua_pushnumber(res - s);
        push_captures();
        return;
      }
    } while (*s1++ && !anchor);
  }
}


x86 assembly (clang, -O0):
<str_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    $0x1,%edi
call   1050 <luaL_check_string@plt>
mov    %rax,-0x8(%rbp)
mov    $0x2,%edi
call   1050 <luaL_check_string@plt>
mov    %rax,-0x10(%rbp)
mov    $0x3,%edi
mov    $0x1,%esi
call   1030 <luaL_opt_number@plt>
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
xor    %ecx,%ecx
cmp    -0x18(%rbp),%rcx
mov    %al,-0x39(%rbp)
jg     120f <str_find+0x6f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
setle  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
mov    $0x3,%esi
lea    0xddd(%rip),%rdx
call   1090 <luaL_arg_check@plt>
mov    $0x4,%edi
call   10b0 <lua_lua2C@plt>
cmp    $0x0,%rax
jne    1256 <str_find+0xb6>
mov    -0x10(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   1080 <strpbrk@plt>
cmp    $0x0,%rax
jne    12bc <str_find+0x11c>
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12b7 <str_find+0x117>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <lua_pushnumber@plt>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,%edi
call   1060 <lua_pushnumber@plt>
jmp    1393 <str_find+0x1f3>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5e,%eax
jne    12e5 <str_find+0x145>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12ef <str_find+0x14f>
xor    %eax,%eax
mov    %eax,-0x54(%rbp)
jmp    12ef <str_find+0x14f>
mov    -0x54(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   10a0 <match@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1352 <str_find+0x1b2>
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <lua_pushnumber@plt>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edi
call   1060 <lua_pushnumber@plt>
mov    $0x0,%al
call   1070 <push_captures@plt>
jmp    1393 <str_find+0x1f3>
jmp    1357 <str_find+0x1b7>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x55(%rbp)
je     1383 <str_find+0x1e3>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1301 <str_find+0x161>
jmp    1393 <str_find+0x1f3>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_find>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%edi
call   1050 <luaL_check_string@plt>
mov    %rax,%rbx
mov    $0x2,%edi
call   1050 <luaL_check_string@plt>
mov    %rax,%r15
mov    $0x3,%edi
mov    $0x1,%esi
call   1030 <luaL_opt_number@plt>
mov    %rax,%r12
lea    -0x1(%rax),%rbp
test   %rax,%rax
jle    11f5 <str_find+0x55>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
xor    %edi,%edi
cmp    %rax,%rbp
setle  %dil
jmp    11f7 <str_find+0x57>
xor    %edi,%edi
lea    0xe02(%rip),%rdx
mov    $0x3,%esi
call   1090 <luaL_arg_check@plt>
mov    $0x4,%edi
call   10b0 <lua_lua2C@plt>
test   %rax,%rax
je     125e <str_find+0xbe>
mov    %rbx,%r14
add    %rbp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     12b5 <str_find+0x115>
mov    %rax,%rbx
sub    %r14d,%ebx
lea    0x1(%rbx),%edi
call   1060 <lua_pushnumber@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %eax,%ebx
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <lua_pushnumber@plt>
lea    0xda8(%rip),%rsi
mov    %r15,%rdi
call   1080 <strpbrk@plt>
test   %rax,%rax
je     1217 <str_find+0x77>
xor    %eax,%eax
cmpb   $0x5e,(%r15)
setne  %bpl
sete   %al
add    %rax,%r15
mov    %rbx,%rax
mov    %rbx,(%rsp)
mov    %rbx,%r14
add    $0xffffffffffffffff,%r14
lea    (%r14,%r12,1),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   10a0 <match@plt>
test   %rax,%rax
jne    12c4 <str_find+0x124>
cmpb   $0x0,(%rbx)
setne  %al
add    $0x1,%r12
test   %bpl,%al
jne    1290 <str_find+0xf0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r13
mov    %r12d,%edi
call   1060 <lua_pushnumber@plt>
sub    (%rsp),%r13d
mov    %r13d,%edi
call   1060 <lua_pushnumber@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <push_captures@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"out of range\00"
@.str.1 = [10 x i8] c"^$*?.([%-\00"
define void @str_find() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = call i8* @luaL_check_string(i32 1)
store i8* %8, i8** %1
%9 = call i8* @luaL_check_string(i32 2)
store i8* %9, i8** %2
%10 = call i64 @luaL_opt_number(i32 3, i32 1)
%11 = sub nsw i64 %10, 1
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp sle i64 0, %12
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = load i64, i64* %3
%16 = load i8*, i8** %1
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = icmp sle i64 %15, %18
br label %20
20: ; preds = %14, %0
%21 = phi i1 [ false, %0 ], [ %19, %14 ]
%22 = zext i1 %21 to i32
%23 = call i32 @luaL_arg_check(i32 %22, i32 3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%24 = call i64 @lua_lua2C(i32 4)
%25 = icmp ne i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = load i8*, i8** %2
%28 = call i32* @strpbrk(i8* %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %59
30: ; preds = %26, %20
%31 = load i8*, i8** %1
%32 = load i64, i64* %3
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8*, i8** %2
%35 = call i8* @strstr(i8* %33, i8* %34)
store i8* %35, i8** %4
%36 = load i8*, i8** %4
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %58
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = load i8*, i8** %1
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = add nsw i64 %43, 1
%45 = trunc i64 %44 to i32
%46 = call i32 @lua_pushnumber(i32 %45)
%47 = load i8*, i8** %4
%48 = load i8*, i8** %1
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = load i8*, i8** %2
%53 = call i32 @strlen(i8* %52)
%54 = sext i32 %53 to i64
%55 = add nsw i64 %51, %54
%56 = trunc i64 %55 to i32
%57 = call i32 @lua_pushnumber(i32 %56)
br label %58
58: ; preds = %38, %30
br label %109
59: ; preds = %26
%60 = load i8*, i8** %2
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 94
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = load i8*, i8** %2
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %2
br label %68
67: ; preds = %59
br label %68
68: ; preds = %67, %64
%69 = phi i32 [ 1, %64 ], [ 0, %67 ]
store i32 %69, i32* %5
%70 = load i8*, i8** %1
%71 = load i64, i64* %3
%72 = getelementptr inbounds i8, i8* %70, i64 %71
store i8* %72, i8** %6
br label %73
73: ; preds = %106, %68
%74 = load i8*, i8** %6
%75 = load i8*, i8** %2
%76 = call i8* @match(i8* %74, i8* %75, i32 0)
store i8* %76, i8** %7
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %95
78: ; preds = %73
%79 = load i8*, i8** %6
%80 = load i8*, i8** %1
%81 = ptrtoint i8* %79 to i64
%82 = ptrtoint i8* %80 to i64
%83 = sub i64 %81, %82
%84 = add nsw i64 %83, 1
%85 = trunc i64 %84 to i32
%86 = call i32 @lua_pushnumber(i32 %85)
%87 = load i8*, i8** %7
%88 = load i8*, i8** %1
%89 = ptrtoint i8* %87 to i64
%90 = ptrtoint i8* %88 to i64
%91 = sub i64 %89, %90
%92 = trunc i64 %91 to i32
%93 = call i32 @lua_pushnumber(i32 %92)
%94 = call i32 (...) @push_captures()
br label %109
95: ; preds = %73
br label %96
96: ; preds = %95
%97 = load i8*, i8** %6
%98 = getelementptr inbounds i8, i8* %97, i32 1
store i8* %98, i8** %6
%99 = load i8, i8* %97
%100 = sext i8 %99 to i32
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %106
102: ; preds = %96
%103 = load i32, i32* %5
%104 = icmp ne i32 %103, 0
%105 = xor i1 %104, true
br label %106
106: ; preds = %102, %96
%107 = phi i1 [ false, %96 ], [ %105, %102 ]
br i1 %107, label %73, label %108
108: ; preds = %106
br label %109
109: ; preds = %78, %108, %58
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"out of range\00"
@.str.1 = [10 x i8] c"^$*?.([%-\00"
define void @str_find() {
%1 = tail call i8* @luaL_check_string(i32 1)
%2 = tail call i8* @luaL_check_string(i32 2)
%3 = tail call i64 @luaL_opt_number(i32 3, i32 1)
%4 = add nsw i64 %3, -1
%5 = icmp sgt i64 %3, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = tail call i32 @strlen(i8* %1)
%8 = sext i32 %7 to i64
%9 = icmp sle i64 %4, %8
%10 = zext i1 %9 to i32
br label %11
11: ; preds = %6, %0
%12 = phi i32 [ 0, %0 ], [ %10, %6 ]
%13 = tail call i32 @luaL_arg_check(i32 %12, i32 3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = tail call i64 @lua_lua2C(i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32* @strpbrk(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %33
19: ; preds = %16, %11
%20 = getelementptr inbounds i8, i8* %1, i64 %4
%21 = tail call i8* @strstr(i8* dereferenceable(1) %20, i8* dereferenceable(1) %2)
%22 = icmp eq i8* %21, null
br i1 %22, label %61, label %23
23: ; preds = %19
%24 = ptrtoint i8* %21 to i64
%25 = ptrtoint i8* %1 to i64
%26 = sub i64 %24, %25
%27 = trunc i64 %26 to i32
%28 = add i32 %27, 1
%29 = tail call i32 @lua_pushnumber(i32 %28)
%30 = tail call i32 @strlen(i8* %2)
%31 = add i32 %30, %27
%32 = tail call i32 @lua_pushnumber(i32 %31)
br label %61
33: ; preds = %16
%34 = load i8, i8* %2
%35 = icmp eq i8 %34, 94
%36 = zext i1 %35 to i64
%37 = getelementptr i8, i8* %2, i64 %36
%38 = xor i1 %35, true
%39 = getelementptr inbounds i8, i8* %1, i64 %4
br label %40
40: ; preds = %56, %33
%41 = phi i8* [ %39, %33 ], [ %57, %56 ]
%42 = tail call i8* @match(i8* %41, i8* %37, i32 0)
%43 = icmp eq i8* %42, null
br i1 %43, label %56, label %44
44: ; preds = %40
%45 = ptrtoint i8* %41 to i64
%46 = ptrtoint i8* %1 to i64
%47 = sub i64 %45, %46
%48 = trunc i64 %47 to i32
%49 = add i32 %48, 1
%50 = tail call i32 @lua_pushnumber(i32 %49)
%51 = ptrtoint i8* %42 to i64
%52 = sub i64 %51, %46
%53 = trunc i64 %52 to i32
%54 = tail call i32 @lua_pushnumber(i32 %53)
%55 = tail call i32 (...) @push_captures()
br label %61
56: ; preds = %40
%57 = getelementptr inbounds i8, i8* %41, i64 1
%58 = load i8, i8* %41
%59 = icmp ne i8 %58, 0
%60 = select i1 %59, i1 %38, i1 false
br i1 %60, label %40, label %61
61: ; preds = %56, %44, %23, %19
ret void
}