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

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
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*/, int /*<<< orphan*/, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int get_mem(char *fname, char **p, off_t *len) {
  struct stat sb;
  int fd;
  char *x;
  fd = open(fname, O_RDWR);
  if (fd == -1) {
    perror("open");
    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", fname);
    return 1;
  }
  x = mmap(0, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (x == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  if (close(fd) == -1) {
    perror("close");
    return 1;
  }
  *len = sb.st_size;
  *p = x;
  return 0;
}


x86 assembly (clang, -O0):
<get_mem>:
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,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11bb <get_mem+0x4b>
lea    0xe56(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <get_mem+0x14f>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11e8 <get_mem+0x78>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <get_mem+0x14f>
mov    -0x24(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    121e <get_mem+0xae>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfe(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <get_mem+0x14f>
mov    -0x28(%rbp),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    0x2d8f(%rip),%rax
or     (%rax),%edx
mov    0x2dae(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
jne    127b <get_mem+0x10b>
lea    0xdb3(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <get_mem+0x14f>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12a4 <get_mem+0x134>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <get_mem+0x14f>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122f <get_mem+0xbf>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     1238 <get_mem+0xc8>
mov    0xc(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     1215 <get_mem+0xa5>
mov    0x8(%rsp),%r12d
mov    0x2e2c(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dbe(%rip),%rax
cmp    (%rax),%rbx
je     1241 <get_mem+0xd1>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     124a <get_mem+0xda>
mov    %r12d,(%r14)
mov    %rbx,(%r15)
xor    %eax,%eax
jmp    125b <get_mem+0xeb>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
jmp    1256 <get_mem+0xe6>
lea    0xdca(%rip),%rdi
jmp    1251 <get_mem+0xe1>
lea    0xdc6(%rip),%rdi
jmp    1251 <get_mem+0xe1>
lea    0xdd5(%rip),%rdi
jmp    1251 <get_mem+0xe1>
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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