#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 __attribute__((weak)) * realloc(void *ptr, size_t size) {
  if (ptr == NULL) {
    return malloc(size);
  } else if (size == 0) {
    free(ptr);
    return NULL;
  } else {
    void *newptr = malloc(size);
    if (newptr) {
      memcpy(newptr, ptr, size);
    }
    return newptr;
  }
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115d <realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11af <realloc+0x7f>
cmpq   $0x0,-0x18(%rbp)
jne    117e <realloc+0x4e>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11af <realloc+0x7f>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a7 <realloc+0x77>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
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
mov    %rsi,%rbx
test   %rdi,%rdi
je     1165 <realloc+0x35>
mov    %rdi,%r14
test   %rbx,%rbx
je     1172 <realloc+0x42>
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     117a <realloc+0x4a>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    117d <realloc+0x4d>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <malloc@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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