#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
void *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
int readFileToMem(char *fileName, void **pbuf) {
  int fd;
  if ((fd = open(fileName, O_RDONLY)) == -1) {
    fprintf(stderr, "open %s error \n ", fileName);
    error("open file error");
  }
  int fsize = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  void *buf;
  if (!(buf = malloc(fsize))) {
    die("malloc error!");
  }
  int rsize = read(fd, buf, fsize);
  close(fd);
  *pbuf = buf;
  return rsize;
}


x86 assembly (clang, -O0):
<readFileToMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d3 <readFileToMem+0x53>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3e(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe43(%rip),%rdi
call   1080 <error@plt>
mov    -0x14(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    121e <readFileToMem+0x9e>
lea    0xe08(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFileToMem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11c6 <readFileToMem+0x46>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
lea    0xe50(%rip),%rdi
call   1080 <error@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,%r15d
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1209 <readFileToMem+0x89>
lea    0xe1d(%rip),%rdi
call   10a0 <die@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"open %s error \0A \00"
@.str.1 = [16 x i8] c"open file error\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [14 x i8] c"malloc error!\00"
define i32 @readFileToMem(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %13, %2
%19 = load i32, i32* %5
%20 = load i32, i32* @SEEK_END
%21 = call i32 @lseek(i32 %19, i32 0, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %5
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
%25 = load i32, i32* %6
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %18
%29 = call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %18
%31 = load i32, i32* %5
%32 = load i8*, i8** %7
%33 = load i32, i32* %6
%34 = call i32 @read(i32 %31, i8* %32, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %4
store i8* %37, i8** %38
%39 = load i32, i32* %8
ret i32 %39
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"open %s error \0A \00"
@.str.1 = [16 x i8] c"open file error\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [14 x i8] c"malloc error!\00"
define i32 @readFileToMem(i8* %0, i8** nocapture writeonly %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %6, %2
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @lseek(i32 %4, i32 0, i32 %11)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %4, i32 0, i32 %13)
%15 = tail call i8* @malloc(i32 %12)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = tail call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
%20 = tail call i32 @read(i32 %4, i8* %15, i32 %12)
%21 = tail call i32 @close(i32 %4)
store i8* %15, i8** %1
ret i32 %20
}