#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;
typedef int ssize_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int getpid();
int /*<<< orphan*/ memset(char *, char, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
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 write(int, char *, int);
int main(void) {
  char tmpfname[256];
  ssize_t len = 1024;
  char data[len];
  void *pa;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_1_1_%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;
  }
  unlink(tmpfname);
  memset(data, 'a', len);
  if (write(fd, data, len) != len) {
    printf("Error at write(): %s\n", strerror(errno));
    return 2;
  }
  pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (pa == MAP_FAILED) {
    printf("Error at mmap: %s\n", strerror(errno));
    return 1;
  }
  if (*(char *)pa != 'a') {
    printf("Test FAILED: The file was not mapped correctly.\n");
    return 1;
  }
  close(fd);
  munmap(pa, len);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x400,-0x114(%rbp)
mov    -0x114(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x120(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x148(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x140(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdd8(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
mov    0x2d5d(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0xffffffff,-0x134(%rbp)
jne    12ba <main+0x10a>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x138(%rbp)
jmp    1430 <main+0x280>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x148(%rbp),%rdi
mov    -0x114(%rbp),%edx
mov    $0x61,%esi
call   1090 <memset@plt>
mov    -0x148(%rbp),%rsi
mov    -0x134(%rbp),%edi
mov    -0x114(%rbp),%edx
call   1040 <write@plt>
cmp    -0x114(%rbp),%eax
je     1336 <main+0x186>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x138(%rbp)
jmp    1430 <main+0x280>
mov    -0x114(%rbp),%esi
mov    0x2cad(%rip),%rax
mov    (%rax),%edx
mov    0x2c5c(%rip),%rax
or     (%rax),%edx
mov    0x2c8b(%rip),%rax
mov    (%rax),%ecx
mov    -0x134(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rax
mov    0x2c19(%rip),%rcx
cmp    (%rcx),%rax
jne    13bd <main+0x20d>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xca0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x138(%rbp)
jmp    1430 <main+0x280>
mov    -0x130(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
je     13f4 <main+0x244>
lea    0xc7c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x138(%rbp)
jmp    1430 <main+0x280>
mov    -0x134(%rbp),%edi
call   10a0 <close@plt>
mov    -0x130(%rbp),%rdi
mov    -0x114(%rbp),%esi
call   10b0 <munmap@plt>
lea    0xc6c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x138(%rbp)
mov    -0x120(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x500,%rsp
mov    %rsp,%r15
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe23(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1090 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2dfa(%rip),%rax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
mov    0x2d87(%rip),%rax
mov    0x2da8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10d0 <open@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x11d>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
lea    0x100(%rsp),%r14
mov    %r14,%rdi
mov    $0x61,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <write@plt>
cmp    $0x400,%eax
jne    12f4 <main+0x134>
mov    0x2d76(%rip),%rax
mov    0x2d27(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d54(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x400,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%r14
mov    0x2cee(%rip),%rax
cmp    (%rax),%r14
je     1328 <main+0x168>
cmpb   $0x61,(%r14)
jne    134e <main+0x18e>
mov    %ebx,%edi
call   10b0 <close@plt>
mov    %r14,%rdi
mov    $0x400,%esi
call   10c0 <munmap@plt>
lea    0xd7f(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1318 <main+0x158>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd23(%rip),%rdi
jmp    1309 <main+0x149>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd21(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x2,%eax
mov    %r15,%rsp
add    $0x500,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd03(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%eax
jmp    1318 <main+0x158>
lea    0xd0a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    1318 <main+0x158>

LLVM IR (-O0):
@.str = [21 x i8] c"/tmp/pts_mmap_1_1_%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 = [22 x i8] c"Error at write(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [19 x i8] c"Error at mmap: %s\0A\00"
@.str.4 = [49 x i8] c"Test FAILED: The file was not mapped correctly.\0A\00"
@.str.5 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 1024, i32* %3
%9 = load i32, i32* %3
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %13, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%17 = call i32 @unlink(i8* %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = load i32, i32* @O_EXCL
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %18, i32 %23, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %0
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 2, i32* %1
store i32 1, i32* %8
br label %77
34: ; preds = %0
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%36 = call i32 @unlink(i8* %35)
%37 = load i32, i32* %3
%38 = call i32 @memset(i8* %12, i8 signext 97, i32 %37)
%39 = load i32, i32* %7
%40 = load i32, i32* %3
%41 = call i32 @write(i32 %39, i8* %12, i32 %40)
%42 = load i32, i32* %3
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %34
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %46)
store i32 2, i32* %1
store i32 1, i32* %8
br label %77
48: ; preds = %34
%49 = load i32, i32* %3
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @PROT_WRITE
%52 = or i32 %50, %51
%53 = load i32, i32* @MAP_SHARED
%54 = load i32, i32* %7
%55 = call i8* @mmap(i32* null, i32 %49, i32 %52, i32 %53, i32 %54, i32 0)
store i8* %55, i8** %6
%56 = load i8*, i8** %6
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %63
59: ; preds = %48
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %61)
store i32 1, i32* %1
store i32 1, i32* %8
br label %77
63: ; preds = %48
%64 = load i8*, i8** %6
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = icmp ne i32 %66, 97
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %8
br label %77
70: ; preds = %63
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
%73 = load i8*, i8** %6
%74 = load i32, i32* %3
%75 = call i32 @munmap(i8* %73, i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
store i32 1, i32* %8
br label %77
77: ; preds = %70, %68, %59, %44, %30
%78 = load i8*, i8** %4
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@.str = [21 x i8] c"/tmp/pts_mmap_1_1_%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 = [22 x i8] c"Error at write(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [19 x i8] c"Error at mmap: %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.6 = [48 x i8] c"Test FAILED: The file was not mapped correctly.\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 i8* @llvm.stacksave()
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @unlink(i8* %2)
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %2, i32 %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %0
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %53
23: ; preds = %0
%24 = call i32 @unlink(i8* %2)
%25 = call i32 @memset(i8* %5, i8 signext 97, i32 1024)
%26 = call i32 @write(i32 %17, i8* %5, i32 1024)
%27 = icmp eq i32 %26, 1024
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %53
32: ; preds = %23
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = call i8* @mmap(i32* null, i32 1024, i32 %35, i32 %36, i32 %17, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %44
40: ; preds = %32
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %53
44: ; preds = %32
%45 = load i8, i8* %37
%46 = icmp eq i8 %45, 97
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
br label %53
49: ; preds = %44
%50 = call i32 @close(i32 %17)
%51 = call i32 @munmap(i8* %37, i32 1024)
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %53
53: ; preds = %49, %47, %40, %28, %19
%54 = phi i32 [ 2, %19 ], [ 2, %28 ], [ 1, %40 ], [ 1, %47 ], [ 0, %49 ]
ret i32 %54
}