#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 */
typedef int /*<<< orphan*/ u_long;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
void *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
void *realloc(void *, size_t);
void *grealloc(void *ptr, size_t size) {
  void *new_ptr;
  if (size == 0) {
    printf("grealloc: zero size\n");
    exit(-1);
  }
  if (ptr == NULL) {
    new_ptr = malloc(size);
  } else {
    new_ptr = realloc(ptr, size);
  }
  if (new_ptr == NULL) {
    printf("grealloc: out of memory (new size %lu bytes)\n", (u_long)size);
    exit(-1);
  }
  return new_ptr;
}


x86 assembly (clang, -O0):
<grealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <grealloc+0x33>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1190 <grealloc+0x50>
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11a1 <grealloc+0x61>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ca <grealloc+0x8a>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grealloc>:
push   %rbx
test   %rsi,%rsi
je     1194 <grealloc+0x44>
mov    %rsi,%rbx
test   %rdi,%rdi
je     116d <grealloc+0x1d>
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     117a <grealloc+0x2a>
pop    %rbx
ret
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    116b <grealloc+0x1b>
lea    0xe7f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"grealloc: zero size\0A\00"
@.str.1 = [46 x i8] c"grealloc: out of memory (new size %lu bytes)\0A\00"
define i8* @grealloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i64, i64* %4
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %5
br label %21
17: ; preds = %11
%18 = load i8*, i8** %3
%19 = load i64, i64* %4
%20 = call i8* @realloc(i8* %18, i64 %19)
store i8* %20, i8** %5
br label %21
21: ; preds = %17, %14
%22 = load i8*, i8** %5
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i64, i64* %4
%26 = trunc i64 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %21
%30 = load i8*, i8** %5
ret i8* %30
}

LLVM IR (-O2):
@.str.1 = [46 x i8] c"grealloc: out of memory (new size %lu bytes)\0A\00"
@str = [20 x i8] c"grealloc: zero size\00"
define i8* @grealloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i8* @malloc(i64 %1)
br label %13
11: ; preds = %7
%12 = tail call i8* @realloc(i8* %0, i64 %1)
br label %13
13: ; preds = %11, %9
%14 = phi i8* [ %10, %9 ], [ %12, %11 ]
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = trunc i64 %1 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
ret i8* %14
}