#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int fd_map(int fd, char **buf, size_t *bufsize) {
  struct stat stats;
  int en = 0;
  if (fstat(fd, &stats) < 0) {
    en = errno;
    close(fd);
    errno = en;
    return -1;
  }
  *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (*buf == MAP_FAILED) {
    *buf = NULL;
    en = errno;
    close(fd);
    errno = en;
    return -1;
  }
  *bufsize = stats.st_size;
  return 0;
}


x86 assembly (clang, -O0):
<fd_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
jge    118c <fd_map+0x5c>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e52(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <fd_map+0xec>
mov    -0x20(%rbp),%rsi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <fd_map+0xda>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2dd4(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <fd_map+0xec>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_map>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    %rsp,%rsi
call   1050 <fstat@plt>
test   %rax,%rax
js     1194 <fd_map+0x64>
mov    (%rsp),%r13
mov    0x2e9d(%rip),%rax
mov    (%rax),%edx
mov    0x2e84(%rip),%rax
mov    (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r13,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r15)
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
je     118d <fd_map+0x5d>
mov    %r13,(%r14)
jmp    11ae <fd_map+0x7e>
movq   $0x0,(%r15)
mov    0x2e35(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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