#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 */
void *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 *);
void *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 mmap_file_ro(void **p, char *name, size_t *size) {
  *size = 0;
  int fd = open(name, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 0;
  }
  struct stat sb;
  if (fstat(fd, &sb) == -1) {
    perror("fstat");
    close(fd);
    return 0;
  }
  if (!S_ISREG(sb.st_mode)) {
    fprintf(stderr, "%s not a file\n", name);
    close(fd);
    return 0;
  }
  *p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (*p == MAP_FAILED) {
    perror("mmap");
    close(fd);
    return 0;
  }
  if (close(fd) == -1) {
    perror("close");
    return 0;
  }
  *size = sb.st_size;
  return 1;
}


x86 assembly (clang, -O0):
<mmap_file_ro>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c6 <mmap_file_ro+0x56>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <mmap_file_ro+0x16a>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11fb <mmap_file_ro+0x8b>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <mmap_file_ro+0x16a>
mov    -0x30(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1239 <mmap_file_ro+0xc9>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdeb(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <mmap_file_ro+0x16a>
mov    -0x38(%rbp),%rsi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    0x2d9b(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
jne    129f <mmap_file_ro+0x12f>
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <mmap_file_ro+0x16a>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12c8 <mmap_file_ro+0x158>
lea    0xd68(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <mmap_file_ro+0x16a>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_file_ro>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r15
movq   $0x0,(%rdx)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1245 <mmap_file_ro+0xd5>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     124e <mmap_file_ro+0xde>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     1215 <mmap_file_ro+0xa5>
mov    (%rsp),%r12
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r15)
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
je     125c <mmap_file_ro+0xec>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1271 <mmap_file_ro+0x101>
mov    %r12,(%r14)
mov    $0x1,%ebp
jmp    1236 <mmap_file_ro+0xc6>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
jmp    1278 <mmap_file_ro+0x108>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
jmp    122d <mmap_file_ro+0xbd>
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1236 <mmap_file_ro+0xc6>
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
jmp    1234 <mmap_file_ro+0xc4>

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

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