#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);
int /*<<< orphan*/ foo();
int fork();
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int ac, char *av[]) {
  int res;
  printf("This program will fork() child process, and the child\n");
  printf("will 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();
  if ((res = fork()) == -1) {
    perror("fork()");
    exit(1);
  } else if (res == 0) {
    foo();
    printf("If you see this statement, it means that the buffer\n");
    printf("overflow never occurred.\n");
    return 0;
  }
  wait(&res);
  printf("parent process terminating\n");
  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)
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
lea    0xe9e(%rip),%rdi
call   1030 <printf@plt>
lea    0xebc(%rip),%rdi
call   1030 <printf@plt>
lea    0xeeb(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e6 <main+0x76>
lea    0xee2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    121b <main+0xab>
mov    $0x0,%al
call   1050 <foo@plt>
lea    0xec2(%rip),%rdi
call   1030 <printf@plt>
lea    0xeeb(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    123c <main+0xcc>
jmp    1220 <main+0xb0>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
lea    0xedf(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
je     11df <main+0x5f>
cmp    $0xffffffff,%eax
je     1202 <main+0x82>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
lea    0xee0(%rip),%rdi
jmp    11f9 <main+0x79>
xor    %eax,%eax
call   1060 <foo@plt>
lea    0xeeb(%rip),%rdi
call   1030 <puts@plt>
lea    0xf13(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [55 x i8] c"This program will fork() child process, and the child\0A\00"
@.str.1 = [42 x i8] c"will 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 = [7 x i8] c"fork()\00"
@.str.5 = [53 x i8] c"If you see this statement, it means that the buffer\0A\00"
@.str.6 = [26 x i8] c"overflow never occurred.\0A\00"
@.str.7 = [28 x i8] c"parent process terminating\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 (...) @getchar()
%12 = call i32 (...) @fork()
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = call i32 (...) @foo()
%22 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %28
24: ; preds = %17
br label %25
25: ; preds = %24
%26 = call i32 @wait(i32* %6)
%27 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %28
28: ; preds = %25, %20
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str.3 = [29 x i8] c"Press any key to continue...\00"
@.str.4 = [7 x i8] c"fork()\00"
@str = [54 x i8] c"This program will fork() child process, and the child\00"
@str.8 = [41 x i8] c"will overflow the buffer using strcpy().\00"
@str.9 = [58 x i8] c"If you get a /bin/sh prompt, then the exploit has worked.\00"
@str.10 = [27 x i8] c"parent process terminating\00"
@str.11 = [52 x i8] c"If you see this statement, it means that the buffer\00"
@str.12 = [25 x i8] c"overflow never occurred.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str.9, i64 0, i64 0))
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i32 (...) @getchar()
%10 = tail call i32 (...) @fork()
store i32 %10, i32* %3
switch i32 %10, label %18 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 (...) @foo()
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.11, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
br label %21
18: ; preds = %2
%19 = call i32 @wait(i32* %3)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0))
br label %21
21: ; preds = %18, %14
ret i32 0
}