#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*/ code;

/* Variables and functions */
int MAP_ANON;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_WRITE;
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcpy(void *, ...);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  unsigned char code[] = {0xb8, 0x00, 0x00, 0x00, 0x00, 0xc3};
  if (argc < 2) {
    fprintf(stderr, "Usage: jit <integer>\n");
    return 1;
  }
  int num = atoi(argv[1]);
  memcpy(&code[1], &num, 4);
  void *mem = mmap(NULL, sizeof(code), PROT_WRITE | PROT_EXEC,
                   MAP_ANON | MAP_PRIVATE, -1, 0);
  memcpy(mem, code, sizeof(code));
  int (*func)() = mem;
  int ret = func();
  fprintf(stdout, "Returned : %d\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xea4(%rip),%eax
mov    %eax,-0x16(%rbp)
mov    0xe9e(%rip),%ax
mov    %ax,-0x12(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1197 <main+0x57>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1243 <main+0x103>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x16(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd7(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x6,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x16(%rbp),%rsi
xor    %eax,%eax
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   *-0x30(%rbp)
mov    %eax,-0x34(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movw   $0xc300,0x10(%rsp)
movl   $0xb8,0xc(%rsp)
cmp    $0x1,%edi
jg     117d <main+0x3d>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%r14d
jmp    1210 <main+0xd0>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,0x14(%rsp)
lea    0xd(%rsp),%rdi
xor    %r14d,%r14d
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
mov    0x2e26(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e04(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x6,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rsi
mov    $0x6,%edx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <memcpy@plt>
xor    %eax,%eax
call   *%rbx
mov    0x2dca(%rip),%rcx
mov    (%rcx),%edi
lea    0xe15(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.code = [6 x i8] c"\B8\00\00\00\00\C3"
@stderr = global i32 0
@.str = [22 x i8] c"Usage: jit <integer>\0A\00"
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@stdout = global i32 0
@.str.1 = [15 x i8] c"Returned : %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [6 x i8]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32 (...)*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [6 x i8]* %6 to i8*
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %41
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %7
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 1
%23 = call i32 (i8*, ...) @memcpy(i8* %22, i32* %7, i32 4)
%24 = load i32, i32* @PROT_WRITE
%25 = load i32, i32* @PROT_EXEC
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_ANON
%28 = load i32, i32* @MAP_PRIVATE
%29 = or i32 %27, %28
%30 = call i8* @mmap(i32* null, i32 6, i32 %26, i32 %29, i32 -1, i32 0)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%33 = call i32 (i8*, ...) @memcpy(i8* %31, i8* %32, i64 6)
%34 = load i8*, i8** %8
%35 = bitcast i8* %34 to i32 (...)*
store i32 (...)* %35, i32 (...)** %9
%36 = load i32 (...)*, i32 (...)** %9
%37 = call i32 (...) %36()
store i32 %37, i32* %10
%38 = load i32, i32* @stdout
%39 = load i32, i32* %10
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %17, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@__const.main.code = [6 x i8] c"\B8\00\00\00\00\C3"
@stderr = global i32 0
@.str = [22 x i8] c"Usage: jit <integer>\0A\00"
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@stdout = global i32 0
@.str.1 = [15 x i8] c"Returned : %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [6 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %2
%11 = bitcast i32* %4 to i8*
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
store i32 %14, i32* %4
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 1
%16 = call i32 (i8*, ...) @memcpy(i8* %15, i32* %4, i32 4)
%17 = load i32, i32* @PROT_WRITE
%18 = load i32, i32* @PROT_EXEC
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_ANON
%21 = load i32, i32* @MAP_PRIVATE
%22 = or i32 %21, %20
%23 = call i8* @mmap(i32* null, i32 6, i32 %19, i32 %22, i32 -1, i32 0)
%24 = call i32 (i8*, ...) @memcpy(i8* %23, i8* %5, i64 6)
%25 = bitcast i8* %23 to i32 (...)*
%26 = call i32 (...) %25()
%27 = load i32, i32* @stdout
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %29
29: ; preds = %10, %7
%30 = phi i32 [ 1, %7 ], [ 0, %10 ]
ret i32 %30
}