#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ fail(char *, unsigned long, char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
char *xstrdup(const char *s) {
  size_t size;
  char *t;
  assert(s != NULL);
  size = strlen(s) + 1;
  t = malloc(size);
  if (!t)
    fail("xstrdup(%lu): %s", (unsigned long)strlen(s),
         "virtual memory exhausted");
  memcpy(t, s, size);
  return t;
}


x86 assembly (clang, -O0):
<xstrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <xstrdup+0x67>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
movslq %eax,%rsi
lea    0xe55(%rip),%rdi
lea    0xe5f(%rip),%rdx
call   1030 <fail@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r14
mov    %r14,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a2 <xstrdup+0x52>
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rsi
lea    0xe6a(%rip),%rdi
lea    0xe74(%rip),%rdx
call   1030 <fail@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"xstrdup(%lu): %s\00"
@.str.1 = [25 x i8] c"virtual memory exhausted\00"
define i8* @xstrdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %3
%13 = load i64, i64* %3
%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 %22, label %17
17: ; preds = %1
%18 = load i8*, i8** %2
%19 = call i32 @strlen(i8* %18)
%20 = sext i32 %19 to i64
%21 = call i32 @fail(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %17, %1
%23 = load i8*, i8** %4
%24 = load i8*, i8** %2
%25 = load i64, i64* %3
%26 = call i32 @memcpy(i8* %23, i8* %24, i64 %25)
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
@.str = [17 x i8] c"xstrdup(%lu): %s\00"
@.str.1 = [25 x i8] c"virtual memory exhausted\00"
define i8* @xstrdup(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = tail call i8* @malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %0)
%12 = sext i32 %11 to i64
%13 = tail call i32 @fail(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %10, %1
%15 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %7)
ret i8* %8
}