#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__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strnlen(char const *, size_t);
char *cpstrn(const char *str, size_t len) {
  char *ret;
  len = strnlen(str, len);
  ret = (char *)malloc((len + 1) * sizeof(char));
  if (ret == NULL)
    return ret;
  memcpy(ret, str, len * sizeof(char));
  ret[len] = 0;
  return ret;
}


x86 assembly (clang, -O0):
<cpstrn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <cpstrn+0x4e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <cpstrn+0x77>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1040 <memcpy@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):
<cpstrn>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strnlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1164 <cpstrn+0x34>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @cpstrn(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
%7 = load i8*, i8** %4
%8 = load i64, i64* %5
%9 = call i64 @strnlen(i8* %7, i64 %8)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = add i64 %10, 1
%12 = mul i64 %11, 1
%13 = call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = load i8*, i8** %6
store i8* %18, i8** %3
br label %29
19: ; preds = %2
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = mul i64 %22, 1
%24 = call i32 @memcpy(i8* %20, i8* %21, i64 %23)
%25 = load i8*, i8** %6
%26 = load i64, i64* %5
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %6
store i8* %28, i8** %3
br label %29
29: ; preds = %19, %17
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
define i8* @cpstrn(i8* %0, i64 %1) {
%3 = tail call i64 @strnlen(i8* %0, i64 %1)
%4 = add i64 %3, 1
%5 = tail call i64 @malloc(i64 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @memcpy(i8* %6, i8* %0, i64 %3)
%10 = getelementptr inbounds i8, i8* %6, i64 %3
store i8 0, i8* %10
br label %11
11: ; preds = %2, %8
ret i8* %6
}