#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("/tmp/mmapped.bin", O_RDONLY);
  if (fd == -1) {
    perror("Error opening file for reading");
    exit(EXIT_FAILURE);
  }
  map = mmap(0, ((1000) * 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 <= (1000); ++i) {
    printf("%d: %d\n", i, map[i]);
  }
  if (munmap(map, ((1000) * 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    0xe62(%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    $0xfa0,%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    0xe21(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x3e8,-0x14(%rbp)
jg     125e <main+0xee>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdfd(%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    $0xfa0,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1281 <main+0x111>
lea    0xdd4(%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   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122f <main+0xbf>
mov    %eax,%r14d
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0xfa0,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dfa(%rip),%rax
cmp    (%rax),%rbx
je     1238 <main+0xc8>
mov    $0x1,%ebp
lea    0xe71(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%edx
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    $0x3e9,%rbp
jne    11e0 <main+0x70>
mov    %rbx,%rdi
mov    $0xfa0,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
je     1221 <main+0xb1>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
jmp    120c <main+0x9c>
lea    0xddb(%rip),%rdi
jmp    1247 <main+0xd7>
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/tmp/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 ([17 x i8], [17 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 4000, 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, 1000
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 4000)
%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 = [17 x i8] c"/tmp/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 ([17 x i8], [17 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 4000, i32 %11, i32 %12, i32 %4, i32 0)
%14 = load i32*, i32** @MAP_FAILED
%15 = icmp eq i32* %13, %14
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = tail call i32 @close(i32 %4)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %10, %21
%22 = phi i64 [ %27, %21 ], [ 1, %10 ]
%23 = getelementptr inbounds i32, i32* %13, i64 %22
%24 = load i32, i32* %23
%25 = trunc i64 %22 to i32
%26 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %24)
%27 = add nuw nsw i64 %22, 1
%28 = icmp eq i64 %27, 1001
br i1 %28, label %29, label %21
29: ; preds = %21
%30 = tail call i32 @munmap(i32* %13, i32 4000)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @close(i32 %4)
ret i32 0
}