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

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ error(char *);
char *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
short *map(char *file, int *size) {
  int fd;
  char *cp;
  struct stat statbuf;
  if (stat(file, &statbuf) != 0)
    error("stat");
  *size = statbuf.st_size;
  if ((fd = open(file, O_RDONLY)) < 0)
    error("open");
  cp = mmap(0, *size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (cp == (char *)-1)
    error("mmap");
  return (short *)cp;
}


x86 assembly (clang, -O0):
<map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1173 <map+0x33>
lea    0xe92(%rip),%rdi
call   1050 <error@plt>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a6 <map+0x66>
lea    0xe64(%rip),%rdi
call   1050 <error@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11ee <map+0xae>
lea    0xe21(%rip),%rdi
call   1050 <error@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     1162 <map+0x22>
lea    0xea3(%rip),%rdi
call   1050 <error@plt>
mov    (%rsp),%eax
mov    %eax,(%rbx)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    118a <map+0x4a>
lea    0xe80(%rip),%rdi
call   1050 <error@plt>
mov    (%rbx),%esi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    0x2e34(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11c0 <map+0x80>
lea    0xe4f(%rip),%rdi
call   1050 <error@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i16* @map(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.stat
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = load i8*, i8** %3
%9 = call i64 @stat(i8* %8, %struct.stat* %7)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32*, i32** %4
store i32 %15, i32* %16
%17 = load i8*, i8** %3
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %13
%24 = load i32*, i32** %4
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_PRIVATE
%28 = load i32, i32* %5
%29 = call i8* @mmap(i32 0, i32 %25, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = icmp eq i8* %30, inttoptr (i64 -1 to i8*)
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %23
%35 = load i8*, i8** %6
%36 = bitcast i8* %35 to i16*
ret i16* %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i16* @map(i8* %0, i32* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %1
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %0, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %9
%18 = load i32, i32* %1
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_PRIVATE
%21 = call i8* @mmap(i32 0, i32 %18, i32 %19, i32 %20, i32 %13, i32 0)
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = bitcast i8* %21 to i16*
ret i16* %26
}