#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 *luaL_check_lstr(int, long *);
int /*<<< orphan*/ luaL_check_number(int);
int /*<<< orphan*/ luaL_opt_number(int, int);
int /*<<< orphan*/ lua_pushlstring(char *, long);
int /*<<< orphan*/ lua_pushstring(char *);
long posrelat(int /*<<< orphan*/, long);
void str_sub(void) {
  long l;
  char *s = luaL_check_lstr(1, &l);
  long start = posrelat(luaL_check_number(2), l);
  long end = posrelat(luaL_opt_number(3, -1), l);
  if (1 <= start && start <= end && end <= l)
    lua_pushlstring(s + start - 1, end - start + 1);
  else
    lua_pushstring("");
}


x86 assembly (clang, -O0):
<str_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
call   1070 <luaL_check_lstr@plt>
mov    %rax,-0x10(%rbp)
mov    $0x2,%edi
call   1040 <luaL_check_number@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1060 <posrelat@plt>
mov    %rax,-0x18(%rbp)
mov    $0x3,%edi
mov    $0xffffffff,%esi
call   1030 <luaL_opt_number@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1060 <posrelat@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%eax
cmp    -0x18(%rbp),%rax
jg     11fe <str_sub+0x9e>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     11fe <str_sub+0x9e>
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jg     11fe <str_sub+0x9e>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x20(%rbp),%rsi
sub    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1050 <lua_pushlstring@plt>
jmp    120a <str_sub+0xaa>
lea    0xdfb(%rip),%rdi
call   1080 <lua_pushstring@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_sub>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rsi
mov    $0x1,%edi
call   1070 <luaL_check_lstr@plt>
mov    %rax,%r14
mov    $0x2,%edi
call   1040 <luaL_check_number@plt>
mov    (%rsp),%rsi
mov    %eax,%edi
call   1060 <posrelat@plt>
mov    %rax,%rbx
mov    $0x3,%edi
mov    $0xffffffff,%esi
call   1030 <luaL_opt_number@plt>
mov    (%rsp),%rsi
mov    %eax,%edi
call   1060 <posrelat@plt>
test   %rbx,%rbx
jle    11cc <str_sub+0x6c>
mov    %rax,%rsi
sub    %rbx,%rsi
jl     11cc <str_sub+0x6c>
cmp    (%rsp),%rax
jg     11cc <str_sub+0x6c>
lea    (%r14,%rbx,1),%rdi
add    $0xffffffffffffffff,%rdi
add    $0x1,%rsi
call   1050 <lua_pushlstring@plt>
jmp    11d8 <str_sub+0x78>
lea    0xe2d(%rip),%rdi
call   1080 <lua_pushstring@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define void @str_sub() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = call i8* @luaL_check_lstr(i32 1, i64* %1)
store i8* %5, i8** %2
%6 = call i32 @luaL_check_number(i32 2)
%7 = load i64, i64* %1
%8 = call i64 @posrelat(i32 %6, i64 %7)
store i64 %8, i64* %3
%9 = call i32 @luaL_opt_number(i32 3, i32 -1)
%10 = load i64, i64* %1
%11 = call i64 @posrelat(i32 %9, i64 %10)
store i64 %11, i64* %4
%12 = load i64, i64* %3
%13 = icmp sle i64 1, %12
br i1 %13, label %14, label %32
14: ; preds = %0
%15 = load i64, i64* %3
%16 = load i64, i64* %4
%17 = icmp sle i64 %15, %16
br i1 %17, label %18, label %32
18: ; preds = %14
%19 = load i64, i64* %4
%20 = load i64, i64* %1
%21 = icmp sle i64 %19, %20
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = load i8*, i8** %2
%24 = load i64, i64* %3
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = getelementptr inbounds i8, i8* %25, i64 -1
%27 = load i64, i64* %4
%28 = load i64, i64* %3
%29 = sub nsw i64 %27, %28
%30 = add nsw i64 %29, 1
%31 = call i32 @lua_pushlstring(i8* %26, i64 %30)
br label %34
32: ; preds = %18, %14, %0
%33 = call i32 @lua_pushstring(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %22
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @str_sub() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = call i8* @luaL_check_lstr(i32 1, i64* %1)
%4 = call i32 @luaL_check_number(i32 2)
%5 = load i64, i64* %1
%6 = call i64 @posrelat(i32 %4, i64 %5)
%7 = call i32 @luaL_opt_number(i32 3, i32 -1)
%8 = load i64, i64* %1
%9 = call i64 @posrelat(i32 %7, i64 %8)
%10 = icmp slt i64 %6, 1
%11 = icmp slt i64 %9, %6
%12 = select i1 %10, i1 true, i1 %11
%13 = load i64, i64* %1
%14 = icmp sgt i64 %9, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %22, label %16
16: ; preds = %0
%17 = getelementptr inbounds i8, i8* %3, i64 %6
%18 = getelementptr inbounds i8, i8* %17, i64 -1
%19 = sub nsw i64 1, %6
%20 = add i64 %19, %9
%21 = call i32 @lua_pushlstring(i8* %18, i64 %20)
br label %24
22: ; preds = %0
%23 = call i32 @lua_pushstring(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
ret void
}