#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*/ fatal_error(char *, size_t);
void *malloc(int);
void *xmalloc(size_t size) {
  void *p = malloc(size);
  if (p)
    return p;
  if (!size) {
    p = malloc(1);
    if (p)
      return p;
  }
  fatal_error("Out of memory: tried to allocate %zu bytes", size);
}


x86 assembly (clang, -O0):
<xmalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1153 <xmalloc+0x33>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1199 <xmalloc+0x79>
cmpq   $0x0,-0x10(%rbp)
jne    1189 <xmalloc+0x69>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1184 <xmalloc+0x64>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1199 <xmalloc+0x79>
jmp    1189 <xmalloc+0x69>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1030 <fatal_error@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmalloc>:
push   %rbx
mov    %rdi,%rbx
call   1040 <malloc@plt>
test   %rax,%rax
je     1130 <xmalloc+0x10>
pop    %rbx
ret
test   %rbx,%rbx
jne    1144 <xmalloc+0x24>
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
jne    112e <xmalloc+0xe>
lea    0xeb5(%rip),%rdi
mov    %rbx,%rsi
call   1030 <fatal_error@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Out of memory: tried to allocate %zu bytes\00"
define i8* @xmalloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = trunc i64 %5 to i32
%7 = call i8* @malloc(i32 %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %4
store i8* %11, i8** %2
br label %25
12: ; preds = %1
%13 = load i64, i64* %3
%14 = icmp ne i64 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = call i8* @malloc(i32 1)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i8*, i8** %4
store i8* %20, i8** %2
br label %25
21: ; preds = %15
br label %22
22: ; preds = %21, %12
%23 = load i64, i64* %3
%24 = call i32 @fatal_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i64 %23)
br label %25
25: ; preds = %22, %19, %10
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
@.str = [43 x i8] c"Out of memory: tried to allocate %zu bytes\00"
define noalias i8* @xmalloc(i64 %0) {
%2 = trunc i64 %0 to i32
%3 = tail call i8* @malloc(i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %12
5: ; preds = %1
%6 = icmp eq i64 %0, 0
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7, %5
%11 = tail call i32 @fatal_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i64 %0)
br label %12
12: ; preds = %7, %1, %10
%13 = phi i8* [ undef, %10 ], [ %3, %1 ], [ %8, %7 ]
ret i8* %13
}