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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int, char *, int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int load_file(char *filename, off_t *datasz) {
  int datafd = open(filename, O_RDONLY);
  if (datafd == -1) {
    printf("Attempted to read: '%s'\n", filename);
    error(EXIT_FAILURE, errno, "Error %d opening payload", errno);
  }
  struct stat datastat;
  fstat(datafd, &datastat);
  *datasz = datastat.st_size;
  return datafd;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a6 <load_file+0x66>
mov    -0x8(%rbp),%rsi
lea    0xe86(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
lea    0xe78(%rip),%rdx
call   1040 <error@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <fstat@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1192 <load_file+0x52>
lea    0xe96(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
lea    0xe8e(%rip),%rdx
mov    %ecx,%esi
call   1040 <error@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1060 <fstat@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [25 x i8] c"Attempted to read: '%s'\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str.1 = [25 x i8] c"Error %d opening payload\00"
define i32 @load_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
store i32* %1, i32** %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = load i32, i32* @errno
%17 = load i32, i32* @errno
%18 = call i32 @error(i32 %15, i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %12, %2
%20 = load i32, i32* %5
%21 = call i32 @fstat(i32 %20, %struct.stat* %6)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32*, i32** %4
store i32 %23, i32* %24
%25 = load i32, i32* %5
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [25 x i8] c"Attempted to read: '%s'\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str.1 = [25 x i8] c"Error %d opening payload\00"
define i32 @load_file(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @EXIT_FAILURE
%10 = load i32, i32* @errno
%11 = tail call i32 @error(i32 %9, i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %7, %2
%13 = bitcast %struct.stat* %3 to i8*
%14 = call i32 @fstat(i32 %5, %struct.stat* %3)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
store i32 %16, i32* %1
ret i32 %5
}