#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 */
scalar_t__ cst_safe_alloc(int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *cst_substr(const char *str, int start, int length) {
  char *nstr = NULL;
  if (str) {
    nstr = ((char *)cst_safe_alloc(sizeof(char) * (length + 1)));
    strncpy(nstr, str + start, length);
    nstr[length] = '\0';
  }
  return nstr;
}


x86 assembly (clang, -O0):
<cst_substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     117f <cst_substr+0x5f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <cst_safe_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cst_substr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     115e <cst_substr+0x3e>
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x1(%r14),%edi
call   1040 <cst_safe_alloc@plt>
mov    %rax,%r15
movslq %ebp,%rax
add    %rax,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    1161 <cst_substr+0x41>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @cst_substr(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* null, i8** %7
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %29
10: ; preds = %3
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @cst_safe_alloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i32, i32* %6
%24 = call i32 @strncpy(i8* %18, i8* %22, i32 %23)
%25 = load i8*, i8** %7
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %10, %3
%30 = load i8*, i8** %7
ret i8* %30
}

LLVM IR (-O2):
define i8* @cst_substr(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = add nsw i32 %2, 1
%7 = tail call i64 @cst_safe_alloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = sext i32 %1 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = tail call i32 @strncpy(i8* %8, i8* %10, i32 %2)
%12 = sext i32 %2 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %5, %3
%15 = phi i8* [ %8, %5 ], [ null, %3 ]
ret i8* %15
}