#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*/ analyze_code(int *, int *, int);
int /*<<< orphan*/ code_transformer(int *, int *, int *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_instructions(int *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ show_shellcode(int *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  if (argc <= 1 || argc > 2) {
    perror("Not args or not enough, filename of file with opcodes it's only "
           "needed :(");
    exit(1);
  }
  srand(time(NULL));
  FILE *fd;
  fd = fopen(argv[1], "r");
  if (!fd) {
    perror("Cannot open file :(");
    exit(1);
  }
  int num_instructions = 0x00;
  int *pnum_instructions = &num_instructions;
  fscanf(fd, "%i", &num_instructions);
  int *instructions = (int *)malloc(num_instructions * sizeof(int) * 1000);
  int *new_shellcode = (int *)malloc(num_instructions * sizeof(int) * 1000);
  read_instructions(instructions, fd, num_instructions);
  fclose(fd);
  printf("\n\n\t\t    ***[ Initializing code analyzer ]***\n\n");
  analyze_code(instructions, new_shellcode, num_instructions);
  printf("\n\n\t\t    ***[ End code analyzer ]***\n\n");
  printf("\n\n\t\t    ***[ Initializing code transformer ]***\n\n");
  code_transformer(instructions, new_shellcode, pnum_instructions);
  printf("\n\n\t\t    ***[ End code transformer ]***\n\n");
  show_shellcode(instructions, num_instructions);
  free(instructions);
  free(new_shellcode);
  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)
cmpl   $0x1,-0x8(%rbp)
jle    120a <main+0x2a>
cmpl   $0x2,-0x8(%rbp)
jle    1220 <main+0x40>
lea    0xdef(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0b(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1269 <main+0x89>
lea    0xdf2(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x1c(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xddd(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <fscanf@plt>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
imul   $0x3e8,%rax,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
imul   $0x3e8,%rax,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1100 <read_instructions@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd83(%rip),%rdi
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10c0 <analyze_code@plt>
lea    0xd96(%rip),%rdi
call   1070 <printf@plt>
lea    0xdb0(%rip),%rdi
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <code_transformer@plt>
lea    0xdc5(%rip),%rdi
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <show_shellcode@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12eb <main+0x10b>
mov    %rsi,%rbx
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
mov    0x8(%rbx),%rdi
lea    0xe3d(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12f4 <main+0x114>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
lea    0xe33(%rip),%rsi
lea    0x4(%rsp),%r15
mov    %rax,%rdi
mov    %r15,%rdx
call   1080 <fscanf@plt>
imul   $0xfa0,0x4(%rsp),%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
imul   $0xfa0,0x4(%rsp),%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1100 <read_instructions@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xde8(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   10c0 <analyze_code@plt>
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <code_transformer@plt>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%esi
mov    %r12,%rdi
call   1060 <show_shellcode@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd0e(%rip),%rdi
jmp    12fb <main+0x11b>
lea    0xd51(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [74 x i8] c"Not args or not enough, filename of file with opcodes it's only needed :(\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"Cannot open file :(\00"
@.str.3 = [3 x i8] c"%i\00"
@.str.4 = [47 x i8] c"\0A\0A\09\09 ***[ Initializing code analyzer ]***\0A\0A\00"
@.str.5 = [38 x i8] c"\0A\0A\09\09 ***[ End code analyzer ]***\0A\0A\00"
@.str.6 = [50 x i8] c"\0A\0A\09\09 ***[ Initializing code transformer ]***\0A\0A\00"
@.str.7 = [41 x i8] c"\0A\0A\09\09 ***[ End code transformer ]***\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp sle i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %13, %2
%17 = call i32 @perror(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = call i32 @time(i32* null)
%21 = call i32 @srand(i32 %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
store i32 0, i32* %7
store i32* %7, i32** %8
%32 = load i32*, i32** %6
%33 = call i32 @fscanf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %7)
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = mul i64 %35, 4
%37 = mul i64 %36, 1000
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %9
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 4
%44 = mul i64 %43, 1000
%45 = trunc i64 %44 to i32
%46 = call i64 @malloc(i32 %45)
%47 = inttoptr i64 %46 to i32*
store i32* %47, i32** %10
%48 = load i32*, i32** %9
%49 = load i32*, i32** %6
%50 = load i32, i32* %7
%51 = call i32 @read_instructions(i32* %48, i32* %49, i32 %50)
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32*, i32** %9
%56 = load i32*, i32** %10
%57 = load i32, i32* %7
%58 = call i32 @analyze_code(i32* %55, i32* %56, i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%60 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0))
%61 = load i32*, i32** %9
%62 = load i32*, i32** %10
%63 = load i32*, i32** %8
%64 = call i32 @code_transformer(i32* %61, i32* %62, i32* %63)
%65 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0))
%66 = load i32*, i32** %9
%67 = load i32, i32* %7
%68 = call i32 @show_shellcode(i32* %66, i32 %67)
%69 = load i32*, i32** %9
%70 = call i32 @free(i32* %69)
%71 = load i32*, i32** %10
%72 = call i32 @free(i32* %71)
ret i32 0
}

LLVM IR (-O2):
@.str = [74 x i8] c"Not args or not enough, filename of file with opcodes it's only needed :(\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"Cannot open file :(\00"
@.str.3 = [3 x i8] c"%i\00"
@str = [46 x i8] c"\0A\0A\09\09 ***[ Initializing code analyzer ]***\0A\00"
@str.8 = [37 x i8] c"\0A\0A\09\09 ***[ End code analyzer ]***\0A\00"
@str.9 = [49 x i8] c"\0A\0A\09\09 ***[ Initializing code transformer ]***\0A\00"
@str.10 = [40 x i8] c"\0A\0A\09\09 ***[ End code transformer ]***\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @time(i32* null)
%10 = tail call i32 @srand(i32 %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%20 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%21 = load i32, i32* %3
%22 = mul i32 %21, 4000
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i32*
%25 = load i32, i32* %3
%26 = mul i32 %25, 4000
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i32*
%29 = load i32, i32* %3
%30 = call i32 @read_instructions(i32* %24, i32* %13, i32 %29)
%31 = call i32 @fclose(i32* %13)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%33 = load i32, i32* %3
%34 = call i32 @analyze_code(i32* %24, i32* %28, i32 %33)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.8, i64 0, i64 0))
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.9, i64 0, i64 0))
%37 = call i32 @code_transformer(i32* %24, i32* %28, i32* %3)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.10, i64 0, i64 0))
%39 = load i32, i32* %3
%40 = call i32 @show_shellcode(i32* %24, i32 %39)
%41 = call i32 @free(i32* %24)
%42 = call i32 @free(i32* %28)
ret i32 0
}