#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 */

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ msync(void *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  int fd;
  char *pmaddr;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDWR)) < 0) {
    perror(argv[1]);
    exit(1);
  }
  if ((pmaddr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  close(fd);
  strcpy(pmaddr, "Hello, Persistent Memory!");
  if (msync((void *)pmaddr, 4096, MS_SYNC) < 0) {
    perror("msync");
    exit(1);
  }
  printf("Done.\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d6 <main+0x46>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    0x2d99(%rip),%rax
or     (%rax),%edx
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d66(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <main+0xd9>
lea    0xdba(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
lea    0xd9d(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1040 <msync@plt>
cmp    $0x0,%rax
jge    12b8 <main+0x128>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd7a(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11ba <main+0x2a>
mov    0x8(%rbx),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
jns    11dc <main+0x4c>
mov    0x8(%rbx),%rdi
jmp    125b <main+0xcb>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe33(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebp
mov    0x2e13(%rip),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2d9b(%rip),%rax
cmp    (%rax),%rbx
jne    1223 <main+0x93>
lea    0xdf3(%rip),%rdi
jmp    125b <main+0xcb>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xde8(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <msync@plt>
test   %rax,%rax
jns    126a <main+0xda>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdc8(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [26 x i8] c"Hello, Persistent Memory!\00"
@MS_SYNC = global i32 0
@.str.3 = [6 x i8] c"msync\00"
@.str.4 = [7 x i8] c"Done.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @perror(i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %31, %32
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %6
%36 = call i8* @mmap(i32* null, i32 4096, i32 %33, i32 %34, i32 %35, i32 0)
store i8* %36, i8** %7
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %30
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i8*, i8** %7
%46 = call i32 @strcpy(i8* %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%47 = load i8*, i8** %7
%48 = load i32, i32* @MS_SYNC
%49 = call i64 @msync(i8* %47, i32 4096, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %42
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %42
%55 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [26 x i8] c"Hello, Persistent Memory!\00"
@MS_SYNC = global i32 0
@.str.3 = [6 x i8] c"msync\00"
@str = [6 x i8] c"Done.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i8*, i8** %10
%17 = tail call i32 @perror(i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i8* @mmap(i32* null, i32 4096, i32 %22, i32 %23, i32 %13, i32 0)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = tail call i32 @close(i32 %13)
%32 = tail call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @MS_SYNC
%34 = tail call i64 @msync(i8* %24, i32 4096, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%41 = tail call i32 @exit(i32 0)
unreachable
}