#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 *malloc(size_t);
char *memcpy(char *, char const *, size_t);
size_t strnlen(char const *, unsigned long);
char *dupstr(const char *str) {
  char *dup;
  size_t len;
  if (!str)
    str = "(null)";
  len = strnlen(str, 4096ul);
  dup = malloc(len + 1);
  if (!dup)
    return NULL;
  dup[len] = 0;
  return memcpy(dup, str, len);
}


x86 assembly (clang, -O0):
<dupstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1152 <dupstr+0x22>
lea    0xeb2(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1030 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118d <dupstr+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11ae <dupstr+0x7e>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupstr>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
lea    0xec1(%rip),%r15
cmovne %rdi,%r15
mov    $0x1000,%esi
mov    %r15,%rdi
call   1030 <strnlen@plt>
mov    %rax,%r14
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1177 <dupstr+0x47>
movb   $0x0,(%rbx,%r14,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"(null)\00"
define i8* @dupstr(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %3
br label %9
9: ; preds = %8, %1
%10 = load i8*, i8** %3
%11 = call i64 @strnlen(i8* %10, i64 4096)
store i64 %11, i64* %5
%12 = load i64, i64* %5
%13 = add i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %9
store i8* null, i8** %2
br label %26
18: ; preds = %9
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8 0, i8* %21
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = load i64, i64* %5
%25 = call i8* @memcpy(i8* %22, i8* %23, i64 %24)
store i8* %25, i8** %2
br label %26
26: ; preds = %18, %17
%27 = load i8*, i8** %2
ret i8* %27
}

LLVM IR (-O2):
@.str = [7 x i8] c"(null)\00"
define noalias i8* @dupstr(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0
%4 = tail call i64 @strnlen(i8* %3, i64 4096)
%5 = add i64 %4, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = getelementptr inbounds i8, i8* %6, i64 %4
store i8 0, i8* %9
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %6, i8* align 1 %3, i64 %4, i1 false)
br label %10
10: ; preds = %1, %8
ret i8* %6
}