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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseeko(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int ftello(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ run_code(char *);
void run_file(char *fname) {
  FILE *fp;
  int fd;
  off_t file_size;
  char *buffer;
  struct stat st;
  fd = open(fname, O_RDONLY);
  if (fd == -1) {
    printf("open(%s) failed\n", fname);
  }
  fp = fdopen(fd, "rb");
  if (fp == NULL) {
    printf("fdopen() failed\n");
  }
  if ((fstat(fd, &st) != 0) || (!S_ISREG(st.st_mode))) {
    printf("fstat() failed, file is not regular\n");
  }
  if (fseeko(fp, 0, SEEK_END) != 0) {
    printf("fseeko() failed\n");
  }
  file_size = ftello(fp);
  if (file_size == -1) {
    printf("file_size == -1\n");
  }
  buffer = (char *)malloc(file_size);
  if (buffer == NULL) {
    printf("malloc(%d) failed\n", (int)file_size);
  }
  fseeko(fp, 0, SEEK_SET);
  while (fread(buffer, file_size, 1, fp) > 0)
    ;
  printf("Successfully read [%d] bytes.\n", (int)file_size);
  run_code(buffer);
}


x86 assembly (clang, -O0):
<run_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11dd <run_file+0x3d>
mov    -0x8(%rbp),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xe2a(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1209 <run_file+0x69>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jne    1230 <run_file+0x90>
mov    -0x28(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    123e <run_file+0x9e>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseeko@plt>
cmp    $0x0,%rax
je     126a <run_file+0xca>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ftello@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    128e <run_file+0xee>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12b6 <run_file+0x116>
mov    -0x18(%rbp),%esi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseeko@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x0,%rax
jle    12ee <run_file+0x14e>
jmp    12ca <run_file+0x12a>
mov    -0x18(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <run_code@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11e1 <run_file+0x31>
lea    0xe29(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
call   1090 <fdopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1203 <run_file+0x53>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
jne    1221 <run_file+0x71>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
jne    122d <run_file+0x7d>
lea    0xe1e(%rip),%rdi
call   1040 <puts@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10b0 <fseeko@plt>
test   %rax,%rax
je     1251 <run_file+0xa1>
lea    0xe2e(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <ftello@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    126c <run_file+0xbc>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    128b <run_file+0xdb>
lea    0xd92(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10b0 <fseeko@plt>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
test   %rax,%rax
jg     12a0 <run_file+0xf0>
lea    0xd69(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    10d0 <run_code@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [17 x i8] c"open(%s) failed\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [17 x i8] c"fdopen() failed\0A\00"
@.str.3 = [37 x i8] c"fstat() failed, file is not regular\0A\00"
@SEEK_END = global i32 0
@.str.4 = [17 x i8] c"fseeko() failed\0A\00"
@.str.5 = [17 x i8] c"file_size == -1\0A\00"
@.str.6 = [19 x i8] c"malloc(%d) failed\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [31 x i8] c"Successfully read [%d] bytes.\0A\00"
define void @run_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.stat
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %1
%17 = load i32, i32* %4
%18 = call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %3
%19 = load i32*, i32** %3
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %4
%25 = call i64 @fstat(i32 %24, %struct.stat* %7)
%26 = icmp ne i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @S_ISREG(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %27, %23
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i32*, i32** %3
%36 = load i32, i32* @SEEK_END
%37 = call i64 @fseeko(i32* %35, i32 0, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = load i32*, i32** %3
%43 = call i32 @ftello(i32* %42)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
%49 = load i32, i32* %5
%50 = call i64 @malloc(i32 %49)
%51 = inttoptr i64 %50 to i8*
store i8* %51, i8** %6
%52 = load i8*, i8** %6
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* %5
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %54, %48
%58 = load i32*, i32** %3
%59 = load i32, i32* @SEEK_SET
%60 = call i64 @fseeko(i32* %58, i32 0, i32 %59)
br label %61
61: ; preds = %67, %57
%62 = load i8*, i8** %6
%63 = load i32, i32* %5
%64 = load i32*, i32** %3
%65 = call i64 @fread(i8* %62, i32 %63, i32 1, i32* %64)
%66 = icmp sgt i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %61
br label %61
68: ; preds = %61
%69 = load i32, i32* %5
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %69)
%71 = load i8*, i8** %6
%72 = call i32 @run_code(i8* %71)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [17 x i8] c"open(%s) failed\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.6 = [19 x i8] c"malloc(%d) failed\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [31 x i8] c"Successfully read [%d] bytes.\0A\00"
@str = [36 x i8] c"fstat() failed, file is not regular\00"
@str.8 = [16 x i8] c"file_size == -1\00"
@str.9 = [16 x i8] c"fseeko() failed\00"
@str.10 = [16 x i8] c"fdopen() failed\00"
define void @run_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%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 %9
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %1
%10 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.10, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = call i64 @fstat(i32 %5, %struct.stat* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @S_ISREG(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17, %14
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = load i32, i32* @SEEK_END
%26 = tail call i64 @fseeko(i32* %10, i32 0, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = tail call i32 @ftello(i32* %10)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = tail call i64 @malloc(i32 %31)
%37 = inttoptr i64 %36 to i8*
%38 = icmp eq i64 %36, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %31)
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* @SEEK_SET
%43 = tail call i64 @fseeko(i32* %10, i32 0, i32 %42)
br label %44
44: ; preds = %44, %41
%45 = tail call i64 @fread(i8* %37, i32 %31, i32 1, i32* %10)
%46 = icmp sgt i64 %45, 0
br i1 %46, label %44, label %47
47: ; preds = %44
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %31)
%49 = tail call i32 @run_code(i8* %37)
ret void
}