#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 *zmemdup(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)) == 0) {
    zfatal("FATAL zmalloc: insufficient memory for %d bytes: %m", n);
  }
  if (n > 0) {
    memcpy(r, ptr, n);
  }
  return r;
}


x86 assembly (clang, -O0):
<zmemdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <zmemdup+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    11c4 <zmemdup+0x94>
cmpl   $0x0,-0x14(%rbp)
jge    117d <zmemdup+0x4d>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a2 <zmemdup+0x72>
mov    -0x14(%rbp),%esi
lea    0xe63(%rip),%rdi
call   1030 <zfatal@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11bc <zmemdup+0x8c>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zmemdup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     115f <zmemdup+0x2f>
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebp,%ebp
test   %esi,%esi
cmovns %esi,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1171 <zmemdup+0x41>
test   %r15d,%r15d
jg     1184 <zmemdup+0x54>
jmp    1191 <zmemdup+0x61>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
jmp    1191 <zmemdup+0x61>
lea    0xe88(%rip),%rdi
mov    %ebp,%esi
call   1030 <zfatal@plt>
test   %r15d,%r15d
jle    1191 <zmemdup+0x61>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [52 x i8] c"FATAL zmalloc: insufficient memory for %d bytes: %m\00"
define i8* @zmemdup(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 %37
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 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* %5
%26 = call i32 @zfatal(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %19
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i8*, i8** %6
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = call i32 @memcpy(i8* %31, i8* %32, i32 %33)
br label %35
35: ; preds = %30, %27
%36 = load i8*, i8** %6
store i8* %36, i8** %3
br label %37
37: ; preds = %35, %9
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@.str = [52 x i8] c"FATAL zmalloc: insufficient memory for %d bytes: %m\00"
define i8* @zmemdup(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*
store i8 0, i8* %6
br label %18
7: ; preds = %2
%8 = icmp sgt i32 %1, 0
%9 = select i1 %8, i32 %1, i32 0
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @zfatal(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %15
15: ; preds = %13, %7
br i1 %8, label %16, label %18
16: ; preds = %15
%17 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %9)
br label %18
18: ; preds = %15, %16, %4
%19 = phi i8* [ %6, %4 ], [ %11, %16 ], [ %11, %15 ]
ret i8* %19
}