#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*/ ENOMEM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, size_t);
void *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *mem_alloc_func(size_t size) {
  void *res;
  if (!size)
    return NULL;
  res = malloc(size);
  if (!res) {
    fprintf(stderr,
            "mem_alloc(): %s trying to allocate "
            "%zu"
            " bytes\n",
            strerror(ENOMEM), size);
    ;
    perror("mem_alloc");
  }
  return res;
}


x86 assembly (clang, -O0):
<mem_alloc_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <mem_alloc_func+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <mem_alloc_func+0x80>
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <mem_alloc_func+0x78>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
lea    0xe59(%rip),%rsi
call   1030 <fprintf@plt>
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_alloc_func>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     118c <mem_alloc_func+0x4c>
mov    %rdi,%rbx
call   1040 <malloc@plt>
test   %rax,%rax
jne    118e <mem_alloc_func+0x4e>
mov    0x2e7c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe8d(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1030 <fprintf@plt>
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"mem_alloc(): %s trying to allocate %zu bytes\0A\00"
@ENOMEM = global i32 0
@.str.1 = [10 x i8] c"mem_alloc\00"
define i8* @mem_alloc_func(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = icmp ne i64 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %1
store i8* null, i8** %2
br label %22
8: ; preds = %1
%9 = load i64, i64* %3
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i32, i32* @ENOMEM
%16 = call i8* @strerror(i32 %15)
%17 = load i64, i64* %3
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %16, i64 %17)
%19 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %13, %8
%21 = load i8*, i8** %4
store i8* %21, i8** %2
br label %22
22: ; preds = %20, %7
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"mem_alloc(): %s trying to allocate %zu bytes\0A\00"
@ENOMEM = global i32 0
@.str.1 = [10 x i8] c"mem_alloc\00"
define noalias i8* @mem_alloc_func(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %12, label %3
3: ; preds = %1
%4 = tail call i8* @malloc(i64 %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = load i32, i32* @ENOMEM
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %9, i64 %0)
%11 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %3, %6, %1
%13 = phi i8* [ null, %1 ], [ null, %6 ], [ %4, %3 ]
ret i8* %13
}