#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 main(int argc, char *argv[]) {
  unsigned char code[] = {0xb8, 0x00, 0x00, 0x00, 0x00, 0xc3};
  if (argc < 2) {
    fprintf(stderr, "Usage: jit0-x64 <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;
  return func();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    1195 <main+0x55>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1220 <main+0xe0>
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    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    0x2e1b(%rip),%rax
or     (%rax),%edx
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
mov    0x2de1(%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,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movw   $0xc300,0x8(%rsp)
movl   $0xb8,0x4(%rsp)
cmp    $0x1,%edi
jg     1175 <main+0x35>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%eax
jmp    11ec <main+0xac>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,0xc(%rsp)
lea    0x5(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
mov    0x2e31(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e17(%rip),%rax
mov    0x2e08(%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    0x4(%rsp),%rsi
mov    $0x6,%edx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <memcpy@plt>
xor    %eax,%eax
call   *%rbx
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@__const.main.code = [6 x i8] c"\B8\00\00\00\00\C3"
@stderr = global i32 0
@.str = [27 x i8] c"Usage: jit0-x64 <integer>\0A\00"
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
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 @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %27
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()
br label %27
27: ; preds = %10, %7
%28 = phi i32 [ 1, %7 ], [ %26, %10 ]
ret i32 %28
}