#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__ EINVAL;
void *MAP_FAILED;
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);
scalar_t__ errno;
int ftruncate(int, size_t);
int getpid();
void *mmap(int /*<<< orphan*/ *, size_t, int, int, 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(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[256];
  void *pa;
  size_t size = 1024;
  int flag = ~0;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_21_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);
  if (ftruncate(fd, size) == -1) {
    printf("Error at ftruncate(): %s\n", strerror(errno));
    return 2;
  }
  pa = mmap(NULL, size, PROT_READ | PROT_WRITE, flag, fd, 0);
  if (pa == MAP_FAILED && errno == EINVAL) {
    printf("Test PASSED\n");
    return 0;
  }
  close(fd);
  munmap(pa, size);
  printf("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)
movl   $0xffffffff,-0x124(%rbp)
ff ff ff
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    0xe0f(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
mov    0x2d9e(%rip),%rax
or     (%rax),%esi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
mov    0x2d9c(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0xffffffff,-0x128(%rbp)
jne    127a <main+0xda>
mov    0x2d63(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1384 <main+0x1e4>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x128(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12cd <main+0x12d>
mov    0x2d10(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1384 <main+0x1e4>
mov    -0x120(%rbp),%rsi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
mov    0x2ccc(%rip),%rax
or     (%rax),%edx
mov    -0x124(%rbp),%ecx
mov    -0x128(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2c84(%rip),%rcx
cmp    (%rcx),%rax
jne    1351 <main+0x1b1>
mov    0x2c94(%rip),%rax
mov    (%rax),%rax
mov    0x2c72(%rip),%rcx
cmp    (%rcx),%rax
jne    1351 <main+0x1b1>
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1384 <main+0x1e4>
mov    -0x128(%rbp),%edi
call   1090 <close@plt>
mov    -0x118(%rbp),%rdi
mov    -0x120(%rbp),%rsi
call   10a0 <munmap@plt>
lea    0xcdc(%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   %r14
push   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe38(%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    0x2e0f(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc5(%rip),%rax
or     (%rax),%esi
mov    0x2da4(%rip),%rax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1299 <main+0xe9>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    $0x400,%esi
mov    %ebx,%edi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12d8 <main+0x128>
mov    0x2dae(%rip),%rax
mov    0x2d67(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x400,%esi
xor    %edi,%edi
mov    $0xffffffff,%ecx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%r14
mov    0x2d2a(%rip),%rax
cmp    (%rax),%r14
jne    12b1 <main+0x101>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    0x2d1c(%rip),%rcx
cmp    (%rcx),%rax
jne    12b1 <main+0x101>
lea    0xdc1(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    12fd <main+0x14d>
mov    0x2d18(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd67(%rip),%rdi
jmp    12ee <main+0x13e>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    $0x400,%esi
mov    %r14,%rdi
call   10b0 <munmap@plt>
lea    0xd79(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12fd <main+0x14d>
mov    0x2cd9(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd3d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/tmp/pts_mmap_21_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 i64 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@EINVAL = global i64 0
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [13 x i8] c"Test FAILED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i64 1024, i64* %4
store i32 -1, i32* %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 ([22 x i8], [22 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 i64, i64* @errno
%26 = call i8* @strerror(i64 %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 %63
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 i32, i32* %6
%32 = load i64, i64* %4
%33 = call i32 @ftruncate(i32 %31, i64 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %37)
store i32 2, i32* %1
br label %63
39: ; preds = %28
%40 = load i64, i64* %4
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* %5
%45 = load i32, i32* %6
%46 = call i8* @mmap(i32* null, i64 %40, i32 %43, i32 %44, i32 %45, i32 0)
store i8* %46, i8** %3
%47 = load i8*, i8** %3
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %56
50: ; preds = %39
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINVAL
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %63
56: ; preds = %50, %39
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i8*, i8** %3
%60 = load i64, i64* %4
%61 = call i32 @munmap(i8* %59, i64 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %63
63: ; preds = %56, %54, %35, %24
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
@.str = [22 x i8] c"/tmp/pts_mmap_21_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 i64 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@EINVAL = global i64 0
@str = [12 x i8] c"Test FAILED\00"
@str.5 = [12 x i8] c"Test 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 ([22 x i8], [22 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 %45
20: ; preds = %0
%21 = call i32 @unlink(i8* %2)
%22 = call i32 @ftruncate(i32 %14, i64 1024)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %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 %45
28: ; preds = %20
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %30, %29
%32 = call i8* @mmap(i32* null, i64 1024, i32 %31, i32 -1, i32 %14, i32 0)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %41
35: ; preds = %28
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINVAL
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %45
41: ; preds = %35, %28
%42 = call i32 @close(i32 %14)
%43 = call i32 @munmap(i8* %32, i64 1024)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %41, %39, %24, %16
%46 = phi i32 [ 2, %16 ], [ 2, %24 ], [ 0, %39 ], [ 1, %41 ]
ret i32 %46
}