#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*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *realloc(void *ptr, size_t size) {
  void *new_data = NULL;
  if (size) {
    if (!ptr) {
      return malloc(size);
    }
    new_data = malloc(size);
    if (new_data) {
      memcpy(new_data, ptr, size);
      free(ptr);
    }
  }
  return new_data;
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a7 <realloc+0x77>
cmpq   $0x0,-0x10(%rbp)
jne    1170 <realloc+0x40>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11af <realloc+0x7f>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a2 <realloc+0x72>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    11a7 <realloc+0x77>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     116d <realloc+0x3d>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %r14,%r14
je     1170 <realloc+0x40>
test   %r15,%r15
je     116d <realloc+0x3d>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1170 <realloc+0x40>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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