#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 */
scalar_t__ EBUSY;
scalar_t__ EPERM;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MCL_CURRENT;
int MS_INVALIDATE;
int MS_SYNC;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ errno;
scalar_t__ ftruncate(int, int);
int mlockall(int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int msync(void *, size_t, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
size_t sysconf(int /*<<< orphan*/);
int main(void) {
  void *page_ptr;
  size_t page_size;
  int result, fd;
  void *foo;
  page_size = sysconf(_SC_PAGESIZE);
  if (errno) {
    perror("An error occurs when calling sysconf()");
    return 2;
  }
  fd = shm_open("/posixtest_3-6", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  if (ftruncate(fd, 8) != 0) {
    perror("An error occurs when calling ftruncate()");
    shm_unlink("/posixtest_3-6");
    return 2;
  }
  foo = mmap(NULL, 8, PROT_WRITE, MAP_SHARED, fd, 0);
  if (foo == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    shm_unlink("/posixtest_3-6");
    return 2;
  }
  if (mlockall(MCL_CURRENT) == -1) {
    if (errno == EPERM) {
      printf("You don't have permission to lock your address space.\nTry to "
             "rerun this test as root.\n");
    } else {
      perror("An error occurs when calling mlockall()");
    }
    return 2;
  }
  page_ptr = (void *)((long)foo - ((long)foo % page_size));
  result = msync(page_ptr, page_size, MS_SYNC | MS_INVALIDATE);
  if (result == -1 && errno == EBUSY) {
    printf("Test PASSED\n");
    return 0;
  } else if (result == 0) {
    printf("The shared memory pages of the process are not locked.\n");
    return 1;
  }
  perror("Unexpected error");
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,-0x18(%rbp)
mov    0x2df8(%rip),%rax
cmpq   $0x0,(%rax)
je     11da <main+0x4a>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13de <main+0x24e>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2e0e(%rip),%rax
or     (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    0x2dc4(%rip),%rax
or     (%rax),%edx
lea    0xe22(%rip),%rdi
call   10a0 <shm_open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    122f <main+0x9f>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13de <main+0x24e>
mov    -0x20(%rbp),%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     126a <main+0xda>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
lea    0xdce(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13de <main+0x24e>
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
mov    0x2d6e(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2ce0(%rip),%rcx
cmp    (%rcx),%rax
jne    12cd <main+0x13d>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
lea    0xd6b(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13de <main+0x24e>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   1070 <mlockall@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x197>
mov    0x2cc5(%rip),%rax
mov    (%rax),%rax
mov    0x2c7b(%rip),%rcx
cmp    (%rcx),%rax
jne    130f <main+0x17f>
lea    0xda6(%rip),%rdi
call   1050 <printf@plt>
jmp    131b <main+0x18b>
lea    0xdec(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13de <main+0x24e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    -0x30(%rbp),%rax
sub    %rdx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2c53(%rip),%rax
mov    (%rax),%edx
mov    0x2c72(%rip),%rax
or     (%rax),%edx
call   1030 <msync@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    13a4 <main+0x214>
mov    0x2c37(%rip),%rax
mov    (%rax),%rax
mov    0x2c05(%rip),%rcx
cmp    (%rcx),%rax
jne    13a4 <main+0x214>
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13de <main+0x24e>
cmpl   $0x0,-0x1c(%rbp)
jne    13c6 <main+0x236>
lea    0xd82(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13de <main+0x24e>
jmp    13cb <main+0x23b>
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    0x2e06(%rip),%r15
cmpq   $0x0,(%r15)
jne    12ed <main+0x15d>
mov    %rax,%r14
mov    0x2e1a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc0(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe45(%rip),%rdi
call   10a0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12f6 <main+0x166>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    12ff <main+0x16f>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d4b(%rip),%rax
cmp    (%rax),%rbx
je     1308 <main+0x178>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1070 <mlockall@plt>
cmp    $0xffffffff,%eax
je     1267 <main+0xd7>
mov    %r14,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     128b <main+0xfb>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
jmp    1292 <main+0x102>
mov    (%r15),%rax
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%rax
jne    132b <main+0x19b>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
jmp    1337 <main+0x1a7>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
sub    %rdx,%rbx
mov    0x2d0c(%rip),%rax
mov    0x2d2d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <msync@plt>
test   %eax,%eax
je     12da <main+0x14a>
cmp    $0xffffffff,%eax
jne    1322 <main+0x192>
mov    (%r15),%rax
mov    0x2cc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1322 <main+0x192>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    133c <main+0x1ac>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    133c <main+0x1ac>
lea    0xd0c(%rip),%rdi
jmp    1332 <main+0x1a2>
lea    0xd39(%rip),%rdi
jmp    1332 <main+0x1a2>
lea    0xd58(%rip),%rdi
jmp    130f <main+0x17f>
lea    0xd78(%rip),%rdi
call   1080 <perror@plt>
lea    0xd0c(%rip),%rdi
call   10b0 <shm_unlink@plt>
jmp    1337 <main+0x1a7>
lea    0xdaa(%rip),%rdi
jmp    1332 <main+0x1a2>
lea    0xd79(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@errno = global i64 0
@.str = [39 x i8] c"An error occurs when calling sysconf()\00"
@.str.1 = [15 x i8] c"/posixtest_3-6\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@.str.3 = [41 x i8] c"An error occurs when calling ftruncate()\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [36 x i8] c"An error occurs when calling mmap()\00"
@MCL_CURRENT = global i32 0
@EPERM = global i64 0
@.str.5 = [87 x i8] c"You don't have permission to lock your address space.\0ATry to rerun this test as root.\0A\00"
@.str.6 = [40 x i8] c"An error occurs when calling mlockall()\00"
@MS_SYNC = global i32 0
@MS_INVALIDATE = global i32 0
@EBUSY = global i64 0
@.str.7 = [13 x i8] c"Test PASSED\0A\00"
@.str.8 = [56 x i8] c"The shared memory pages of the process are not locked.\0A\00"
@.str.9 = [17 x i8] c"Unexpected error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @_SC_PAGESIZE
%8 = call i64 @sysconf(i32 %7)
store i64 %8, i64* %3
%9 = load i64, i64* @errno
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %87
13: ; preds = %0
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %87
25: ; preds = %13
%26 = load i32, i32* %5
%27 = call i64 @ftruncate(i32 %26, i32 8)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %87
32: ; preds = %25
%33 = load i32, i32* @PROT_WRITE
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %5
%36 = call i8* @mmap(i32* null, i32 8, i32 %33, i32 %34, i32 %35, i32 0)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %87
43: ; preds = %32
%44 = load i32, i32* @MCL_CURRENT
%45 = call i32 @mlockall(i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %56
47: ; preds = %43
%48 = load i64, i64* @errno
%49 = load i64, i64* @EPERM
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.5, i64 0, i64 0))
br label %55
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %51
store i32 2, i32* %1
br label %87
56: ; preds = %43
%57 = load i8*, i8** %6
%58 = ptrtoint i8* %57 to i64
%59 = load i8*, i8** %6
%60 = ptrtoint i8* %59 to i64
%61 = load i64, i64* %3
%62 = urem i64 %60, %61
%63 = sub i64 %58, %62
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %2
%65 = load i8*, i8** %2
%66 = load i64, i64* %3
%67 = load i32, i32* @MS_SYNC
%68 = load i32, i32* @MS_INVALIDATE
%69 = or i32 %67, %68
%70 = call i32 @msync(i8* %65, i64 %66, i32 %69)
store i32 %70, i32* %4
%71 = load i32, i32* %4
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %79
73: ; preds = %56
%74 = load i64, i64* @errno
%75 = load i64, i64* @EBUSY
%76 = icmp eq i64 %74, %75
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %87
79: ; preds = %73, %56
%80 = load i32, i32* %4
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %87
84: ; preds = %79
br label %85
85: ; preds = %84
%86 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
store i32 2, i32* %1
br label %87
87: ; preds = %85, %82, %77, %55, %40, %29, %23, %11
%88 = load i32, i32* %1
ret i32 %88
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@errno = global i64 0
@.str = [39 x i8] c"An error occurs when calling sysconf()\00"
@.str.1 = [15 x i8] c"/posixtest_3-6\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@.str.3 = [41 x i8] c"An error occurs when calling ftruncate()\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [36 x i8] c"An error occurs when calling mmap()\00"
@MCL_CURRENT = global i32 0
@EPERM = global i64 0
@.str.6 = [40 x i8] c"An error occurs when calling mlockall()\00"
@MS_SYNC = global i32 0
@MS_INVALIDATE = global i32 0
@EBUSY = global i64 0
@.str.9 = [17 x i8] c"Unexpected error\00"
@str = [55 x i8] c"The shared memory pages of the process are not locked.\00"
@str.10 = [12 x i8] c"Test PASSED\00"
@str.11 = [86 x i8] c"You don't have permission to lock your address space.\0ATry to rerun this test as root.\00"
define i32 @main() {
%1 = load i32, i32* @_SC_PAGESIZE
%2 = tail call i64 @sysconf(i32 %1)
%3 = load i64, i64* @errno
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %64
7: ; preds = %0
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %64
18: ; preds = %7
%19 = tail call i64 @ftruncate(i32 %14, i32 8)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %64
24: ; preds = %18
%25 = load i32, i32* @PROT_WRITE
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32* null, i32 8, i32 %25, i32 %26, i32 %14, i32 0)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %64
33: ; preds = %24
%34 = load i32, i32* @MCL_CURRENT
%35 = tail call i32 @mlockall(i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %45
37: ; preds = %33
%38 = load i64, i64* @errno
%39 = load i64, i64* @EPERM
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str.11, i64 0, i64 0))
br label %64
43: ; preds = %37
%44 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %64
45: ; preds = %33
%46 = ptrtoint i8* %27 to i64
%47 = urem i64 %46, %2
%48 = sub i64 %46, %47
%49 = inttoptr i64 %48 to i8*
%50 = load i32, i32* @MS_SYNC
%51 = load i32, i32* @MS_INVALIDATE
%52 = or i32 %51, %50
%53 = tail call i32 @msync(i8* %49, i64 %2, i32 %52)
switch i32 %53, label %62 [
i32 -1, label %54
i32 0, label %60
]
54: ; preds = %45
%55 = load i64, i64* @errno
%56 = load i64, i64* @EBUSY
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
br label %64
60: ; preds = %45
%61 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
br label %64
62: ; preds = %54, %45
%63 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
br label %64
64: ; preds = %41, %43, %62, %60, %58, %30, %21, %16, %5
%65 = phi i32 [ 2, %5 ], [ 2, %16 ], [ 2, %21 ], [ 2, %30 ], [ 0, %58 ], [ 1, %60 ], [ 2, %62 ], [ 2, %43 ], [ 2, %41 ]
ret i32 %65
}