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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int rand();
void fill_with_junk(int *instructions, int *new_shellcode, int *num_inst) {
  char num_rep, num_oprt, uns_sign, rnd_regs, add_junk;
  int junk_opt[4] = {0x00000000, 0x00000020, 0x00000022, 0x00000025};
  int pos = 0;
  int tot_inst = (*num_inst);
  int act_inst = 0x0;
  for (unsigned volatile int i = 0; i < tot_inst * 1000 & pos < tot_inst;) {
    if (pos < tot_inst) {
      instructions[i] = new_shellcode[pos++];
    }
    add_junk = rand() % 2;
    if (add_junk) {
      printf("Adding junk for instruction: 0x%08x\n\n", instructions[i]);
      num_rep = rand() % 50;
      (*num_inst) += num_rep;
      for (unsigned volatile int j = 1; j < num_rep + 1; j++) {
        num_oprt = rand() % 4;
        act_inst = junk_opt[num_oprt];
        uns_sign = rand() % 2;
        rnd_regs = rand() % 26 + 2;
        if (uns_sign) {
          if (act_inst % 2 == 0)
            act_inst |= 0x00000001;
          else
            act_inst &= 0xFFFFFFFE;
        }
        act_inst |= (rnd_regs << 20);
        act_inst |= (rnd_regs << 10);
        instructions[i + j] = act_inst;
        printf("\t -> Added instruction 0x%08x\n", act_inst);
      }
      printf("\n\n-------------------------------------------------\n\n");
      i += num_rep + 1;
    } else
      i++;
  }
}


