#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ push_string(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ setup_pointer_array(int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  if (argc > 1) {
    FILE *f = fopen("output.c", "w");
    fprintf(f, "#include <stdio.h>\n#include <unistd.h>\nvoid printHex(const "
               "char *s);\n\n//Assemlbly comments in NASM syntax.\nchar "
               "*shellcode=\"\\x31\\xc0\" //xor eax, eax\n");
    push_string(f, argv[1]);
    fprintf(f, "\"\\x89\\xe3\" //mov ebx, esp\n");
    setup_pointer_array(f, (argc - 1));
    int i;
    for (i = 2; i < argc; i++) {
      push_string(f, argv[i]);
      fprintf(f,
              "\"\\x89\\xe2\" //mov edx, esp\n\"\\x89\\x51\\x%x\" //mov "
              "[ecx+%d], edx\n",
              ((i - 1) * 4), ((i - 1) * 4));
    }
    fprintf(
        f,
        "\"\\x50\" //push eax\n\"\\x89\\xe2\" //mov edx, esp\n\"\\xb0\\x0b\" "
        "//mov al, 11\n\"\\xcd\\x80\"; //int $0x80\n\nint main() {\n  "
        "printf(\"Shellcode : \");\n  printHex(shellcode);\n  int (*ret)() = "
        "(int(*)())shellcode;\n  ret();\n}\n\nvoid printHex(const char *s) {\n "
        " while (*s)\n    printf(\"\\\\x%%02x\", (unsigned int) *s++ & "
        "0xff);\n  printf(\"\\n\");\n}\n");
    fclose(f);
  } else {
    printf("Usage: ./shellcode_generator.out <desired command> "
           "<(OPTIONAL)desired args>... \n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1255 <main+0xf5>
lea    0xe79(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1060 <push_string@plt>
mov    -0x18(%rbp),%rdi
lea    0xedc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
call   1070 <setup_pointer_array@plt>
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1235 <main+0xd5>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1060 <push_string@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
shl    $0x2,%edx
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
shl    $0x2,%ecx
lea    0xe9c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e2 <main+0x82>
mov    -0x18(%rbp),%rdi
lea    0xeba(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1261 <main+0x101>
lea    0xfde(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     1254 <main+0xe4>
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe6f(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r13
lea    0xe64(%rip),%rdi
mov    $0x95,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    0x8(%r14),%rsi
mov    %r13,%rdi
call   1060 <push_string@plt>
lea    0xed5(%rip),%rdi
mov    $0x1a,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1090 <fwrite@plt>
lea    -0x1(%r15),%esi
mov    %r13,%rdi
call   1070 <setup_pointer_array@plt>
cmp    $0x2,%r15d
je     1239 <main+0xc9>
mov    %r15d,%r12d
mov    $0x2,%ebx
mov    $0x4,%ebp
lea    0xeb8(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14,%rbx,8),%rsi
mov    %r13,%rdi
call   1060 <push_string@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%rbx
add    $0x4,%ebp
cmp    %rbx,%r12
jne    1210 <main+0xa0>
lea    0xeba(%rip),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
jmp    1260 <main+0xf0>
lea    0xfdf(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"output.c\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [150 x i8] c"#include <stdio.h>\0A#include <unistd.h>\0Avoid printHex(const char *s);\0A\0A//Assemlbly comments in NASM syntax.\0Achar *shellcode=\22\\x31\\xc0\22 //xor eax, eax\0A\00"
@.str.3 = [27 x i8] c"\22\\x89\\xe3\22 //mov ebx, esp\0A\00"
@.str.4 = [62 x i8] c"\22\\x89\\xe2\22 //mov edx, esp\0A\22\\x89\\x51\\x%x\22 //mov [ecx+%d], edx\0A\00"
@.str.5 = [320 x i8] c"\22\\x50\22 //push eax\0A\22\\x89\\xe2\22 //mov edx, esp\0A\22\\xb0\\x0b\22 //mov al, 11\0A\22\\xcd\\x80\22; //int $0x80\0A\0Aint main() {\0A printf(\22Shellcode : \22);\0A printHex(shellcode);\0A int (*ret)() = (int(*)())shellcode;\0A ret();\0A}\0A\0Avoid printHex(const char *s) {\0A while (*s)\0A printf(\22\\\\x%%02x\22, (unsigned int) *s++ & 0xff);\0A printf(\22\\n\22);\0A}\0A\00"
@.str.6 = [81 x i8] c"Usage: ./shellcode_generator.out <desired command> <(OPTIONAL)desired args>... \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %53
10: ; preds = %2
%11 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32*, i32** %6
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @push_string(i32* %14, i8* %17)
%19 = load i32*, i32** %6
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32*, i32** %6
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = call i32 @setup_pointer_array(i32* %21, i32 %23)
store i32 2, i32* %7
br label %25
25: ; preds = %45, %10
%26 = load i32, i32* %7
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = load i32*, i32** %6
%31 = load i8**, i8*** %5
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @push_string(i32* %30, i8* %35)
%37 = load i32*, i32** %6
%38 = load i32, i32* %7
%39 = sub nsw i32 %38, 1
%40 = mul nsw i32 %39, 4
%41 = load i32, i32* %7
%42 = sub nsw i32 %41, 1
%43 = mul nsw i32 %42, 4
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %43)
br label %45
45: ; preds = %29
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %25
48: ; preds = %25
%49 = load i32*, i32** %6
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([320 x i8], [320 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
br label %55
53: ; preds = %2
%54 = call i32 @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %48
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [9 x i8] c"output.c\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [150 x i8] c"#include <stdio.h>\0A#include <unistd.h>\0Avoid printHex(const char *s);\0A\0A//Assemlbly comments in NASM syntax.\0Achar *shellcode=\22\\x31\\xc0\22 //xor eax, eax\0A\00"
@.str.3 = [27 x i8] c"\22\\x89\\xe3\22 //mov ebx, esp\0A\00"
@.str.4 = [62 x i8] c"\22\\x89\\xe2\22 //mov edx, esp\0A\22\\x89\\x51\\x%x\22 //mov [ecx+%d], edx\0A\00"
@.str.5 = [320 x i8] c"\22\\x50\22 //push eax\0A\22\\x89\\xe2\22 //mov edx, esp\0A\22\\xb0\\x0b\22 //mov al, 11\0A\22\\xcd\\x80\22; //int $0x80\0A\0Aint main() {\0A printf(\22Shellcode : \22);\0A printHex(shellcode);\0A int (*ret)() = (int(*)())shellcode;\0A ret();\0A}\0A\0Avoid printHex(const char *s) {\0A while (*s)\0A printf(\22\\\\x%%02x\22, (unsigned int) *s++ & 0xff);\0A printf(\22\\n\22);\0A}\0A\00"
@str = [80 x i8] c"Usage: ./shellcode_generator.out <desired command> <(OPTIONAL)desired args>... \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %30
4: ; preds = %2
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i64 @fwrite(i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.2, i64 0, i64 0), i64 149, i64 1, i32* %5)
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @push_string(i32* %5, i8* %8)
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i64 26, i64 1, i32* %5)
%11 = add nsw i32 %0, -1
%12 = tail call i32 @setup_pointer_array(i32* %5, i32 %11)
%13 = icmp eq i32 %0, 2
br i1 %13, label %27, label %14
14: ; preds = %4
%15 = zext i32 %0 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 2, %14 ], [ %25, %16 ]
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = tail call i32 @push_string(i32* %5, i8* %19)
%21 = trunc i64 %17 to i32
%22 = shl i32 %21, 2
%23 = add i32 %22, -4
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %23, i32 %23)
%25 = add nuw nsw i64 %17, 1
%26 = icmp eq i64 %25, %15
br i1 %26, label %27, label %16
27: ; preds = %16, %4
%28 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([320 x i8], [320 x i8]* @.str.5, i64 0, i64 0))
%29 = tail call i32 @fclose(i32* %5)
br label %32
30: ; preds = %2
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
ret i32 0
}