#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ posix_memalign(unsigned int **, int, int);
int /*<<< orphan*/ stderr;
unsigned int *allocate_memory(int num_pages) {
  unsigned int *mem;
  int i;
  if (posix_memalign(&mem, 4096, 4096 * num_pages)) {
    fprintf(stderr, "memory allocation failed\n");
    exit(2);
  }
  for (i = 0; i < 1024 * num_pages; i++) {
    mem[i] = 0;
  }
  return mem;
}


x86 assembly (clang, -O0):
<allocate_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edx
shl    $0xc,%edx
lea    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <posix_memalign@plt>
cmp    $0x0,%rax
je     1178 <allocate_memory+0x48>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x2,%edi
call   1040 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x4(%rbp),%ecx
shl    $0xa,%ecx
cmp    %ecx,%eax
jge    11ad <allocate_memory+0x7d>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117f <allocate_memory+0x4f>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_memory>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %edi,%edx
shl    $0xc,%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1060 <posix_memalign@plt>
test   %rax,%rax
jne    1192 <allocate_memory+0x52>
test   %ebx,%ebx
jle    1183 <allocate_memory+0x43>
shl    $0xa,%ebx
mov    (%rsp),%r14
cmp    $0x2,%ebx
mov    $0x1,%edx
cmovge %ebx,%edx
shl    $0x2,%rdx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
jmp    1187 <allocate_memory+0x47>
mov    (%rsp),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"memory allocation failed\0A\00"
define i32* @allocate_memory(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = mul nsw i32 4096, %5
%7 = call i64 @posix_memalign(i32** %3, i32 4096, i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 2)
unreachable
13: ; preds = %1
store i32 0, i32* %4
br label %14
14: ; preds = %24, %13
%15 = load i32, i32* %4
%16 = load i32, i32* %2
%17 = mul nsw i32 1024, %16
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load i32*, i32** %3
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 0, i32* %23
br label %24
24: ; preds = %19
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %14
27: ; preds = %14
%28 = load i32*, i32** %3
ret i32* %28
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"memory allocation failed\0A\00"
define i32* @allocate_memory(i32 %0) {
%2 = alloca i32*
%3 = bitcast i32** %2 to i8*
%4 = shl nsw i32 %0, 12
%5 = call i64 @posix_memalign(i32** %2, i32 4096, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %1
%8 = icmp sgt i32 %0, 0
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = load i32*, i32** %2
br label %22
11: ; preds = %7
%12 = shl i32 %0, 10
%13 = load i32*, i32** %2
%14 = bitcast i32* %13 to i8*
%15 = call i32 @llvm.smax.i32(i32 %12, i32 1)
%16 = zext i32 %15 to i64
%17 = shl nuw nsw i64 %16, 2
br label %22
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %9, %11
%23 = phi i32* [ %10, %9 ], [ %13, %11 ]
ret i32* %23
}