#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*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ reuse_out_of_mem();
char *xmalloc(size_t);
char *xmemdup(char const *str, size_t size) {
  char *ptr;
  if (str == NULL)
    str = "";
  ptr = xmalloc(size + 1);
  if (ptr == NULL)
    reuse_out_of_mem();
  memcpy(ptr, str, size);
  ptr[size] = 0;
  return ptr;
}


x86 assembly (clang, -O0):
<xmemdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1156 <xmemdup+0x26>
lea    0xeae(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1030 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1179 <xmemdup+0x49>
mov    $0x0,%al
call   1050 <reuse_out_of_mem@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmemdup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1030 <xmalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1153 <xmemdup+0x23>
xor    %eax,%eax
call   1050 <reuse_out_of_mem@plt>
test   %r15,%r15
lea    0xea3(%rip),%rsi
cmovne %r15,%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @xmemdup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %3
br label %9
9: ; preds = %8, %2
%10 = load i64, i64* %4
%11 = add i64 %10, 1
%12 = call i8* @xmalloc(i64 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 (...) @reuse_out_of_mem()
br label %17
17: ; preds = %15, %9
%18 = load i8*, i8** %5
%19 = load i8*, i8** %3
%20 = load i64, i64* %4
%21 = call i32 @memcpy(i8* %18, i8* %19, i64 %20)
%22 = load i8*, i8** %5
%23 = load i64, i64* %4
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %5
ret i8* %25
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @xmemdup(i8* %0, i64 %1) {
%3 = add i64 %1, 1
%4 = tail call i8* @xmalloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @reuse_out_of_mem()
br label %8
8: ; preds = %6, %2
%9 = icmp eq i8* %0, null
%10 = select i1 %9, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%11 = tail call i32 @memcpy(i8* %4, i8* %10, i64 %1)
%12 = getelementptr inbounds i8, i8* %4, i64 %1
store i8 0, i8* %12
ret i8* %4
}