#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_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ error(char *);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int);
scalar_t__ stat(char *, struct stat *);
char *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, 2)) < 0)
    error("open");
  cp = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (cp == (char *)-1)
    error("mmap");
  return 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    $0x2,%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a2 <map+0x62>
lea    0xe68(%rip),%rdi
call   1050 <error@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e18(%rip),%rax
or     (%rax),%edx
mov    0x2e27(%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    11f3 <map+0xb3>
lea    0xe1c(%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    %rbp,%rdi
mov    $0x2,%esi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1186 <map+0x46>
lea    0xe84(%rip),%rdi
call   1050 <error@plt>
mov    (%rbx),%esi
mov    0x2e69(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e47(%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    11c5 <map+0x85>
lea    0xe4a(%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"
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i8* @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 = call i32 @open(i8* %17, i32 2)
store i32 %18, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %13
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %5
%30 = call i8* @mmap(i32 0, i32 %24, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp eq i8* %31, inttoptr (i64 -1 to i8*)
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %22
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i8* @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 = call i32 @open(i8* %0, i32 2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* %1
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_SHARED
%22 = call i8* @mmap(i32 0, i32 %17, i32 %20, i32 %21, i32 %12, i32 0)
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
ret i8* %22
}