#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, unsigned int,
                           char const *, size_t);
void *malloc(size_t);
int /*<<< orphan*/ stderr;
void *checked_malloc(size_t size, char *filename, unsigned int line,
                     const char *func) {
  void *p = malloc(size);
  if (!p) {
    fprintf(stderr, "%s:%u: %s: OUT OF MEMORY malloc(%zu)\n", filename, line,
            func, size);
    exit(1);
  }
  return p;
}


x86 assembly (clang, -O0):
<checked_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118d <checked_malloc+0x5d>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x8(%rbp),%r9
lea    0xe82(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_malloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1040 <malloc@plt>
test   %rax,%rax
je     1158 <checked_malloc+0x28>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
mov    %rbp,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
mov    %rbx,%r9
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"%s:%u: %s: OUT OF MEMORY malloc(%zu)\0A\00"
define noalias i8* @checked_malloc(i64 %0, i8* %1, i32 %2, i8* %3) {
%5 = tail call i8* @malloc(i64 %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %2, i8* %3, i64 %0)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %4
ret i8* %5
}