#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);
unsigned char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, unsigned char *, int);
scalar_t__ stat(char const *, struct stat *);
int readexecfile(const unsigned char *filename, unsigned char **buffer) {
  int fd;
  struct stat st;
  unsigned char *buf;
  const char *fname = (const char *)filename;
  if (stat(fname, &st))
    return -1;
  buf = malloc(sizeof(unsigned char) * st.st_size);
  if (buf == NULL)
    return -1;
  fd = open(fname, O_RDONLY);
  if (fd == -1)
    return -1;
  read(fd, buf, st.st_size);
  close(fd);
  *buffer = buf;
  return st.st_size;
}


x86 assembly (clang, -O0):
<readexecfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     118b <readexecfile+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <readexecfile+0xb8>
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <readexecfile+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <readexecfile+0xb8>
mov    -0x30(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e0 <readexecfile+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <readexecfile+0xb8>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readexecfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11b1 <readexecfile+0x61>
mov    (%rsp),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b1 <readexecfile+0x61>
mov    %rax,%r15
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b1 <readexecfile+0x61>
mov    %eax,%ebx
mov    (%rsp),%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1040 <read@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r15,(%r14)
mov    (%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i32 @readexecfile(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %21, label %12
12: ; preds = %7
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* %8
%18 = call i32 @read(i32 %14, i8* %10, i32 %17)
%19 = call i32 @close(i32 %14)
store i8* %10, i8** %1
%20 = load i32, i32* %8
br label %21
21: ; preds = %12, %7, %2, %16
%22 = phi i32 [ %20, %16 ], [ -1, %2 ], [ -1, %7 ], [ -1, %12 ]
ret i32 %22
}