x86 assembly (clang, -O0):
<fill_with_junk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xec5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xec2(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
imul   $0x3e8,-0x38(%rbp),%ecx
cmp    %ecx,%eax
setb   %al
and    $0x1,%al
movzbl %al,%eax
mov    -0x34(%rbp),%ecx
cmp    -0x38(%rbp),%ecx
setl   %cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1336 <fill_with_junk+0x216>
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    11c3 <fill_with_junk+0xa3>
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x1d(%rbp)
cmpb   $0x0,-0x1d(%rbp)
je     1328 <fill_with_junk+0x208>
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,4),%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x32,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
movsbl -0x19(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jae    1306 <fill_with_junk+0x1e6>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x1a(%rbp)
movsbq -0x1a(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x1b(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x2,%edx
mov    %dl,%al
mov    %al,-0x1c(%rbp)
cmpb   $0x0,-0x1b(%rbp)
je     12b9 <fill_with_junk+0x199>
mov    -0x3c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12ab <fill_with_junk+0x18b>
mov    -0x3c(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12b4 <fill_with_junk+0x194>
mov    -0x3c(%rbp),%eax
and    $0xfffffffe,%eax
mov    %eax,-0x3c(%rbp)
jmp    12b9 <fill_with_junk+0x199>
movsbl -0x1c(%rbp),%eax
shl    $0x14,%eax
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movsbl -0x1c(%rbp),%eax
shl    $0xa,%eax
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    -0x44(%rbp),%esi
add    %esi,%ecx
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%esi
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1222 <fill_with_junk+0x102>
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x19(%rbp),%ecx
add    $0x1,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x40(%rbp)
jmp    1331 <fill_with_junk+0x211>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1168 <fill_with_junk+0x48>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_with_junk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,0x28(%rsp)
mov    %rdx,0x20(%rsp)
mov    (%rdx),%ecx
movl   $0x0,0xc(%rsp)
mov    0xc(%rsp),%eax
mov    %rcx,0x18(%rsp)
test   %ecx,%ecx
jle    132c <fill_with_junk+0x1fc>
imul   $0x3e8,0x18(%rsp),%ecx
mov    %ecx,0x14(%rsp)
cmp    %ecx,%eax
jae    132c <fill_with_junk+0x1fc>
mov    %rdi,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    0x28(%rsp),%rax
mov    (%rax,%rbp,4),%eax
add    $0x1,%rbp
mov    0xc(%rsp),%ecx
mov    %eax,(%rbx,%rcx,4)
xor    %eax,%eax
call   1050 <rand@plt>
mov    %eax,%edx
shr    $0x1f,%edx
add    %eax,%edx
and    $0xfe,%edx
mov    0xc(%rsp),%ecx
cmp    %dl,%al
je     1310 <fill_with_junk+0x1e0>
mov    %rbp,0x30(%rsp)
mov    (%rbx,%rcx,4),%esi
lea    0xe4e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x24,%rcx
add    %edx,%ecx
imul   $0x32,%ecx,%ecx
mov    %eax,%edx
sub    %ecx,%edx
mov    0x20(%rsp),%rsi
add    %edx,(%rsi)
movl   $0x1,0x10(%rsp)
neg    %ecx
lea    (%rax,%rcx,1),%r14d
add    $0x1,%r14d
jmp    1245 <fill_with_junk+0x115>
nopl   0x0(%rax,%rax,1)
and    $0xfffffffe,%r15d
lea    (%rax,%rcx,1),%esi
add    $0x2,%esi
mov    %esi,%eax
shl    $0x14,%eax
shl    $0xa,%esi
or     %eax,%esi
or     %r15d,%esi
mov    0xc(%rsp),%eax
add    0x10(%rsp),%eax
mov    %esi,(%rbx,%rax,4)
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
addl   $0x1,0x10(%rsp)
mov    0x10(%rsp),%eax
cmp    %r14d,%eax
jae    12f0 <fill_with_junk+0x1c0>
xor    %eax,%eax
call   1050 <rand@plt>
mov    %eax,%r12d
lea    0x3(%r12),%eax
test   %r12d,%r12d
cmovns %r12d,%eax
and    $0xfc,%eax
sub    %eax,%r12d
shl    $0x38,%r12
mov    %r12,%rax
sar    $0x36,%rax
lea    0xd7e(%rip),%rcx
mov    (%rax,%rcx,1),%r15d
xor    %eax,%eax
call   1050 <rand@plt>
mov    %eax,%r13d
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
and    $0xfe,%ebp
xor    %eax,%eax
call   1050 <rand@plt>
movslq %eax,%rcx
imul   $0x4ec4ec4f,%rcx,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rdx
add    %ecx,%edx
lea    (%rdx,%rdx,4),%ecx
lea    (%rcx,%rcx,4),%ecx
add    %edx,%ecx
neg    %ecx
cmp    %bpl,%r13b
je     1214 <fill_with_junk+0xe4>
movabs $0x300000000000000,%rdx
cmp    %rdx,%r12
je     1210 <fill_with_junk+0xe0>
or     $0x1,%r15d
jmp    1214 <fill_with_junk+0xe4>
nopw   0x0(%rax,%rax,1)
lea    0xd5e(%rip),%rdi
call   1030 <puts@plt>
add    %r14d,0xc(%rsp)
mov    0x30(%rsp),%rbp
jmp    1317 <fill_with_junk+0x1e7>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ecx
mov    %ecx,0xc(%rsp)
mov    0xc(%rsp),%eax
cmp    0x18(%rsp),%rbp
jae    132c <fill_with_junk+0x1fc>
cmp    0x14(%rsp),%eax
jb     1180 <fill_with_junk+0x50>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.fill_with_junk.junk_opt = [4 x i32] [i32 0, i32 32, i32 34, i32 37]
@.str = [38 x i8] c"Adding junk for instruction: 0x%08x\0A\0A\00"
@.str.1 = [31 x i8] c"\09 -> Added instruction 0x%08x\0A\00"
@.str.2 = [54 x i8] c"\0A\0A-------------------------------------------------\0A\0A\00"
define void @fill_with_junk(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = alloca [4 x i32]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%18 = bitcast [4 x i32]* %12 to i8*
store i32 0, i32* %13
%19 = load i32*, i32** %6
%20 = load i32, i32* %19
store i32 %20, i32* %14
store i32 0, i32* %15
store volatile i32 0, i32* %16
br label %21
21: ; preds = %136, %3
%22 = load volatile i32, i32* %16
%23 = load i32, i32* %14
%24 = mul nsw i32 %23, 1000
%25 = icmp ult i32 %22, %24
%26 = zext i1 %25 to i32
%27 = load i32, i32* %13
%28 = load i32, i32* %14
%29 = icmp slt i32 %27, %28
%30 = zext i1 %29 to i32
%31 = and i32 %26, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %137
33: ; preds = %21
%34 = load i32, i32* %13
%35 = load i32, i32* %14
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %48
37: ; preds = %33
%38 = load i32*, i32** %5
%39 = load i32, i32* %13
%40 = add nsw i32 %39, 1
store i32 %40, i32* %13
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = load i32, i32* %42
%44 = load i32*, i32** %4
%45 = load volatile i32, i32* %16
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
store i32 %43, i32* %47
br label %48
48: ; preds = %37, %33
%49 = call i32 (...) @rand()
%50 = srem i32 %49, 2
%51 = trunc i32 %50 to i8
store i8 %51, i8* %11
%52 = load i8, i8* %11
%53 = icmp ne i8 %52, 0
br i1 %53, label %54, label %133
54: ; preds = %48
%55 = load i32*, i32** %4
%56 = load volatile i32, i32* %16
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
%59 = load i32, i32* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %59)
%61 = call i32 (...) @rand()
%62 = srem i32 %61, 50
%63 = trunc i32 %62 to i8
store i8 %63, i8* %7
%64 = load i8, i8* %7
%65 = sext i8 %64 to i32
%66 = load i32*, i32** %6
%67 = load i32, i32* %66
%68 = add nsw i32 %67, %65
store i32 %68, i32* %66
store volatile i32 1, i32* %17
br label %69
69: ; preds = %123, %54
%70 = load volatile i32, i32* %17
%71 = load i8, i8* %7
%72 = sext i8 %71 to i32
%73 = add nsw i32 %72, 1
%74 = icmp ult i32 %70, %73
br i1 %74, label %75, label %126
75: ; preds = %69
%76 = call i32 (...) @rand()
%77 = srem i32 %76, 4
%78 = trunc i32 %77 to i8
store i8 %78, i8* %8
%79 = load i8, i8* %8
%80 = sext i8 %79 to i64
%81 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 %80
%82 = load i32, i32* %81
store i32 %82, i32* %15
%83 = call i32 (...) @rand()
%84 = srem i32 %83, 2
%85 = trunc i32 %84 to i8
store i8 %85, i8* %9
%86 = call i32 (...) @rand()
%87 = srem i32 %86, 26
%88 = add nsw i32 %87, 2
%89 = trunc i32 %88 to i8
store i8 %89, i8* %10
%90 = load i8, i8* %9
%91 = icmp ne i8 %90, 0
br i1 %91, label %92, label %103
92: ; preds = %75
%93 = load i32, i32* %15
%94 = srem i32 %93, 2
%95 = icmp eq i32 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %92
%97 = load i32, i32* %15
%98 = or i32 %97, 1
store i32 %98, i32* %15
br label %102
99: ; preds = %92
%100 = load i32, i32* %15
%101 = and i32 %100, -2
store i32 %101, i32* %15
br label %102
102: ; preds = %99, %96
br label %103
103: ; preds = %102, %75
%104 = load i8, i8* %10
%105 = sext i8 %104 to i32
%106 = shl i32 %105, 20
%107 = load i32, i32* %15
%108 = or i32 %107, %106
store i32 %108, i32* %15
%109 = load i8, i8* %10
%110 = sext i8 %109 to i32
%111 = shl i32 %110, 10
%112 = load i32, i32* %15
%113 = or i32 %112, %111
store i32 %113, i32* %15
%114 = load i32, i32* %15
%115 = load i32*, i32** %4
%116 = load volatile i32, i32* %16
%117 = load volatile i32, i32* %17
%118 = add i32 %116, %117
%119 = zext i32 %118 to i64
%120 = getelementptr inbounds i32, i32* %115, i64 %119
store i32 %114, i32* %120
%121 = load i32, i32* %15
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %121)
br label %123
123: ; preds = %103
%124 = load volatile i32, i32* %17
%125 = add i32 %124, 1
store volatile i32 %125, i32* %17
br label %69
126: ; preds = %69
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%128 = load i8, i8* %7
%129 = sext i8 %128 to i32
%130 = add nsw i32 %129, 1
%131 = load volatile i32, i32* %16
%132 = add i32 %131, %130
store volatile i32 %132, i32* %16
br label %136
133: ; preds = %48
%134 = load volatile i32, i32* %16
%135 = add i32 %134, 1
store volatile i32 %135, i32* %16
br label %136
136: ; preds = %133, %126
br label %21
137: ; preds = %21
ret void
}

LLVM IR (-O2):
@__const.fill_with_junk.junk_opt = [4 x i32] [i32 0, i32 32, i32 34, i32 37]
@.str = [38 x i8] c"Adding junk for instruction: 0x%08x\0A\0A\00"
@.str.1 = [31 x i8] c"\09 -> Added instruction 0x%08x\0A\00"
@str = [53 x i8] c"\0A\0A-------------------------------------------------\0A\00"
define void @fill_with_junk(i32* nocapture %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* %2
%7 = bitcast i32* %4 to i8*
store volatile i32 0, i32* %4
%8 = mul nsw i32 %6, 1000
%9 = load volatile i32, i32* %4
%10 = icmp ult i32 %9, %8
%11 = icmp sgt i32 %6, 0
%12 = and i1 %11, %10
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = zext i32 %6 to i64
%15 = bitcast i32* %5 to i8*
br label %17
16: ; preds = %85, %3
ret void
17: ; preds = %13, %85
%18 = phi i64 [ 0, %13 ], [ %19, %85 ]
%19 = add nuw nsw i64 %18, 1
%20 = getelementptr inbounds i32, i32* %1, i64 %18
%21 = load i32, i32* %20
%22 = load volatile i32, i32* %4
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %0, i64 %23
store i32 %21, i32* %24
%25 = tail call i32 (...) @rand()
%26 = srem i32 %25, 2
%27 = trunc i32 %26 to i8
%28 = icmp eq i8 %27, 0
%29 = load volatile i32, i32* %4
br i1 %28, label %83, label %30
30: ; preds = %17
%31 = zext i32 %29 to i64
%32 = getelementptr inbounds i32, i32* %0, i64 %31
%33 = load i32, i32* %32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = tail call i32 (...) @rand()
%36 = srem i32 %35, 50
%37 = load i32, i32* %2
%38 = add nsw i32 %37, %36
store i32 %38, i32* %2
store volatile i32 1, i32* %5
%39 = add nsw i32 %36, 1
%40 = load volatile i32, i32* %5
%41 = icmp ult i32 %40, %39
br i1 %41, label %46, label %42
42: ; preds = %67, %30
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%44 = load volatile i32, i32* %4
%45 = add i32 %44, %39
store volatile i32 %45, i32* %4
br label %85
46: ; preds = %30, %67
%47 = tail call i32 (...) @rand()
%48 = srem i32 %47, 4
%49 = zext i32 %48 to i64
%50 = shl i64 %49, 56
%51 = ashr exact i64 %50, 56
%52 = getelementptr inbounds [4 x i32], [4 x i32]* @__const.fill_with_junk.junk_opt, i64 0, i64 %51
%53 = load i32, i32* %52
%54 = tail call i32 (...) @rand()
%55 = srem i32 %54, 2
%56 = trunc i32 %55 to i8
%57 = tail call i32 (...) @rand()
%58 = srem i32 %57, 26
%59 = add nsw i32 %58, 2
%60 = icmp eq i8 %56, 0
br i1 %60, label %67, label %61
61: ; preds = %46
%62 = icmp eq i64 %50, 216172782113783808
br i1 %62, label %65, label %63
63: ; preds = %61
%64 = or i32 %53, 1
br label %67
65: ; preds = %61
%66 = and i32 %53, -2
br label %67
67: ; preds = %63, %65, %46
%68 = phi i32 [ %64, %63 ], [ %66, %65 ], [ %53, %46 ]
%69 = shl nsw i32 %59, 20
%70 = shl nsw i32 %59, 10
%71 = or i32 %70, %69
%72 = or i32 %71, %68
%73 = load volatile i32, i32* %4
%74 = load volatile i32, i32* %5
%75 = add i32 %74, %73
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds i32, i32* %0, i64 %76
store i32 %72, i32* %77
%78 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %72)
%79 = load volatile i32, i32* %5
%80 = add i32 %79, 1
store volatile i32 %80, i32* %5
%81 = load volatile i32, i32* %5
%82 = icmp ult i32 %81, %39
br i1 %82, label %46, label %42
83: ; preds = %17
%84 = add i32 %29, 1
store volatile i32 %84, i32* %4
br label %85
85: ; preds = %83, %42
%86 = load volatile i32, i32* %4
%87 = icmp ult i32 %86, %8
%88 = icmp ult i64 %19, %14
%89 = and i1 %88, %87
br i1 %89, label %17, label %16
}