#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 MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
void *mmap(void *, size_t, int, int, int, long);
int open(char *, int /*<<< orphan*/);
void *map_file(void *start, size_t length, int prot, int flags, char *name,
               long offset) {
  void *m;
  int fd;
  fd = open(name, (flags & MAP_SHARED) ? O_RDWR : O_RDONLY);
  if (fd == -1) {
    return NULL;
  }
  m = mmap(start, length, prot, flags, fd, offset);
  if (m == (void *)-1) {
    close(fd);
    return NULL;
  }
  close(fd);
  return m;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e90(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     117c <map_file+0x4c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    1188 <map_file+0x58>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11ae <map_file+0x7e>
movq   $0x0,-0x8(%rbp)
jmp    1203 <map_file+0xd3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
mov    -0x30(%rbp),%r9
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x38(%rbp)
jne    11f3 <map_file+0xc3>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1203 <map_file+0xd3>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r14
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r13
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
and    %ecx,%eax
jne    115f <map_file+0x2f>
mov    0x2e7b(%rip),%rax
jmp    1166 <map_file+0x36>
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    %r8,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a3 <map_file+0x73>
mov    %eax,%ebx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
mov    %r14,%r9
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
cmp    $0xffffffffffffffff,%rbp
cmovne %rbp,%rax
jmp    11a5 <map_file+0x75>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAP_SHARED = global i32 0
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
define i8* @map_file(i8* %0, i64 %1, i32 %2, i32 %3, i8* %4, i64 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
store i8* %0, i8** %8
store i64 %1, i64* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
%16 = load i8*, i8** %12
%17 = load i32, i32* %11
%18 = load i32, i32* @MAP_SHARED
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %6
%22 = load i32, i32* @O_RDWR
br label %25
23: ; preds = %6
%24 = load i32, i32* @O_RDONLY
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %24, %23 ]
%27 = call i32 @open(i8* %16, i32 %26)
store i32 %27, i32* %15
%28 = load i32, i32* %15
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %25
store i8* null, i8** %7
br label %48
31: ; preds = %25
%32 = load i8*, i8** %8
%33 = load i64, i64* %9
%34 = load i32, i32* %10
%35 = load i32, i32* %11
%36 = load i32, i32* %15
%37 = load i64, i64* %13
%38 = call i8* @mmap(i8* %32, i64 %33, i32 %34, i32 %35, i32 %36, i64 %37)
store i8* %38, i8** %14
%39 = load i8*, i8** %14
%40 = icmp eq i8* %39, inttoptr (i64 -1 to i8*)
br i1 %40, label %41, label %44
41: ; preds = %31
%42 = load i32, i32* %15
%43 = call i32 @close(i32 %42)
store i8* null, i8** %7
br label %48
44: ; preds = %31
%45 = load i32, i32* %15
%46 = call i32 @close(i32 %45)
%47 = load i8*, i8** %14
store i8* %47, i8** %7
br label %48
48: ; preds = %44, %41, %30
%49 = load i8*, i8** %7
ret i8* %49
}

LLVM IR (-O2):
@MAP_SHARED = global i32 0
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
define i8* @map_file(i8* %0, i64 %1, i32 %2, i32 %3, i8* nocapture %4, i64 %5) {
%7 = load i32, i32* @MAP_SHARED
%8 = and i32 %7, %3
%9 = icmp eq i32 %8, 0
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_RDONLY
%12 = select i1 %9, i32 %11, i32 %10
%13 = tail call i32 @open(i8* %4, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %20, label %15
15: ; preds = %6
%16 = tail call i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %13, i64 %5)
%17 = icmp eq i8* %16, inttoptr (i64 -1 to i8*)
%18 = tail call i32 @close(i32 %13)
%19 = select i1 %17, i8* null, i8* %16
br label %20
20: ; preds = %15, %6
%21 = phi i8* [ null, %6 ], [ %19, %15 ]
ret i8* %21
}