#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 void FILE;

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fileno(void *);
void *fopen(char const *, char *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int /*<<< orphan*/);
int syn_file_length(void *);
void *syn_mmapfile(const char *filename, int *n) {
  FILE *fd = fopen(filename, "r");
  if (!fd)
    return fd;
  *n = syn_file_length(fd);
  return (mmap(NULL, *n, PROT_READ, MAP_PRIVATE, fileno(fd), 0));
}


x86 assembly (clang, -O0):
<syn_mmapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117c <syn_mmapfile+0x3c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d3 <syn_mmapfile+0x93>
mov    -0x20(%rbp),%rdi
call   1030 <syn_file_length@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fileno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syn_mmapfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a6 <syn_mmapfile+0x66>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <syn_file_length@plt>
mov    %eax,%ebp
mov    %eax,(%r14)
mov    0x2e86(%rip),%rax
mov    (%rax),%r14d
mov    0x2e5c(%rip),%rax
mov    (%rax),%r15d
mov    %rbx,%rdi
call   1050 <fileno@plt>
xor    %edi,%edi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <mmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @syn_mmapfile(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%7 = load i8*, i8** %4
%8 = call i8* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = load i8*, i8** %6
store i8* %12, i8** %3
br label %24
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = call i32 @syn_file_length(i8* %14)
%16 = load i32*, i32** %5
store i32 %15, i32* %16
%17 = load i32*, i32** %5
%18 = load i32, i32* %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_PRIVATE
%21 = load i8*, i8** %6
%22 = call i32 @fileno(i8* %21)
%23 = call i8* @mmap(i32* null, i32 %18, i32 %19, i32 %20, i32 %22, i32 0)
store i8* %23, i8** %3
br label %24
24: ; preds = %13, %11
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @syn_mmapfile(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = tail call i8* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = tail call i32 @syn_file_length(i8* %3)
store i32 %6, i32* %1
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @MAP_PRIVATE
%9 = tail call i32 @fileno(i8* %3)
%10 = tail call i8* @mmap(i32* null, i32 %6, i32 %7, i32 %8, i32 %9, i32 0)
br label %11
11: ; preds = %2, %5
%12 = phi i8* [ %10, %5 ], [ null, %2 ]
ret i8* %12
}