#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int stat(char const *, struct stat *);
int file2mem(const char *fname, char **p, int *size) {
  char *mem = NULL;
  struct stat sb;
  int fd = -1;
  if (stat(fname, &sb) == -1) {
    perror("stat");
    return -1;
  }
  mem = (char *)malloc(sb.st_size);
  if (!mem) {
    perror("malloc");
    return -1;
  }
  fd = open(fname, O_RDONLY, 0644);
  if (fd == -1) {
    perror("open");
    free(mem);
    return -1;
  }
  read(fd, mem, sb.st_size);
  *p = mem;
  *size = sb.st_size;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<file2mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11c1 <file2mem+0x51>
lea    0xe50(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <file2mem+0xf7>
mov    -0x30(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f0 <file2mem+0x80>
lea    0xe26(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <file2mem+0xf7>
mov    -0x10(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1235 <file2mem+0xc5>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <file2mem+0xf7>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <read@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file2mem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11ea <file2mem+0x7a>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f3 <file2mem+0x83>
mov    %rax,%r13
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1206 <file2mem+0x96>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
mov    %r13,(%r15)
mov    %ebp,(%r14)
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0f(%rip),%rdi
jmp    11fa <file2mem+0x8a>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    11db <file2mem+0x6b>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    11db <file2mem+0x6b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@.str.1 = [7 x i8] c"malloc\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
define i32 @file2mem(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i8* null, i8** %8
store i32 -1, i32* %10
%11 = load i8*, i8** %5
%12 = call i32 @stat(i8* %11, %struct.stat* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
16: ; preds = %3
%17 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
25: ; preds = %16
%26 = load i8*, i8** %5
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27, i32 420)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = load i8*, i8** %8
%34 = call i32 @free(i8* %33)
store i32 -1, i32* %4
br label %48
35: ; preds = %25
%36 = load i32, i32* %10
%37 = load i8*, i8** %8
%38 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @read(i32 %36, i8* %37, i32 %39)
%41 = load i8*, i8** %8
%42 = load i8**, i8*** %6
store i8* %41, i8** %42
%43 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32*, i32** %7
store i32 %44, i32* %45
%46 = load i32, i32* %10
%47 = call i32 @close(i32 %46)
store i32 0, i32* %4
br label %48
48: ; preds = %35, %31, %23, %14
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@.str.1 = [7 x i8] c"malloc\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
define i32 @file2mem(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %4)
%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 %28
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %28
18: ; preds = %10
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* %0, i32 %19, i32 420)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @free(i8* %14)
br label %28
25: ; preds = %18
%26 = tail call i32 @read(i32 %20, i8* %14, i32 %12)
store i8* %14, i8** %1
store i32 %12, i32* %2
%27 = tail call i32 @close(i32 %20)
br label %28
28: ; preds = %25, %22, %16, %8
%29 = phi i32 [ -1, %8 ], [ -1, %22 ], [ 0, %25 ], [ -1, %16 ]
ret i32 %29
}