#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 {
  unsigned int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int file_map(const char *filename, int *_fd, void **_map, unsigned int *_size) {
  int fd;
  void *map;
  struct stat st;
  if (_fd == 0 || _map == 0 || _size == 0)
    return -1;
  fd = open(filename, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "fixdep: error opening file \"%s\": %s\n", filename,
            strerror(errno));
    return -1;
  }
  if (fstat(fd, &st) < 0) {
    fprintf(stderr, "fixdep: error fstat'ing file: %s\n", strerror(errno));
    goto err;
  }
  if (st.st_size == 0)
    goto err;
  map = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if ((long)map == -1) {
    perror("fixdep: mmap");
    goto err;
  }
  *_fd = fd;
  *_map = map;
  *_size = st.st_size;
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<file_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a9 <file_map+0x39>
cmpq   $0x0,-0x20(%rbp)
je     11a9 <file_map+0x39>
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <file_map+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <file_map+0x18b>
mov    -0x10(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    121a <file_map+0xaa>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <file_map+0x18b>
mov    -0x2c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    1263 <file_map+0xf3>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12ec <file_map+0x17c>
cmpl   $0x0,-0x40(%rbp)
jne    1272 <file_map+0x102>
jmp    12ec <file_map+0x17c>
mov    -0x40(%rbp),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
mov    0x2d3b(%rip),%rax
or     (%rax),%edx
mov    0x2d2a(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    12c3 <file_map+0x153>
lea    0xd8e(%rip),%rdi
call   1070 <perror@plt>
jmp    12ec <file_map+0x17c>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12fb <file_map+0x18b>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_map>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rsi,%rsi
je     127c <file_map+0x10c>
mov    %rdx,%r13
test   %rdx,%rdx
je     127c <file_map+0x10c>
mov    %rcx,%r12
test   %rcx,%rcx
je     127c <file_map+0x10c>
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     121b <file_map+0xab>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     124a <file_map+0xda>
mov    (%rsp),%ebp
test   %ebp,%ebp
je     1274 <file_map+0x104>
mov    0x2e1a(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc8(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     128e <file_map+0x11e>
mov    %r15d,(%rbx)
mov    %rax,0x0(%r13)
mov    %ebp,(%r12)
xor    %r14d,%r14d
jmp    127c <file_map+0x10c>
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc7(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    127c <file_map+0x10c>
mov    0x2d7f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdbd(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   1070 <perror@plt>
jmp    1274 <file_map+0x104>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"fixdep: error opening file \22%s\22: %s\0A\00"
@errno = global i32 0
@.str.1 = [34 x i8] c"fixdep: error fstat'ing file: %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [13 x i8] c"fixdep: mmap\00"
define i32 @file_map(i8* %0, i32* %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.stat
store i8* %0, i8** %6
store i32* %1, i32** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
%13 = load i32*, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %21, label %15
15: ; preds = %4
%16 = load i8**, i8*** %8
%17 = icmp eq i8** %16, null
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32*, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18, %15, %4
store i32 -1, i32* %5
br label %73
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %6
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %30, i8* %32)
store i32 -1, i32* %5
br label %73
34: ; preds = %22
%35 = load i32, i32* %10
%36 = call i64 @fstat(i32 %35, %struct.stat* %12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %70
43: ; preds = %34
%44 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
br label %70
48: ; preds = %43
%49 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = load i32, i32* @MAP_PRIVATE
%55 = load i32, i32* %10
%56 = call i8* @mmap(i32* null, i32 %50, i32 %53, i32 %54, i32 %55, i32 0)
store i8* %56, i8** %11
%57 = load i8*, i8** %11
%58 = ptrtoint i8* %57 to i64
%59 = icmp eq i64 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %48
%61 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %70
62: ; preds = %48
%63 = load i32, i32* %10
%64 = load i32*, i32** %7
store i32 %63, i32* %64
%65 = load i8*, i8** %11
%66 = load i8**, i8*** %8
store i8* %65, i8** %66
%67 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32*, i32** %9
store i32 %68, i32* %69
store i32 0, i32* %5
br label %73
70: ; preds = %60, %47, %38
%71 = load i32, i32* %10
%72 = call i32 @close(i32 %71)
store i32 -1, i32* %5
br label %73
73: ; preds = %70, %62, %28, %21
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"fixdep: error opening file \22%s\22: %s\0A\00"
@errno = global i32 0
@.str.1 = [34 x i8] c"fixdep: error fstat'ing file: %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [13 x i8] c"fixdep: mmap\00"
define i32 @file_map(i8* %0, i32* writeonly %1, i8** writeonly %2, i32* writeonly %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = icmp eq i32* %1, null
%8 = icmp eq i8** %2, null
%9 = or i1 %7, %8
%10 = icmp eq i32* %3, null
%11 = or i1 %9, %10
br i1 %11, label %45, label %12
12: ; preds = %4
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %19)
br label %45
21: ; preds = %12
%22 = call i64 @fstat(i32 %14, %struct.stat* %5)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %43
29: ; preds = %21
%30 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %29
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %35, %34
%37 = load i32, i32* @MAP_PRIVATE
%38 = tail call i8* @mmap(i32* null, i32 %31, i32 %36, i32 %37, i32 %14, i32 0)
%39 = icmp eq i8* %38, inttoptr (i64 -1 to i8*)
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %43
42: ; preds = %33
store i32 %14, i32* %1
store i8* %38, i8** %2
store i32 %31, i32* %3
br label %45
43: ; preds = %29, %40, %24
%44 = tail call i32 @close(i32 %14)
br label %45
45: ; preds = %4, %43, %42, %16
%46 = phi i32 [ -1, %16 ], [ -1, %43 ], [ 0, %42 ], [ -1, %4 ]
ret i32 %46
}