#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*/ memmove(char *, char *, size_t);
char *str_create(int /*<<< orphan*/ *);
char *str_reserve(char *, size_t);
char *str_substr(char *src, size_t len) {
  char *str = NULL;
  str = str_create(NULL);
  if (str == NULL)
    return (NULL);
  str = str_reserve(str, len + 1);
  if (str == NULL)
    return (NULL);
  memmove(str, src, len);
  str[len] = '\0';
  return (str);
}


x86 assembly (clang, -O0):
<str_substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <str_create@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116d <str_substr+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    11bf <str_substr+0x8f>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1030 <str_reserve@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119a <str_substr+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    11bf <str_substr+0x8f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memmove@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_substr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <str_create@plt>
test   %rax,%rax
je     1174 <str_substr+0x44>
lea    0x1(%r14),%rsi
mov    %rax,%rdi
call   1030 <str_reserve@plt>
test   %rax,%rax
je     1172 <str_substr+0x42>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <memmove@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    1174 <str_substr+0x44>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_substr(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* null, i8** %6
%7 = call i8* @str_create(i32* null)
store i8* %7, i8** %6
%8 = load i8*, i8** %6
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %28
11: ; preds = %2
%12 = load i8*, i8** %6
%13 = load i64, i64* %5
%14 = add i64 %13, 1
%15 = call i8* @str_reserve(i8* %12, i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %11
store i8* null, i8** %3
br label %28
19: ; preds = %11
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = call i32 @memmove(i8* %20, i8* %21, i64 %22)
%24 = load i8*, i8** %6
%25 = load i64, i64* %5
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %6
store i8* %27, i8** %3
br label %28
28: ; preds = %19, %18, %10
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
define i8* @str_substr(i8* %0, i64 %1) {
%3 = tail call i8* @str_create(i32* null)
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = add i64 %1, 1
%7 = tail call i8* @str_reserve(i8* %3, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i32 @memmove(i8* %7, i8* %0, i64 %1)
%11 = getelementptr inbounds i8, i8* %7, i64 %1
store i8 0, i8* %11
br label %12
12: ; preds = %5, %2, %9
%13 = phi i8* [ %7, %9 ], [ null, %2 ], [ null, %5 ]
ret i8* %13
}