#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *OICStrdup(const char *str) {
  if (!str) {
    return NULL;
  }
  size_t length = strlen(str);
  char *dup = (char *)malloc(length + 1);
  if (NULL != dup) {
    memcpy(dup, str, length + 1);
  }
  return dup;
}


x86 assembly (clang, -O0):
<OICStrdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <OICStrdup+0x24>
movq   $0x0,-0x8(%rbp)
jmp    119b <OICStrdup+0x6b>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     1193 <OICStrdup+0x63>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
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):
<OICStrdup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1169 <OICStrdup+0x39>
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     116c <OICStrdup+0x3c>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    116c <OICStrdup+0x3c>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @OICStrdup(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 %9, label %8
8: ; preds = %1
store i8* null, i8** %2
br label %26
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = add i64 %12, 1
%14 = call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp ne i8* null, %16
br i1 %17, label %18, label %24
18: ; preds = %9
%19 = load i8*, i8** %5
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = add i64 %21, 1
%23 = call i32 @memcpy(i8* %19, i8* %20, i64 %22)
br label %24
24: ; preds = %18, %9
%25 = load i8*, i8** %5
store i8* %25, i8** %2
br label %26
26: ; preds = %24, %8
%27 = load i8*, i8** %2
ret i8* %27
}

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