#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);
char *substring(char *string, int position, int length) {
  char *pointer = malloc(length + 1);
  int c;
  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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1163 <substring+0x53>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1138 <substring+0x28>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11a1 <substring+0x91>
mov    -0x8(%rbp),%rax
mov    (%rax),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116a <substring+0x5a>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r12d
mov    %rdi,%r14
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    1167 <substring+0x47>
lea    -0x2(%r12),%eax
add    $0x1,%rax
xor    %ecx,%ecx
cmp    $0x2,%r12d
cmovge %rax,%rcx
add    %rcx,%r14
mov    %ebx,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
jmp    1169 <substring+0x49>
xor    %ebx,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @substring(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = add nsw i32 %9, 1
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %7
store i32 0, i32* %8
br label %12
12: ; preds = %20, %3
%13 = load i32, i32* %8
%14 = load i32, i32* %5
%15 = sub nsw i32 %14, 1
%16 = icmp slt i32 %13, %15
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %4
br label %20
20: ; preds = %17
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
br label %12
23: ; preds = %12
store i32 0, i32* %8
br label %24
24: ; preds = %37, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i8*, i8** %4
%30 = load i8, i8* %29
%31 = load i8*, i8** %7
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 %30, i8* %34
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %4
br label %37
37: ; preds = %28
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %24
40: ; preds = %24
%41 = load i8*, i8** %7
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %7
ret i8* %45
}

LLVM IR (-O2):
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 sgt i32 %2, 0
br i1 %6, label %7, label %16
7: ; preds = %3
%8 = icmp sgt i32 %1, 1
%9 = add nsw i32 %1, -2
%10 = zext i32 %9 to i64
%11 = add nuw nsw i64 %10, 1
%12 = select i1 %8, i64 %11, i64 0
%13 = getelementptr i8, i8* %0, i64 %12
%14 = zext i32 %2 to i64
%15 = zext i32 %2 to i64
br label %16
16: ; preds = %7, %3
%17 = phi i64 [ 0, %3 ], [ %15, %7 ]
%18 = getelementptr inbounds i8, i8* %5, i64 %17
store i8 0, i8* %18
ret i8* %5
}