#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 ENAMETOOLONG;
int ENOMEM;
char *strdup(char const *);
int strnlen(char const *, int);
int tapdisk_namedup(char **dup, const char *name) {
  *dup = NULL;
  if (strnlen(name, 1000) >= 1000)
    return -ENAMETOOLONG;
  *dup = strdup(name);
  if (!*dup)
    return -ENOMEM;
  return 0;
}


x86 assembly (clang, -O0):
<tapdisk_namedup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    $0x3e8,%esi
call   1030 <strnlen@plt>
cmp    $0x3e8,%eax
jl     1167 <tapdisk_namedup+0x47>
xor    %eax,%eax
mov    0x2e7b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <tapdisk_namedup+0x82>
mov    -0x18(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119b <tapdisk_namedup+0x7b>
xor    %eax,%eax
mov    0x2e4f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <tapdisk_namedup+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tapdisk_namedup>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0x0,(%rdi)
mov    %rsi,%rdi
mov    $0x3e8,%esi
call   1030 <strnlen@plt>
cmp    $0x3e8,%eax
jl     114e <tapdisk_namedup+0x2e>
mov    0x2e8c(%rip),%rcx
jmp    116a <tapdisk_namedup+0x4a>
mov    %rbx,%rdi
call   1040 <strdup@plt>
mov    %rax,%rcx
mov    %rax,(%r14)
xor    %eax,%eax
test   %rcx,%rcx
jne    116e <tapdisk_namedup+0x4e>
mov    0x2e76(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ENAMETOOLONG = global i32 0
@ENOMEM = global i32 0
define i32 @tapdisk_namedup(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%6 = load i8**, i8*** %4
store i8* null, i8** %6
%7 = load i8*, i8** %5
%8 = call i32 @strnlen(i8* %7, i32 1000)
%9 = icmp sge i32 %8, 1000
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @ENAMETOOLONG
%12 = sub nsw i32 0, %11
store i32 %12, i32* %3
br label %24
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i8* @strdup(i8* %14)
%16 = load i8**, i8*** %4
store i8* %15, i8** %16
%17 = load i8**, i8*** %4
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = load i32, i32* @ENOMEM
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %24
23: ; preds = %13
store i32 0, i32* %3
br label %24
24: ; preds = %23, %20, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@ENAMETOOLONG = global i32 0
@ENOMEM = global i32 0
define i32 @tapdisk_namedup(i8** nocapture writeonly %0, i8* %1) {
store i8* null, i8** %0
%3 = tail call i32 @strnlen(i8* %1, i32 1000)
%4 = icmp sgt i32 %3, 999
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @ENAMETOOLONG
%7 = sub nsw i32 0, %6
br label %14
8: ; preds = %2
%9 = tail call i8* @strdup(i8* %1)
store i8* %9, i8** %0
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @ENOMEM
%13 = sub nsw i32 0, %12
br label %14
14: ; preds = %8, %11, %5
%15 = phi i32 [ %7, %5 ], [ %13, %11 ], [ 0, %8 ]
ret i32 %15
}