#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 ENOMEM;
int errno;
void *realloc(void *, size_t const);
void *spx_realloc(void *p, const size_t size, int *err) {
  void *ptr = realloc(p, size);
  if (NULL == ptr) {
    *err = 0 == errno ? ENOMEM : errno;
    return NULL;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<spx_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1185 <spx_realloc+0x75>
xor    %eax,%eax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%eax
jne    1163 <spx_realloc+0x53>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    116f <spx_realloc+0x5f>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    118d <spx_realloc+0x7d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spx_realloc>:
push   %rbx
mov    %rdx,%rbx
call   1030 <realloc@plt>
test   %rax,%rax
je     1120 <spx_realloc+0x10>
pop    %rbx
ret
mov    0x2eb1(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,%ecx
jne    1136 <spx_realloc+0x26>
mov    0x2eac(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i32 0
@ENOMEM = global i32 0
define i8* @spx_realloc(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = call i8* @realloc(i8* %9, i64 %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp eq i8* null, %12
br i1 %13, label %14, label %24
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = icmp eq i32 0, %15
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32, i32* @ENOMEM
br label %21
19: ; preds = %14
%20 = load i32, i32* @errno
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
%23 = load i32*, i32** %7
store i32 %22, i32* %23
store i8* null, i8** %4
br label %26
24: ; preds = %3
%25 = load i8*, i8** %8
store i8* %25, i8** %4
br label %26
26: ; preds = %24, %21
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
@errno = global i32 0
@ENOMEM = global i32 0
define noalias i8* @spx_realloc(i8* nocapture %0, i64 %1, i32* nocapture writeonly %2) {
%4 = tail call i8* @realloc(i8* %0, i64 %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = icmp eq i32 %7, 0
%9 = load i32, i32* @ENOMEM
%10 = select i1 %8, i32 %9, i32 %7
store i32 %10, i32* %2
br label %11
11: ; preds = %3, %6
ret i8* %4
}