#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strdup(char const *);
char *cstrdup(const char *src, const char *function, int line) {
  char *tgt = strdup(src ? src : "");
  if (!tgt) {
    perror("cstrdup(src, function, line)");
    fprintf(stderr, "error: %s: %d\n", function, line);
    exit(2);
  }
  return tgt;
}


x86 assembly (clang, -O0):
<cstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     116b <cstrdup+0x2b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117b <cstrdup+0x3b>
lea    0xeb9(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    117b <cstrdup+0x3b>
mov    -0x28(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c5 <cstrdup+0x85>
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstrdup>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
test   %rdi,%rdi
lea    0xed9(%rip),%rax
cmove  %rax,%rdi
call   1060 <strdup@plt>
test   %rax,%rax
je     1167 <cstrdup+0x27>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe92(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [29 x i8] c"cstrdup(src, function, line)\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"error: %s: %d\0A\00"
define i8* @cstrdup(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = load i8*, i8** %4
br label %13
12: ; preds = %3
br label %13
13: ; preds = %12, %10
%14 = phi i8* [ %11, %10 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %12 ]
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %25, label %18
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 %22)
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %13
%26 = load i8*, i8** %7
ret i8* %26
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [29 x i8] c"cstrdup(src, function, line)\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"error: %s: %d\0A\00"
define noalias i8* @cstrdup(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
%5 = select i1 %4, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%6 = tail call i8* @strdup(i8* %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %2)
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %3
ret i8* %6
}