#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 *emalloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ strcpy(char *, char *);
char *substr(const char *s, size_t len) {
  char *p;
  if (len == 0) {
    p = emalloc(1);
    strcpy(p, "");
  } else {
    p = emalloc(len + 1);
    memcpy(p, s, len);
    p[len] = '\0';
  }
  return p;
}


x86 assembly (clang, -O0):
<substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116e <substr+0x3e>
mov    $0x1,%edi
call   1050 <emalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1030 <strcpy@plt>
jmp    119c <substr+0x6c>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <emalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1161 <substr+0x31>
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1050 <emalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
jmp    117d <substr+0x4d>
mov    $0x1,%edi
call   1050 <emalloc@plt>
mov    %rax,%r14
lea    0xe8b(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @substr(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = call i8* @emalloc(i64 1)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %23
12: ; preds = %2
%13 = load i64, i64* %4
%14 = add i64 %13, 1
%15 = call i8* @emalloc(i64 %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = load i8*, i8** %3
%18 = load i64, i64* %4
%19 = call i32 @memcpy(i8* %16, i8* %17, i64 %18)
%20 = load i8*, i8** %5
%21 = load i64, i64* %4
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %12, %8
%24 = load i8*, i8** %5
ret i8* %24
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @substr(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i8* @emalloc(i64 1)
%6 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %12
7: ; preds = %2
%8 = add i64 %1, 1
%9 = tail call i8* @emalloc(i64 %8)
%10 = tail call i32 @memcpy(i8* %9, i8* %0, i64 %1)
%11 = getelementptr inbounds i8, i8* %9, i64 %1
store i8 0, i8* %11
br label %12
12: ; preds = %7, %4
%13 = phi i8* [ %5, %4 ], [ %9, %7 ]
ret i8* %13
}