#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
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);
int /*<<< orphan*/ errno;
int ftruncate(int, size_t);
int getpid();
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int /*<<< orphan*/);
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_5_%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_PRIVATE, fd, 0);
  if (pa == MAP_FAILED) {
    printf("Error at mmap(): %s\n", strerror(errno));
    return 1;
  }
  munmap(pa, size);
  close(fd);
  printf("Succesfully mapped readonly file with "
         "PROT_WRITE, MAP_PRIVATE\n"
         "Test PASSED\n");
  return 0;
}


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    0xe19(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    0x2da6(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    126f <main+0xcf>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x239>
mov    -0x124(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0x115>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x239>
mov    -0x124(%rbp),%edi
call   1090 <close@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
mov    0x2cd1(%rip),%rax
mov    (%rax),%edx
mov    0x2cf0(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    1325 <main+0x185>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x239>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x120(%rbp),%rsi
mov    0x2c71(%rip),%rax
mov    (%rax),%edx
mov    0x2c58(%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    0x2c26(%rip),%rcx
cmp    (%rcx),%rax
jne    13a6 <main+0x206>
mov    0x2c36(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x239>
mov    -0x118(%rbp),%rdi
mov    -0x120(%rbp),%rsi
call   10a0 <munmap@plt>
mov    -0x124(%rbp),%edi
call   1090 <close@plt>
lea    0xca7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe37(%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    0x2e0e(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc4(%rip),%rax
or     (%rax),%esi
mov    0x2da3(%rip),%r14
mov    0x2dc4(%rip),%rbp
mov    0x0(%rbp),%edx
or     (%r14),%edx
mov    %rbx,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12c3 <main+0x113>
mov    %eax,%ebx
mov    $0x400,%esi
mov    %eax,%edi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12da <main+0x12a>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x0(%rbp),%edx
or     (%r14),%edx
mov    %rsp,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12f1 <main+0x141>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
mov    0x2d25(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
mov    $0x400,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    0x2d00(%rip),%rcx
cmp    (%rcx),%rax
je     1323 <main+0x173>
mov    $0x400,%esi
mov    %rax,%rdi
call   10b0 <munmap@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
lea    0xdb6(%rip),%rdi
call   1040 <puts@plt>
jmp    1315 <main+0x165>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd3d(%rip),%rdi
jmp    1306 <main+0x156>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd3b(%rip),%rdi
jmp    1306 <main+0x156>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd3e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd25(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
jmp    1315 <main+0x165>

LLVM IR (-O0):
@.str = [21 x i8] c"/tmp/pts_mmap_6_5_%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 i32 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_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [21 x i8] c"Error at mmap(): %s\0A\00"
@.str.5 = [75 x i8] c"Succesfully mapped file with PROT_WRITE, MAP_PRIVATE\0ATest PASSED\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 i32, i32* @errno
%25 = call i8* @strerror(i32 %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 %73
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 i32, i32* @errno
%34 = call i8* @strerror(i32 %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 %73
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 i32, i32* @errno
%49 = call i8* @strerror(i32 %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 %73
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_PRIVATE
%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 %66
62: ; preds = %51
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %64)
store i32 1, i32* %1
br label %73
66: ; preds = %51
%67 = load i8*, i8** %3
%68 = load i64, i64* %4
%69 = call i32 @munmap(i8* %67, i64 %68)
%70 = load i32, i32* %5
%71 = call i32 @close(i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %73
73: ; preds = %66, %62, %47, %32, %23
%74 = load i32, i32* %1
ret i32 %74
}

LLVM IR (-O2):
@.str = [21 x i8] c"/tmp/pts_mmap_6_5_%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 i32 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_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [21 x i8] c"Error at mmap(): %s\0A\00"
@str = [74 x i8] c"Succesfully mapped file with PROT_WRITE, MAP_PRIVATE\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 i32, i32* @errno
%18 = call i8* @strerror(i32 %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 i32, i32* @errno
%25 = call i8* @strerror(i32 %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 i32, i32* @errno
%37 = call i8* @strerror(i32 %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_PRIVATE
%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 %50
46: ; preds = %39
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %48)
br label %54
50: ; preds = %39
%51 = call i32 @munmap(i8* %43, i64 1024)
%52 = call i32 @close(i32 %33)
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([74 x i8], [74 x i8]* @str, i64 0, i64 0))
br label %54
54: ; preds = %50, %46, %35, %23, %16
%55 = phi i32 [ 2, %16 ], [ 2, %23 ], [ 2, %35 ], [ 1, %46 ], [ 0, %50 ]
ret i32 %55
}