#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ STDOUT_FILENO;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpagesize();
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd, size;
  char *area, *fname = "/sys/kernel/debug/my_rc_file0";
  if (argc > 1)
    fname = argv[1];
  size = 16 * getpagesize();
  if (argc > 2)
    size = atoi(argv[2]);
  printf(" Memory Mapping %s, size %d bytes\n", fname, size);
  if ((fd = open(fname, O_RDONLY)) < 0) {
    perror("problems opening the file ");
    exit(errno);
  }
  area = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  if (area == MAP_FAILED) {
    perror("error mmaping");
    exit(errno);
  }
  write(STDOUT_FILENO, area, 64 * 20);
  close(fd);
  exit(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)
lea    0xe53(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c7 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
shl    $0x4,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11ee <main+0x5e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xe22(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1239 <main+0xa9>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    0x2d94(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <main+0x100>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    $0x500,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    11b8 <main+0x28>
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x8(%rsi),%r14
xor    %eax,%eax
call   10a0 <getpagesize@plt>
cmp    $0x2,%ebp
jne    11cd <main+0x3d>
mov    %eax,%ebx
shl    $0x4,%ebx
jmp    11d8 <main+0x48>
xor    %eax,%eax
call   10a0 <getpagesize@plt>
mov    %eax,%ebx
shl    $0x4,%ebx
lea    0xe35(%rip),%r14
jmp    11d8 <main+0x48>
mov    0x10(%r15),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
lea    0xe3f(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <printf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    1207 <main+0x77>
lea    0xe3c(%rip),%rdi
jmp    123d <main+0xad>
mov    %eax,%ebp
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    1250 <main+0xc0>
lea    0xe1f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d91(%rip),%rcx
mov    (%rcx),%edi
mov    %rax,%rsi
mov    $0x500,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"/sys/kernel/debug/my_rc_file0\00"
@.str.1 = [35 x i8] c" Memory Mapping %s, size %d bytes\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [27 x i8] c"problems opening the file \00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [14 x i8] c"error mmaping\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %9
br label %16
16: ; preds = %12, %2
%17 = call i32 (...) @getpagesize()
%18 = mul nsw i32 16, %17
store i32 %18, i32* %7
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
br label %26
26: ; preds = %21, %16
%27 = load i8*, i8** %9
%28 = load i32, i32* %7
%29 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %27, i32 %28)
%30 = load i8*, i8** %9
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @errno
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %26
%39 = load i32, i32* %7
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* %6
%43 = call i8* @mmap(i32* null, i32 %39, i32 %40, i32 %41, i32 %42, i32 0)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %51
47: ; preds = %38
%48 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* @errno
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %38
%52 = load i32, i32* @STDOUT_FILENO
%53 = load i8*, i8** %8
%54 = call i32 @write(i32 %52, i8* %53, i32 1280)
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [30 x i8] c"/sys/kernel/debug/my_rc_file0\00"
@.str.1 = [35 x i8] c" Memory Mapping %s, size %d bytes\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [27 x i8] c"problems opening the file \00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [14 x i8] c"error mmaping\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 (...) @getpagesize()
%6 = shl nsw i32 %5, 4
br label %17
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 (...) @getpagesize()
%11 = shl nsw i32 %10, 4
%12 = icmp eq i32 %0, 2
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
br label %17
17: ; preds = %4, %13, %7
%18 = phi i8* [ %9, %13 ], [ %9, %7 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), %4 ]
%19 = phi i32 [ %16, %13 ], [ %11, %7 ], [ %6, %4 ]
%20 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %19)
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 @open(i8* %18, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @errno
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i8* @mmap(i32* null, i32 %19, i32 %29, i32 %30, i32 %22, i32 0)
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @errno
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %28
%39 = load i32, i32* @STDOUT_FILENO
%40 = tail call i32 @write(i32 %39, i8* %31, i32 1280)
%41 = tail call i32 @close(i32 %22)
%42 = tail call i32 @exit(i32 0)
unreachable
}