#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 INT_MAX;
char *lua_check_string(int, char *);
int lua_opt_number(int, int, char *);
int /*<<< orphan*/ lua_pushnil();
int /*<<< orphan*/ lua_pushnumber(int);
int strlen(char *);
char *strstr(char *, char *);
void str_find(void) {
  char *s1 = lua_check_string(1, "strfind");
  char *s2 = lua_check_string(2, "strfind");
  int init = lua_opt_number(3, 1, "strfind") - 1;
  char *f = (init >= 0 && init <= strlen(s1)) ? strstr(s1 + init, s2) : NULL;
  if (f != NULL) {
    int pos = f - s1 + 1;
    if (lua_opt_number(4, INT_MAX, "strfind") >= pos + strlen(s2) - 1)
      lua_pushnumber(pos);
    else
      lua_pushnil();
  } else
    lua_pushnil();
}


x86 assembly (clang, -O0):
<str_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x1,%edi
lea    0xe8c(%rip),%rsi
call   1030 <lua_check_string@plt>
mov    %rax,-0x8(%rbp)
mov    $0x2,%edi
lea    0xe77(%rip),%rsi
call   1030 <lua_check_string@plt>
mov    %rax,-0x10(%rbp)
mov    $0x3,%edi
mov    $0x1,%esi
lea    0xe5d(%rip),%rdx
call   1050 <lua_opt_number@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11f1 <str_find+0x91>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jg     11f1 <str_find+0x91>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x30(%rbp)
jmp    11fc <str_find+0x9c>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    11fc <str_find+0x9c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127b <str_find+0x11b>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edi
lea    0xdca(%rip),%rdx
call   1050 <lua_opt_number@plt>
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%ecx
mov    %eax,%edx
mov    -0x34(%rbp),%eax
add    %edx,%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     126f <str_find+0x10f>
mov    -0x24(%rbp),%edi
call   1060 <lua_pushnumber@plt>
jmp    1276 <str_find+0x116>
mov    $0x0,%al
call   1070 <lua_pushnil@plt>
jmp    1282 <str_find+0x122>
mov    $0x0,%al
call   1070 <lua_pushnil@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_find>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe92(%rip),%rbp
mov    $0x1,%edi
mov    %rbp,%rsi
call   1030 <lua_check_string@plt>
mov    %rax,%rbx
mov    $0x2,%edi
mov    %rbp,%rsi
call   1030 <lua_check_string@plt>
mov    %rax,%r14
mov    $0x3,%edi
mov    $0x1,%esi
mov    %rbp,%rdx
call   1050 <lua_opt_number@plt>
test   %eax,%eax
jle    11fb <str_find+0x9b>
mov    %eax,%ebp
add    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jg     11fb <str_find+0x9b>
mov    %ebp,%eax
mov    %rbx,%rdi
add    %rax,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11fb <str_find+0x9b>
mov    %rax,%r15
sub    %ebx,%r15d
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdx
mov    $0x4,%edi
call   1050 <lua_opt_number@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r15d,%eax
cmp    %eax,%ebx
jge    120c <str_find+0xac>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <lua_pushnil@plt>
add    $0x1,%r15d
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <lua_pushnumber@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"strfind\00"
@INT_MAX = global i32 0
define void @str_find() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = call i8* @lua_check_string(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %1
%7 = call i8* @lua_check_string(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %2
%8 = call i32 @lua_opt_number(i32 3, i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %0
%13 = load i32, i32* %3
%14 = load i8*, i8** %1
%15 = call i32 @strlen(i8* %14)
%16 = icmp sle i32 %13, %15
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i8*, i8** %1
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8*, i8** %2
%23 = call i8* @strstr(i8* %21, i8* %22)
br label %25
24: ; preds = %12, %0
br label %25
25: ; preds = %24, %17
%26 = phi i8* [ %23, %17 ], [ null, %24 ]
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %51
29: ; preds = %25
%30 = load i8*, i8** %4
%31 = load i8*, i8** %1
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = add nsw i64 %34, 1
%36 = trunc i64 %35 to i32
store i32 %36, i32* %5
%37 = load i32, i32* @INT_MAX
%38 = call i32 @lua_opt_number(i32 4, i32 %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* %5
%40 = load i8*, i8** %2
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %39, %41
%43 = sub nsw i32 %42, 1
%44 = icmp sge i32 %38, %43
br i1 %44, label %45, label %48
45: ; preds = %29
%46 = load i32, i32* %5
%47 = call i32 @lua_pushnumber(i32 %46)
br label %50
48: ; preds = %29
%49 = call i32 (...) @lua_pushnil()
br label %50
50: ; preds = %48, %45
br label %53
51: ; preds = %25
%52 = call i32 (...) @lua_pushnil()
br label %53
53: ; preds = %51, %50
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"strfind\00"
@INT_MAX = global i32 0
define void @str_find() {
%1 = tail call i8* @lua_check_string(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%2 = tail call i8* @lua_check_string(i32 2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @lua_opt_number(i32 3, i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = add nsw i32 %3, -1
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %29
6: ; preds = %0
%7 = tail call i32 @strlen(i8* %1)
%8 = icmp sgt i32 %4, %7
br i1 %8, label %29, label %9
9: ; preds = %6
%10 = zext i32 %4 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i8* @strstr(i8* dereferenceable(1) %11, i8* dereferenceable(1) %2)
%13 = icmp eq i8* %12, null
br i1 %13, label %29, label %14
14: ; preds = %9
%15 = ptrtoint i8* %12 to i64
%16 = ptrtoint i8* %1 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
%19 = load i32, i32* @INT_MAX
%20 = tail call i32 @lua_opt_number(i32 4, i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @strlen(i8* %2)
%22 = add i32 %21, %18
%23 = icmp slt i32 %20, %22
br i1 %23, label %27, label %24
24: ; preds = %14
%25 = add i32 %18, 1
%26 = tail call i32 @lua_pushnumber(i32 %25)
br label %31
27: ; preds = %14
%28 = tail call i32 (...) @lua_pushnil()
br label %31
29: ; preds = %0, %6, %9
%30 = tail call i32 (...) @lua_pushnil()
br label %31
31: ; preds = %24, %27, %29
ret void
}