#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*/ CMUTIL_MemLogWithStack(char *);
char *CMUTIL_MemRcyAlloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
char *CMUTIL_MemRcyStrdup(const char *str) {
  if (str) {
    size_t size = strlen(str) + 1;
    char *res = CMUTIL_MemRcyAlloc(size);
    memcpy(res, str, size);
    return res;
  } else {
    CMUTIL_MemLogWithStack("strdup with null pointer");
    return NULL;
  }
}


x86 assembly (clang, -O0):
<CMUTIL_MemRcyStrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <CMUTIL_MemRcyStrdup+0x54>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <CMUTIL_MemRcyAlloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a8 <CMUTIL_MemRcyStrdup+0x68>
lea    0xe65(%rip),%rdi
call   1060 <CMUTIL_MemLogWithStack@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CMUTIL_MemRcyStrdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1173 <CMUTIL_MemRcyStrdup+0x33>
mov    %rdi,%rbx
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1030 <CMUTIL_MemRcyAlloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
jmp    1182 <CMUTIL_MemRcyStrdup+0x42>
lea    0xe86(%rip),%rdi
call   1060 <CMUTIL_MemLogWithStack@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"strdup with null pointer\00"
define i8* @CMUTIL_MemRcyStrdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %20
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = call i8* @CMUTIL_MemRcyAlloc(i64 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = load i8*, i8** %3
%17 = load i64, i64* %4
%18 = call i32 @memcpy(i8* %15, i8* %16, i64 %17)
%19 = load i8*, i8** %5
store i8* %19, i8** %2
br label %22
20: ; preds = %1
%21 = call i32 @CMUTIL_MemLogWithStack(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %22
22: ; preds = %20, %8
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@.str = [25 x i8] c"strdup with null pointer\00"
define i8* @CMUTIL_MemRcyStrdup(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %9, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i8* @CMUTIL_MemRcyAlloc(i64 %6)
%8 = tail call i32 @memcpy(i8* %7, i8* %0, i64 %6)
br label %11
9: ; preds = %1
%10 = tail call i32 @CMUTIL_MemLogWithStack(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = phi i8* [ %7, %3 ], [ null, %9 ]
ret i8* %12
}