#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 */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ MS_ASYNC;
int /*<<< orphan*/ MS_INVALIDATE;
int /*<<< orphan*/ MS_SYNC;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, size_t const, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int msync(void *, size_t const, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t const);
int open(char const *, int, int);
int /*<<< orphan*/ unlink(char const *);
scalar_t__ write(int, char *, size_t const);
int main(void) {
  char buffer[8192];
  static const char *filename = "/tmp/test-msync.tmp";
  int fd, ret, err = 1;
  void *ptr;
  ssize_t rc;
  const size_t sz = sizeof buffer;
  (void)memset(buffer, 0, sizeof(buffer));
  fd = open(filename, O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    return 1;
  (void)unlink(filename);
  rc = write(fd, buffer, sz);
  if (rc != (ssize_t)sz) {
    goto err;
  }
  ptr = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (ptr == MAP_FAILED)
    goto err;
  ret = msync(ptr, sz, MS_ASYNC);
  (void)ret;
  ret = msync(ptr, sz, MS_SYNC);
  (void)ret;
  ret = msync(ptr, sz, MS_INVALIDATE);
  (void)ret;
  (void)munmap(ptr, sz);
  err = 0;
err:
  (void)close(fd);
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x201c(%rbp)
movq   $0x2000,-0x2038(%rbp)
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1070 <memset@plt>
mov    0x2e9f(%rip),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,-0x2014(%rbp)
cmpl   $0x0,-0x2014(%rbp)
jge    11fc <main+0x7c>
movl   $0x1,-0x4(%rbp)
jmp    1321 <main+0x1a1>
mov    0x2e5d(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x2014(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <write@plt>
mov    %rax,-0x2030(%rbp)
cmpq   $0x2000,-0x2030(%rbp)
je     123c <main+0xbc>
jmp    130d <main+0x18d>
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    0x2d64(%rip),%rax
or     (%rax),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
mov    -0x2014(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,-0x2028(%rbp)
mov    -0x2028(%rbp),%rax
mov    0x2d1c(%rip),%rcx
cmp    (%rcx),%rax
jne    1292 <main+0x112>
jmp    130d <main+0x18d>
mov    -0x2028(%rbp),%rdi
mov    0x2d28(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   1050 <msync@plt>
mov    %eax,-0x2018(%rbp)
mov    -0x2028(%rbp),%rdi
mov    0x2cf8(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   1050 <msync@plt>
mov    %eax,-0x2018(%rbp)
mov    -0x2028(%rbp),%rdi
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   1050 <msync@plt>
mov    %eax,-0x2018(%rbp)
mov    -0x2028(%rbp),%rdi
mov    $0x2000,%esi
call   1090 <munmap@plt>
movl   $0x0,-0x201c(%rbp)
mov    -0x2014(%rbp),%edi
call   1080 <close@plt>
mov    -0x201c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1070 <memset@plt>
mov    0x2e37(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
mov    $0x1b6,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1289 <main+0x109>
mov    %eax,%ebx
lea    0xe32(%rip),%rdi
call   1030 <unlink@plt>
mov    %rsp,%rsi
mov    $0x2000,%edx
mov    %ebx,%edi
call   1040 <write@plt>
mov    $0x1,%ebp
cmp    $0x2000,%rax
jne    1280 <main+0x100>
mov    0x2df6(%rip),%rax
mov    0x2daf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d9c(%rip),%rax
mov    (%rax),%ecx
mov    $0x2000,%esi
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%r14
mov    0x2d76(%rip),%rax
cmp    (%rax),%r14
je     1280 <main+0x100>
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
mov    %r14,%rdi
call   1050 <msync@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
mov    %r14,%rdi
call   1050 <msync@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
mov    %r14,%rdi
call   1050 <msync@plt>
mov    $0x2000,%esi
mov    %r14,%rdi
call   1090 <munmap@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
jmp    128e <main+0x10e>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@main.filename = internal global i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0)
@.str = [20 x i8] c"/tmp/test-msync.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@MS_ASYNC = global i32 0
@MS_SYNC = global i32 0
@MS_INVALIDATE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [8192 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 0, i32* %1
store i32 1, i32* %5
store i64 8192, i64* %8
%9 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 8192)
%11 = load i8*, i8** @main.filename
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14, i32 438)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %0
store i32 1, i32* %1
br label %58
19: ; preds = %0
%20 = load i8*, i8** @main.filename
%21 = call i32 @unlink(i8* %20)
%22 = load i32, i32* %3
%23 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%24 = call i64 @write(i32 %22, i8* %23, i64 8192)
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = icmp ne i64 %25, 8192
br i1 %26, label %27, label %28
27: ; preds = %19
br label %54
28: ; preds = %19
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_PRIVATE
%33 = load i32, i32* %3
%34 = call i8* @mmap(i32* null, i64 8192, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %39
38: ; preds = %28
br label %54
39: ; preds = %28
%40 = load i8*, i8** %6
%41 = load i32, i32* @MS_ASYNC
%42 = call i32 @msync(i8* %40, i64 8192, i32 %41)
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = load i8*, i8** %6
%45 = load i32, i32* @MS_SYNC
%46 = call i32 @msync(i8* %44, i64 8192, i32 %45)
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = load i8*, i8** %6
%49 = load i32, i32* @MS_INVALIDATE
%50 = call i32 @msync(i8* %48, i64 8192, i32 %49)
store i32 %50, i32* %4
%51 = load i32, i32* %4
%52 = load i8*, i8** %6
%53 = call i32 @munmap(i8* %52, i64 8192)
store i32 0, i32* %5
br label %54
54: ; preds = %39, %38, %27
%55 = load i32, i32* %3
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %5
store i32 %57, i32* %1
br label %58
58: ; preds = %54, %18
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@.str = [20 x i8] c"/tmp/test-msync.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@MS_ASYNC = global i32 0
@MS_SYNC = global i32 0
@MS_INVALIDATE = global i32 0
define i32 @main() {
%1 = alloca [8192 x i8]
%2 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 8192)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
%8 = icmp slt i32 %7, 0
br i1 %8, label %32, label %9
9: ; preds = %0
%10 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = call i64 @write(i32 %7, i8* %2, i64 8192)
%12 = icmp eq i64 %11, 8192
br i1 %12, label %13, label %29
13: ; preds = %9
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_PRIVATE
%18 = call i8* @mmap(i32* null, i64 8192, i32 %16, i32 %17, i32 %7, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %29, label %21
21: ; preds = %13
%22 = load i32, i32* @MS_ASYNC
%23 = call i32 @msync(i8* %18, i64 8192, i32 %22)
%24 = load i32, i32* @MS_SYNC
%25 = call i32 @msync(i8* %18, i64 8192, i32 %24)
%26 = load i32, i32* @MS_INVALIDATE
%27 = call i32 @msync(i8* %18, i64 8192, i32 %26)
%28 = call i32 @munmap(i8* %18, i64 8192)
br label %29
29: ; preds = %13, %9, %21
%30 = phi i32 [ 1, %9 ], [ 1, %13 ], [ 0, %21 ]
%31 = call i32 @close(i32 %7)
br label %32
32: ; preds = %0, %29
%33 = phi i32 [ %30, %29 ], [ 1, %0 ]
ret i32 %33
}