#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 *);
int /*<<< orphan*/ lua_error(char *);
void *realloc(void *, size_t);
void *luaM_realloc(void *block, unsigned long size) {
  size_t s = (size_t)size;
  if (s != size)
    lua_error("memory allocation error: block too big");
  if (size == 0) {
    free(block);
    return NULL;
  }
  block = realloc(block, s);
  if (block == NULL)
    lua_error("not enough memory");
  return block;
}


x86 assembly (clang, -O0):
<luaM_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1162 <luaM_realloc+0x32>
lea    0xea3(%rip),%rdi
call   1050 <lua_error@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1183 <luaM_realloc+0x53>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <luaM_realloc+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ab <luaM_realloc+0x7b>
lea    0xe81(%rip),%rdi
call   1050 <lua_error@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<luaM_realloc>:
push   %rax
test   %rsi,%rsi
je     1142 <luaM_realloc+0x12>
call   1040 <realloc@plt>
test   %rax,%rax
je     1149 <luaM_realloc+0x19>
pop    %rcx
ret
call   1030 <free@plt>
jmp    1155 <luaM_realloc+0x25>
lea    0xeb0(%rip),%rdi
call   1050 <lua_error@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [39 x i8] c"memory allocation error: block too big\00"
@.str.1 = [18 x i8] c"not enough memory\00"
define i8* @luaM_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = load i64, i64* %5
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @lua_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i64, i64* %5
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = call i32 @free(i8* %17)
store i8* null, i8** %3
br label %29
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = load i64, i64* %6
%22 = call i8* @realloc(i8* %20, i64 %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @lua_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = load i8*, i8** %4
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %16
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"not enough memory\00"
define noalias i8* @luaM_realloc(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @free(i8* %0)
br label %11
6: ; preds = %2
%7 = tail call i8* @realloc(i8* %0, i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @lua_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %6, %9, %4
%12 = phi i8* [ null, %4 ], [ null, %9 ], [ %7, %6 ]
ret i8* %12
}