#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 *);
scalar_t__ luaL_check_number(int);
scalar_t__ luaL_opt_number(int, int);
int /*<<< orphan*/ lua_pushlstring(char *, long);
int /*<<< orphan*/ lua_pushstring(char *);
long posrelat(long, long);
void str_sub(void) {
  long l;
  char *s = luaL_check_lstr(1, &l);
  long start = posrelat(((long)luaL_check_number(2)), l);
  long end = posrelat(((long)luaL_opt_number(3, -1)), l);
  if (start < 1)
    start = 1;
  if (end > l)
    end = l;
  if (start <= end)
    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    %rax,%rdi
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    %rax,%rdi
mov    -0x8(%rbp),%rsi
call   1060 <posrelat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x18(%rbp)
jge    11c6 <str_sub+0x66>
movq   $0x1,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jle    11dc <str_sub+0x7c>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     120c <str_sub+0xac>
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    1218 <str_sub+0xb8>
lea    0xded(%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    %rax,%rdi
call   1060 <posrelat@plt>
mov    %rax,%rbx
mov    $0x3,%edi
mov    $0xffffffff,%esi
call   1030 <luaL_opt_number@plt>
mov    (%rsp),%rsi
mov    %rax,%rdi
call   1060 <posrelat@plt>
test   %rbx,%rbx
mov    $0x1,%ecx
cmovg  %rbx,%rcx
mov    (%rsp),%rdx
cmp    %rdx,%rax
cmovg  %rdx,%rax
sub    %rcx,%rax
jge    11d2 <str_sub+0x72>
lea    0xe35(%rip),%rdi
call   1080 <lua_pushstring@plt>
jmp    11e6 <str_sub+0x86>
lea    (%r14,%rcx,1),%rdi
add    $0xffffffffffffffff,%rdi
add    $0x1,%rax
mov    %rax,%rsi
call   1050 <lua_pushlstring@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 i64 @luaL_check_number(i32 2)
%7 = load i64, i64* %1
%8 = call i64 @posrelat(i64 %6, i64 %7)
store i64 %8, i64* %3
%9 = call i64 @luaL_opt_number(i32 3, i32 -1)
%10 = load i64, i64* %1
%11 = call i64 @posrelat(i64 %9, i64 %10)
store i64 %11, i64* %4
%12 = load i64, i64* %3
%13 = icmp slt i64 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %0
store i64 1, i64* %3
br label %15
15: ; preds = %14, %0
%16 = load i64, i64* %4
%17 = load i64, i64* %1
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i64, i64* %1
store i64 %20, i64* %4
br label %21
21: ; preds = %19, %15
%22 = load i64, i64* %3
%23 = load i64, i64* %4
%24 = icmp sle i64 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = load i64, i64* %3
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -1
%30 = load i64, i64* %4
%31 = load i64, i64* %3
%32 = sub nsw i64 %30, %31
%33 = add nsw i64 %32, 1
%34 = call i32 @lua_pushlstring(i8* %29, i64 %33)
br label %37
35: ; preds = %21
%36 = call i32 @lua_pushstring(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %25
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 i64 @luaL_check_number(i32 2)
%5 = load i64, i64* %1
%6 = call i64 @posrelat(i64 %4, i64 %5)
%7 = call i64 @luaL_opt_number(i32 3, i32 -1)
%8 = load i64, i64* %1
%9 = call i64 @posrelat(i64 %7, i64 %8)
%10 = icmp sgt i64 %6, 1
%11 = select i1 %10, i64 %6, i64 1
%12 = load i64, i64* %1
%13 = icmp sgt i64 %9, %12
%14 = select i1 %13, i64 %12, i64 %9
%15 = icmp slt i64 %14, %11
br i1 %15, label %22, label %16
16: ; preds = %0
%17 = getelementptr inbounds i8, i8* %3, i64 %11
%18 = getelementptr inbounds i8, i8* %17, i64 -1
%19 = sub nsw i64 1, %11
%20 = add i64 %19, %14
%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
}