#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 */
scalar_t__ lua_check_number(int, char *);
char *lua_check_string(int, char *);
scalar_t__ lua_getparam(int);
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_getparam(3) == 0) ? 0 : (int)lua_check_number(3, "strfind") - 1;
  char *f = strstr(s1 + init, s2);
  if (f != NULL) {
    int pos = f - s1 + 1;
    if (lua_getparam(4) == 0)
      lua_pushnumber(pos);
    else if ((int)lua_check_number(4, "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    $0x30,%rsp
mov    $0x1,%edi
lea    0xe7c(%rip),%rsi
call   1040 <lua_check_string@plt>
mov    %rax,-0x8(%rbp)
mov    $0x2,%edi
lea    0xe67(%rip),%rsi
call   1040 <lua_check_string@plt>
mov    %rax,-0x10(%rbp)
mov    $0x3,%edi
call   1070 <lua_getparam@plt>
cmp    $0x0,%rax
jne    11c0 <str_find+0x50>
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
jmp    11d7 <str_find+0x67>
mov    $0x3,%edi
lea    0xe34(%rip),%rsi
call   1030 <lua_check_number@plt>
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1289 <str_find+0x119>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    $0x4,%edi
call   1070 <lua_getparam@plt>
cmp    $0x0,%rax
jne    1233 <str_find+0xc3>
mov    -0x24(%rbp),%edi
call   1060 <lua_pushnumber@plt>
jmp    1284 <str_find+0x114>
mov    $0x4,%edi
lea    0xdc1(%rip),%rsi
call   1030 <lua_check_number@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%ecx
mov    %eax,%edx
mov    -0x2c(%rbp),%eax
add    %edx,%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1278 <str_find+0x108>
mov    -0x24(%rbp),%edi
call   1060 <lua_pushnumber@plt>
jmp    127f <str_find+0x10f>
mov    $0x0,%al
call   1080 <lua_pushnil@plt>
jmp    1284 <str_find+0x114>
jmp    1290 <str_find+0x120>
mov    $0x0,%al
call   1080 <lua_pushnil@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_find>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe82(%rip),%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1040 <lua_check_string@plt>
mov    %rax,%r15
mov    $0x2,%edi
mov    %rbx,%rsi
call   1040 <lua_check_string@plt>
mov    %rax,%r14
mov    $0x3,%edi
call   1070 <lua_getparam@plt>
test   %rax,%rax
je     11c6 <str_find+0x56>
lea    0xe4c(%rip),%rsi
mov    $0x3,%edi
call   1030 <lua_check_number@plt>
add    $0xffffffff,%eax
movslq %eax,%rdi
jmp    11c8 <str_find+0x58>
xor    %edi,%edi
add    %r15,%rdi
mov    %r14,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1213 <str_find+0xa3>
mov    %rax,%rbx
sub    %r15d,%ebx
lea    0x1(%rbx),%r15d
mov    $0x4,%edi
call   1070 <lua_getparam@plt>
test   %rax,%rax
je     1224 <str_find+0xb4>
lea    0xe08(%rip),%rsi
mov    $0x4,%edi
call   1030 <lua_check_number@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %ebx,%eax
cmp    %ebp,%eax
jle    1224 <str_find+0xb4>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <lua_pushnil@plt>
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"
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 i64 @lua_getparam(i32 3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
br label %15
11: ; preds = %0
%12 = call i64 @lua_check_number(i32 3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = trunc i64 %12 to i32
%14 = sub nsw i32 %13, 1
br label %15
15: ; preds = %11, %10
%16 = phi i32 [ 0, %10 ], [ %14, %11 ]
store i32 %16, i32* %3
%17 = load i8*, i8** %1
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8*, i8** %2
%22 = call i8* @strstr(i8* %20, i8* %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %54
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = load i8*, i8** %1
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = add nsw i64 %30, 1
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
%33 = call i64 @lua_getparam(i32 4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i32, i32* %5
%37 = call i32 @lua_pushnumber(i32 %36)
br label %53
38: ; preds = %25
%39 = call i64 @lua_check_number(i32 4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%40 = trunc i64 %39 to i32
%41 = load i32, i32* %5
%42 = load i8*, i8** %2
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %41, %43
%45 = sub nsw i32 %44, 1
%46 = icmp sge i32 %40, %45
br i1 %46, label %47, label %50
47: ; preds = %38
%48 = load i32, i32* %5
%49 = call i32 @lua_pushnumber(i32 %48)
br label %52
50: ; preds = %38
%51 = call i32 (...) @lua_pushnil()
br label %52
52: ; preds = %50, %47
br label %53
53: ; preds = %52, %35
br label %56
54: ; preds = %15
%55 = call i32 (...) @lua_pushnil()
br label %56
56: ; preds = %54, %53
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"strfind\00"
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 i64 @lua_getparam(i32 3)
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = tail call i64 @lua_check_number(i32 3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = shl i64 %6, 32
%8 = add i64 %7, -4294967296
%9 = ashr exact i64 %8, 32
br label %10
10: ; preds = %0, %5
%11 = phi i64 [ %9, %5 ], [ 0, %0 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = tail call i8* @strstr(i8* dereferenceable(1) %12, i8* dereferenceable(1) %2)
%14 = icmp eq i8* %13, null
br i1 %14, label %35, label %15
15: ; preds = %10
%16 = ptrtoint i8* %13 to i64
%17 = ptrtoint i8* %1 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = add i32 %19, 1
%21 = tail call i64 @lua_getparam(i32 4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = tail call i32 @lua_pushnumber(i32 %20)
br label %37
25: ; preds = %15
%26 = tail call i64 @lua_check_number(i32 4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%27 = trunc i64 %26 to i32
%28 = tail call i32 @strlen(i8* %2)
%29 = add i32 %28, %19
%30 = icmp sgt i32 %29, %27
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = tail call i32 @lua_pushnumber(i32 %20)
br label %37
33: ; preds = %25
%34 = tail call i32 (...) @lua_pushnil()
br label %37
35: ; preds = %10
%36 = tail call i32 (...) @lua_pushnil()
br label %37
37: ; preds = %23, %33, %31, %35
ret void
}