#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 *memdup2(const void *src1, size_t n1, const void *src2, size_t n2) {
  void *dest;
  if (!(dest = malloc(n1 + n2)))
    return NULL;
  memcpy(dest, src1, n1);
  memcpy(dest + n1, src2, n2);
  return dest;
}


x86 assembly (clang, -O0):
<memdup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1160 <memdup2+0x40>
movq   $0x0,-0x8(%rbp)
jmp    118e <memdup2+0x6e>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memdup2>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
lea    (%rcx,%rsi,1),%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1165 <memdup2+0x45>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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