#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*/ MS_SYNC;
int O_APPEND;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int msync(char *, int, int /*<<< orphan*/);
int munmap(char *, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int stat(char *, struct stat *);
int main(void) {
  int fd;
  char *buf;
  char *p;
  int i;
  struct stat statbuf;
  if (stat("test.txt", &statbuf) == -1) {
    perror("fail to get stat");
    exit(1);
  }
  fd = open("test.txt", O_RDWR | O_APPEND);
  if (fd == -1) {
    perror("fail to open");
    exit(1);
  }
  buf = (char *)mmap(NULL, statbuf.st_size, PROT_WRITE, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("fail to mmap");
    exit(1);
  }
  i = 0;
  p = buf;
  while (i < 3) {
    sleep(3);
    printf("hello world\n");
    memcpy(p, "hello world\n", 12);
    i++;
    p += 12;
    if (msync(buf, 18, MS_SYNC) == -1) {
      perror("fail to msync");
      exit(1);
    }
  }
  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    0xe3a(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11ee <main+0x3e>
lea    0xe2a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
lea    0xdf9(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    122f <main+0x7f>
lea    0xdfa(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%esi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
mov    0x2dae(%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    0x2d5d(%rip),%rcx
cmp    (%rcx),%rax
jne    1282 <main+0xd2>
lea    0xdb4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    1316 <main+0x166>
mov    $0x3,%edi
call   10d0 <sleep@plt>
lea    0xd88(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xd78(%rip),%rsi
mov    $0xc,%edx
call   1080 <memcpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2cea(%rip),%rax
mov    (%rax),%edx
mov    $0x12,%esi
call   1030 <msync@plt>
cmp    $0xffffffff,%eax
jne    1311 <main+0x161>
lea    0xd3f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1291 <main+0xe1>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1090 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1341 <main+0x191>
lea    0xd1d(%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   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe3d(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1340 <main+0x190>
mov    0x2e03(%rip),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe11(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1349 <main+0x199>
mov    %eax,%r15d
mov    0x8(%rsp),%r14d
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x2d8f(%rip),%rax
cmp    %rbp,(%rax)
je     1352 <main+0x1a2>
mov    $0x3,%edi
call   10d0 <sleep@plt>
lea    0xe13(%rip),%rdi
call   1030 <puts@plt>
lea    0xddd(%rip),%rsi
mov    %rbp,%rdi
mov    $0xc,%edx
call   1080 <memcpy@plt>
mov    0x2d65(%rip),%r12
mov    (%r12),%edx
mov    %rbp,%rdi
mov    $0x12,%esi
call   1040 <msync@plt>
cmp    $0xffffffff,%eax
je     1337 <main+0x187>
lea    0xc(%rbp),%rbx
mov    $0x3,%edi
call   10d0 <sleep@plt>
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
lea    0xd8e(%rip),%rsi
mov    %rbx,%rdi
mov    $0xc,%edx
call   1080 <memcpy@plt>
mov    (%r12),%edx
mov    %rbp,%rdi
mov    $0x12,%esi
call   1040 <msync@plt>
cmp    $0xffffffff,%eax
je     1337 <main+0x187>
mov    %rbp,%rbx
add    $0x18,%rbx
mov    $0x3,%edi
call   10d0 <sleep@plt>
lea    0xd7d(%rip),%rdi
call   1030 <puts@plt>
lea    0xd47(%rip),%rsi
mov    %rbx,%rdi
mov    $0xc,%edx
call   1080 <memcpy@plt>
mov    (%r12),%edx
mov    %rbp,%rdi
mov    $0x12,%esi
call   1040 <msync@plt>
cmp    $0xffffffff,%eax
je     1337 <main+0x187>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1090 <munmap@plt>
cmp    $0xffffffff,%eax
je     135b <main+0x1ab>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd03(%rip),%rdi
jmp    1362 <main+0x1b2>
lea    0xcc2(%rip),%rdi
jmp    1362 <main+0x1b2>
lea    0xcca(%rip),%rdi
jmp    1362 <main+0x1b2>
lea    0xcce(%rip),%rdi
jmp    1362 <main+0x1b2>
lea    0xced(%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
@O_APPEND = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@PROT_WRITE = 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 = [13 x i8] c"hello world\0A\00"
@MS_SYNC = global i32 0
@.str.5 = [14 x i8] c"fail to msync\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 i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i32 0, i32* %1
%7 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_WRITE
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %2
%28 = call i64 @mmap(i32* null, i32 %24, i32 %25, i32 %26, i32 %27, i32 0)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %3
%30 = load i8*, i8** %3
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %22
store i32 0, i32* %5
%37 = load i8*, i8** %3
store i8* %37, i8** %4
br label %38
38: ; preds = %57, %36
%39 = load i32, i32* %5
%40 = icmp slt i32 %39, 3
br i1 %40, label %41, label %58
41: ; preds = %38
%42 = call i32 @sleep(i32 3)
%43 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%44 = load i8*, i8** %4
%45 = call i32 @memcpy(i8* %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 12)
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
store i32 %47, i32* %5
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i64 12
store i8* %49, i8** %4
%50 = load i8*, i8** %3
%51 = load i32, i32* @MS_SYNC
%52 = call i32 @msync(i8* %50, i32 18, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %41
%55 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %41
br label %38
58: ; preds = %38
%59 = load i8*, i8** %3
%60 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @munmap(i8* %59, i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %58
%68 = load i32, i32* %2
%69 = call i32 @close(i32 %68)
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
@O_APPEND = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@PROT_WRITE = 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 = [13 x i8] c"hello world\0A\00"
@MS_SYNC = global i32 0
@.str.5 = [14 x i8] c"fail to msync\00"
@.str.6 = [15 x i8] c"fail to munmap\00"
@str = [12 x i8] c"hello world\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 = load i32, i32* @O_APPEND
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i64 @mmap(i32* null, i32 %19, i32 %20, i32 %21, i32 %12, i32 0)
%23 = inttoptr i64 %22 to i8*
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %24, %23
br i1 %25, label %33, label %26
26: ; preds = %17
%27 = tail call i32 @sleep(i32 3)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 @memcpy(i8* %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 12)
%30 = load i32, i32* @MS_SYNC
%31 = tail call i32 @msync(i8* %23, i32 18, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %52, label %36
33: ; preds = %17
%34 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = getelementptr inbounds i8, i8* %23, i64 12
%38 = tail call i32 @sleep(i32 3)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @memcpy(i8* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 12)
%41 = load i32, i32* @MS_SYNC
%42 = tail call i32 @msync(i8* %23, i32 18, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %52, label %44
44: ; preds = %36
%45 = getelementptr inbounds i8, i8* %23, i64 24
%46 = tail call i32 @sleep(i32 3)
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%48 = tail call i32 @memcpy(i8* %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 12)
%49 = load i32, i32* @MS_SYNC
%50 = tail call i32 @msync(i8* %23, i32 18, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %44, %36, %26
%53 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%54 = tail call i32 @exit(i32 1)
unreachable
55: ; preds = %44
%56 = tail call i32 @munmap(i8* %23, i32 %19)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%60 = tail call i32 @exit(i32 1)
unreachable
61: ; preds = %55
%62 = tail call i32 @close(i32 %12)
ret i32 0
}