#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(int);
int /*<<< orphan*/ memcpy(void *, void const *, int);
void *V3DMPMemDup(const void *buf, int total) {
  void *rtn_buf = NULL;
  if ((buf == NULL) || (total <= 0))
    return (rtn_buf);
  rtn_buf = malloc(total);
  if (rtn_buf == NULL)
    return (rtn_buf);
  else
    memcpy(rtn_buf, buf, total);
  return (rtn_buf);
}


x86 assembly (clang, -O0):
<V3DMPMemDup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114c <V3DMPMemDup+0x2c>
cmpl   $0x0,-0x14(%rbp)
jg     1159 <V3DMPMemDup+0x39>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1195 <V3DMPMemDup+0x75>
mov    -0x14(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117d <V3DMPMemDup+0x5d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1195 <V3DMPMemDup+0x75>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
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):
<V3DMPMemDup>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1154 <V3DMPMemDup+0x34>
mov    %esi,%ebp
test   %esi,%esi
jle    1154 <V3DMPMemDup+0x34>
mov    %rdi,%r14
mov    %ebp,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1152 <V3DMPMemDup+0x32>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
jmp    1154 <V3DMPMemDup+0x34>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @V3DMPMemDup(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp slt i32 %1, 1
%5 = or i1 %3, %4
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i8* @malloc(i32 %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, i32 %1)
br label %11
11: ; preds = %6, %2, %9
%12 = phi i8* [ %7, %9 ], [ null, %2 ], [ null, %6 ]
ret i8* %12
}