#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 */
void *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ must_fail(char *, int);
void *must_mmap_file(int size, int prot, int flags, int fd) {
  if (prot == 0)
    prot = PROT_READ;
  if (flags == 0)
    flags = MAP_SHARED;
  void *mem = mmap(NULL, size, prot, flags, fd, 0);
  if (mem == MAP_FAILED)
    must_fail("mmap file", size);
  return mem;
}


x86 assembly (clang, -O0):
<must_mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    114a <must_mmap_file+0x2a>
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1160 <must_mmap_file+0x40>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <must_mmap_file+0x80>
mov    -0x4(%rbp),%esi
lea    0xe65(%rip),%rdi
call   1030 <must_fail@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<must_mmap_file>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%r8d
mov    %edx,%ecx
mov    %esi,%edx
mov    %edi,%ebp
test   %esi,%esi
jne    1139 <must_mmap_file+0x19>
mov    0x2ec1(%rip),%rax
mov    (%rax),%edx
test   %ecx,%ecx
jne    1146 <must_mmap_file+0x26>
mov    0x2ea4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2e74(%rip),%rax
cmp    (%rax),%rbx
jne    116f <must_mmap_file+0x4f>
lea    0xe98(%rip),%rdi
mov    %ebp,%esi
call   1030 <must_fail@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [10 x i8] c"mmap file\00"
define i8* @must_mmap_file(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = load i32, i32* @PROT_READ
store i32 %13, i32* %6
br label %14
14: ; preds = %12, %4
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32, i32* @MAP_SHARED
store i32 %18, i32* %7
br label %19
19: ; preds = %17, %14
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = call i8* @mmap(i32* null, i32 %20, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32, i32* %5
%30 = call i32 @must_fail(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %28, %19
%32 = load i8*, i8** %9
ret i8* %32
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [10 x i8] c"mmap file\00"
define i8* @must_mmap_file(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %1, 0
%6 = load i32, i32* @PROT_READ
%7 = select i1 %5, i32 %6, i32 %1
%8 = icmp eq i32 %2, 0
%9 = load i32, i32* @MAP_SHARED
%10 = select i1 %8, i32 %9, i32 %2
%11 = tail call i8* @mmap(i32* null, i32 %0, i32 %7, i32 %10, i32 %3, i32 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = tail call i32 @must_fail(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %16
16: ; preds = %14, %4
ret i8* %11
}