#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*/ assert(size_t);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ svz_log(int /*<<< orphan*/, char *);
void *svz_malloc(size_t);
scalar_t__ svz_realloc_func(void *, size_t);
void *svz_realloc(void *ptr, size_t size) {
  assert(size);
  if (ptr) {
    if ((ptr = (void *)svz_realloc_func(ptr, size + 2 * sizeof(size_t))) !=
        NULL) {
      return ptr;
    } else {
      svz_log(0, "realloc: virtual memory exhausted\n");
      exit(1);
    }
  } else {
    ptr = svz_malloc(size);
    return ptr;
  }
}


x86 assembly (clang, -O0):
<svz_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
je     11b8 <svz_realloc+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x10,%rsi
call   1040 <svz_realloc_func@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11a0 <svz_realloc+0x50>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cd <svz_realloc+0x7d>
xor    %edi,%edi
lea    0xe57(%rip),%rsi
call   1070 <svz_log@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <svz_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svz_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <assert@plt>
test   %r14,%r14
je     1183 <svz_realloc+0x33>
add    $0x10,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <svz_realloc_func@plt>
test   %rax,%rax
je     1192 <svz_realloc+0x42>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <svz_malloc@plt>
lea    0xe67(%rip),%rsi
xor    %edi,%edi
call   1070 <svz_log@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"realloc: virtual memory exhausted\0A\00"
define i8* @svz_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%6 = load i64, i64* %5
%7 = call i32 @assert(i64 %6)
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = add i64 %12, 16
%14 = call i64 @svz_realloc_func(i8* %11, i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i8*, i8** %4
store i8* %18, i8** %3
br label %26
19: ; preds = %10
%20 = call i32 @svz_log(i32 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i64, i64* %5
%24 = call i8* @svz_malloc(i64 %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
store i8* %25, i8** %3
br label %26
26: ; preds = %22, %17
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@.str = [35 x i8] c"realloc: virtual memory exhausted\0A\00"
define i8* @svz_realloc(i8* %0, i64 %1) {
%3 = tail call i32 @assert(i64 %1)
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = add i64 %1, 16
%7 = tail call i64 @svz_realloc_func(i8* %0, i64 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = inttoptr i64 %7 to i8*
br label %16
11: ; preds = %5
%12 = tail call i32 @svz_log(i32 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i8* @svz_malloc(i64 %1)
br label %16
16: ; preds = %14, %9
%17 = phi i8* [ %10, %9 ], [ %15, %14 ]
ret i8* %17
}