#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*/ log_error_format(char *, char const *, char const *,
                                    unsigned long);
int /*<<< orphan*/ log_function_error_message(char *, char *);
void *malloc(size_t);
extern void *safe_malloc(size_t size, const char *modulename,
                         const char *funcname, unsigned long lineno) {
  void *ptr = malloc(size);
  if (!ptr) {
    log_function_error_message("stdlib_util.safe_malloc", "out of memory!");
    if (modulename != NULL && funcname != NULL && lineno > 0) {
      log_error_format("[safe_malloc stacktrace] %s:%s:%u\n", modulename,
                       funcname, lineno);
    }
    exit(1);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<safe_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <safe_malloc+0x86>
lea    0xe89(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1030 <log_function_error_message@plt>
cmpq   $0x0,-0x10(%rbp)
je     11bc <safe_malloc+0x7c>
cmpq   $0x0,-0x18(%rbp)
je     11bc <safe_malloc+0x7c>
cmpq   $0x0,-0x20(%rbp)
jbe    11bc <safe_malloc+0x7c>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe70(%rip),%rdi
call   1040 <log_error_format@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_malloc>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
call   1050 <malloc@plt>
test   %rax,%rax
je     115e <safe_malloc+0x1e>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe9b(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1030 <log_function_error_message@plt>
test   %rbx,%rbx
je     1195 <safe_malloc+0x55>
test   %r15,%r15
je     1195 <safe_malloc+0x55>
test   %r14,%r14
je     1195 <safe_malloc+0x55>
lea    0xea0(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <log_error_format@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"stdlib_util.safe_malloc\00"
@.str.1 = [15 x i8] c"out of memory!\00"
@.str.2 = [35 x i8] c"[safe_malloc stacktrace] %s:%s:%u\0A\00"
define i8* @safe_malloc(i64 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i64 %0, i64* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %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 %31, label %14
14: ; preds = %4
%15 = call i32 @log_function_error_message(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = load i64, i64* %8
%23 = icmp ugt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i8*, i8** %6
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = call i32 @log_error_format(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %26, i64 %27)
br label %29
29: ; preds = %24, %21, %18, %14
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %4
%32 = load i8*, i8** %9
ret i8* %32
}

LLVM IR (-O2):
@.str = [24 x i8] c"stdlib_util.safe_malloc\00"
@.str.1 = [15 x i8] c"out of memory!\00"
@.str.2 = [35 x i8] c"[safe_malloc stacktrace] %s:%s:%u\0A\00"
define noalias i8* @safe_malloc(i64 %0, i8* %1, i8* %2, i64 %3) {
%5 = tail call i8* @malloc(i64 %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %18
7: ; preds = %4
%8 = tail call i32 @log_function_error_message(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp ne i8* %1, null
%10 = icmp ne i8* %2, null
%11 = and i1 %9, %10
%12 = icmp ne i64 %3, 0
%13 = and i1 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @log_error_format(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2, i64 %3)
br label %16
16: ; preds = %14, %7
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %4
ret i8* %5
}