#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
void stub1();
int read_and_exec_file(char *file) {
  char *buf = malloc(10000);
  int f, n;
  if ((f = open(file, O_RDONLY, 0)) < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if ((n = read(f, buf, 100000)) < 0) {
    perror("read");
    exit(EXIT_FAILURE);
  }
  ((void (*)(void))buf)();
  printf("==> Done.\n");
  return 0;
}


x86 assembly (clang, -O0):
<read_and_exec_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x2710,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11b4 <read_and_exec_file+0x54>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x186a0,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11eb <read_and_exec_file+0x8b>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
call   *-0x10(%rbp)
lea    0xe15(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_and_exec_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2710,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11b7 <read_and_exec_file+0x57>
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x186a0,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11c0 <read_and_exec_file+0x60>
call   *%r14
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe42(%rip),%rdi
jmp    11c7 <read_and_exec_file+0x67>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [11 x i8] c"==> Done.\0A\00"
define i32 @read_and_exec_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = call i8* @malloc(i32 10000)
store i8* %6, i8** %3
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i8*, i8** %3
%18 = call i32 @read(i32 %16, i8* %17, i32 100000)
store i32 %18, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = load i8*, i8** %3
%26 = bitcast i8* %25 to void ()*
%27 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"read\00"
@str = [10 x i8] c"==> Done.\00"
define i32 @read_and_exec_file(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(10000) i8* @malloc(i32 10000)
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = tail call i32 @read(i32 %4, i8* %2, i32 100000)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = bitcast i8* %2 to void ()*
tail call void %18()
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
ret i32 0
}