#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 */
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void const *, size_t);
void *xmemdupz(const void *data, size_t len) {
  void *ret;
  if (len + 1 < len) {
    return NULL;
  }
  ret = malloc(len + 1);
  if (!ret) {
    return NULL;
  }
  ((char *)ret)[len] = 0;
  memcpy(ret, data, len);
  return ret;
}


x86 assembly (clang, -O0):
<xmemdupz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jae    114f <xmemdupz+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    119d <xmemdupz+0x7d>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1178 <xmemdupz+0x58>
movq   $0x0,-0x8(%rbp)
jmp    119d <xmemdupz+0x7d>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmemdupz>:
push   %r15
push   %r14
push   %rbx
cmp    $0xffffffffffffffff,%rsi
je     1156 <xmemdupz+0x36>
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1156 <xmemdupz+0x36>
mov    %rax,%r14
movb   $0x0,(%rax,%rbx,1)
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
jmp    1159 <xmemdupz+0x39>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @xmemdupz(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = add i64 %7, 1
%9 = load i64, i64* %5
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %28
12: ; preds = %2
%13 = load i64, i64* %5
%14 = add i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %12
store i8* null, i8** %3
br label %28
19: ; preds = %12
%20 = load i8*, i8** %6
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = call i32 @memcpy(i8* %23, i8* %24, i64 %25)
%27 = load i8*, i8** %6
store i8* %27, i8** %3
br label %28
28: ; preds = %19, %18, %11
%29 = load i8*, i8** %3
ret i8* %29
}

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