#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 GetStackSize(int const, char *);
int GetStringLength(char *);
char *ReturnNullMallocStack(char *);
char *ReturnOutputMallocStack(char *, char *, int const);
char *Substring(char *output, char *input, const int start, const int length) {
  int length0 = GetStringLength(input);
  if (length0 < 1 || start < 0 || length < 1 || start + length > length0) {
    return ReturnNullMallocStack(output);
  }
  char output2[GetStackSize(length, "char")];
  int i = 0;
  for (i = 0; i < length; i++) {
    output2[i] = input[start + i];
  }
  output2[i] = '\0';
  return ReturnOutputMallocStack(output, output2, length);
}


x86 assembly (clang, -O0):
<Substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <GetStringLength@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jl     118f <Substring+0x4f>
cmpl   $0x0,-0x1c(%rbp)
jl     118f <Substring+0x4f>
cmpl   $0x1,-0x20(%rbp)
jl     118f <Substring+0x4f>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    11a1 <Substring+0x61>
mov    -0x10(%rbp),%rdi
call   1030 <ReturnNullMallocStack@plt>
mov    %rax,-0x8(%rbp)
jmp    123b <Substring+0xfb>
mov    -0x20(%rbp),%edi
lea    0xe55(%rip),%rsi
call   1060 <GetStackSize@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1218 <Substring+0xd8>
mov    -0x48(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%edx
add    -0x3c(%rbp),%edx
movslq %edx,%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x3c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11e3 <Substring+0xa3>
mov    -0x48(%rbp),%rsi
movslq -0x3c(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%edx
call   1040 <ReturnOutputMallocStack@plt>
mov    %rax,-0x8(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Substring>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <GetStringLength@plt>
test   %r13d,%r13d
jle    11eb <Substring+0x9b>
test   %r12d,%r12d
js     11eb <Substring+0x9b>
test   %eax,%eax
jle    11eb <Substring+0x9b>
lea    (%r12,%r13,1),%ecx
cmp    %eax,%ecx
jg     11eb <Substring+0x9b>
lea    0xe71(%rip),%rsi
mov    %r13d,%edi
call   1070 <GetStackSize@plt>
mov    %rsp,-0x30(%rbp)
mov    %eax,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r12d,%eax
add    %rax,%r15
mov    %r13d,%r12d
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1040 <ReturnOutputMallocStack@plt>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ReturnNullMallocStack@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"char\00"
define i8* @Substring(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i8*, i8** %7
%15 = call i32 @GetStringLength(i8* %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp slt i32 %16, 1
br i1 %17, label %30, label %18
18: ; preds = %4
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %18
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 1
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = add nsw i32 %25, %26
%28 = load i32, i32* %10
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %24, %21, %18, %4
%31 = load i8*, i8** %6
%32 = call i8* @ReturnNullMallocStack(i8* %31)
store i8* %32, i8** %5
br label %65
33: ; preds = %24
%34 = load i32, i32* %9
%35 = call i32 @GetStackSize(i32 %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%36 = zext i32 %35 to i64
%37 = call i8* @llvm.stacksave()
store i8* %37, i8** %11
%38 = alloca i8, i64 %36
store i64 %36, i64* %12
store i32 0, i32* %13
store i32 0, i32* %13
br label %39
39: ; preds = %54, %33
%40 = load i32, i32* %13
%41 = load i32, i32* %9
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %57
43: ; preds = %39
%44 = load i8*, i8** %7
%45 = load i32, i32* %8
%46 = load i32, i32* %13
%47 = add nsw i32 %45, %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
%50 = load i8, i8* %49
%51 = load i32, i32* %13
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %38, i64 %52
store i8 %50, i8* %53
br label %54
54: ; preds = %43
%55 = load i32, i32* %13
%56 = add nsw i32 %55, 1
store i32 %56, i32* %13
br label %39
57: ; preds = %39
%58 = load i32, i32* %13
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %38, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %6
%62 = load i32, i32* %9
%63 = call i8* @ReturnOutputMallocStack(i8* %61, i8* %38, i32 %62)
store i8* %63, i8** %5
%64 = load i8*, i8** %11
br label %65
65: ; preds = %57, %30
%66 = load i8*, i8** %5
ret i8* %66
}

LLVM IR (-O2):
@.str = [5 x i8] c"char\00"
define i8* @Substring(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @GetStringLength(i8* %1)
%6 = icmp slt i32 %5, 1
%7 = icmp slt i32 %2, 0
%8 = or i1 %7, %6
%9 = icmp slt i32 %3, 1
%10 = or i1 %9, %8
%11 = add nsw i32 %3, %2
%12 = icmp sgt i32 %11, %5
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = tail call i8* @ReturnNullMallocStack(i8* %0)
br label %27
16: ; preds = %4
%17 = tail call i32 @GetStackSize(i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = zext i32 %17 to i64
%19 = tail call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
%21 = zext i32 %2 to i64
%22 = getelementptr i8, i8* %1, i64 %21
%23 = zext i32 %3 to i64
%24 = zext i32 %3 to i64
%25 = getelementptr inbounds i8, i8* %20, i64 %24
store i8 0, i8* %25
%26 = call i8* @ReturnOutputMallocStack(i8* %0, i8* %20, i32 %3)
br label %27
27: ; preds = %16, %14
%28 = phi i8* [ %15, %14 ], [ %26, %16 ]
ret i8* %28
}