#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 unsigned long off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ MADV_DONTNEED;
int /*<<< orphan*/ MADV_SEQUENTIAL;
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
unsigned long lseek(int, long, int /*<<< orphan*/);
unsigned char madvise(unsigned char *, int, int /*<<< orphan*/);
unsigned char *mmap(int /*<<< orphan*/, unsigned long, int /*<<< orphan*/,
                    int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(unsigned char *, unsigned long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long, unsigned long, ...);
int main(int argc, char *argv[]) {
  int fd;
  unsigned char madvise_ret;
  unsigned long i, j;
  unsigned char *map;
  fd = open("pb.pb", O_RDONLY);
  if (fd == -1) {
    perror("Error opening file for reading");
    exit(EXIT_FAILURE);
  }
  off_t filesize = lseek(fd, 0L, SEEK_END);
  off_t curpos = lseek(fd, 0L, SEEK_SET);
  map = mmap(0, filesize, PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    close(fd);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
  }
  madvise_ret = madvise(map, filesize, MADV_SEQUENTIAL);
  if (madvise_ret != 0) {
    close(fd);
    perror("Error madvise the file");
    exit(EXIT_FAILURE);
  }
  for (i = 1; i <= filesize; ++i) {
    printf("%ld: %c\n", i, map[i]);
    j = i & (1024 * 4L) - 1;
    if (j == 0 && i >= (1024 * 4L)) {
      printf("BLAH i%d j%d bottom%d top%d\n", i, j, i - (1024 * 4L), i - 1);
      madvise(map + i - (1024 * 4L), (1024 * 4L) - 1, MADV_DONTNEED);
      if (madvise_ret != 0) {
        close(fd);
        perror("Error madvise the file");
        exit(EXIT_FAILURE);
      }
    }
  }
  if (munmap(map, filesize) == -1) {
    perror("Error un-mmapping the file");
  }
  close(fd);
  return 0;
}


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)
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e2 <main+0x52>
lea    0xe37(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d60(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <main+0xe3>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xdc5(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
call   1080 <madvise@plt>
mov    %al,-0x15(%rbp)
movzbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     12bd <main+0x12d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x38(%rbp),%rax
ja     13af <main+0x21f>
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,%edx
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
and    $0xfff,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1399 <main+0x209>
cmpq   $0x1000,-0x20(%rbp)
jb     1399 <main+0x209>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
sub    $0x1000,%rcx
mov    -0x20(%rbp),%r8
sub    $0x1,%r8
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0xfffffffffffff000,%rdi
mov    0x2c67(%rip),%rax
mov    (%rax),%edx
mov    $0xfff,%esi
call   1080 <madvise@plt>
movzbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     1394 <main+0x204>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xcbc(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c23(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1399 <main+0x209>
jmp    139e <main+0x20e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    12c5 <main+0x135>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    13d1 <main+0x241>
lea    0xcae(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     130e <main+0x17e>
mov    %eax,%ebx
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,%r12
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2d9a(%rip),%rax
cmp    (%rax),%rbp
je     1317 <main+0x187>
mov    %ebx,0x4(%rsp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r12d,%esi
call   1080 <madvise@plt>
test   %al,%al
jne    1327 <main+0x197>
test   %r12,%r12
je     12d6 <main+0x146>
mov    $0xfffffffffffff001,%rbx
lea    0xe06(%rip),%r15
mov    0x2d6b(%rip),%r13
jmp    1273 <main+0xe3>
nopw   0x0(%rax,%rax,1)
lea    0x1(%rbx),%rax
add    $0x1001,%rbx
cmp    %r12,%rbx
mov    %rax,%rbx
ja     12d6 <main+0x146>
lea    0x1000(%rbx),%r14
movzbl 0x1000(%rbp,%rbx,1),%edx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1000,%r14
jb     1260 <main+0xd0>
mov    %r14d,%eax
and    $0xfff,%eax
test   %rax,%rax
jne    1260 <main+0xd0>
lea    0xfff(%rbx),%r8
lea    0xdaa(%rip),%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
lea    (%rbx,%rbp,1),%rdi
mov    0x0(%r13),%edx
mov    $0xfff,%esi
call   1080 <madvise@plt>
jmp    1260 <main+0xd0>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x170>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e6 <main+0x156>
lea    0xcf1(%rip),%rdi
jmp    1337 <main+0x1a7>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd00(%rip),%rdi
jmp    1337 <main+0x1a7>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0xd06(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"pb.pb\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Error opening file for reading\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"Error mmapping the file\00"
@MADV_SEQUENTIAL = global i32 0
@.str.3 = [23 x i8] c"Error madvise the file\00"
@.str.4 = [9 x i8] c"%ld: %c\0A\00"
@.str.5 = [29 x i8] c"BLAH i%d j%d bottom%d top%d\0A\00"
@MADV_DONTNEED = global i32 0
@.str.6 = [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 i8
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i32, i32* %6
%23 = load i32, i32* @SEEK_END
%24 = call i64 @lseek(i32 %22, i64 0, i32 %23)
store i64 %24, i64* %11
%25 = load i32, i32* %6
%26 = load i32, i32* @SEEK_SET
%27 = call i64 @lseek(i32 %25, i64 0, i32 %26)
store i64 %27, i64* %12
%28 = load i64, i64* %11
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %6
%32 = call i8* @mmap(i32 0, i64 %28, i32 %29, i32 %30, i32 %31, i32 0)
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %42
36: ; preds = %21
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %21
%43 = load i8*, i8** %10
%44 = load i64, i64* %11
%45 = trunc i64 %44 to i32
%46 = load i32, i32* @MADV_SEQUENTIAL
%47 = call zeroext i8 @madvise(i8* %43, i32 %45, i32 %46)
store i8 %47, i8* %7
%48 = load i8, i8* %7
%49 = zext i8 %48 to i32
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %42
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %42
store i64 1, i64* %8
br label %58
58: ; preds = %102, %57
%59 = load i64, i64* %8
%60 = load i64, i64* %11
%61 = icmp ule i64 %59, %60
br i1 %61, label %62, label %105
62: ; preds = %58
%63 = load i64, i64* %8
%64 = load i8*, i8** %10
%65 = load i64, i64* %8
%66 = getelementptr inbounds i8, i8* %64, i64 %65
%67 = load i8, i8* %66
%68 = zext i8 %67 to i64
%69 = call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %63, i64 %68)
%70 = load i64, i64* %8
%71 = and i64 %70, 4095
store i64 %71, i64* %9
%72 = load i64, i64* %9
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %101
74: ; preds = %62
%75 = load i64, i64* %8
%76 = icmp uge i64 %75, 4096
br i1 %76, label %77, label %101
77: ; preds = %74
%78 = load i64, i64* %8
%79 = load i64, i64* %9
%80 = load i64, i64* %8
%81 = sub i64 %80, 4096
%82 = load i64, i64* %8
%83 = sub i64 %82, 1
%84 = call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i64 %78, i64 %79, i64 %81, i64 %83)
%85 = load i8*, i8** %10
%86 = load i64, i64* %8
%87 = getelementptr inbounds i8, i8* %85, i64 %86
%88 = getelementptr inbounds i8, i8* %87, i64 -4096
%89 = load i32, i32* @MADV_DONTNEED
%90 = call zeroext i8 @madvise(i8* %88, i32 4095, i32 %89)
%91 = load i8, i8* %7
%92 = zext i8 %91 to i32
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %100
94: ; preds = %77
%95 = load i32, i32* %6
%96 = call i32 @close(i32 %95)
%97 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%98 = load i32, i32* @EXIT_FAILURE
%99 = call i32 @exit(i32 %98)
unreachable
100: ; preds = %77
br label %101
101: ; preds = %100, %74, %62
br label %102
102: ; preds = %101
%103 = load i64, i64* %8
%104 = add i64 %103, 1
store i64 %104, i64* %8
br label %58
105: ; preds = %58
%106 = load i8*, i8** %10
%107 = load i64, i64* %11
%108 = call i32 @munmap(i8* %106, i64 %107)
%109 = icmp eq i32 %108, -1
br i1 %109, label %110, label %112
110: ; preds = %105
%111 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %112
112: ; preds = %110, %105
%113 = load i32, i32* %6
%114 = call i32 @close(i32 %113)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"pb.pb\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Error opening file for reading\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"Error mmapping the file\00"
@MADV_SEQUENTIAL = global i32 0
@.str.3 = [23 x i8] c"Error madvise the file\00"
@.str.4 = [9 x i8] c"%ld: %c\0A\00"
@.str.5 = [29 x i8] c"BLAH i%d j%d bottom%d top%d\0A\00"
@MADV_DONTNEED = global i32 0
@.str.6 = [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 ([6 x i8], [6 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* @SEEK_END
%12 = tail call i64 @lseek(i32 %4, i64 0, i32 %11)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @lseek(i32 %4, i64 0, i32 %13)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap(i32 0, i64 %12, i32 %15, i32 %16, i32 %4, i32 0)
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %25
20: ; preds = %10
%21 = tail call i32 @close(i32 %4)
%22 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %10
%26 = trunc i64 %12 to i32
%27 = load i32, i32* @MADV_SEQUENTIAL
%28 = tail call zeroext i8 @madvise(i8* %17, i32 %26, i32 %27)
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = icmp eq i64 %12, 0
br i1 %31, label %57, label %37
32: ; preds = %25
%33 = tail call i32 @close(i32 %4)
%34 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %30, %54
%38 = phi i64 [ %55, %54 ], [ 1, %30 ]
%39 = getelementptr inbounds i8, i8* %17, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i64
%42 = tail call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 %38, i64 %41)
%43 = and i64 %38, 4095
%44 = icmp eq i64 %43, 0
%45 = icmp ugt i64 %38, 4095
%46 = and i1 %45, %44
br i1 %46, label %47, label %54
47: ; preds = %37
%48 = add i64 %38, -4096
%49 = add i64 %38, -1
%50 = tail call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i64 %38, i64 0, i64 %48, i64 %49)
%51 = getelementptr inbounds i8, i8* %39, i64 -4096
%52 = load i32, i32* @MADV_DONTNEED
%53 = tail call zeroext i8 @madvise(i8* %51, i32 4095, i32 %52)
br label %54
54: ; preds = %47, %37
%55 = add i64 %38, 1
%56 = icmp ugt i64 %55, %12
br i1 %56, label %57, label %37
57: ; preds = %54, %30
%58 = tail call i32 @munmap(i8* %17, i64 %12)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %60, %57
%63 = tail call i32 @close(i32 %4)
ret i32 0
}