#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int /*<<< orphan*/ O_WRONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ memset(void *, int, size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
size_t sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
scalar_t__ write(int, void *, size_t);
int main(int argc, char *argv[]) {
  void *buf;
  size_t pagesize;
  ssize_t r;
  int fd, i, ret, pagecount;
  if (argc != 2) {
    usage(argv[0]);
    return 2;
  }
  errno = 0;
  pagecount = strtol(argv[1], NULL, 0);
  if (errno != 0) {
    usage(argv[0]);
    return 2;
  }
  ret = 1;
  pagesize = sysconf(_SC_PAGESIZE);
  buf = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (buf == MAP_FAILED) {
    perror("mmap");
    goto err_return;
  }
  memset(buf, 0xa5, pagesize);
  fd = open("/dev/null", O_WRONLY);
  if (fd < 0) {
    perror("open");
    goto err_unmap;
  }
  for (i = 0; i < pagecount; i++) {
    r = write(fd, buf, pagesize);
    if (r < 0) {
      perror("write");
      goto err_close;
    }
  }
  ret = 0;
err_close:
  close(fd);
err_unmap:
  munmap(buf, pagesize);
err_return:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    135e <main+0x1be>
mov    0x2de9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,-0x38(%rbp)
mov    0x2dc5(%rip),%rax
cmpq   $0x0,(%rax)
je     1225 <main+0x85>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    135e <main+0x1be>
movl   $0x1,-0x34(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    0x2d6e(%rip),%rax
or     (%rax),%edx
mov    0x2d5d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d74(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
jne    12a1 <main+0x101>
lea    0xd69(%rip),%rdi
call   10b0 <perror@plt>
jmp    1358 <main+0x1b8>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0xa5,%esi
call   1050 <memset@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
lea    0xd42(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12e6 <main+0x146>
lea    0xd33(%rip),%rdi
call   10b0 <perror@plt>
jmp    134b <main+0x1ab>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    133c <main+0x19c>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1329 <main+0x189>
lea    0xcf5(%rip),%rdi
call   10b0 <perror@plt>
jmp    1343 <main+0x1a3>
jmp    132e <main+0x18e>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12ed <main+0x14d>
movl   $0x0,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <munmap@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11d1 <main+0x31>
mov    0x2e11(%rip),%r14
movq   $0x0,(%r14)
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
cmpq   $0x0,(%r14)
je     11eb <main+0x4b>
mov    (%rbx),%rdi
call   1090 <usage@plt>
mov    $0x2,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %rax,%r14
mov    0x2df3(%rip),%rax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da1(%rip),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
mov    0x2d71(%rip),%rax
cmp    (%rax),%r12
je     12ae <main+0x10e>
mov    %r12,%rdi
mov    $0xa5,%esi
mov    %r14,%rdx
call   1050 <memset@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
lea    0xda1(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12c5 <main+0x125>
mov    %eax,%ebx
xor    %r15d,%r15d
test   %ebp,%ebp
jle    1297 <main+0xf7>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     12d9 <main+0x139>
add    $0xffffffff,%ebp
jne    1280 <main+0xe0>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1080 <munmap@plt>
jmp    11df <main+0x3f>
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r15d
jmp    11df <main+0x3f>
lea    0xd43(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r15d
jmp    129e <main+0xfe>
lea    0xd34(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r15d
jmp    1297 <main+0xf7>

LLVM IR (-O0):
@errno = global i64 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @usage(i8* %18)
store i32 2, i32* %3
br label %86
20: ; preds = %2
store i64 0, i64* @errno
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @strtol(i8* %23, i32* null, i32 0)
store i32 %24, i32* %12
%25 = load i64, i64* @errno
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = call i32 @usage(i8* %30)
store i32 2, i32* %3
br label %86
32: ; preds = %20
store i32 1, i32* %11
%33 = load i32, i32* @_SC_PAGESIZE
%34 = call i64 @sysconf(i32 %33)
store i64 %34, i64* %7
%35 = load i64, i64* %7
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_PRIVATE
%40 = load i32, i32* @MAP_ANONYMOUS
%41 = or i32 %39, %40
%42 = call i8* @mmap(i32* null, i64 %35, i32 %38, i32 %41, i32 -1, i32 0)
store i8* %42, i8** %6
%43 = load i8*, i8** %6
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %48
46: ; preds = %32
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %84
48: ; preds = %32
%49 = load i8*, i8** %6
%50 = load i64, i64* %7
%51 = call i32 @memset(i8* %49, i32 165, i64 %50)
%52 = load i32, i32* @O_WRONLY
%53 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %52)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %80
58: ; preds = %48
store i32 0, i32* %10
br label %59
59: ; preds = %73, %58
%60 = load i32, i32* %10
%61 = load i32, i32* %12
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %76
63: ; preds = %59
%64 = load i32, i32* %9
%65 = load i8*, i8** %6
%66 = load i64, i64* %7
%67 = call i64 @write(i32 %64, i8* %65, i64 %66)
store i64 %67, i64* %8
%68 = load i64, i64* %8
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %63
%71 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %77
72: ; preds = %63
br label %73
73: ; preds = %72
%74 = load i32, i32* %10
%75 = add nsw i32 %74, 1
store i32 %75, i32* %10
br label %59
76: ; preds = %59
store i32 0, i32* %11
br label %77
77: ; preds = %76, %70
%78 = load i32, i32* %9
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %77, %56
%81 = load i8*, i8** %6
%82 = load i64, i64* %7
%83 = call i32 @munmap(i8* %81, i64 %82)
br label %84
84: ; preds = %80, %46
%85 = load i32, i32* %11
store i32 %85, i32* %3
br label %86
86: ; preds = %84, %27, %15
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@errno = global i64 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %54
7: ; preds = %2
store i64 0, i64* @errno
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strtol(i8* nocapture %9, i32* null, i32 0)
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = load i8*, i8** %1
%15 = tail call i32 @usage(i8* %14)
br label %54
16: ; preds = %7
%17 = load i32, i32* @_SC_PAGESIZE
%18 = tail call i64 @sysconf(i32 %17)
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_PRIVATE
%23 = load i32, i32* @MAP_ANONYMOUS
%24 = or i32 %23, %22
%25 = tail call i8* @mmap(i32* null, i64 %18, i32 %21, i32 %24, i32 -1, i32 0)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %54
30: ; preds = %16
%31 = tail call i32 @memset(i8* %25, i32 165, i64 %18)
%32 = load i32, i32* @O_WRONLY
%33 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = icmp sgt i32 %10, 0
br i1 %36, label %41, label %48
37: ; preds = %30
%38 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %51
39: ; preds = %41
%40 = icmp eq i32 %45, %10
br i1 %40, label %48, label %41
41: ; preds = %35, %39
%42 = phi i32 [ %45, %39 ], [ 0, %35 ]
%43 = tail call i64 @write(i32 %33, i8* %25, i64 %18)
%44 = icmp slt i64 %43, 0
%45 = add nuw nsw i32 %42, 1
br i1 %44, label %46, label %39
46: ; preds = %41
%47 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %39, %35, %46
%49 = phi i32 [ 1, %46 ], [ 0, %35 ], [ 0, %39 ]
%50 = tail call i32 @close(i32 %33)
br label %51
51: ; preds = %48, %37
%52 = phi i32 [ 1, %37 ], [ %49, %48 ]
%53 = tail call i32 @munmap(i8* %25, i64 %18)
br label %54
54: ; preds = %28, %51, %13, %4
%55 = phi i32 [ 2, %4 ], [ 2, %13 ], [ 1, %28 ], [ %52, %51 ]
ret i32 %55
}