#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 {
  scalar_t__ st_size;
  int /*<<< orphan*/ member_0;
};
typedef size_t off_t;
typedef int /*<<< orphan*/ mode_t;
typedef int /*<<< orphan*/ intmax_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(char *, size_t);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char, ...);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  const char *filepath = "/tmp/mmapped.bin";
  int fd = open(filepath, O_RDONLY, (mode_t)0600);
  if (fd == -1) {
    perror("Error opening file for writing");
    exit(EXIT_FAILURE);
  }
  struct stat fileInfo = {0};
  if (fstat(fd, &fileInfo) == -1) {
    perror("Error getting the file size");
    exit(EXIT_FAILURE);
  }
  if (fileInfo.st_size == 0) {
    fprintf(stderr, "Error: File is empty, nothing to do\n");
    exit(EXIT_FAILURE);
  }
  printf("File size is %ji\n", (intmax_t)fileInfo.st_size);
  char *map = mmap(0, fileInfo.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    close(fd);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
  }
  for (off_t i = 0; i < fileInfo.st_size; i++) {
    printf("Found character %c at %ji\n", map[i], (intmax_t)i);
  }
  if (munmap(map, fileInfo.st_size) == -1) {
    close(fd);
    perror("Error un-mmapping the file");
    exit(EXIT_FAILURE);
  }
  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)
lea    0xe43(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ff <main+0x5f>
lea    0xe25(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    123e <main+0x9e>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    126c <main+0xcc>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rax
lea    0xdfa(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rsi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d03(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <main+0x140>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1329 <main+0x189>
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x40(%rbp),%rcx
mov    %ecx,%edx
lea    0xd8d(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    12e8 <main+0x148>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1361 <main+0x1c1>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x1c(%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   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xf3>
mov    %eax,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
je     128c <main+0xfc>
mov    (%rsp),%r12
test   %r12,%r12
je     1295 <main+0x105>
movsbl %r12b,%esi
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2d91(%rip),%rax
cmp    (%rax),%rbp
je     12ac <main+0x11c>
lea    0xe64(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl 0x0(%rbp,%rbx,1),%esi
mov    %r15,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1240 <main+0xb0>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <munmap@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%ebx
je     12bd <main+0x12d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd87(%rip),%rdi
jmp    12c4 <main+0x134>
lea    0xd9d(%rip),%rdi
jmp    12c4 <main+0x134>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c9 <main+0x139>
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xdc8(%rip),%rdi
jmp    12c4 <main+0x134>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [17 x i8] c"/tmp/mmapped.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Error opening file for writing\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"Error getting the file size\00"
@stderr = global i32 0
@.str.3 = [37 x i8] c"Error: File is empty, nothing to do\0A\00"
@.str.4 = [18 x i8] c"File size is %ji\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [24 x i8] c"Error mmapping the file\00"
@.str.6 = [27 x i8] c"Found character %c at %ji\0A\00"
@.str.7 = [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 i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %6
%11 = load i8*, i8** %6
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12, i32 384)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = bitcast %struct.stat* %8 to i8*
%22 = load i32, i32* %7
%23 = call i32 @fstat(i32 %22, %struct.stat* %8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %20
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i64, i64* %30
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i32
%42 = trunc i32 %41 to i8
%43 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8 signext %42)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%45 = load i64, i64* %44
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @MAP_SHARED
%48 = load i32, i32* %7
%49 = call i8* @mmap(i32 0, i64 %45, i32 %46, i32 %47, i32 %48, i32 0)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %50, %51
br i1 %52, label %53, label %59
53: ; preds = %38
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
%56 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %38
store i64 0, i64* %10
br label %60
60: ; preds = %73, %59
%61 = load i64, i64* %10
%62 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%63 = load i64, i64* %62
%64 = icmp ult i64 %61, %63
br i1 %64, label %65, label %76
65: ; preds = %60
%66 = load i8*, i8** %9
%67 = load i64, i64* %10
%68 = getelementptr inbounds i8, i8* %66, i64 %67
%69 = load i8, i8* %68
%70 = load i64, i64* %10
%71 = trunc i64 %70 to i32
%72 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8 signext %69, i32 %71)
br label %73
73: ; preds = %65
%74 = load i64, i64* %10
%75 = add i64 %74, 1
store i64 %75, i64* %10
br label %60
76: ; preds = %60
%77 = load i8*, i8** %9
%78 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%79 = load i64, i64* %78
%80 = call i32 @munmap(i8* %77, i64 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %88
82: ; preds = %76
%83 = load i32, i32* %7
%84 = call i32 @close(i32 %83)
%85 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%86 = load i32, i32* @EXIT_FAILURE
%87 = call i32 @exit(i32 %86)
unreachable
88: ; preds = %76
%89 = load i32, i32* %7
%90 = call i32 @close(i32 %89)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [17 x i8] c"/tmp/mmapped.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"Error opening file for writing\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"Error getting the file size\00"
@stderr = global i32 0
@.str.3 = [37 x i8] c"Error: File is empty, nothing to do\0A\00"
@.str.4 = [18 x i8] c"File size is %ji\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [24 x i8] c"Error mmapping the file\00"
@.str.6 = [27 x i8] c"Found character %c at %ji\0A\00"
@.str.7 = [27 x i8] c"Error un-mmapping the file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4, i32 384)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = bitcast %struct.stat* %3 to i8*
%13 = call i32 @fstat(i32 %5, %struct.stat* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = trunc i64 %21 to i8
%30 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8 signext %29)
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @MAP_SHARED
%33 = tail call i8* @mmap(i32 0, i64 %21, i32 %31, i32 %32, i32 %5, i32 0)
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %45
36: ; preds = %28
%37 = tail call i32 @close(i32 %5)
%38 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %45
%42 = tail call i32 @munmap(i8* %33, i64 %21)
%43 = icmp eq i32 %42, -1
%44 = tail call i32 @close(i32 %5)
br i1 %43, label %53, label %57
45: ; preds = %28, %45
%46 = phi i64 [ %51, %45 ], [ 0, %28 ]
%47 = getelementptr inbounds i8, i8* %33, i64 %46
%48 = load i8, i8* %47
%49 = trunc i64 %46 to i32
%50 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8 signext %48, i32 %49)
%51 = add nuw i64 %46, 1
%52 = icmp eq i64 %51, %21
br i1 %52, label %41, label %45
53: ; preds = %41
%54 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = tail call i32 @exit(i32 %55)
unreachable
57: ; preds = %41
ret i32 0
}