#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  struct stat inode;
  void *mem;
  int fd, i;
  if ((fd = open("mmap.dat", O_RDONLY)) == -1) {
    perror("mmap.dat");
    return -1;
  }
  fstat(fd, &inode);
  if ((mem = mmap(0, inode.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    return -1;
  }
  for (i = 0; i < inode.st_size; i++) {
    int pr = 0;
    pr += ((char *)mem)[i];
  }
  while (1) {
    for (i = 0; i < inode.st_size; i++) {
      printf("%c\n", ((char *)mem)[i]);
    }
    printf("\n");
    sleep(10);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a8 <main+0x48>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <main+0x13b>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x8(%rbp),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e21(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <main+0xa4>
lea    0xe16(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <main+0x13b>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    123e <main+0xde>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120b <main+0xab>
jmp    1243 <main+0xe3>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    127e <main+0x11e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    124a <main+0xea>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
jmp    1243 <main+0xe3>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%r14
mov    %r14,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1231 <main+0xc1>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    (%rsp),%r14d
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
mov    0x2df3(%rip),%rax
cmp    (%rax),%r12
je     122a <main+0xba>
lea    0xe2d(%rip),%r15
jmp    1204 <main+0x94>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
test   %r14d,%r14d
jle    11f0 <main+0x80>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1210 <main+0xa0>
jmp    11f0 <main+0x80>
lea    0xdd8(%rip),%r14
mov    %r14,%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"mmap.dat\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [4 x i8] c"%c\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
12: ; preds = %0
%13 = load i32, i32* %4
%14 = call i32 @fstat(i32 %13, %struct.stat* %2)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %4
%20 = call i8* @mmap(i32 0, i32 %16, i32 %17, i32 %18, i32 %19, i32 0)
store i8* %20, i8** %3
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
25: ; preds = %12
store i32 0, i32* %5
br label %26
26: ; preds = %40, %25
%27 = load i32, i32* %5
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%29 = load i32, i32* %28
%30 = icmp slt i32 %27, %29
br i1 %30, label %31, label %43
31: ; preds = %26
store i32 0, i32* %6
%32 = load i8*, i8** %3
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = load i32, i32* %6
%39 = add nsw i32 %38, %37
store i32 %39, i32* %6
br label %40
40: ; preds = %31
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
br label %26
43: ; preds = %26
br label %44
44: ; preds = %43, %61
store i32 0, i32* %5
br label %45
45: ; preds = %58, %44
%46 = load i32, i32* %5
%47 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%48 = load i32, i32* %47
%49 = icmp slt i32 %46, %48
br i1 %49, label %50, label %61
50: ; preds = %45
%51 = load i8*, i8** %3
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %50
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %45
61: ; preds = %45
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @sleep(i32 10)
br label %44
64: ; preds = %23, %10
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"mmap.dat\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [4 x i8] c"%c\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %30, label %6
6: ; preds = %0
%7 = call i32 @fstat(i32 %4, %struct.stat* %1)
%8 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32 0, i32 %9, i32 %10, i32 %11, i32 %4, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %30, label %15
15: ; preds = %6
%16 = icmp sgt i32 %9, 0
%17 = zext i32 %9 to i64
br label %18
18: ; preds = %15, %27
br i1 %16, label %19, label %27
19: ; preds = %18, %19
%20 = phi i64 [ %25, %19 ], [ 0, %18 ]
%21 = getelementptr inbounds i8, i8* %12, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = add nuw nsw i64 %20, 1
%26 = icmp eq i64 %25, %17
br i1 %26, label %27, label %19
27: ; preds = %19, %18
%28 = tail call i32 @putchar(i32 10)
%29 = tail call i32 @sleep(i32 10)
br label %18
30: ; preds = %6, %0
%31 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %6 ]
%32 = tail call i32 @perror(i8* %31)
ret i32 -1
}