#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 *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void exploit(char *InputString) {
  char buf1[5];
  char buf2[5];
  strcpy(buf1, "     ");
  strcpy(buf2, "     ");
  strcpy(buf2, InputString);
  printf("input: [%p]'%s'\n", InputString, InputString);
  printf("buf1: [%p]'%s'\n", buf1, buf1);
  printf("buf2: [%p]'%s'\n", buf2, buf2);
}


x86 assembly (clang, -O0):
<exploit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xd(%rbp),%rdi
lea    0xec9(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x12(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x12(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe9e(%rip),%rdi
call   1040 <printf@plt>
lea    -0xd(%rbp),%rsi
lea    -0xd(%rbp),%rdx
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
lea    -0x12(%rbp),%rsi
lea    -0x12(%rbp),%rdx
lea    0xe97(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exploit>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xecb(%rip),%r14
lea    0x13(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xe(%rsp),%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xe9f(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
call   1040 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    %r15,%rsi
mov    %r15,%rdx
call   1040 <printf@plt>
lea    0xe9c(%rip),%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1040 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c" \00"
@.str.1 = [17 x i8] c"input: [%p]'%s'\0A\00"
@.str.2 = [16 x i8] c"buf1: [%p]'%s'\0A\00"
@.str.3 = [16 x i8] c"buf2: [%p]'%s'\0A\00"
define void @exploit(i8* %0) {
%2 = alloca i8*
%3 = alloca [5 x i8]
%4 = alloca [5 x i8]
store i8* %0, i8** %2
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%10 = load i8*, i8** %2
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = load i8*, i8** %2
%13 = load i8*, i8** %2
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %13)
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %16)
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %18, i8* %19)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c" \00"
@.str.1 = [17 x i8] c"input: [%p]'%s'\0A\00"
@.str.2 = [16 x i8] c"buf1: [%p]'%s'\0A\00"
@.str.3 = [16 x i8] c"buf2: [%p]'%s'\0A\00"
define void @exploit(i8* %0) {
%2 = alloca [5 x i8]
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strcpy(i8* %5, i8* %0)
%9 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %0)
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %4)
%11 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %5)
ret void
}