#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 /*<<< orphan*/ tmpfname;

/* Variables and functions */
scalar_t__ EACCES;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ftruncate(int, size_t);
int getpid();
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[256];
  void *pa;
  size_t size = 1024;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_6_4_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("Error at open(): %s\n", strerror(errno));
    return 2;
  }
  if (ftruncate(fd, size) == -1) {
    printf("Error at ftruncate(): %s\n", strerror(errno));
    return 2;
  }
  close(fd);
  fd = open(tmpfname, O_RDONLY, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("Error at 2nd open(): %s\n", strerror(errno));
    return 2;
  }
  unlink(tmpfname);
  pa = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
  if (pa == MAP_FAILED && errno == EACCES) {
    printf("EACCES on attempt to map readonly file as "
           "PROT_WRITE, MAP_SHARED\n"
           "Test PASSED\n");
    return 0;
  }
  printf("Maping readonly file with PROT_WRITE, MAP_SHARED have not "
         "returned EACCES\n"
         "Test FAILED\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x400,-0x120(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x130(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe29(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    0x2dae(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    1260 <main+0xd0>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13b7 <main+0x227>
mov    -0x124(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12a7 <main+0x117>
mov    0x2d26(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13b7 <main+0x227>
mov    -0x124(%rbp),%edi
call   1090 <close@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
mov    0x2cf6(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    1318 <main+0x188>
mov    0x2cb5(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13b7 <main+0x227>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x120(%rbp),%rsi
mov    0x2c6e(%rip),%rax
mov    (%rax),%edx
mov    0x2cad(%rip),%rax
mov    (%rax),%ecx
mov    -0x124(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2c2b(%rip),%rcx
cmp    (%rcx),%rax
jne    13a2 <main+0x212>
mov    0x2c33(%rip),%rax
mov    (%rax),%rax
mov    0x2c41(%rip),%rcx
cmp    (%rcx),%rax
jne    13a2 <main+0x212>
lea    0xcce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b7 <main+0x227>
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe46(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e1d(%rip),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc3(%rip),%rax
or     (%rax),%esi
mov    0x2da2(%rip),%r14
mov    0x2dcb(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    %rbx,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12b2 <main+0x112>
mov    %eax,%ebx
mov    $0x400,%esi
mov    %eax,%edi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x12a>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    (%r15),%edx
or     (%r14),%edx
mov    %rsp,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12f5 <main+0x155>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
mov    0x2d7c(%rip),%rax
mov    (%rax),%ecx
mov    $0x400,%esi
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    12e2 <main+0x142>
mov    0x2d15(%rip),%rax
mov    (%rax),%rax
mov    0x2d23(%rip),%rcx
cmp    (%rcx),%rax
jne    12e2 <main+0x142>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    131a <main+0x17a>
mov    0x2cef(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd4d(%rip),%rdi
jmp    130b <main+0x16b>
mov    0x2cd7(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd4a(%rip),%rdi
jmp    130b <main+0x16b>
lea    0xd74(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    131a <main+0x17a>
mov    0x2cac(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd39(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/tmp/pts_mmap_6_4_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [21 x i8] c"Error at open(): %s\0A\00"
@errno = global i64 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [25 x i8] c"Error at 2nd open(): %s\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EACCES = global i64 0
@.str.4 = [78 x i8] c"EACCES on attempt to map file as PROT_WRITE, MAP_SHARED\0ATest PASSED\0A\00"
@.str.5 = [87 x i8] c"Maping file with PROT_WRITE, MAP_SHARED have not returned EACCES\0ATest FAILED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
store i64 1024, i64* %4
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%10 = call i32 @unlink(i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = load i32, i32* @O_EXCL
%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 @open(i8* %11, 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 %27
23: ; preds = %0
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 2, i32* %1
br label %70
27: ; preds = %0
%28 = load i32, i32* %5
%29 = load i64, i64* %4
%30 = call i32 @ftruncate(i32 %28, i64 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 2, i32* %1
br label %70
36: ; preds = %27
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%40 = load i32, i32* @O_RDONLY
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IWUSR
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %39, i32 %40, i32 %43)
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %36
%48 = load i64, i64* @errno
%49 = call i8* @strerror(i64 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %49)
store i32 2, i32* %1
br label %70
51: ; preds = %36
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%53 = call i32 @unlink(i8* %52)
%54 = load i64, i64* %4
%55 = load i32, i32* @PROT_WRITE
%56 = load i32, i32* @MAP_SHARED
%57 = load i32, i32* %5
%58 = call i8* @mmap(i32* null, i64 %54, i32 %55, i32 %56, i32 %57, i32 0)
store i8* %58, i8** %3
%59 = load i8*, i8** %3
%60 = load i8*, i8** @MAP_FAILED
%61 = icmp eq i8* %59, %60
br i1 %61, label %62, label %68
62: ; preds = %51
%63 = load i64, i64* @errno
%64 = load i64, i64* @EACCES
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %70
68: ; preds = %62, %51
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %70
70: ; preds = %68, %66, %47, %32, %23
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
@.str = [21 x i8] c"/tmp/pts_mmap_6_4_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [21 x i8] c"Error at open(): %s\0A\00"
@errno = global i64 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [25 x i8] c"Error at 2nd open(): %s\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EACCES = global i64 0
@str = [86 x i8] c"Maping file with PROT_WRITE, MAP_SHARED have not returned EACCES\0ATest FAILED\00"
@str.6 = [77 x i8] c"EACCES on attempt to map file as PROT_WRITE, MAP_SHARED\0ATest PASSED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @unlink(i8* %2)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %2, i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = load i64, i64* @errno
%18 = call i8* @strerror(i64 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %54
20: ; preds = %0
%21 = call i32 @ftruncate(i32 %14, i64 1024)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %54
27: ; preds = %20
%28 = call i32 @close(i32 %14)
%29 = load i32, i32* @O_RDONLY
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %31, %30
%33 = call i32 @open(i8* %2, i32 %29, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %54
39: ; preds = %27
%40 = call i32 @unlink(i8* %2)
%41 = load i32, i32* @PROT_WRITE
%42 = load i32, i32* @MAP_SHARED
%43 = call i8* @mmap(i32* null, i64 1024, i32 %41, i32 %42, i32 %33, i32 0)
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %52
46: ; preds = %39
%47 = load i64, i64* @errno
%48 = load i64, i64* @EACCES
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([77 x i8], [77 x i8]* @str.6, i64 0, i64 0))
br label %54
52: ; preds = %46, %39
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str, i64 0, i64 0))
br label %54
54: ; preds = %52, %50, %35, %23, %16
%55 = phi i32 [ 2, %16 ], [ 2, %23 ], [ 2, %35 ], [ 0, %50 ], [ 1, %52 ]
ret i32 %55
}