#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*/ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ foo();
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int main(int ac, char *av[]) {
  if (ac == 1) {
    printf("This program will exec() a new program.  The new program will\n");
    printf("overflow the buffer using strcpy().\n");
    printf("If you get a /bin/sh prompt, then the exploit has worked.\n");
    printf("Press any key to continue...");
    getchar();
    execl(av[0], av[0], "xyz", NULL);
    perror("execl()");
  } else if ((ac != 2) || (strcmp(av[1], "xyz") != 0)) {
    printf("usage: %s\n", av[0]);
    exit(1);
  }
  foo();
  printf("If you see this statement, it means that the buffer overflow\n");
  printf("never occurred.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11fe <main+0x8e>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xede(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xed8(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <execl@plt>
lea    0xecc(%rip),%rdi
call   1070 <perror@plt>
jmp    124a <main+0xda>
cmpl   $0x2,-0x8(%rbp)
jne    1226 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xea5(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     1245 <main+0xd5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    124a <main+0xda>
mov    $0x0,%al
call   1060 <foo@plt>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11c5 <main+0x45>
mov    0x8(%rbx),%rdi
lea    0xe89(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1228 <main+0xa8>
xor    %eax,%eax
call   1070 <foo@plt>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
cmp    $0x1,%edi
jne    1228 <main+0xa8>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
lea    0xefa(%rip),%rdi
call   1030 <puts@plt>
lea    0xe0b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    (%rbx),%rdi
lea    0xe10(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10a0 <execl@plt>
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
jmp    11a2 <main+0x22>
mov    (%rbx),%rsi
lea    0xdf7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [63 x i8] c"This program will exec() a new program. The new program will\0A\00"
@.str.1 = [37 x i8] c"overflow the buffer using strcpy().\0A\00"
@.str.2 = [59 x i8] c"If you get a /bin/sh prompt, then the exploit has worked.\0A\00"
@.str.3 = [29 x i8] c"Press any key to continue...\00"
@.str.4 = [4 x i8] c"xyz\00"
@.str.5 = [8 x i8] c"execl()\00"
@.str.6 = [11 x i8] c"usage: %s\0A\00"
@.str.7 = [62 x i8] c"If you see this statement, it means that the buffer overflow\0A\00"
@.str.8 = [17 x i8] c"never occurred.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %22
8: ; preds = %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 (...) @getchar()
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @execl(i8* %16, i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* null)
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %38
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 2
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %25, %22
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %25
br label %38
38: ; preds = %37, %8
%39 = call i32 (...) @foo()
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [29 x i8] c"Press any key to continue...\00"
@.str.4 = [4 x i8] c"xyz\00"
@.str.5 = [8 x i8] c"execl()\00"
@.str.6 = [11 x i8] c"usage: %s\0A\00"
@str = [61 x i8] c"If you see this statement, it means that the buffer overflow\00"
@str.9 = [16 x i8] c"never occurred.\00"
@str.10 = [62 x i8] c"This program will exec() a new program. The new program will\00"
@str.11 = [36 x i8] c"overflow the buffer using strcpy().\00"
@str.12 = [58 x i8] c"If you get a /bin/sh prompt, then the exploit has worked.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %17 [
i32 1, label %3
i32 2, label %12
]
3: ; preds = %2
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.10, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.11, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str.12, i64 0, i64 0))
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%8 = tail call i32 (...) @getchar()
%9 = load i8*, i8** %1
%10 = tail call i32 @execl(i8* %9, i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* null)
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %21
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %2, %12
%18 = load i8*, i8** %1
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12, %3
%22 = tail call i32 (...) @foo()
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
ret i32 0
}