#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 *, size_t);
void *mrealloc(void *old_ptr, size_t old_size, size_t new_size) {
  void *new_ptr = malloc(new_size);
  if (new_ptr == NULL)
    return NULL;
  if (old_ptr)
    memcpy(new_ptr, old_ptr,
           (((old_size) < (new_size)) ? (old_size) : (new_size)));
  return new_ptr;
}


x86 assembly (clang, -O0):
<mrealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1159 <mrealloc+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <mrealloc+0x90>
cmpq   $0x0,-0x10(%rbp)
je     11a8 <mrealloc+0x88>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    118f <mrealloc+0x6f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1197 <mrealloc+0x77>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %r15,%r15
je     115b <mrealloc+0x3b>
test   %r14,%r14
je     115b <mrealloc+0x3b>
cmp    %rbx,%r12
cmovb  %r12,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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