#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 EINVAL;
int ENOMEM;
int errno;
void *malloc(size_t const);
void *spx_malloc(const size_t s, int *err) {
  if (0 == s) {
    *err = EINVAL;
    return NULL;
  }
  void *p = malloc(s);
  if (NULL == p) {
    *err = 0 == errno ? ENOMEM : errno;
  }
  return p;
}


x86 assembly (clang, -O0):
<spx_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1148 <spx_malloc+0x38>
mov    0x2e9d(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11a0 <spx_malloc+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1198 <spx_malloc+0x88>
xor    %eax,%eax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%eax
jne    1183 <spx_malloc+0x73>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    118f <spx_malloc+0x7f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spx_malloc>:
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     1125 <spx_malloc+0x15>
call   1030 <malloc@plt>
test   %rax,%rax
je     112e <spx_malloc+0x1e>
pop    %rbx
ret
mov    0x2ea4(%rip),%rax
jmp    1142 <spx_malloc+0x32>
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
test   %eax,%eax
jne    1144 <spx_malloc+0x34>
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define noalias i8* @spx_malloc(i64 %0, i32* nocapture writeonly %1) {
%3 = icmp eq i64 %0, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @EINVAL
br label %14
6: ; preds = %2
%7 = tail call i8* @malloc(i64 %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %16
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = icmp eq i32 %10, 0
%12 = load i32, i32* @ENOMEM
%13 = select i1 %11, i32 %12, i32 %10
br label %14
14: ; preds = %4, %9
%15 = phi i32 [ %13, %9 ], [ %5, %4 ]
store i32 %15, i32* %1
br label %16
16: ; preds = %14, %6
%17 = phi i8* [ %7, %6 ], [ null, %14 ]
ret i8* %17
}