#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 scalar_t__ 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 /*<<< orphan*/ free(char *);
int getpid();
char *malloc(scalar_t__);
int /*<<< orphan*/ memset(char *, char, scalar_t__);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, scalar_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 *);
scalar_t__ write(int, char *, scalar_t__);
int main(void) {
  char tmpfname[256];
  char *data;
  void *pa;
  ssize_t len = 1024;
  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);
  data = malloc(len);
  memset(data, 'a', len);
  if (write(fd, data, len) != len) {
    printf("Error at write(): %s\n", strerror(errno));
    return 2;
  }
  free(data);
  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    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x400,-0x128(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x138(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xde9(%rip),%rdx
call   1090 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
mov    0x2d70(%rip),%rax
or     (%rax),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
mov    0x2d6e(%rip),%rax
or     (%rax),%edx
call   10e0 <open@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0xffffffff,-0x12c(%rbp)
jne    129f <main+0xcf>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1411 <main+0x241>
lea    -0x110(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x128(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
mov    -0x128(%rbp),%rdx
mov    $0x61,%esi
call   10a0 <memset@plt>
mov    -0x12c(%rbp),%edi
mov    -0x118(%rbp),%rsi
mov    -0x128(%rbp),%rdx
call   1050 <write@plt>
cmp    -0x128(%rbp),%rax
je     1327 <main+0x157>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1411 <main+0x241>
mov    -0x118(%rbp),%rdi
call   1030 <free@plt>
mov    -0x128(%rbp),%rsi
mov    0x2caf(%rip),%rax
mov    (%rax),%edx
mov    0x2c5e(%rip),%rax
or     (%rax),%edx
mov    0x2c8d(%rip),%rax
mov    (%rax),%ecx
mov    -0x12c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    0x2c1b(%rip),%rcx
cmp    (%rcx),%rax
jne    13b1 <main+0x1e1>
mov    0x2c23(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xca2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1411 <main+0x241>
mov    -0x120(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
je     13de <main+0x20e>
lea    0xc88(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1411 <main+0x241>
mov    -0x12c(%rbp),%edi
call   10b0 <close@plt>
mov    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   10d0 <munmap@plt>
lea    0xc81(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1070 <getpid@plt>
lea    0xe09(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   10a0 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
mov    0x2de0(%rip),%rax
mov    0x2db1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d8e(%rip),%rax
or     (%rax),%esi
mov    0x2d6d(%rip),%rax
mov    0x2d8e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     1304 <main+0x124>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1040 <unlink@plt>
mov    $0x400,%edi
call   10d0 <malloc@plt>
mov    %rax,%rbx
mov    $0x400,%edx
mov    %rax,%rdi
mov    $0x61,%esi
call   10b0 <memset@plt>
mov    $0x400,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <write@plt>
cmp    $0x400,%rax
jne    131b <main+0x13b>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d4e(%rip),%rax
mov    0x2cff(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d2c(%rip),%rax
mov    (%rax),%ecx
mov    $0x400,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1080 <mmap@plt>
mov    %rax,%rbx
mov    0x2cc6(%rip),%rax
cmp    (%rax),%rbx
je     1349 <main+0x169>
cmpb   $0x61,(%rbx)
jne    136f <main+0x18f>
mov    %ebp,%edi
call   10c0 <close@plt>
mov    $0x400,%esi
mov    %rbx,%rdi
call   10e0 <munmap@plt>
lea    0xd58(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
jmp    133f <main+0x15f>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xcfc(%rip),%rdi
jmp    1330 <main+0x150>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xcfa(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x2,%eax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xce2(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x1,%eax
jmp    133f <main+0x15f>
lea    0xce9(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
jmp    133f <main+0x15f>

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 i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
store i64 1024, i64* %5
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = call i32 @unlink(i8* %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDWR
%15 = or i32 %13, %14
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %12, i32 %17, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %0
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 2, i32* %1
br label %77
28: ; preds = %0
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%30 = call i32 @unlink(i8* %29)
%31 = load i64, i64* %5
%32 = call i8* @malloc(i64 %31)
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = load i64, i64* %5
%35 = call i32 @memset(i8* %33, i8 signext 97, i64 %34)
%36 = load i32, i32* %6
%37 = load i8*, i8** %3
%38 = load i64, i64* %5
%39 = call i64 @write(i32 %36, i8* %37, i64 %38)
%40 = load i64, i64* %5
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %28
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 2, i32* %1
br label %77
46: ; preds = %28
%47 = load i8*, i8** %3
%48 = call i32 @free(i8* %47)
%49 = load i64, i64* %5
%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* %6
%55 = call i8* @mmap(i32* null, i64 %49, i32 %52, i32 %53, i32 %54, i32 0)
store i8* %55, i8** %4
%56 = load i8*, i8** %4
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %63
59: ; preds = %46
%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
br label %77
63: ; preds = %46
%64 = load i8*, i8** %4
%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
br label %77
70: ; preds = %63
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
%73 = load i8*, i8** %4
%74 = load i64, i64* %5
%75 = call i32 @munmap(i8* %73, i64 %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
br label %77
77: ; preds = %70, %68, %59, %42, %24
%78 = load i32, i32* %1
ret i32 %78
}

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 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 %52
20: ; preds = %0
%21 = call i32 @unlink(i8* %2)
%22 = call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%23 = call i32 @memset(i8* %22, i8 signext 97, i64 1024)
%24 = call i64 @write(i32 %14, i8* %22, i64 1024)
%25 = icmp eq i64 %24, 1024
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %52
30: ; preds = %20
%31 = call i32 @free(i8* %22)
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %33, %32
%35 = load i32, i32* @MAP_SHARED
%36 = call i8* @mmap(i32* null, i64 1024, i32 %34, i32 %35, i32 %14, i32 0)
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %43
39: ; preds = %30
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %52
43: ; preds = %30
%44 = load i8, i8* %36
%45 = icmp eq i8 %44, 97
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
br label %52
48: ; preds = %43
%49 = call i32 @close(i32 %14)
%50 = call i32 @munmap(i8* %36, i64 1024)
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %48, %46, %39, %26, %16
%53 = phi i32 [ 2, %16 ], [ 2, %26 ], [ 1, %39 ], [ 1, %46 ], [ 0, %48 ]
ret i32 %53
}