#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 *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *substring(char *s, int start, int length) {
  if (length <= 0)
    return "";
  char *sub = malloc(length + 1);
  memcpy(sub, &s[start], length);
  sub[length] = '\0';
  return sub;
}


x86 assembly (clang, -O0):
<substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     114c <substring+0x2c>
lea    0xebd(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1186 <substring+0x66>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substring>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1159 <substring+0x39>
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r15
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
movslq %ebp,%rax
add    %rax,%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
jmp    1160 <substring+0x40>
lea    0xea0(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @substring(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp sle i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %4
br label %28
12: ; preds = %3
%13 = load i32, i32* %7
%14 = add nsw i32 %13, 1
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %7
%22 = call i32 @memcpy(i8* %16, i8* %20, i32 %21)
%23 = load i8*, i8** %8
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %8
store i8* %27, i8** %4
br label %28
28: ; preds = %12, %11
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @substring(i8* %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %2, 1
br i1 %4, label %13, label %5
5: ; preds = %3
%6 = add nuw nsw i32 %2, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = sext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = tail call i32 @memcpy(i8* %7, i8* %9, i32 %2)
%11 = zext i32 %2 to i64
%12 = getelementptr inbounds i8, i8* %7, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %3, %5
%14 = phi i8* [ %7, %5 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %3 ]
ret i8* %14
}