#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 const *, size_t);
void *xmalloc(size_t, size_t);
void *xrealloc(void *, size_t, size_t);
void *reallocdup(void *heap, size_t nnew, const void *stack, size_t nstack,
                 size_t size) {
  if (heap) {
    heap = xrealloc(heap, nnew, size);
  } else {
    heap = xmalloc(nnew, size);
    if (stack) {
      memcpy(heap, stack, size * nstack);
    }
  }
  return heap;
}


x86 assembly (clang, -O0):
<reallocdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1171 <reallocdup+0x41>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <xrealloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11a8 <reallocdup+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <xmalloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a3 <reallocdup+0x73>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
imul   -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
jmp    11a8 <reallocdup+0x78>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reallocdup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
test   %rdi,%rdi
je     1153 <reallocdup+0x23>
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <xrealloc@plt>
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rdi
mov    %rbx,%rsi
call   1040 <xmalloc@plt>
mov    %rax,%r12
test   %r14,%r14
je     117e <reallocdup+0x4e>
imul   %r15,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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