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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int file_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *map_file(FILE *f, int len, int off) {
  char *r;
  if (!f) {
    fprintf(stderr, "map_file: cannot map null file");
    return NULL;
  }
  if (len == 0) {
    len = file_size(f);
  }
  r = mmap(NULL, len, PROT_READ, MAP_SHARED, fileno(f), off);
  if (!r) {
    fprintf(stderr, "map_file: cannot map file\n");
    perror("map_file");
  }
  fclose(f);
  return r;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119f <map_file+0x3f>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1236 <map_file+0xd6>
cmpl   $0x0,-0x14(%rbp)
jne    11b5 <map_file+0x55>
mov    -0x10(%rbp),%rdi
call   1050 <file_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %eax,%r8d
mov    -0x18(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1225 <map_file+0xc5>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     11c6 <map_file+0x66>
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
test   %esi,%esi
jne    1183 <map_file+0x23>
mov    %rbx,%rdi
call   1050 <file_size@plt>
mov    %eax,%ebp
mov    0x2e6e(%rip),%rax
mov    (%rax),%r15d
mov    0x2e54(%rip),%rax
mov    (%rax),%r12d
mov    %rbx,%rdi
call   1070 <fileno@plt>
xor    %edi,%edi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r12d,%ecx
mov    %eax,%r8d
mov    %r14d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11e9 <map_file+0x89>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11dd <map_file+0x7d>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
jmp    11bc <map_file+0x5c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"map_file: cannot map null file\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [27 x i8] c"map_file: cannot map file\0A\00"
@.str.2 = [9 x i8] c"map_file\00"
define i8* @map_file(i32* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %38
14: ; preds = %3
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32*, i32** %5
%19 = call i32 @file_size(i32* %18)
store i32 %19, i32* %6
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %6
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @MAP_SHARED
%24 = load i32*, i32** %5
%25 = call i32 @fileno(i32* %24)
%26 = load i32, i32* %7
%27 = call i8* @mmap(i32* null, i32 %21, i32 %22, i32 %23, i32 %25, i32 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %30, %20
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %8
store i8* %37, i8** %4
br label %38
38: ; preds = %34, %11
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"map_file: cannot map null file\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [27 x i8] c"map_file: cannot map file\0A\00"
@.str.2 = [9 x i8] c"map_file\00"
define i8* @map_file(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32* %0, null
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %25
8: ; preds = %3
%9 = icmp eq i32 %1, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @file_size(i32* %0)
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ %11, %10 ], [ %1, %8 ]
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i32 @fileno(i32* %0)
%17 = tail call i8* @mmap(i32* null, i32 %13, i32 %14, i32 %15, i32 %16, i32 %2)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %19, %12
%24 = tail call i32 @fclose(i32* %0)
br label %25
25: ; preds = %23, %5
%26 = phi i8* [ %17, %23 ], [ null, %5 ]
ret i8* %26
}