#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 */
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void const *, size_t);
void *lsmash_memdup(const void *ptr, size_t size) {
  if (!ptr || size == 0)
    return NULL;
  void *dst = malloc(size);
  if (!dst)
    return NULL;
  memcpy(dst, ptr, size);
  return dst;
}


x86 assembly (clang, -O0):
<lsmash_memdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <lsmash_memdup+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1153 <lsmash_memdup+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1191 <lsmash_memdup+0x71>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1178 <lsmash_memdup+0x58>
movq   $0x0,-0x8(%rbp)
jmp    1191 <lsmash_memdup+0x71>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsmash_memdup>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1159 <lsmash_memdup+0x39>
mov    %rsi,%r14
test   %rsi,%rsi
je     1159 <lsmash_memdup+0x39>
mov    %rdi,%r15
mov    %r14,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1157 <lsmash_memdup+0x37>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <memcpy@plt>
jmp    1159 <lsmash_memdup+0x39>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @lsmash_memdup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i8* null, i8** %3
br label %25
13: ; preds = %9
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %13
store i8* null, i8** %3
br label %25
19: ; preds = %13
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = call i32 @memcpy(i8* %20, i8* %21, i64 %22)
%24 = load i8*, i8** %6
store i8* %24, i8** %3
br label %25
25: ; preds = %19, %18, %12
%26 = load i8*, i8** %3
ret i8* %26
}

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