#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_pushliteral(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_getparam(3) == 0) ? strlen(s) : (int)lua_check_number(3, "strsub");
  if (end < start || start < 1 || end > strlen(s))
    lua_pushliteral("");
  else {
    char temp = s[end];
    s[end] = 0;
    lua_pushstring(&s[start - 1]);
    s[end] = temp;
  }
}


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    $0x3,%edi
call   1070 <lua_getparam@plt>
cmp    $0x0,%rax
jne    11b6 <str_sub+0x56>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
jmp    11ca <str_sub+0x6a>
mov    $0x3,%edi
lea    0xe3e(%rip),%rsi
call   1030 <lua_check_number@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1202 <str_sub+0xa2>
cmpl   $0x1,-0xc(%rbp)
jl     1202 <str_sub+0xa2>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jle    1213 <str_sub+0xb3>
lea    0xdfd(%rip),%rdi
call   1060 <lua_pushliteral@plt>
jmp    124f <str_sub+0xef>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rdi
call   1080 <lua_pushstring@plt>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_sub>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe92(%rip),%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1040 <lua_check_string@plt>
mov    %rax,%r14
mov    $0x2,%edi
mov    %rbx,%rsi
call   1030 <lua_check_number@plt>
mov    %rax,%r15
mov    $0x3,%edi
call   1070 <lua_getparam@plt>
test   %rax,%rax
je     11b8 <str_sub+0x58>
lea    0xe5c(%rip),%rsi
mov    $0x3,%edi
call   1030 <lua_check_number@plt>
mov    %rax,%rbx
test   %r15d,%r15d
jg     11c7 <str_sub+0x67>
jmp    11d8 <str_sub+0x78>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
test   %r15d,%r15d
jle    11d8 <str_sub+0x78>
cmp    %r15d,%ebx
jl     11d8 <str_sub+0x78>
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebx
jle    11ee <str_sub+0x8e>
lea    0xe27(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <lua_pushliteral@plt>
movslq %ebx,%rbx
mov    (%r14,%rbx,1),%bpl
movb   $0x0,(%r14,%rbx,1)
add    $0xffffffff,%r15d
mov    %r14,%rdi
add    %r15,%rdi
call   1080 <lua_pushstring@plt>
mov    %bpl,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 = alloca i8
%5 = call i8* @lua_check_string(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %1
%6 = call i64 @lua_check_number(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = trunc i64 %6 to i32
store i32 %7, i32* %2
%8 = call i64 @lua_getparam(i32 3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i8*, i8** %1
%12 = call i32 @strlen(i8* %11)
br label %16
13: ; preds = %0
%14 = call i64 @lua_check_number(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = trunc i64 %14 to i32
br label %16
16: ; preds = %13, %10
%17 = phi i32 [ %12, %10 ], [ %15, %13 ]
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = load i32, i32* %2
%20 = icmp slt i32 %18, %19
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 1
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = load i32, i32* %3
%26 = load i8*, i8** %1
%27 = call i32 @strlen(i8* %26)
%28 = icmp sgt i32 %25, %27
br i1 %28, label %29, label %31
29: ; preds = %24, %21, %16
%30 = call i32 @lua_pushliteral(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %52
31: ; preds = %24
%32 = load i8*, i8** %1
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
store i8 %36, i8* %4
%37 = load i8*, i8** %1
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %1
%42 = load i32, i32* %2
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = call i32 @lua_pushstring(i8* %45)
%47 = load i8, i8* %4
%48 = load i8*, i8** %1
%49 = load i32, i32* %3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
br label %52
52: ; preds = %31, %29
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 i64 @lua_getparam(i32 3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @strlen(i8* %1)
br label %11
8: ; preds = %0
%9 = tail call i64 @lua_check_number(i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = trunc i64 %9 to i32
br label %11
11: ; preds = %8, %6
%12 = phi i32 [ %7, %6 ], [ %10, %8 ]
%13 = icmp slt i32 %12, %3
%14 = icmp slt i32 %3, 1
%15 = or i1 %14, %13
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = tail call i32 @strlen(i8* %1)
%18 = icmp sgt i32 %12, %17
br i1 %18, label %19, label %21
19: ; preds = %16, %11
%20 = tail call i32 @lua_pushliteral(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %29
21: ; preds = %16
%22 = sext i32 %12 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
%24 = load i8, i8* %23
store i8 0, i8* %23
%25 = add i64 %2, 4294967295
%26 = and i64 %25, 4294967295
%27 = getelementptr inbounds i8, i8* %1, i64 %26
%28 = tail call i32 @lua_pushstring(i8* %27)
store i8 %24, i8* %23
br label %29
29: ; preds = %21, %19
ret void
}