#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*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, int /*<<< orphan*/);
char *strdup(char const *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *xstrdup(const char *restrict s) {
  char *r;
  if (!s)
    error("%s", strerror(EINVAL));
  if (!(r = strdup(s)))
    error("%s", strerror(errno));
  return r;
}


x86 assembly (clang, -O0):
<xstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1163 <xstrdup+0x33>
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xea2(%rip),%rdi
call   1030 <error@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1196 <xstrdup+0x66>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe6f(%rip),%rdi
call   1030 <error@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrdup>:
push   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1155 <xstrdup+0x25>
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xeb2(%rip),%rdi
mov    %eax,%esi
call   1030 <error@plt>
mov    %rbx,%rdi
call   1040 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1181 <xstrdup+0x51>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
call   1030 <error@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@EINVAL = global i32 0
@errno = global i32 0
define i8* @xstrdup(i8* noalias %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = load i32, i32* @EINVAL
%8 = call i32 @strerror(i32 %7)
%9 = call i32 @error(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %6, %1
%11 = load i8*, i8** %2
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @error(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %10
%19 = load i8*, i8** %3
ret i8* %19
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@EINVAL = global i32 0
@errno = global i32 0
define noalias i8* @xstrdup(i8* noalias %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = load i32, i32* @EINVAL
%5 = tail call i32 @strerror(i32 %4)
%6 = tail call i32 @error(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %3, %1
%8 = tail call i8* @strdup(i8* %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @error(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10, %7
ret i8* %8
}