#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 {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int mapfile(char *filename, char **addr, size_t *length) {
  struct stat sb;
  int fd;
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "ERROR: Error opening '%s': ", filename);
    perror(NULL);
    return 1;
  }
  if (fstat(fd, &sb) == -1) {
    perror("fstat");
    return 1;
  }
  if (!S_ISREG(sb.st_mode)) {
    fprintf(stderr, "%s is not a file\n", filename);
    return 1;
  }
  *length = sb.st_size;
  *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (*addr == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  if (close(fd) == -1) {
    perror("close");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11d1 <mapfile+0x61>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <mapfile+0x15e>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11fe <mapfile+0x8e>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <mapfile+0x15e>
mov    -0x28(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1234 <mapfile+0xc4>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <mapfile+0x15e>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rsi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2d43(%rip),%rcx
cmp    (%rcx),%rax
jne    129e <mapfile+0x12e>
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <mapfile+0x15e>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12c7 <mapfile+0x157>
lea    0xd83(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <mapfile+0x15e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1225 <mapfile+0xb5>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     1241 <mapfile+0xd1>
mov    0x10(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     120b <mapfile+0x9b>
mov    0x8(%rsp),%rsi
mov    %rsi,(%r15)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
je     124a <mapfile+0xda>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    125b <mapfile+0xeb>
lea    0xe30(%rip),%rdi
jmp    1251 <mapfile+0xe1>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
jmp    1256 <mapfile+0xe6>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
xor    %edi,%edi
jmp    1251 <mapfile+0xe1>
lea    0xdd4(%rip),%rdi
jmp    1251 <mapfile+0xe1>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"ERROR: Error opening '%s': \00"
@.str.1 = [6 x i8] c"fstat\00"
@.str.2 = [18 x i8] c"%s is not a file\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @mapfile(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %5
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @perror(i8* null)
store i32 1, i32* %4
br label %59
20: ; preds = %3
%21 = load i32, i32* %9
%22 = call i32 @fstat(i32 %21, %struct.stat* %8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %59
26: ; preds = %20
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @S_ISREG(i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %5
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %33)
store i32 1, i32* %4
br label %59
35: ; preds = %26
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64*, i64** %7
store i64 %37, i64* %38
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @MAP_SHARED
%43 = load i32, i32* %9
%44 = call i8* @mmap(i32* null, i64 %40, i32 %41, i32 %42, i32 %43, i32 0)
%45 = load i8**, i8*** %6
store i8* %44, i8** %45
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %52
50: ; preds = %35
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %59
52: ; preds = %35
%53 = load i32, i32* %9
%54 = call i32 @close(i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %4
br label %59
58: ; preds = %52
store i32 0, i32* %4
br label %59
59: ; preds = %58, %56, %50, %31, %24, %15
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"ERROR: Error opening '%s': \00"
@.str.1 = [6 x i8] c"fstat\00"
@.str.2 = [18 x i8] c"%s is not a file\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @mapfile(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i32 @perror(i8* null)
br label %41
13: ; preds = %3
%14 = call i32 @fstat(i32 %7, %struct.stat* %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %41
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = tail call i32 @S_ISREG(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %41
26: ; preds = %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %2
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i8* @mmap(i32* null, i64 %28, i32 %29, i32 %30, i32 %7, i32 0)
store i8* %31, i8** %1
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %41
36: ; preds = %26
%37 = tail call i32 @close(i32 %7)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %36, %39, %34, %23, %16, %9
%42 = phi i32 [ 1, %9 ], [ 1, %16 ], [ 1, %34 ], [ 1, %39 ], [ 1, %23 ], [ 0, %36 ]
ret i32 %42
}