#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 */

/* Variables and functions */
scalar_t__ EBADF;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
scalar_t__ errno;
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  void *pa;
  int fd = -1;
  pa = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (pa == MAP_FAILED && errno == EBADF) {
    printf("Test PASSED\n");
    return 0;
  }
  if (pa == MAP_FAILED)
    perror("mmap()");
  printf("Test FAILED: Did not get EBADF when fd is invalid\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2eab(%rip),%rax
mov    (%rax),%edx
mov    0x2e72(%rip),%rax
or     (%rax),%edx
mov    0x2e89(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <main+0x90>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <main+0x90>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f3 <main+0xc3>
mov    -0x10(%rbp),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <main+0xb0>
lea    0xe32(%rip),%rdi
call   1050 <perror@plt>
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2ebd(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e9b(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x400,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e4a(%rip),%rcx
lea    0xe92(%rip),%rbx
cmp    (%rcx),%rax
je     1182 <main+0x52>
mov    $0x1,%r14d
jmp    119f <main+0x6f>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <main+0x82>
lea    0xe9a(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
jmp    117a <main+0x4a>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@errno = global i64 0
@EBADF = global i64 0
@.str = [13 x i8] c"Test PASSED\0A\00"
@.str.1 = [7 x i8] c"mmap()\00"
@.str.2 = [51 x i8] c"Test FAILED: Did not get EBADF when fd is invalid\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %3
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_SHARED
%8 = load i32, i32* %3
%9 = call i8* @mmap(i32* null, i32 1024, i32 %6, i32 %7, i32 %8, i32 0)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i64, i64* @errno
%15 = load i64, i64* @EBADF
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %27
19: ; preds = %13, %0
%20 = load i8*, i8** %2
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %27
27: ; preds = %25, %17
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@errno = global i64 0
@EBADF = global i64 0
@.str.1 = [7 x i8] c"mmap()\00"
@str = [50 x i8] c"Test FAILED: Did not get EBADF when fd is invalid\00"
@str.3 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_SHARED
%5 = tail call i8* @mmap(i32* null, i32 1024, i32 %3, i32 %4, i32 -1, i32 0)
%6 = load i8*, i8** @MAP_FAILED
%7 = icmp eq i8* %5, %6
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @EBADF
%11 = icmp eq i64 %9, %10
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %0, %8
%15 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @str.3, i64 0, i64 0), %8 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0), %12 ]
%16 = phi i32 [ 0, %8 ], [ 1, %0 ], [ 1, %12 ]
%17 = tail call i32 @puts(i8* dereferenceable(1) %15)
ret i32 %16
}