#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(char const *);
char *cbmyfatal(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *cbmemdup(const char *ptr, int size) {
  char *p;
  assert(ptr);
  if (size < 0)
    size = strlen(ptr);
  (((p) = malloc(size + 1)) ? (p) : cbmyfatal("out of memory"));
  memcpy(p, ptr, size);
  p[size] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<cbmemdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
jge    117e <cbmemdup+0x2e>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a0 <cbmemdup+0x50>
mov    -0x18(%rbp),%rax
jmp    11ac <cbmemdup+0x5c>
lea    0xe59(%rip),%rdi
call   1070 <cbmyfatal@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbmemdup>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
call   1040 <assert@plt>
test   %r15d,%r15d
jns    1170 <cbmemdup+0x20>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118d <cbmemdup+0x3d>
lea    0xe78(%rip),%rdi
call   1070 <cbmyfatal@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
define i8* @cbmemdup(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = call i32 @assert(i8* %6)
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %4
br label %13
13: ; preds = %10, %2
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i8*, i8** %5
br label %22
20: ; preds = %13
%21 = call i8* @cbmyfatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = phi i8* [ %19, %18 ], [ %21, %20 ]
%24 = load i8*, i8** %5
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = call i32 @memcpy(i8* %24, i8* %25, i32 %26)
%28 = load i8*, i8** %5
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %5
ret i8* %32
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
define i8* @cbmemdup(i8* %0, i32 %1) {
%3 = tail call i32 @assert(i8* %0)
%4 = icmp slt i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
br label %7
7: ; preds = %5, %2
%8 = phi i32 [ %6, %5 ], [ %1, %2 ]
%9 = add nsw i32 %8, 1
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i8* @cbmyfatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %7, %12
%15 = tail call i32 @memcpy(i8* %10, i8* %0, i32 %8)
%16 = sext i32 %8 to i64
%17 = getelementptr inbounds i8, i8* %10, i64 %16
store i8 0, i8* %17
ret i8* %10
}