#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 O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fd_map(int, char **, size_t *);
int open(char const *, int);
int file_map(const char *filename, char **buf, size_t *bufsize, int flags) {
  int fd, en, rc = 0;
  if ((fd = open(filename, flags ? flags : O_RDONLY)) < 0)
    return -1;
  if (fd_map(fd, buf, bufsize) < 0)
    rc = -1;
  en = errno;
  close(fd);
  errno = en;
  return rc;
}


x86 assembly (clang, -O0):
<file_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     116b <file_map+0x3b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1177 <file_map+0x47>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1040 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    119b <file_map+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <file_map+0xb2>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <fd_map@plt>
cmp    $0x0,%rax
jge    11bc <file_map+0x8c>
movl   $0xffffffff,-0x30(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2dfe(%rip),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_map>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
test   %ecx,%ecx
jne    114a <file_map+0x1a>
mov    0x2ea0(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%esi
call   1040 <open@plt>
test   %eax,%eax
js     1181 <file_map+0x51>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <fd_map@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    0x2e66(%rip),%r14
mov    (%r14),%r15d
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r15d,(%r14)
jmp    1186 <file_map+0x56>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @file_map(i8* %0, i8** %1, i64* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i32 %3, i32* %9
store i32 0, i32* %12
%13 = load i8*, i8** %6
%14 = load i32, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = load i32, i32* %9
br label %20
18: ; preds = %4
%19 = load i32, i32* @O_RDONLY
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ %17, %16 ], [ %19, %18 ]
%22 = call i32 @open(i8* %13, i32 %21)
store i32 %22, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 -1, i32* %5
br label %38
25: ; preds = %20
%26 = load i32, i32* %10
%27 = load i8**, i8*** %7
%28 = load i64*, i64** %8
%29 = call i64 @fd_map(i32 %26, i8** %27, i64* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %12
br label %32
32: ; preds = %31, %25
%33 = load i32, i32* @errno
store i32 %33, i32* %11
%34 = load i32, i32* %10
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %11
store i32 %36, i32* @errno
%37 = load i32, i32* %12
store i32 %37, i32* %5
br label %38
38: ; preds = %32, %24
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @file_map(i8* nocapture %0, i8** %1, i64* %2, i32 %3) {
%5 = icmp eq i32 %3, 0
%6 = load i32, i32* @O_RDONLY
%7 = select i1 %5, i32 %6, i32 %3
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = tail call i64 @fd_map(i32 %8, i8** %1, i64* %2)
%12 = ashr i64 %11, 63
%13 = trunc i64 %12 to i32
%14 = load i32, i32* @errno
%15 = tail call i32 @close(i32 %8)
store i32 %14, i32* @errno
br label %16
16: ; preds = %4, %10
%17 = phi i32 [ %13, %10 ], [ -1, %4 ]
ret i32 %17
}