#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;
void *calloc(size_t, size_t);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ stderr;
void *alloc_memory(size_t nmemb, size_t size, void *to_free[], int n) {
  void *ptr = calloc(nmemb, size);
  if (ptr == NULL) {
    fprintf(stderr, "Unable to allocate memory.\n");
    for (int i = 0; i < n; i++)
      free(to_free[i]);
    exit(EXIT_FAILURE);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<alloc_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <alloc_memory+0x88>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ba <alloc_memory+0x7a>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    118f <alloc_memory+0x4f>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_memory>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%rbx
call   1040 <calloc@plt>
test   %rax,%rax
je     1158 <alloc_memory+0x18>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
call   1050 <fprintf@plt>
test   %ebp,%ebp
jle    1192 <alloc_memory+0x52>
mov    %ebp,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx,%rbp,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1180 <alloc_memory+0x40>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Unable to allocate memory.\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @alloc_memory(i64 %0, i64 %1, i8** %2, i32 %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i64 %0, i64* %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
store i32 %3, i32* %8
%11 = load i64, i64* %5
%12 = load i64, i64* %6
%13 = call i8* @calloc(i64 %11, i64 %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %36
16: ; preds = %4
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %10
br label %19
19: ; preds = %30, %16
%20 = load i32, i32* %10
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = load i8**, i8*** %7
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %23
%31 = load i32, i32* %10
%32 = add nsw i32 %31, 1
store i32 %32, i32* %10
br label %19
33: ; preds = %19
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %4
%37 = load i8*, i8** %9
ret i8* %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Unable to allocate memory.\0A\00"
@EXIT_FAILURE = global i32 0
define noalias i8* @alloc_memory(i64 %0, i64 %1, i8** nocapture %2, i32 %3) {
%5 = tail call i8* @calloc(i64 %0, i64 %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%10 = icmp sgt i32 %3, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = zext i32 %3 to i64
br label %16
13: ; preds = %16, %7
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %11, %16
%17 = phi i64 [ 0, %11 ], [ %21, %16 ]
%18 = getelementptr inbounds i8*, i8** %2, i64 %17
%19 = load i8*, i8** %18
%20 = tail call i32 @free(i8* %19)
%21 = add nuw nsw i64 %17, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %13, label %16
23: ; preds = %4
ret i8* %5
}