#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 /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PATH_MAX;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int getpid();
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *pts_get_tmpdir();
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[PATH_MAX];
  void *pa;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname),
           "%s/"
           "pts_mmap_32_1"
           "_pid-%d",
           pts_get_tmpdir(), 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);
  pa = mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (pa == MAP_FAILED && errno == EINVAL) {
    printf("Got EINVAL\n");
    printf("Test PASSED\n");
    close(fd);
    return 0;
  }
  if (pa == MAP_FAILED)
    printf("Test FAILED: Expected EINVAL got %s\n", strerror(errno));
  else
    printf("Test FAILED: mmap() succedded unexpectedly\n");
  close(fd);
  munmap(pa, 0);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1090 <pts_get_tmpdir@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rdi
mov    %eax,%r8d
lea    0xdfb(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
mov    0x2d7e(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1289 <main+0xe9>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1391 <main+0x1f1>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
mov    0x2cfe(%rip),%rax
or     (%rax),%edx
mov    0x2d2d(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%esi
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2cb9(%rip),%rcx
cmp    (%rcx),%rax
jne    1329 <main+0x189>
mov    0x2cc9(%rip),%rax
mov    (%rax),%rax
mov    0x2ca7(%rip),%rcx
cmp    (%rcx),%rax
jne    1329 <main+0x189>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1391 <main+0x1f1>
mov    -0x20(%rbp),%rax
mov    0x2c54(%rip),%rcx
cmp    (%rcx),%rax
jne    1362 <main+0x1c2>
mov    0x2c64(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1370 <main+0x1d0>
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   10a0 <munmap@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e1f(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   10a0 <pts_get_tmpdir@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe10(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rcx
mov    %eax,%r8d
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2de8(%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   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1295 <main+0xe5>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2d9b(%rip),%rax
mov    0x2d44(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d71(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
xor    %esi,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2d06(%rip),%rax
cmp    (%rax),%rbx
je     12bc <main+0x10c>
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
jmp    130c <main+0x15c>
mov    0x2d0c(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd6d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    1323 <main+0x173>
mov    0x2ce5(%rip),%rax
mov    (%rax),%rdi
mov    0x2cc3(%rip),%rax
cmp    (%rax),%rdi
jne    12f6 <main+0x146>
lea    0xda4(%rip),%rdi
call   1040 <puts@plt>
lea    0xda3(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    1323 <main+0x173>
call   10d0 <strerror@plt>
lea    0xd2b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <munmap@plt>
mov    $0x1,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [24 x i8] c"%s/pts_mmap_32_1_pid-%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
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EINVAL = global i64 0
@.str.2 = [12 x i8] c"Got EINVAL\0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [37 x i8] c"Test FAILED: Expected EINVAL got %s\0A\00"
@.str.5 = [44 x i8] c"Test FAILED: mmap() succedded unexpectedly\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %2
%10 = alloca i8, i64 %8
store i64 %8, i64* %3
%11 = trunc i64 %8 to i32
%12 = call i8* (...) @pts_get_tmpdir()
%13 = call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13)
%15 = call i32 @unlink(i8* %10)
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %16, %17
%19 = load i32, i32* @O_EXCL
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %10, i32 %20, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %0
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 2, i32* %1
store i32 1, i32* %6
br label %66
31: ; preds = %0
%32 = call i32 @unlink(i8* %10)
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %33, %34
%36 = load i32, i32* @MAP_SHARED
%37 = load i32, i32* %5
%38 = call i8* @mmap(i32* null, i32 0, i32 %35, i32 %36, i32 %37, i32 0)
store i8* %38, i8** %4
%39 = load i8*, i8** %4
%40 = load i8*, i8** @MAP_FAILED
%41 = icmp eq i8* %39, %40
br i1 %41, label %42, label %51
42: ; preds = %31
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINVAL
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
store i32 0, i32* %1
store i32 1, i32* %6
br label %66
51: ; preds = %42, %31
%52 = load i8*, i8** %4
%53 = load i8*, i8** @MAP_FAILED
%54 = icmp eq i8* %52, %53
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i64, i64* @errno
%57 = call i8* @strerror(i64 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %57)
br label %61
59: ; preds = %51
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %59, %55
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
%64 = load i8*, i8** %4
%65 = call i32 @munmap(i8* %64, i32 0)
store i32 1, i32* %1
store i32 1, i32* %6
br label %66
66: ; preds = %61, %46, %27
%67 = load i8*, i8** %2
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [24 x i8] c"%s/pts_mmap_32_1_pid-%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
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EINVAL = global i64 0
@.str.4 = [37 x i8] c"Test FAILED: Expected EINVAL got %s\0A\00"
@str = [43 x i8] c"Test FAILED: mmap() succedded unexpectedly\00"
@str.6 = [11 x i8] c"Got EINVAL\00"
@str.7 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i8* (...) @pts_get_tmpdir()
%5 = tail call i32 (...) @getpid()
%6 = call i32 @snprintf(i8* %3, i32 %1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %5)
%7 = call i32 @unlink(i8* %3)
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = load i32, i32* @O_EXCL
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = call i32 @open(i8* %3, i32 %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %0
%19 = load i64, i64* @errno
%20 = call i8* @strerror(i64 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %47
22: ; preds = %0
%23 = call i32 @unlink(i8* %3)
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %25, %24
%27 = load i32, i32* @MAP_SHARED
%28 = call i8* @mmap(i32* null, i32 0, i32 %26, i32 %27, i32 %16, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %42
31: ; preds = %22
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINVAL
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%38 = call i32 @close(i32 %16)
br label %47
39: ; preds = %31
%40 = call i8* @strerror(i64 %32)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %40)
br label %44
42: ; preds = %22
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = call i32 @close(i32 %16)
%46 = call i32 @munmap(i8* %28, i32 0)
br label %47
47: ; preds = %44, %35, %18
%48 = phi i32 [ 2, %18 ], [ 0, %35 ], [ 1, %44 ]
ret i32 %48
}