#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 PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ error(char *);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, 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");
  printf("mmaping %s, size %d.\n", file, statbuf.st_size);
  cp = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE, 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   1050 <stat@plt>
cmp    $0x0,%rax
je     1183 <map+0x33>
lea    0xe82(%rip),%rdi
call   1060 <error@plt>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11b2 <map+0x62>
lea    0xe58(%rip),%rdi
call   1060 <error@plt>
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%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    1216 <map+0xc6>
lea    0xe0f(%rip),%rdi
call   1060 <error@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1179 <map+0x29>
lea    0xe8c(%rip),%rdi
call   1060 <error@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119f <map+0x4f>
lea    0xe6b(%rip),%rdi
call   1060 <error@plt>
mov    0x8(%rsp),%edx
lea    0xe60(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    (%r14),%esi
mov    0x2e3c(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e02(%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    11f2 <map+0xa2>
lea    0xe33(%rip),%rdi
call   1060 <error@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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"
@.str.2 = [22 x i8] c"mmaping %s, size %d.\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.3 = [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 i8*, i8** %3
%24 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %23, i32 %25)
%27 = load i32*, i32** %4
%28 = load i32, i32* %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_PRIVATE
%33 = load i32, i32* %5
%34 = call i8* @mmap(i32 0, i32 %28, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = icmp eq i8* %35, inttoptr (i64 -1 to i8*)
br i1 %36, label %37, label %39
37: ; preds = %22
%38 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %22
%40 = load i8*, i8** %6
ret i8* %40
}

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