#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*/ EXIT_FAILURE;
int /*<<< orphan*/ clean_up();
void *compile(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ perror(char *);
void stub1();
void eval(const char *code) {
  void (*compiled)(void);
  FILE *fout = fopen("library.c", "w");
  if (fout == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  fprintf(fout,
          "#include <stdio.h>\n"
          "void compiled(void){\n"
          "%s\n"
          ";\n"
          "}\n",
          code);
  fclose(fout);
  *(void **)&compiled = compile("compiled");
  (*compiled)();
  clean_up();
}


x86 assembly (clang, -O0):
<eval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <eval+0x48>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe66(%rip),%rdi
call   1050 <compile@plt>
mov    %rax,-0x10(%rbp)
call   *-0x10(%rbp)
mov    $0x0,%al
call   1040 <clean_up@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eval>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe82(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c8 <eval+0x58>
mov    %rax,%rbx
lea    0xe79(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe8f(%rip),%rdi
call   1050 <compile@plt>
call   *%rax
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <clean_up@plt>
lea    0xe3d(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"library.c\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [48 x i8] c"#include <stdio.h>\0Avoid compiled(void){\0A%s\0A;\0A}\0A\00"
@.str.4 = [9 x i8] c"compiled\00"
define void @eval(i8* %0) {
%2 = alloca i8*
%3 = alloca void ()*
%4 = alloca i32*
store i8* %0, i8** %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = load i8*, i8** %2
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
%18 = call i8* @compile(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%19 = bitcast void ()** %3 to i8**
store i8* %18, i8** %19
%20 = load void ()*, void ()** %3
%21 = call i32 (...) @clean_up()
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"library.c\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [48 x i8] c"#include <stdio.h>\0Avoid compiled(void){\0A%s\0A;\0A}\0A\00"
@.str.4 = [9 x i8] c"compiled\00"
define void @eval(i8* %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %1
%9 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%10 = tail call i32 @fclose(i32* %2)
%11 = tail call i8* @compile(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%12 = bitcast i8* %11 to void ()*
tail call void %12()
%13 = tail call i32 (...) @clean_up()
ret void
}