#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*/ getchar();
int getpagesize();
scalar_t__ posix_memalign(void **, int, int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  void *buf;
  int pagesz = getpagesize();
  int num = 100000;
  if (posix_memalign(&buf, pagesz, pagesz * num)) {
    printf("cannot allocate\n");
  }
  for (int i = 0; i < num / 2; i++) {
    ((char *)buf)[i * pagesz] = 0;
  }
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x186a0,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
imul   -0x20(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1060 <posix_memalign@plt>
cmp    $0x0,%rax
je     1190 <main+0x50>
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    11d5 <main+0x95>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1197 <main+0x57>
mov    $0x0,%al
call   1040 <getchar@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%ebx
imul   $0x186a0,%eax,%edx
lea    0x8(%rsp),%rdi
mov    %eax,%esi
call   1060 <posix_memalign@plt>
test   %rax,%rax
je     1171 <main+0x31>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
movslq %ebx,%rax
lea    0x0(,%rax,4),%r8
lea    (%rax,%rax,4),%r9
lea    (%rax,%rax,2),%rsi
lea    (%rax,%rax,1),%rdi
mov    $0xc350,%ebx
xor    %ecx,%ecx
nop
mov    0x8(%rsp),%rdx
movb   $0x0,(%rdx,%rcx,1)
mov    0x8(%rsp),%rdx
add    %rax,%rdx
movb   $0x0,(%rcx,%rdx,1)
mov    0x8(%rsp),%rdx
add    %rdi,%rdx
movb   $0x0,(%rcx,%rdx,1)
mov    0x8(%rsp),%rdx
add    %rsi,%rdx
movb   $0x0,(%rcx,%rdx,1)
mov    0x8(%rsp),%rdx
add    %r8,%rdx
movb   $0x0,(%rcx,%rdx,1)
add    %r9,%rcx
add    $0xfffffffffffffffb,%rbx
jne    1190 <main+0x50>
xor    %eax,%eax
call   1040 <getchar@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"cannot allocate\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @getpagesize()
store i32 %10, i32* %7
store i32 100000, i32* %8
%11 = load i32, i32* %7
%12 = load i32, i32* %7
%13 = load i32, i32* %8
%14 = mul nsw i32 %12, %13
%15 = call i64 @posix_memalign(i8** %6, i32 %11, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
store i32 0, i32* %9
br label %20
20: ; preds = %32, %19
%21 = load i32, i32* %9
%22 = load i32, i32* %8
%23 = sdiv i32 %22, 2
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %35
25: ; preds = %20
%26 = load i8*, i8** %6
%27 = load i32, i32* %9
%28 = load i32, i32* %7
%29 = mul nsw i32 %27, %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %25
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
br label %20
35: ; preds = %20
%36 = call i32 (...) @getchar()
ret i32 0
}

LLVM IR (-O2):
@str = [16 x i8] c"cannot allocate\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32 (...) @getpagesize()
%6 = mul nsw i32 %5, 100000
%7 = call i64 @posix_memalign(i8** %3, i32 %5, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = sext i32 %5 to i64
br label %15
13: ; preds = %15
%14 = call i32 (...) @getchar()
ret i32 0
15: ; preds = %15, %11
%16 = phi i64 [ 0, %11 ], [ %36, %15 ]
%17 = load i8*, i8** %3
%18 = mul nsw i64 %16, %12
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 0, i8* %19
%20 = add nuw nsw i64 %16, 1
%21 = load i8*, i8** %3
%22 = mul nsw i64 %20, %12
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
%24 = add nuw nsw i64 %16, 2
%25 = load i8*, i8** %3
%26 = mul nsw i64 %24, %12
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
%28 = add nuw nsw i64 %16, 3
%29 = load i8*, i8** %3
%30 = mul nsw i64 %28, %12
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 0, i8* %31
%32 = add nuw nsw i64 %16, 4
%33 = load i8*, i8** %3
%34 = mul nsw i64 %32, %12
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = add nuw nsw i64 %16, 5
%37 = icmp eq i64 %36, 50000
br i1 %37, label %13, label %15
}