#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int main(void) {
  int fd;
  char *buf;
  int i;
  struct stat statbuf;
  if (stat("test.txt", &statbuf) == -1) {
    perror("fail to get stat");
    exit(1);
  }
  fd = open("test.txt", O_RDONLY);
  if (fd == -1) {
    perror("fail to open");
    exit(1);
  }
  buf = (char *)mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (buf == MAP_FAILED) {
    perror("fail to mmap");
    exit(1);
  }
  i = 0;
  while (i < statbuf.st_size) {
    printf("%c", buf[i]);
    i++;
  }
  printf("\n");
  if (munmap(buf, statbuf.st_size) == -1) {
    perror("fail to munmap");
    exit(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11be <main+0x3e>
lea    0xe5a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f6 <main+0x76>
lea    0xe33(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jne    1249 <main+0xc9>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1284 <main+0x104>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1250 <main+0xd0>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12bd <main+0x13d>
lea    0xd8b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%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
lea    0xe72(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xc2>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    %eax,%r14d
mov    (%rsp),%r15d
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    0x2dfc(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2dd8(%rip),%rax
cmp    %rbp,(%rax)
je     1254 <main+0xd4>
test   %r15d,%r15d
jle    1213 <main+0x93>
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl 0x0(%rbp,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1200 <main+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     125d <main+0xdd>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
jmp    1264 <main+0xe4>
lea    0xdc8(%rip),%rdi
jmp    1264 <main+0xe4>
lea    0xdcc(%rip),%rdi
jmp    1264 <main+0xe4>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [17 x i8] c"fail to get stat\00"
@O_RDONLY = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [13 x i8] c"fail to mmap\00"
@.str.6 = [15 x i8] c"fail to munmap\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_PRIVATE
%20 = tail call i64 @mmap(i32* null, i32 %17, i32 %18, i32 %19, i32 %10, i32 0)
%21 = inttoptr i64 %20 to i8*
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %22, %21
br i1 %23, label %28, label %24
24: ; preds = %15
%25 = icmp sgt i32 %17, 0
br i1 %25, label %26, label %39
26: ; preds = %24
%27 = zext i32 %17 to i64
br label %31
28: ; preds = %15
%29 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %26, %31
%32 = phi i64 [ 0, %26 ], [ %37, %31 ]
%33 = getelementptr inbounds i8, i8* %21, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = tail call i32 @putchar(i32 %35)
%37 = add nuw nsw i64 %32, 1
%38 = icmp eq i64 %37, %27
br i1 %38, label %39, label %31
39: ; preds = %31, %24
%40 = tail call i32 @putchar(i32 10)
%41 = tail call i32 @munmap(i8* %21, i32 %17)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = tail call i32 @close(i32 %10)
ret i32 0
}