#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 */
int EBADF;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main() {
  void *p;
  p = mmap(0, 1024 * 1024, PROT_READ, MAP_PRIVATE, -1, 0);
  if (p != MAP_FAILED) {
    fprintf(stderr, "mmap should fail, but returns %p\n", p);
    return 1;
  }
  if (errno != EBADF) {
    fprintf(stderr, "expected EBADF, but got %d\n", errno);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ec2(%rip),%rax
mov    (%rax),%edx
mov    0x2e81(%rip),%rax
mov    (%rax),%ecx
xor    %r9d,%r9d
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     1196 <main+0x76>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xe7b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11df <main+0xbf>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%eax
je     11d8 <main+0xb8>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
lea    0xe5b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11df <main+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ed0(%rip),%rax
mov    (%rax),%edx
mov    0x2e8f(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     116a <main+0x4a>
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%edi
lea    0xe9a(%rip),%rsi
mov    %eax,%edx
jmp    118e <main+0x6e>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    0x2e4e(%rip),%rax
cmp    (%rax),%edx
je     1198 <main+0x78>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [34 x i8] c"mmap should fail, but returns %p\0A\00"
@errno = global i32 0
@EBADF = global i32 0
@.str.1 = [28 x i8] c"expected EBADF, but got %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @MAP_PRIVATE
%5 = call i8* @mmap(i32 0, i32 1048576, i32 %3, i32 %4, i32 -1, i32 0)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = load i8*, i8** @MAP_FAILED
%8 = icmp ne i8* %6, %7
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = ptrtoint i8* %11 to i32
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 1, i32* %1
br label %23
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = load i32, i32* @EBADF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 1, i32* %1
br label %23
22: ; preds = %14
store i32 0, i32* %1
br label %23
23: ; preds = %22, %18, %9
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [34 x i8] c"mmap should fail, but returns %p\0A\00"
@errno = global i32 0
@EBADF = global i32 0
@.str.1 = [28 x i8] c"expected EBADF, but got %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @MAP_PRIVATE
%3 = tail call i8* @mmap(i32 0, i32 1048576, i32 %1, i32 %2, i32 -1, i32 0)
%4 = load i8*, i8** @MAP_FAILED
%5 = icmp eq i8* %3, %4
br i1 %5, label %11, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = ptrtoint i8* %3 to i64
%9 = trunc i64 %8 to i32
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %18
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = load i32, i32* @EBADF
%14 = icmp eq i32 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %18
18: ; preds = %11, %15, %6
%19 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 0, %11 ]
ret i32 %19
}