#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 */
typedef int /*<<< orphan*/ pointer;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *strdup(char *);
char *substring(char *string, int position, int length) {
  char pointer[length + 1];
  memset(&pointer[0], 0, sizeof(pointer));
  int c;
  for (c = 0; c < length; c++) {
    pointer[c] = *(string + position);
    string++;
  }
  pointer[c] = '\0';
  return strdup(pointer);
}


x86 assembly (clang, -O0):
<substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11a7 <substring+0x87>
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0xc(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116b <substring+0x4b>
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1040 <strdup@plt>
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
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    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
lea    0x1(%r15),%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
test   %r15d,%r15d
jle    1185 <substring+0x55>
movslq %r12d,%rax
add    %rax,%r14
mov    %r15d,%r13d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r13,1)
mov    %rbx,%rdi
call   1050 <strdup@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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 i64
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = trunc i64 %12 to i32
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
store i32 0, i32* %9
br label %18
18: ; preds = %33, %3
%19 = load i32, i32* %9
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %14, i64 %29
store i8 %27, i8* %30
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
br label %33
33: ; preds = %22
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %18
36: ; preds = %18
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %14, i64 %38
store i8 0, i8* %39
%40 = call i8* @strdup(i8* %14)
%41 = load i8*, i8** %7
ret i8* %40
}

LLVM IR (-O2):
define noalias i8* @substring(i8* nocapture %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @memset(i8* %6, i32 0, i32 %4)
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = sext i32 %1 to i64
%11 = getelementptr i8, i8* %0, i64 %10
%12 = zext i32 %2 to i64
%13 = zext i32 %2 to i64
br label %14
14: ; preds = %9, %3
%15 = phi i64 [ 0, %3 ], [ %13, %9 ]
%16 = getelementptr inbounds i8, i8* %6, i64 %15
store i8 0, i8* %16
%17 = call i8* @strdup(i8* %6)
ret i8* %17
}