#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 */
struct stat {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
scalar_t__ close(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char const *, int /*<<< orphan*/);
void *map_input(const char *name, size_t *len) {
  struct stat stat;
  void *mapped;
  int fd;
  fd = open(name, O_RDONLY);
  if (fd < 0)
    return NULL;
  if (fstat(fd, &stat) < 0) {
    close(fd);
    return NULL;
  }
  *len = stat.st_size;
  mapped = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (close(fd) < 0) {
    (void)munmap(mapped, stat.st_size);
    return NULL;
  }
  return mapped;
}


x86 assembly (clang, -O0):
<map_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    118c <map_input+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1220 <map_input+0xd0>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    11b7 <map_input+0x67>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1220 <map_input+0xd0>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1218 <map_input+0xc8>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <munmap@plt>
movq   $0x0,-0x8(%rbp)
jmp    1220 <map_input+0xd0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_input>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11ce <map_input+0x7e>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     11c7 <map_input+0x77>
mov    0x8(%rsp),%r14
mov    %r14,(%r15)
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    0x2e4f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11d1 <map_input+0x81>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <munmap@plt>
jmp    11ce <map_input+0x7e>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @map_input(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %42
15: ; preds = %2
%16 = load i32, i32* %8
%17 = call i64 @fstat(i32 %16, %struct.stat* %6)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %8
%21 = call i64 @close(i32 %20)
store i8* null, i8** %3
br label %42
22: ; preds = %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64*, i64** %5
store i64 %24, i64* %25
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %8
%31 = call i8* @mmap(i32* null, i64 %27, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %7
%32 = load i32, i32* %8
%33 = call i64 @close(i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %22
%36 = load i8*, i8** %7
%37 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%38 = load i64, i64* %37
%39 = call i32 @munmap(i8* %36, i64 %38)
store i8* null, i8** %3
br label %42
40: ; preds = %22
%41 = load i8*, i8** %7
store i8* %41, i8** %3
br label %42
42: ; preds = %40, %35, %19, %14
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @map_input(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %2
%9 = call i64 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i64 @close(i32 %6)
br label %23
13: ; preds = %8
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i64, i64* %14
store i64 %15, i64* %1
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i8* @mmap(i32* null, i64 %15, i32 %16, i32 %17, i32 %6, i32 0)
%19 = tail call i64 @close(i32 %6)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @munmap(i8* %18, i64 %15)
br label %23
23: ; preds = %13, %2, %21, %11
%24 = phi i8* [ null, %11 ], [ null, %21 ], [ null, %2 ], [ %18, %13 ]
ret i8* %24
}