#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
char *pchar_substring(const char *s, int index, int length) {
  int slen = strlen(s);
  if (index + length > slen) {
    fprintf(stderr, "Out of range substring\n");
    return NULL;
  }
  char *newstr = (char *)malloc(length + 1);
  memset(newstr, 0, length + 1);
  memcpy(newstr, &s[index], length);
  return newstr;
}


x86 assembly (clang, -O0):
<pchar_substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    119f <pchar_substring+0x4f>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11de <pchar_substring+0x8e>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pchar_substring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%r14,%r15,1),%ecx
cmp    %eax,%ecx
jle    1188 <pchar_substring+0x38>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1050 <fprintf@plt>
xor    %r12d,%r12d
jmp    11b6 <pchar_substring+0x66>
lea    0x1(%r14),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
movslq %r15d,%rax
add    %rax,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <memcpy@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Out of range substring\0A\00"
define i8* @pchar_substring(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %6
%13 = load i32, i32* %7
%14 = add nsw i32 %12, %13
%15 = load i32, i32* %8
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %37
20: ; preds = %3
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
%28 = call i32 @memset(i8* %25, i32 0, i32 %27)
%29 = load i8*, i8** %9
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %7
%35 = call i32 @memcpy(i8* %29, i8* %33, i32 %34)
%36 = load i8*, i8** %9
store i8* %36, i8** %4
br label %37
37: ; preds = %20, %17
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Out of range substring\0A\00"
define i8* @pchar_substring(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %2, %1
%6 = icmp sgt i32 %5, %4
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %18
10: ; preds = %3
%11 = add nsw i32 %2, 1
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @memset(i8* %13, i32 0, i32 %11)
%15 = sext i32 %1 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = tail call i32 @memcpy(i8* %13, i8* %16, i32 %2)
br label %18
18: ; preds = %10, %7
%19 = phi i8* [ null, %7 ], [ %13, %10 ]
ret i8* %19
}