#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_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int getpagesize();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
int main(void) {
  int fd;
  struct stat sst;
  int page_size;
  char *memory_map;
  int memory_map_size;
  if (stat("test.txt", &sst) == 0) {
    printf("sst.st_size = %d\n", sst.st_size);
  }
  page_size = getpagesize();
  printf("page_size = %d\n", page_size);
  memory_map_size = (sst.st_size / page_size + 1) * page_size;
  printf("memory_map_size = %d\n", memory_map_size);
  fd = open("test.txt", O_RDONLY);
  if (fd == -1) {
    perror("open error!");
    return -1;
  }
  memory_map =
      (char *)mmap(NULL, memory_map_size, PROT_READ, MAP_SHARED, fd, 0);
  if (memory_map == MAP_FAILED) {
    perror("mmap error!");
    return -2;
  }
  printf("-----begin-----\n");
  printf("%s\n", memory_map);
  printf("-----end-----\n");
  munmap(memory_map, memory_map_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11ba <main+0x3a>
mov    -0x10(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x14(%rbp)
add    $0x1,%eax
imul   -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1231 <main+0xb1>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <main+0x14f>
mov    -0x24(%rbp),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2da4(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
jne    1286 <main+0x106>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12cf <main+0x14f>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe62(%rip),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    11bc <main+0x2c>
mov    (%rsp),%esi
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10b0 <getpagesize@plt>
mov    %eax,%ebp
lea    0xe4f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%eax
cltd
idiv   %ebp
mov    %eax,%ebx
add    $0x1,%ebx
imul   %ebp,%ebx
lea    0xe41(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xf3>
mov    %eax,%r15d
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    0x2dc8(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d8a(%rip),%rax
cmp    %rbp,(%rax)
je     1297 <main+0x107>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1080 <munmap@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1275 <main+0xe5>
lea    0xdaf(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xfffffffe,%r14d
jmp    1275 <main+0xe5>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [18 x i8] c"sst.st_size = %d\0A\00"
@.str.2 = [16 x i8] c"page_size = %d\0A\00"
@.str.3 = [22 x i8] c"memory_map_size = %d\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [12 x i8] c"open error!\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [12 x i8] c"mmap error!\00"
@str = [16 x i8] c"-----begin-----\00"
@str.9 = [14 x i8] c"-----end-----\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%7 = load i32, i32* %6
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %5, %0
%10 = call i32 (...) @getpagesize()
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sdiv i32 %13, %10
%15 = add nsw i32 %14, 1
%16 = mul nsw i32 %15, %10
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %9
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %38
23: ; preds = %9
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_SHARED
%26 = call i64 @mmap(i32* null, i32 %16, i32 %24, i32 %25, i32 %19, i32 0)
%27 = inttoptr i64 %26 to i8*
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %28, %27
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %38
32: ; preds = %23
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%34 = call i32 @puts(i8* dereferenceable(1) %27)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%36 = call i32 @munmap(i8* %27, i32 %16)
%37 = call i32 @close(i32 %19)
br label %38
38: ; preds = %32, %30, %21
%39 = phi i32 [ -1, %21 ], [ -2, %30 ], [ 0, %32 ]
ret i32 %39
}