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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
scalar_t__ msync(void *, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int sysconf(int /*<<< orphan*/);
int write(int, char *, int);
void test_msync(char *output, char *text, size_t length) {
  int pagesize = sysconf(_SC_PAGE_SIZE);
  if (pagesize < 0) {
    perror("Cannot get page size");
    return;
  }
  int fd =
      open(output, (O_CREAT | O_TRUNC | O_RDWR), (S_IRWXU | S_IRWXG | S_IRWXO));
  if (fd < 0) {
    perror("Cannot open file");
    return;
  }
  off_t lastoffset = lseek(fd, pagesize, SEEK_SET);
  ssize_t written = write(fd, " ", 1);
  if (written != 1) {
    perror("Write error. ");
    close(fd);
    return;
  }
  off_t my_offset = 0;
  void *address = mmap(NULL, pagesize, PROT_WRITE, MAP_SHARED, fd, my_offset);
  if (address == MAP_FAILED) {
    perror("Map error. ");
    close(fd);
    return;
  }
  (void)strcpy((char *)address, text);
  if (msync(address, pagesize, MS_SYNC) < 0) {
    perror("msync failed with error:");
  }
  close(fd);
  sleep(5);
}


x86 assembly (clang, -O0):
<test_msync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e0 <test_msync+0x40>
lea    0xe2a(%rip),%rdi
call   10a0 <perror@plt>
jmp    1336 <test_msync+0x196>
mov    -0x8(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2dc3(%rip),%rax
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
mov    0x2da9(%rip),%rax
or     (%rax),%edx
mov    0x2dc8(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    123d <test_msync+0x9d>
lea    0xde2(%rip),%rdi
call   10a0 <perror@plt>
jmp    1336 <test_msync+0x196>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
lea    0xdd4(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
je     128e <test_msync+0xee>
lea    0xdaa(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
jmp    1336 <test_msync+0x196>
movl   $0x0,-0x2c(%rbp)
mov    -0x1c(%rbp),%esi
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
mov    0x2d30(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1060 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2cbe(%rip),%rcx
cmp    (%rcx),%rax
jne    12ec <test_msync+0x14c>
lea    0xd5a(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
jmp    1336 <test_msync+0x196>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2c99(%rip),%rax
mov    (%rax),%edx
call   1050 <msync@plt>
cmp    $0x0,%rax
jge    1324 <test_msync+0x184>
lea    0xd21(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   10c0 <sleep@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_msync>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
test   %eax,%eax
js     12ad <test_msync+0x10d>
mov    %eax,%r15d
mov    0x2e2b(%rip),%rax
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2de0(%rip),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12b6 <test_msync+0x116>
mov    %eax,%ebx
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15d,%esi
call   1070 <lseek@plt>
lea    0xe0a(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12cc <test_msync+0x12c>
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
mov    0x2d8b(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbp
mov    0x2d1f(%rip),%rax
cmp    (%rax),%rbp
je     12d5 <test_msync+0x135>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15d,%esi
call   1050 <msync@plt>
test   %rax,%rax
js     12f2 <test_msync+0x152>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0x5,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <sleep@plt>
lea    0xd4c(%rip),%rdi
jmp    12bd <test_msync+0x11d>
lea    0xd58(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <perror@plt>
lea    0xd53(%rip),%rdi
jmp    12dc <test_msync+0x13c>
lea    0xd58(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <close@plt>
lea    0xd47(%rip),%rdi
call   10a0 <perror@plt>
jmp    1292 <test_msync+0xf2>

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@.str = [21 x i8] c"Cannot get page size\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [17 x i8] c"Cannot open file\00"
@SEEK_SET = global i32 0
@.str.2 = [2 x i8] c" \00"
@.str.3 = [14 x i8] c"Write error. \00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"Map error. \00"
@MS_SYNC = global i32 0
@.str.5 = [25 x i8] c"msync failed with error:\00"
define void @test_msync(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%13 = load i32, i32* @_SC_PAGE_SIZE
%14 = call i32 @sysconf(i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %78
19: ; preds = %3
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @O_RDWR
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRWXU
%27 = load i32, i32* @S_IRWXG
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRWXO
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %20, i32 %25, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %78
36: ; preds = %19
%37 = load i32, i32* %8
%38 = load i32, i32* %7
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %37, i32 %38, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %8
%42 = call i32 @write(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %49
45: ; preds = %36
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %8
%48 = call i32 @close(i32 %47)
br label %78
49: ; preds = %36
store i32 0, i32* %11
%50 = load i32, i32* %7
%51 = load i32, i32* @PROT_WRITE
%52 = load i32, i32* @MAP_SHARED
%53 = load i32, i32* %8
%54 = load i32, i32* %11
%55 = call i8* @mmap(i32* null, i32 %50, i32 %51, i32 %52, i32 %53, i32 %54)
store i8* %55, i8** %12
%56 = load i8*, i8** %12
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %63
59: ; preds = %49
%60 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
br label %78
63: ; preds = %49
%64 = load i8*, i8** %12
%65 = load i8*, i8** %5
%66 = call i32 @strcpy(i8* %64, i8* %65)
%67 = load i8*, i8** %12
%68 = load i32, i32* %7
%69 = load i32, i32* @MS_SYNC
%70 = call i64 @msync(i8* %67, i32 %68, i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %63
%73 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %74
74: ; preds = %72, %63
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
%77 = call i32 @sleep(i32 5)
br label %78
78: ; preds = %74, %59, %45, %34, %17
ret void
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str = [21 x i8] c"Cannot get page size\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [17 x i8] c"Cannot open file\00"
@SEEK_SET = global i32 0
@.str.2 = [2 x i8] c" \00"
@.str.3 = [14 x i8] c"Write error. \00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"Map error. \00"
@MS_SYNC = global i32 0
@.str.5 = [25 x i8] c"msync failed with error:\00"
define void @test_msync(i8* nocapture %0, i8* %1, i64 %2) {
%4 = load i32, i32* @_SC_PAGE_SIZE
%5 = tail call i32 @sysconf(i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %51
9: ; preds = %3
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %11, %10
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRWXU
%16 = load i32, i32* @S_IRWXG
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRWXO
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %0, i32 %14, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %9
%23 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %51
24: ; preds = %9
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @lseek(i32 %20, i32 %5, i32 %25)
%27 = tail call i32 @write(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%28 = icmp eq i32 %27, 1
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @close(i32 %20)
br label %51
32: ; preds = %24
%33 = load i32, i32* @PROT_WRITE
%34 = load i32, i32* @MAP_SHARED
%35 = tail call i8* @mmap(i32* null, i32 %5, i32 %33, i32 %34, i32 %20, i32 0)
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @close(i32 %20)
br label %51
41: ; preds = %32
%42 = tail call i32 @strcpy(i8* %35, i8* %1)
%43 = load i32, i32* @MS_SYNC
%44 = tail call i64 @msync(i8* %35, i32 %5, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
%49 = tail call i32 @close(i32 %20)
%50 = tail call i32 @sleep(i32 5)
br label %51
51: ; preds = %22, %38, %48, %29, %7
ret void
}