#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*/ memcpy(void *, void *, size_t);
void *solv_malloc2(size_t, size_t);
void *solv_memdup2(void *buf, size_t num, size_t len) {
  void *newbuf;
  if (!buf)
    return 0;
  newbuf = solv_malloc2(num, len);
  if (num)
    memcpy(newbuf, buf, num * len);
  return newbuf;
}


x86 assembly (clang, -O0):
<solv_memdup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114c <solv_memdup2+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    1186 <solv_memdup2+0x66>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <solv_malloc2@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117e <solv_memdup2+0x5e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
imul   -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<solv_memdup2>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     115d <solv_memdup2+0x3d>
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1030 <solv_malloc2@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     1160 <solv_memdup2+0x40>
imul   %rbx,%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
jmp    1160 <solv_memdup2+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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