#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, void const *, int);
int /*<<< orphan*/ zfatal(char *, int);
char *zmemdupnull(const void *ptr, int n) {
  char *r;
  if (!ptr) {
    r = (char *)malloc(1);
    r[0] = 0;
    return r;
  }
  if (n < 0) {
    n = 0;
  }
  if ((r = (char *)malloc(n + 1)) == 0) {
    zfatal("FATAL zmalloc: insufficient memory for %d bytes: %m", (n + 1));
  }
  if (n > 0) {
    memcpy(r, ptr, n);
  }
  r[n] = 0;
  return r;
}


x86 assembly (clang, -O0):
<zmemdupnull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <zmemdupnull+0x3c>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <zmemdupnull+0xa6>
cmpl   $0x0,-0x14(%rbp)
jge    117d <zmemdupnull+0x4d>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a8 <zmemdupnull+0x78>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
lea    0xe5d(%rip),%rdi
call   1030 <zfatal@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11c2 <zmemdupnull+0x92>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zmemdupnull>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     117c <zmemdupnull+0x4c>
mov    %esi,%ebp
mov    %rdi,%r15
xor    %r14d,%r14d
test   %esi,%esi
cmovns %esi,%r14d
lea    0x1(%r14),%r12d
mov    %r12d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1198 <zmemdupnull+0x68>
test   %ebp,%ebp
jle    1171 <zmemdupnull+0x41>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
mov    %r14d,%ecx
mov    %rbx,%rax
add    %rcx,%rax
jmp    1189 <zmemdupnull+0x59>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe61(%rip),%rdi
mov    %r12d,%esi
call   1030 <zfatal@plt>
test   %ebp,%ebp
jg     1163 <zmemdupnull+0x33>
jmp    1171 <zmemdupnull+0x41>

LLVM IR (-O0):
@.str = [52 x i8] c"FATAL zmalloc: insufficient memory for %d bytes: %m\00"
define i8* @zmemdupnull(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = call i64 @malloc(i32 1)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
%14 = load i8*, i8** %6
store i8* %14, i8** %3
br label %43
15: ; preds = %2
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 0, i32* %5
br label %19
19: ; preds = %18, %15
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %6
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
%28 = call i32 @zfatal(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %19
%30 = load i32, i32* %5
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = call i32 @memcpy(i8* %33, i8* %34, i32 %35)
br label %37
37: ; preds = %32, %29
%38 = load i8*, i8** %6
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %6
store i8* %42, i8** %3
br label %43
43: ; preds = %37, %9
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
@.str = [52 x i8] c"FATAL zmalloc: insufficient memory for %d bytes: %m\00"
define i8* @zmemdupnull(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i64 @malloc(i32 1)
%6 = inttoptr i64 %5 to i8*
br label %22
7: ; preds = %2
%8 = icmp sgt i32 %1, 0
%9 = select i1 %8, i32 %1, i32 0
%10 = add nuw nsw i32 %9, 1
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @zfatal(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %16
16: ; preds = %14, %7
br i1 %8, label %17, label %19
17: ; preds = %16
%18 = tail call i32 @memcpy(i8* %12, i8* %0, i32 %9)
br label %19
19: ; preds = %17, %16
%20 = zext i32 %9 to i64
%21 = getelementptr inbounds i8, i8* %12, i64 %20
br label %22
22: ; preds = %19, %4
%23 = phi i8* [ %21, %19 ], [ %6, %4 ]
%24 = phi i8* [ %12, %19 ], [ %6, %4 ]
store i8 0, i8* %23
ret i8* %24
}