#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_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(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_WRITE, MAP_PRIVATE, fd, 0);
  if (buf == MAP_FAILED) {
    perror("fail to mmap");
    exit(1);
  }
  strcpy(buf, "China beijing");
  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    0x2e23(%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    0x2dd8(%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   1040 <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>
mov    -0x10(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1284 <main+0x104>
lea    0xdcd(%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   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     1225 <main+0xa5>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122e <main+0xae>
mov    %eax,%ebx
mov    0x8(%rsp),%r14d
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2dd9(%rip),%rax
cmp    %rbp,(%rax)
je     1237 <main+0xb7>
lea    0xe39(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     1240 <main+0xc0>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddd(%rip),%rdi
jmp    1247 <main+0xc7>
lea    0xde5(%rip),%rdi
jmp    1247 <main+0xc7>
lea    0xde9(%rip),%rdi
jmp    1247 <main+0xc7>
lea    0xdfb(%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_RDWR = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@PROT_WRITE = 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 = [14 x i8] c"China beijing\00"
@.str.5 = [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_WRITE
%22 = load i32, i32* @MAP_PRIVATE
%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 = load i8*, i8** %3
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%35 = load i8*, i8** %3
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @munmap(i8* %35, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = load i32, i32* %2
%45 = call i32 @close(i32 %44)
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_WRITE = 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 = [14 x i8] c"China beijing\00"
@.str.5 = [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_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_WRITE
%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 %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 @strcpy(i8* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @munmap(i8* %21, i32 %17)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = tail call i32 @close(i32 %10)
ret i32 0
}