#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 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 *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[256];
  void *pa;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_31_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);
  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    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe34(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    0x2dbb(%rip),%rax
or     (%rax),%esi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0xffffffff,-0x11c(%rbp)
jne    1255 <main+0xc5>
mov    0x2d80(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1367 <main+0x1d7>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
mov    0x2d37(%rip),%rax
or     (%rax),%edx
mov    0x2d66(%rip),%rax
mov    (%rax),%ecx
mov    -0x11c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%esi
call   1050 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2ce9(%rip),%rcx
cmp    (%rcx),%rax
jne    12fd <main+0x16d>
mov    0x2cf9(%rip),%rax
mov    (%rax),%rax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%rax
jne    12fd <main+0x16d>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x11c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1367 <main+0x1d7>
mov    -0x118(%rbp),%rax
mov    0x2c85(%rip),%rcx
cmp    (%rcx),%rax
jne    1339 <main+0x1a9>
mov    0x2c95(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1347 <main+0x1b7>
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x11c(%rbp),%edi
call   1080 <close@plt>
mov    -0x118(%rbp),%rdi
xor    %esi,%esi
call   1090 <munmap@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    0xe48(%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    0x2e1f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcd(%rip),%rax
or     (%rax),%esi
mov    0x2dac(%rip),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     125d <main+0xbd>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    0x2dd3(%rip),%rax
mov    0x2d84(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
xor    %esi,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%r14
mov    0x2d46(%rip),%rax
cmp    (%rax),%r14
je     1284 <main+0xe4>
lea    0xdfa(%rip),%rdi
call   1040 <puts@plt>
jmp    12d3 <main+0x133>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xda3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    12e9 <main+0x149>
mov    0x2d25(%rip),%rax
mov    (%rax),%rdi
mov    0x2d03(%rip),%rax
cmp    (%rax),%rdi
jne    12bd <main+0x11d>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    12e9 <main+0x149>
call   10c0 <strerror@plt>
lea    0xd62(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <munmap@plt>
mov    $0x1,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

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

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