#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*/ errno;
int /*<<< orphan*/ kmr_error2(int /*<<< orphan*/, char *, char const *,
                              int const, char const *);
void *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, size_t, char *);
char *strerror(int /*<<< orphan*/);
void *kmr_safe_malloc(size_t s, char *szexpr, const char *file, const int line,
                      const char *func) {
  void *p = malloc(s);
  if (p == 0) {
    char ee[80];
    char *m = strerror(errno);
    snprintf(ee, 80, "malloc((%s)=%zd): %s", szexpr, s, m);
    kmr_error2(0, ee, file, line, func);
  }
  return p;
}


x86 assembly (clang, -O0):
<kmr_safe_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c5 <kmr_safe_malloc+0x85>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    -0x88(%rbp),%r9
mov    $0x50,%esi
lea    0xe56(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x80(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
xor    %edi,%edi
call   1040 <kmr_error2@plt>
mov    -0x30(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmr_safe_malloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8,%r14
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
call   1050 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a8 <kmr_safe_malloc+0x68>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe81(%rip),%rdx
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %r13,%rcx
mov    %rbx,%r8
mov    %rax,%r9
call   1030 <snprintf@plt>
xor    %edi,%edi
mov    %rsp,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1040 <kmr_error2@plt>
mov    %rbp,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [21 x i8] c"malloc((%s)=%zd): %s\00"
define i8* @kmr_safe_malloc(i64 %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [80 x i8]
%13 = alloca i8*
store i64 %0, i64* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%14 = load i64, i64* %6
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %11
%16 = load i8*, i8** %11
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %31
18: ; preds = %5
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
store i8* %20, i8** %13
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%22 = load i8*, i8** %7
%23 = load i64, i64* %6
%24 = load i8*, i8** %13
%25 = call i32 @snprintf(i8* %21, i32 80, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %22, i64 %23, i8* %24)
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%27 = load i8*, i8** %8
%28 = load i32, i32* %9
%29 = load i8*, i8** %10
%30 = call i32 @kmr_error2(i32 0, i8* %26, i8* %27, i32 %28, i8* %29)
br label %31
31: ; preds = %18, %5
%32 = load i8*, i8** %11
ret i8* %32
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [21 x i8] c"malloc((%s)=%zd): %s\00"
define noalias i8* @kmr_safe_malloc(i64 %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca [80 x i8]
%7 = tail call i8* @malloc(i64 %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = call i32 @snprintf(i8* %10, i32 80, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %1, i64 %0, i8* %12)
%14 = call i32 @kmr_error2(i32 0, i8* %10, i8* %2, i32 %3, i8* %4)
br label %15
15: ; preds = %9, %5
ret i8* %7
}