#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_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 ftruncate(int, size_t);
int getpid();
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(void) {
  char tmpfname[256];
  void *pa;
  size_t size = 1024 * 4 * 1024;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_1_2_%d", getpid());
  shm_unlink(tmpfname);
  fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("Error at shm_open(): %s\n", strerror(errno));
    return 2;
  }
  shm_unlink(tmpfname);
  if (ftruncate(fd, size) == -1) {
    printf("Error at ftruncate(): %s\n", strerror(errno));
    return 2;
  }
  if (write(fd, "a", 1) != 1) {
    printf("Error at write(): %s\n", strerror(errno));
    return 2;
  }
  pa = mmap(NULL, size, 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, size);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x400000,-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    0xe09(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   10d0 <shm_unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    0x2d90(%rip),%rax
or     (%rax),%esi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
mov    0x2d8e(%rip),%rax
or     (%rax),%edx
call   10b0 <shm_open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    127f <main+0xcf>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13fa <main+0x24a>
lea    -0x110(%rbp),%rdi
call   10d0 <shm_unlink@plt>
mov    -0x124(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12d1 <main+0x121>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13fa <main+0x24a>
mov    -0x124(%rbp),%edi
lea    0xd65(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     131c <main+0x16c>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13fa <main+0x24a>
mov    -0x120(%rbp),%rsi
mov    0x2cc6(%rip),%rax
mov    (%rax),%edx
mov    0x2c75(%rip),%rax
or     (%rax),%edx
mov    0x2ca4(%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    0x2c32(%rip),%rcx
cmp    (%rcx),%rax
jne    139a <main+0x1ea>
mov    0x2c3a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13fa <main+0x24a>
mov    -0x118(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
je     13c7 <main+0x217>
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13fa <main+0x24a>
mov    -0x124(%rbp),%edi
call   1090 <close@plt>
mov    -0x118(%rbp),%rdi
mov    -0x120(%rbp),%rsi
call   10a0 <munmap@plt>
lea    0xcb3(%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   %r14
push   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe28(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   10e0 <shm_unlink@plt>
mov    0x2dd7(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dad(%rip),%rax
or     (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    0x2dad(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10c0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12d9 <main+0x119>
mov    %eax,%ebx
mov    %rsp,%rdi
call   10e0 <shm_unlink@plt>
mov    $0x400000,%esi
mov    %ebx,%edi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0x130>
lea    0xded(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1307 <main+0x147>
mov    0x2d7e(%rip),%rax
mov    0x2d2f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d5c(%rip),%rax
mov    (%rax),%ecx
mov    $0x400000,%esi
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%r14
mov    0x2cf6(%rip),%rax
cmp    (%rax),%r14
je     1336 <main+0x176>
cmpb   $0x61,(%r14)
jne    135c <main+0x19c>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    $0x400000,%esi
mov    %r14,%rdi
call   10b0 <munmap@plt>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    132b <main+0x16b>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd22(%rip),%rdi
jmp    131c <main+0x15c>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd24(%rip),%rdi
jmp    131c <main+0x15c>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd29(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd10(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
jmp    132b <main+0x16b>
lea    0xd17(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    132b <main+0x16b>

LLVM IR (-O0):
@.str = [16 x i8] c"pts_mmap_1_2_%d\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [25 x i8] c"Error at shm_open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [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.5 = [19 x i8] c"Error at mmap: %s\0A\00"
@.str.6 = [49 x i8] c"Test FAILED: The file was not mapped correctly.\0A\00"
@.str.7 = [13 x i8] c"Test 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 4194304, 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 ([16 x i8], [16 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 @shm_unlink(i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%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 @shm_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 ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 2, i32* %1
br label %75
27: ; preds = %0
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%29 = call i32 @shm_unlink(i8* %28)
%30 = load i32, i32* %5
%31 = load i64, i64* %4
%32 = call i32 @ftruncate(i32 %30, i64 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 2, i32* %1
br label %75
38: ; preds = %27
%39 = load i32, i32* %5
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %46
42: ; preds = %38
%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.4, i64 0, i64 0), i8* %44)
store i32 2, i32* %1
br label %75
46: ; preds = %38
%47 = load i64, i64* %4
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @PROT_WRITE
%50 = or i32 %48, %49
%51 = load i32, i32* @MAP_SHARED
%52 = load i32, i32* %5
%53 = call i8* @mmap(i32* null, i64 %47, i32 %50, i32 %51, i32 %52, i32 0)
store i8* %53, i8** %3
%54 = load i8*, i8** %3
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %61
57: ; preds = %46
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %59)
store i32 1, i32* %1
br label %75
61: ; preds = %46
%62 = load i8*, i8** %3
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 97
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %75
68: ; preds = %61
%69 = load i32, i32* %5
%70 = call i32 @close(i32 %69)
%71 = load i8*, i8** %3
%72 = load i64, i64* %4
%73 = call i32 @munmap(i8* %71, i64 %72)
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %75
75: ; preds = %68, %66, %57, %42, %34, %23
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
@.str = [16 x i8] c"pts_mmap_1_2_%d\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [25 x i8] c"Error at shm_open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [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.5 = [19 x i8] c"Error at mmap: %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.8 = [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 ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @shm_unlink(i8* %2)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%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 @shm_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 ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %56
20: ; preds = %0
%21 = call i32 @shm_unlink(i8* %2)
%22 = call i32 @ftruncate(i32 %14, i64 4194304)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %56
28: ; preds = %20
%29 = call i32 @write(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%30 = icmp eq i32 %29, 1
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %56
35: ; preds = %28
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %37, %36
%39 = load i32, i32* @MAP_SHARED
%40 = call i8* @mmap(i32* null, i64 4194304, i32 %38, i32 %39, i32 %14, i32 0)
%41 = load i8*, i8** @MAP_FAILED
%42 = icmp eq i8* %40, %41
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %45)
br label %56
47: ; preds = %35
%48 = load i8, i8* %40
%49 = icmp eq i8 %48, 97
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.8, i64 0, i64 0))
br label %56
52: ; preds = %47
%53 = call i32 @close(i32 %14)
%54 = call i32 @munmap(i8* %40, i64 4194304)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %52, %50, %43, %31, %24, %16
%57 = phi i32 [ 2, %16 ], [ 2, %24 ], [ 2, %31 ], [ 1, %43 ], [ 1, %50 ], [ 0, %52 ]
ret i32 %57
}