#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 */
int /*<<< orphan*/ addnchar(char *, long);
char *luaI_addchar(int /*<<< orphan*/);
int /*<<< orphan*/ luaI_emptybuff();
scalar_t__ luaL_check_number(int);
char *luaL_check_string(int);
scalar_t__ luaL_opt_number(int, int);
int /*<<< orphan*/ lua_pushstring(char *);
long strlen(char *);
void str_sub(void) {
  char *s = luaL_check_string(1);
  long l = strlen(s);
  long start = (long)luaL_check_number(2);
  long end = (long)luaL_opt_number(3, -1);
  if (start < 0)
    start = l + start + 1;
  if (end < 0)
    end = l + end + 1;
  if (1 <= start && start <= end && end <= l) {
    luaI_emptybuff();
    addnchar(s + start - 1, end - start + 1);
    lua_pushstring(luaI_addchar(0));
  } else
    lua_pushstring("");
}


x86 assembly (clang, -O0):
<str_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
call   1080 <luaL_check_string@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    $0x2,%edi
call   1060 <luaL_check_number@plt>
mov    %rax,-0x18(%rbp)
mov    $0x3,%edi
mov    $0xffffffff,%esi
call   1030 <luaL_opt_number@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11df <str_sub+0x5f>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11fa <str_sub+0x7a>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%eax
cmp    -0x18(%rbp),%rax
jg     125d <str_sub+0xdd>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     125d <str_sub+0xdd>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jg     125d <str_sub+0xdd>
mov    $0x0,%al
call   1070 <luaI_emptybuff@plt>
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x20(%rbp),%rsi
sub    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1040 <addnchar@plt>
xor    %edi,%edi
call   1090 <luaI_addchar@plt>
mov    %rax,%rdi
call   10a0 <lua_pushstring@plt>
jmp    1269 <str_sub+0xe9>
lea    0xd9c(%rip),%rdi
call   10a0 <lua_pushstring@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_sub>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x1,%edi
call   1080 <luaL_check_string@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
mov    $0x2,%edi
call   1060 <luaL_check_number@plt>
mov    %rax,%r12
mov    $0x3,%edi
mov    $0xffffffff,%esi
call   1030 <luaL_opt_number@plt>
lea    0x1(%r15),%rcx
mov    %r12,%rbx
sar    $0x3f,%rbx
and    %rcx,%rbx
add    %r12,%rbx
lea    0xe2c(%rip),%rdi
test   %rbx,%rbx
jle    1218 <str_sub+0x98>
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rcx,%rdx
add    %rax,%rdx
mov    %rdx,%r12
sub    %rbx,%r12
jl     1218 <str_sub+0x98>
cmp    %r15,%rdx
jg     1218 <str_sub+0x98>
xor    %eax,%eax
call   1070 <luaI_emptybuff@plt>
lea    (%r14,%rbx,1),%rdi
add    $0xffffffffffffffff,%rdi
add    $0x1,%r12
mov    %r12,%rsi
call   1040 <addnchar@plt>
xor    %edi,%edi
call   1090 <luaI_addchar@plt>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    10a0 <lua_pushstring@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define void @str_sub() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = call i8* @luaL_check_string(i32 1)
store i8* %5, i8** %1
%6 = load i8*, i8** %1
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %2
%8 = call i64 @luaL_check_number(i32 2)
store i64 %8, i64* %3
%9 = call i64 @luaL_opt_number(i32 3, i32 -1)
store i64 %9, i64* %4
%10 = load i64, i64* %3
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = load i64, i64* %2
%14 = load i64, i64* %3
%15 = add nsw i64 %13, %14
%16 = add nsw i64 %15, 1
store i64 %16, i64* %3
br label %17
17: ; preds = %12, %0
%18 = load i64, i64* %4
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i64, i64* %2
%22 = load i64, i64* %4
%23 = add nsw i64 %21, %22
%24 = add nsw i64 %23, 1
store i64 %24, i64* %4
br label %25
25: ; preds = %20, %17
%26 = load i64, i64* %3
%27 = icmp sle i64 1, %26
br i1 %27, label %28, label %49
28: ; preds = %25
%29 = load i64, i64* %3
%30 = load i64, i64* %4
%31 = icmp sle i64 %29, %30
br i1 %31, label %32, label %49
32: ; preds = %28
%33 = load i64, i64* %4
%34 = load i64, i64* %2
%35 = icmp sle i64 %33, %34
br i1 %35, label %36, label %49
36: ; preds = %32
%37 = call i32 (...) @luaI_emptybuff()
%38 = load i8*, i8** %1
%39 = load i64, i64* %3
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = getelementptr inbounds i8, i8* %40, i64 -1
%42 = load i64, i64* %4
%43 = load i64, i64* %3
%44 = sub nsw i64 %42, %43
%45 = add nsw i64 %44, 1
%46 = call i32 @addnchar(i8* %41, i64 %45)
%47 = call i8* @luaI_addchar(i32 0)
%48 = call i32 @lua_pushstring(i8* %47)
br label %51
49: ; preds = %32, %28, %25
%50 = call i32 @lua_pushstring(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %51
51: ; preds = %49, %36
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @str_sub() {
%1 = tail call i8* @luaL_check_string(i32 1)
%2 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%3 = tail call i64 @luaL_check_number(i32 2)
%4 = tail call i64 @luaL_opt_number(i32 3, i32 -1)
%5 = icmp slt i64 %3, 0
%6 = add i64 %2, 1
%7 = select i1 %5, i64 %6, i64 0
%8 = add i64 %3, %7
%9 = icmp slt i64 %4, 0
%10 = select i1 %9, i64 %6, i64 0
%11 = add i64 %4, %10
%12 = icmp slt i64 %8, 1
%13 = icmp slt i64 %11, %8
%14 = select i1 %12, i1 true, i1 %13
%15 = icmp sgt i64 %11, %2
%16 = select i1 %14, i1 true, i1 %15
br i1 %16, label %25, label %17
17: ; preds = %0
%18 = tail call i32 (...) @luaI_emptybuff()
%19 = getelementptr inbounds i8, i8* %1, i64 %8
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = sub nsw i64 1, %8
%22 = add i64 %21, %11
%23 = tail call i32 @addnchar(i8* %20, i64 %22)
%24 = tail call i8* @luaI_addchar(i32 0)
br label %25
25: ; preds = %0, %17
%26 = phi i8* [ %24, %17 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %0 ]
%27 = tail call i32 @lua_pushstring(i8* %26)
ret void
}