#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*/ printf(char *);
char *Substring(char *string, int position, int length) {
  char *pointer;
  int c;
  pointer = malloc(length + 1);
  if (pointer == NULL) {
    printf("Unable to allocate memory.\n");
    return NULL;
  }
  for (c = 0; c < position - 1; c++)
    string++;
  for (c = 0; c < length; c++) {
    *(pointer + c) = *string;
    string++;
  }
  *(pointer + c) = '\0';
  return pointer;
}


x86 assembly (clang, -O0):
<Substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1165 <Substring+0x45>
lea    0xead(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <Substring+0xc9>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1197 <Substring+0x77>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116c <Substring+0x4c>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11d5 <Substring+0xb5>
mov    -0x10(%rbp),%rax
mov    (%rax),%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119e <Substring+0x7e>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Substring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1180 <Substring+0x50>
test   %r15d,%r15d
jle    118e <Substring+0x5e>
lea    -0x2(%r12),%eax
add    $0x1,%rax
xor    %ecx,%ecx
cmp    $0x2,%r12d
cmovge %rax,%rcx
add    %rcx,%r14
mov    %r15d,%r15d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
jmp    1191 <Substring+0x61>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
jmp    1196 <Substring+0x66>
xor    %r15d,%r15d
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Unable to allocate memory.\0A\00"
define i8* @Substring(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = add nsw i32 %10, 1
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %52
17: ; preds = %3
store i32 0, i32* %9
br label %18
18: ; preds = %26, %17
%19 = load i32, i32* %9
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
br label %26
26: ; preds = %23
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
br label %18
29: ; preds = %18
store i32 0, i32* %9
br label %30
30: ; preds = %43, %29
%31 = load i32, i32* %9
%32 = load i32, i32* %7
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %46
34: ; preds = %30
%35 = load i8*, i8** %5
%36 = load i8, i8* %35
%37 = load i8*, i8** %8
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
br label %43
43: ; preds = %34
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
br label %30
46: ; preds = %30
%47 = load i8*, i8** %8
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %8
store i8* %51, i8** %4
br label %52
52: ; preds = %46, %15
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
@str = [27 x i8] c"Unable to allocate memory.\00"
define noalias i8* @Substring(i8* nocapture %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = icmp sgt i32 %2, 0
br i1 %8, label %11, label %20
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %23
11: ; preds = %7
%12 = icmp sgt i32 %1, 1
%13 = add nsw i32 %1, -2
%14 = zext i32 %13 to i64
%15 = add nuw nsw i64 %14, 1
%16 = select i1 %12, i64 %15, i64 0
%17 = getelementptr i8, i8* %0, i64 %16
%18 = zext i32 %2 to i64
%19 = zext i32 %2 to i64
br label %20
20: ; preds = %11, %7
%21 = phi i64 [ 0, %7 ], [ %19, %11 ]
%22 = getelementptr inbounds i8, i8* %5, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %20, %9
ret i8* %5
}