#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 /*<<< orphan*/ st_size;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int mprotect(char *, int /*<<< orphan*/, int);
int munmap(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  int fd;
  char *buf;
  struct stat statbuf;
  if (stat("test.txt", &statbuf) == -1) {
    perror("fail to get stat");
    exit(1);
  }
  fd = open("test.txt", O_RDWR);
  if (fd == -1) {
    perror("fail to open");
    exit(1);
  }
  buf = (char *)mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("fail to mmap");
    exit(1);
  }
  printf("chang mode\n");
  if (mprotect(buf, statbuf.st_size, PROT_READ | PROT_WRITE) == -1) {
    perror("fail to alter permission");
    exit(1);
  }
  printf("write done\n");
  strcpy(buf, "China\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    0xe4a(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x3e>
lea    0xe3a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe12(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1216 <main+0x76>
lea    0xe13(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    0x2dbf(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <main+0xc9>
lea    0xdcd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    0x2d3c(%rip),%rax
or     (%rax),%edx
call   1090 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    12b2 <main+0x112>
lea    0xd9d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xda0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12f9 <main+0x159>
lea    0xd82(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <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    0xe52(%rip),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     127a <main+0xda>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xe3>
mov    %eax,%r14d
mov    (%rsp),%ebp
mov    0x2e0e(%rip),%r15
mov    (%r15),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2db1(%rip),%rax
cmp    %rbx,(%rax)
je     128c <main+0xec>
lea    0xe48(%rip),%rdi
call   1040 <puts@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <mprotect@plt>
cmp    $0xffffffff,%eax
je     1295 <main+0xf5>
lea    0xe2c(%rip),%rdi
call   1040 <puts@plt>
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <munmap@plt>
cmp    $0xffffffff,%eax
je     129e <main+0xfe>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd88(%rip),%rdi
jmp    12a5 <main+0x105>
lea    0xd90(%rip),%rdi
jmp    12a5 <main+0x105>
lea    0xd94(%rip),%rdi
jmp    12a5 <main+0x105>
lea    0xd98(%rip),%rdi
jmp    12a5 <main+0x105>
lea    0xdaf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <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_RDWR = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [13 x i8] c"fail to mmap\00"
@.str.4 = [12 x i8] c"chang mode\0A\00"
@PROT_WRITE = global i32 0
@.str.5 = [25 x i8] c"fail to alter permission\00"
@.str.6 = [12 x i8] c"write done\0A\00"
@.str.7 = [7 x i8] c"China\0A\00"
@.str.8 = [15 x i8] c"fail to munmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i32 0, i32* %1
%5 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %2
%24 = call i64 @mmap(i32* null, i32 %20, i32 %21, i32 %22, i32 %23, i32 0)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%34 = load i8*, i8** %3
%35 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @PROT_WRITE
%39 = or i32 %37, %38
%40 = call i32 @mprotect(i8* %34, i32 %36, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %32
%46 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%47 = load i8*, i8** %3
%48 = call i32 @strcpy(i8* %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%49 = load i8*, i8** %3
%50 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @munmap(i8* %49, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %45
%55 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %45
%58 = load i32, i32* %2
%59 = call i32 @close(i32 %58)
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_RDWR = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [13 x i8] c"fail to mmap\00"
@PROT_WRITE = global i32 0
@.str.5 = [25 x i8] c"fail to alter permission\00"
@.str.7 = [7 x i8] c"China\0A\00"
@.str.8 = [15 x i8] c"fail to munmap\00"
@str = [11 x i8] c"chang mode\00"
@str.9 = [11 x i8] c"write done\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_RDWR
%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_SHARED
%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 %24, label %27
24: ; preds = %15
%25 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %15
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %30, %29
%32 = tail call i32 @mprotect(i8* %21, i32 %17, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
%39 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%40 = tail call i32 @munmap(i8* %21, i32 %17)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %37
%46 = tail call i32 @close(i32 %10)
ret i32 0
}