#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*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("usage: ./program arg1. Exiting...\n");
    exit(-1);
  }
  if (strlen(argv[1]) > 20) {
    printf("Buffer Overflow Attempt. Exiting...\n");
    exit(-2);
  }
  char *p2 = malloc(20);
  char *p3 = malloc(20);
  memset(p3, 1, 20);
  strcpy(p2, argv[1]);
  return 0;
}


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   $0x2,-0x8(%rbp)
je     1196 <main+0x36>
lea    0xe79(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
cmp    $0x14,%eax
jle    11c2 <main+0x62>
lea    0xe70(%rip),%rdi
call   1050 <printf@plt>
mov    $0xfffffffe,%edi
call   1080 <exit@plt>
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11b9 <main+0x59>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1050 <strlen@plt>
cmp    $0x15,%eax
jge    11cf <main+0x6f>
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe64(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xe2a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffe,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"usage: ./program arg1. Exiting...\0A\00"
@.str.1 = [37 x i8] c"Buffer Overflow Attempt. Exiting...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strlen(i8* %16)
%18 = icmp sgt i32 %17, 20
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -2)
unreachable
22: ; preds = %13
%23 = call i8* @malloc(i32 20)
store i8* %23, i8** %6
%24 = call i8* @malloc(i32 20)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = call i32 @memset(i8* %25, i32 1, i32 20)
%27 = load i8*, i8** %6
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @strcpy(i8* %27, i8* %30)
ret i32 0
}

LLVM IR (-O2):
@str = [36 x i8] c"Buffer Overflow Attempt. Exiting...\00"
@str.2 = [34 x i8] c"usage: ./program arg1. Exiting...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.2, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strlen(i8* %9)
%11 = icmp sgt i32 %10, 20
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -2)
unreachable
15: ; preds = %7
%16 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%17 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%18 = tail call i32 @memset(i8* %17, i32 1, i32 20)
%19 = load i8*, i8** %8
%20 = tail call i32 @strcpy(i8* %16, i8* %19)
ret i32 0
}