#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
void *malloc(long);
int /*<<< orphan*/ printf(char *, ...);
void *memory_alloc(long nbytes, const char *file, int line) {
  void *ptr;
  do {
    if ((nbytes) <= 0) {
      printf("Error: try to allocate negative-sized memory block!\n");
      printf("  raised in %s at %s:%d\n", "memory_alloc", file, line);
      exit(EXIT_FAILURE);
    }
  } while (0);
  ptr = malloc(nbytes);
  do {
    if ((ptr) == NULL) {
      printf("Error: failed to allocate memory!\n");
      printf("  raised in %s at %s:%d\n", "memory_alloc", file, line);
      exit(EXIT_FAILURE);
    }
  } while (0);
  return ptr;
}


x86 assembly (clang, -O0):
<memory_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jg     1186 <memory_alloc+0x56>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xecb(%rip),%rdi
lea    0xedd(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    118b <memory_alloc+0x5b>
mov    -0x8(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <memory_alloc+0xab>
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe76(%rip),%rdi
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11e0 <memory_alloc+0xb0>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memory_alloc>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
test   %rdi,%rdi
jle    115e <memory_alloc+0x1e>
call   1050 <malloc@plt>
test   %rax,%rax
je     1167 <memory_alloc+0x27>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xee3(%rip),%rdi
jmp    116e <memory_alloc+0x2e>
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
lea    0xe86(%rip),%rdi
lea    0xe98(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"Error: try to allocate negative-sized memory block!\0A\00"
@.str.1 = [25 x i8] c" raised in %s at %s:%d\0A\00"
@.str.2 = [13 x i8] c"memory_alloc\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [35 x i8] c"Error: failed to allocate memory!\0A\00"
define i8* @memory_alloc(i64 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %3
%9 = load i64, i64* %4
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %8
br label %19
19: ; preds = %18
%20 = load i64, i64* %4
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %7
br label %22
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
br label %33
33: ; preds = %32
%34 = load i8*, i8** %7
ret i8* %34
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c" raised in %s at %s:%d\0A\00"
@.str.2 = [13 x i8] c"memory_alloc\00"
@EXIT_FAILURE = global i32 0
@str = [34 x i8] c"Error: failed to allocate memory!\00"
@str.4 = [52 x i8] c"Error: try to allocate negative-sized memory block!\00"
define noalias i8* @memory_alloc(i64 %0, i8* %1, i32 %2) {
%4 = icmp slt i64 %0, 1
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.4, i64 0, i64 0))
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %2)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %3
%11 = tail call i8* @malloc(i64 %0)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %2)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
ret i8* %11
}