#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 */
char *luaI_addchar(char);
scalar_t__ lua_check_number(int, char *);
char *lua_check_string(int, char *);
int lua_opt_number(int, int, char *);
int /*<<< orphan*/ lua_pushstring(char *);
int strlen(char *);
void str_sub(void) {
  char *s = lua_check_string(1, "strsub");
  int start = (int)lua_check_number(2, "strsub");
  int end = lua_opt_number(3, strlen(s), "strsub");
  if (end < start || start < 1 || end > strlen(s))
    lua_pushstring("");
  else {
    luaI_addchar(0);
    while (start <= end)
      luaI_addchar(s[start++ - 1]);
    lua_pushstring(luaI_addchar(0));
  }
}


x86 assembly (clang, -O0):
<str_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
lea    0xe8c(%rip),%rsi
call   1040 <lua_check_string@plt>
mov    %rax,-0x8(%rbp)
mov    $0x2,%edi
lea    0xe77(%rip),%rsi
call   1030 <lua_check_number@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
mov    $0x3,%edi
lea    0xe58(%rip),%rdx
call   1070 <lua_opt_number@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     11e2 <str_sub+0x82>
cmpl   $0x1,-0xc(%rbp)
jl     11e2 <str_sub+0x82>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jle    11f3 <str_sub+0x93>
lea    0xe1d(%rip),%rdi
call   1080 <lua_pushstring@plt>
jmp    1238 <str_sub+0xd8>
xor    %edi,%edi
call   1060 <luaI_addchar@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1229 <str_sub+0xc9>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0xc(%rbp)
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <luaI_addchar@plt>
jmp    11fa <str_sub+0x9a>
xor    %edi,%edi
call   1060 <luaI_addchar@plt>
mov    %rax,%rdi
call   1080 <lua_pushstring@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_sub>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rbp
mov    $0x1,%edi
mov    %rbp,%rsi
call   1040 <lua_check_string@plt>
mov    %rax,%r13
mov    $0x2,%edi
mov    %rbp,%rsi
call   1030 <lua_check_number@plt>
mov    %rax,%r14
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    $0x3,%edi
mov    %eax,%esi
mov    %rbp,%rdx
call   1070 <lua_opt_number@plt>
lea    0xe56(%rip),%r15
test   %r14d,%r14d
jle    11fd <str_sub+0x9d>
mov    %eax,%r12d
mov    %eax,%ebp
sub    %r14d,%ebp
jl     11fd <str_sub+0x9d>
mov    %r13,%rdi
call   1050 <strlen@plt>
cmp    %eax,%r12d
jg     11fd <str_sub+0x9d>
xor    %edi,%edi
call   1060 <luaI_addchar@plt>
movslq %r14d,%rax
add    %rax,%r13
mov    $0xffffffffffffffff,%rbx
movsbl 0x0(%r13,%rbx,1),%edi
call   1060 <luaI_addchar@plt>
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    11e0 <str_sub+0x80>
xor    %edi,%edi
call   1060 <luaI_addchar@plt>
mov    %rax,%r15
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <lua_pushstring@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"strsub\00"
@.str.1 = [1 x i8] zeroinitializer
define void @str_sub() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = call i8* @lua_check_string(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = call i64 @lua_check_number(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = trunc i64 %5 to i32
store i32 %6, i32* %2
%7 = load i8*, i8** %1
%8 = call i32 @strlen(i8* %7)
%9 = call i32 @lua_opt_number(i32 3, i32 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = load i32, i32* %2
%12 = icmp slt i32 %10, %11
br i1 %12, label %21, label %13
13: ; preds = %0
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 1
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = load i32, i32* %3
%18 = load i8*, i8** %1
%19 = call i32 @strlen(i8* %18)
%20 = icmp sgt i32 %17, %19
br i1 %20, label %21, label %23
21: ; preds = %16, %13, %0
%22 = call i32 @lua_pushstring(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %41
23: ; preds = %16
%24 = call i8* @luaI_addchar(i8 signext 0)
br label %25
25: ; preds = %29, %23
%26 = load i32, i32* %2
%27 = load i32, i32* %3
%28 = icmp sle i32 %26, %27
br i1 %28, label %29, label %38
29: ; preds = %25
%30 = load i8*, i8** %1
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
%33 = sub nsw i32 %31, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %30, i64 %34
%36 = load i8, i8* %35
%37 = call i8* @luaI_addchar(i8 signext %36)
br label %25
38: ; preds = %25
%39 = call i8* @luaI_addchar(i8 signext 0)
%40 = call i32 @lua_pushstring(i8* %39)
br label %41
41: ; preds = %38, %21
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"strsub\00"
@.str.1 = [1 x i8] zeroinitializer
define void @str_sub() {
%1 = tail call i8* @lua_check_string(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%2 = tail call i64 @lua_check_number(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%3 = trunc i64 %2 to i32
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @lua_opt_number(i32 3, i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %5, %3
%7 = icmp slt i32 %3, 1
%8 = or i1 %7, %6
br i1 %8, label %28, label %9
9: ; preds = %0
%10 = tail call i32 @strlen(i8* %1)
%11 = icmp sgt i32 %5, %10
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = tail call i8* @luaI_addchar(i8 signext 0)
%14 = shl i64 %2, 32
%15 = ashr exact i64 %14, 32
%16 = add i32 %5, 1
br label %17
17: ; preds = %12, %17
%18 = phi i64 [ %15, %12 ], [ %19, %17 ]
%19 = add nsw i64 %18, 1
%20 = add nsw i64 %18, -1
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = load i8, i8* %21
%23 = tail call i8* @luaI_addchar(i8 signext %22)
%24 = trunc i64 %19 to i32
%25 = icmp eq i32 %16, %24
br i1 %25, label %26, label %17
26: ; preds = %17
%27 = tail call i8* @luaI_addchar(i8 signext 0)
br label %28
28: ; preds = %0, %9, %26
%29 = phi i8* [ %27, %26 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %0 ]
%30 = tail call i32 @lua_pushstring(i8* %29)
ret void
}