#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 */
size_t atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
size_t strlen(char const *);
char *substring(char const *, size_t const, size_t const);
int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <string> <start> [end]\n", argv[0]);
    return -1;
  }
  const char *str = argv[1];
  const size_t start = atoi(argv[2]);
  const size_t end = (argc == 4 ? atoi(argv[3]) : strlen(str));
  char *substr = substring(str, start, end);
  fprintf(stdout, "substring: %s\n", (substr ? substr : "bad input!"));
  free(substr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1198 <main+0x48>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <main+0x106>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11d5 <main+0x85>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <atoi@plt>
mov    %rax,-0x38(%rbp)
jmp    11e2 <main+0x92>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <substring@plt>
mov    %rax,-0x30(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1223 <main+0xd3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1233 <main+0xe3>
lea    0xe07(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1233 <main+0xe3>
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%rdx
lea    0xde1(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     117e <main+0x2e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe8e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11e6 <main+0x96>
mov    %edi,%ebp
mov    0x8(%rbx),%r15
mov    0x10(%rbx),%rdi
call   1070 <atoi@plt>
mov    %rax,%r14
cmp    $0x4,%ebp
jne    11a0 <main+0x50>
mov    0x18(%rbx),%rdi
call   1070 <atoi@plt>
jmp    11a8 <main+0x58>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <substring@plt>
mov    %rax,%rbx
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
test   %rbx,%rbx
lea    0xe65(%rip),%rdx
cmovne %rbx,%rdx
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Usage: %s <string> <start> [end]\0A\00"
@stdout = global i32 0
@.str.1 = [15 x i8] c"substring: %s\0A\00"
@.str.2 = [11 x i8] c"bad input!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %53
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i64 @atoi(i8* %24)
store i64 %25, i64* %7
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 4
br i1 %27, label %28, label %33
28: ; preds = %18
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = call i64 @atoi(i8* %31)
br label %36
33: ; preds = %18
%34 = load i8*, i8** %6
%35 = call i64 @strlen(i8* %34)
br label %36
36: ; preds = %33, %28
%37 = phi i64 [ %32, %28 ], [ %35, %33 ]
store i64 %37, i64* %8
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = load i64, i64* %8
%41 = call i8* @substring(i8* %38, i64 %39, i64 %40)
store i8* %41, i8** %9
%42 = load i32, i32* @stdout
%43 = load i8*, i8** %9
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = load i8*, i8** %9
br label %48
47: ; preds = %36
br label %48
48: ; preds = %47, %45
%49 = phi i8* [ %46, %45 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %47 ]
%50 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %9
%52 = call i32 @free(i8* %51)
store i32 0, i32* %3
br label %53
53: ; preds = %48, %12
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Usage: %s <string> <start> [end]\0A\00"
@stdout = global i32 0
@.str.1 = [15 x i8] c"substring: %s\0A\00"
@.str.2 = [11 x i8] c"bad input!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %29
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i64 @atoi(i8* %12)
%14 = icmp eq i32 %0, 4
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = tail call i64 @atoi(i8* %17)
br label %21
19: ; preds = %8
%20 = tail call i64 @strlen(i8* dereferenceable(1) %10)
br label %21
21: ; preds = %19, %15
%22 = phi i64 [ %18, %15 ], [ %20, %19 ]
%23 = tail call i8* @substring(i8* %10, i64 %13, i64 %22)
%24 = load i32, i32* @stdout
%25 = icmp eq i8* %23, null
%26 = select i1 %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %23
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = tail call i32 @free(i8* %23)
br label %29
29: ; preds = %21, %4
%30 = phi i32 [ -1, %4 ], [ 0, %21 ]
ret i32 %30
}