#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;
int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int munmap(int *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int main(int argc, char *argv[]) {
  int i;
  int fd;
  int *map;
  fd = open("/home/george/Documents/C/mmapped.bin", O_RDONLY);
  if (fd == -1) {
    perror("Error opening file for reading");
    exit(EXIT_FAILURE);
  }
  map = mmap(0, ((10) * sizeof(int)), PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    close(fd);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
  }
  for (i = 1; i <= (10); ++i) {
    printf("%d: %d\n", i, map[i]);
  }
  if (munmap(map, ((10) * sizeof(int))) == -1) {
    perror("Error un-mmapping the file");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c2 <main+0x52>
lea    0xe76(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x28,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1222 <main+0xb2>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jg     125b <main+0xeb>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1229 <main+0xb9>
mov    -0x20(%rbp),%rdi
mov    $0x28,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0x10e>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12a2 <main+0x132>
mov    %eax,%r14d
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x28,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dfd(%rip),%rax
cmp    (%rax),%rbx
je     12ab <main+0x13b>
mov    0x4(%rbx),%edx
lea    0xe86(%rip),%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    0x8(%rbx),%edx
mov    %rbp,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    0xc(%rbx),%edx
mov    %rbp,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    0x10(%rbx),%edx
mov    %rbp,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    0x14(%rbx),%edx
mov    %rbp,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    0x18(%rbx),%edx
mov    %rbp,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    0x1c(%rbx),%edx
mov    %rbp,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    0x20(%rbx),%edx
mov    %rbp,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    0x24(%rbx),%edx
mov    %rbp,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    0x28(%rbx),%edx
mov    %rbp,%rdi
mov    $0xa,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x28,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
je     1294 <main+0x124>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1080 <perror@plt>
jmp    1285 <main+0x115>
lea    0xd7c(%rip),%rdi
jmp    12ba <main+0x14a>
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xd8a(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"/home/george/Documents/C/mmapped.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Error opening file for reading\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [24 x i8] c"Error mmapping the file\00"
@.str.3 = [8 x i8] c"%d: %d\0A\00"
@.str.4 = [27 x i8] c"Error un-mmapping the file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_SHARED
%20 = load i32, i32* %7
%21 = call i32* @mmap(i32 0, i32 40, i32 %18, i32 %19, i32 %20, i32 0)
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = load i32*, i32** @MAP_FAILED
%24 = icmp eq i32* %22, %23
br i1 %24, label %25, label %31
25: ; preds = %17
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
%28 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %17
store i32 1, i32* %6
br label %32
32: ; preds = %43, %31
%33 = load i32, i32* %6
%34 = icmp sle i32 %33, 10
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i32, i32* %6
%37 = load i32*, i32** %8
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %41)
br label %43
43: ; preds = %35
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %32
46: ; preds = %32
%47 = load i32*, i32** %8
%48 = call i32 @munmap(i32* %47, i32 40)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
ret i32 0
}

LLVM IR (-O2):
@.str = [37 x i8] c"/home/george/Documents/C/mmapped.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Error opening file for reading\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [24 x i8] c"Error mmapping the file\00"
@.str.3 = [8 x i8] c"%d: %d\0A\00"
@.str.4 = [27 x i8] c"Error un-mmapping the file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i32* @mmap(i32 0, i32 40, i32 %11, i32 %12, i32 %4, i32 0)
%14 = load i32*, i32** @MAP_FAILED
%15 = icmp eq i32* %13, %14
br i1 %15, label %49, label %16
16: ; preds = %10
%17 = getelementptr inbounds i32, i32* %13, i64 1
%18 = load i32, i32* %17
%19 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %18)
%20 = getelementptr inbounds i32, i32* %13, i64 2
%21 = load i32, i32* %20
%22 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 2, i32 %21)
%23 = getelementptr inbounds i32, i32* %13, i64 3
%24 = load i32, i32* %23
%25 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 3, i32 %24)
%26 = getelementptr inbounds i32, i32* %13, i64 4
%27 = load i32, i32* %26
%28 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 %27)
%29 = getelementptr inbounds i32, i32* %13, i64 5
%30 = load i32, i32* %29
%31 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 5, i32 %30)
%32 = getelementptr inbounds i32, i32* %13, i64 6
%33 = load i32, i32* %32
%34 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 6, i32 %33)
%35 = getelementptr inbounds i32, i32* %13, i64 7
%36 = load i32, i32* %35
%37 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 7, i32 %36)
%38 = getelementptr inbounds i32, i32* %13, i64 8
%39 = load i32, i32* %38
%40 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 8, i32 %39)
%41 = getelementptr inbounds i32, i32* %13, i64 9
%42 = load i32, i32* %41
%43 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 9, i32 %42)
%44 = getelementptr inbounds i32, i32* %13, i64 10
%45 = load i32, i32* %44
%46 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 10, i32 %45)
%47 = tail call i32 @munmap(i32* %13, i32 40)
%48 = icmp eq i32 %47, -1
br i1 %48, label %54, label %56
49: ; preds = %10
%50 = tail call i32 @close(i32 %4)
%51 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = tail call i32 @exit(i32 %52)
unreachable
54: ; preds = %16
%55 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %16
%57 = tail call i32 @close(i32 %4)
ret i32 0
}