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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ elf_run(char *, char **, char **);
int /*<<< orphan*/ elf_sym(char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int main(int argc, char *argv[], char *envp[]) {
  FILE *f = fopen(argv[1], "rb");
  fseek(f, 0, SEEK_END);
  int size = ftell(f);
  fseek(f, 0L, SEEK_SET);
  char *buf = malloc(size);
  fread(buf, size, 1, f);
  char *_argv[] = {
      argv[0],
      "arg1",
      "arg2",
      NULL,
  };
  char *_env[] = {
      "HOME=/tmp",
      NULL,
  };
  printf("main: %p\n", elf_sym(buf, "main"));
  elf_run(buf, argv, envp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe57(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
lea    0xde6(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xde0(%rip),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2bc5(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2bc2(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1050 <elf_sym@plt>
mov    %eax,%esi
lea    0xdb4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <elf_run@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
lea    0xe67(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%r12d
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xe16(%rip),%rsi
mov    %rbp,%rdi
call   1050 <elf_sym@plt>
lea    0xdfd(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <elf_run@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [5 x i8] c"arg1\00"
@.str.2 = [5 x i8] c"arg2\00"
@.str.3 = [10 x i8] c"HOME=/tmp\00"
@__const.main._env = [2 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.4 = [10 x i8] c"main: %p\0A\00"
@.str.5 = [5 x i8] c"main\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [4 x i8*]
%12 = alloca [2 x i8*]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%13 = load i8**, i8*** %6
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i64 0, i32 %18)
%20 = load i32*, i32** %8
%21 = call i32 @ftell(i32* %20)
store i32 %21, i32* %9
%22 = load i32*, i32** %8
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i64 0, i32 %23)
%25 = load i32, i32* %9
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = load i32, i32* %9
%29 = load i32*, i32** %8
%30 = call i32 @fread(i8* %27, i32 %28, i32 1, i32* %29)
%31 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
%32 = load i8**, i8*** %6
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
store i8* %34, i8** %31
%35 = getelementptr inbounds i8*, i8** %31, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %35
%36 = getelementptr inbounds i8*, i8** %35, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %36
%37 = getelementptr inbounds i8*, i8** %36, i64 1
store i8* null, i8** %37
%38 = bitcast [2 x i8*]* %12 to i8*
%39 = load i8*, i8** %10
%40 = call i32 @elf_sym(i8* %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = load i8*, i8** %10
%43 = load i8**, i8*** %6
%44 = load i8**, i8*** %7
%45 = call i32 @elf_run(i8* %42, i8** %43, i8** %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.4 = [10 x i8] c"main: %p\0A\00"
@.str.5 = [5 x i8] c"main\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %6, i64 0, i32 %7)
%9 = tail call i32 @ftell(i32* %6)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %6, i64 0, i32 %10)
%12 = tail call i8* @malloc(i32 %9)
%13 = tail call i32 @fread(i8* %12, i32 %9, i32 1, i32* %6)
%14 = tail call i32 @elf_sym(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%15 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = tail call i32 @elf_run(i8* %12, i8** %1, i8** %2)
ret i32 0
}