#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_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int getpagesize();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ msync(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
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_RDWR);
  if (fd == -1) {
    perror("open error!");
    return -1;
  }
  memory_map =
      (char *)mmap(NULL, memory_map_size, PROT_WRITE, MAP_SHARED, fd, 0);
  if (memory_map == MAP_FAILED) {
    perror("mmap error!");
    return -2;
  }
  strcpy(memory_map, "VWXYZ\n");
  msync(memory_map, memory_map_size, 0);
  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    0xe4a(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    11d8 <main+0x38>
mov    -0x10(%rbp),%esi
lea    0xe36(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10c0 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe2f(%rip),%rdi
call   1060 <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    0xe1f(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    0xddf(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    124b <main+0xab>
lea    0xe07(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <main+0x139>
mov    -0x24(%rbp),%esi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    0x2d92(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <main+0x100>
lea    0xdbe(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12d9 <main+0x139>
mov    -0x20(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %edx,%edx
call   1040 <msync@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1090 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1070 <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    0xe52(%rip),%rdi
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    11ca <main+0x2a>
mov    (%rsp),%esi
lea    0xe44(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   10c0 <getpagesize@plt>
mov    %eax,%ebp
lea    0xe41(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    (%rsp),%eax
cltd
idiv   %ebp
mov    %eax,%ebx
add    $0x1,%ebx
imul   %ebp,%ebx
lea    0xe35(%rip),%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xe8>
mov    %eax,%r15d
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x2d80(%rip),%rax
cmp    %rbp,(%rax)
je     129c <main+0xfc>
lea    0xe05(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <msync@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1090 <munmap@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    127a <main+0xda>
lea    0xdaa(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xfffffffe,%r14d
jmp    127a <main+0xda>

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_RDWR = global i32 0
@.str.4 = [12 x i8] c"open error!\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [12 x i8] c"mmap error!\00"
@.str.6 = [7 x i8] c"VWXYZ\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 @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 @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 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @O_RDWR
%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 %55
32: ; preds = %13
%33 = load i32, i32* %6
%34 = load i32, i32* @PROT_WRITE
%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 %55
44: ; preds = %32
%45 = load i8*, i8** %5
%46 = call i32 @strcpy(i8* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%47 = load i8*, i8** %5
%48 = load i32, i32* %6
%49 = call i32 @msync(i8* %47, i32 %48, i32 0)
%50 = load i8*, i8** %5
%51 = load i32, i32* %6
%52 = call i32 @munmap(i8* %50, i32 %51)
%53 = load i32, i32* %2
%54 = call i32 @close(i32 %53)
store i32 0, i32* %1
br label %55
55: ; preds = %44, %42, %30
%56 = load i32, i32* %1
ret i32 %56
}

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_RDWR = global i32 0
@.str.4 = [12 x i8] c"open error!\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [12 x i8] c"mmap error!\00"
@.str.6 = [7 x i8] c"VWXYZ\0A\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 @printf(i8* 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 @printf(i8* 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 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @O_RDWR
%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 %37
23: ; preds = %9
%24 = load i32, i32* @PROT_WRITE
%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 %37
32: ; preds = %23
%33 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 @msync(i8* %27, i32 %16, i32 0)
%35 = call i32 @munmap(i8* %27, i32 %16)
%36 = call i32 @close(i32 %19)
br label %37
37: ; preds = %32, %30, %21
%38 = phi i32 [ -1, %21 ], [ -2, %30 ], [ 0, %32 ]
ret i32 %38
}