#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 PATH_MAX;
int /*<<< orphan*/ execl(char *, char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  char prog[PATH_MAX];
  int len;
  strcpy(prog, argv[0]);
  len = strlen(prog);
  memcpy(prog + len - 15, "crashme", 7);
  prog[len - 8] = 0;
  printf("foll-exec is about to execl(crashme)...\n");
  execl(prog, prog, (char *)0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffff1,%rdi
lea    0xe47(%rip),%rsi
mov    $0x7,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x8,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xe2d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1070 <execl@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%rsi),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%r14
lea    (%r14,%rbx,1),%rdi
add    $0xfffffffffffffff1,%rdi
lea    0xe6a(%rip),%rsi
mov    $0x7,%edx
call   1060 <memcpy@plt>
movb   $0x0,-0x8(%r14,%rbx,1)
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <execl@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [8 x i8] c"crashme\00"
@.str.1 = [41 x i8] c"foll-exec is about to execl(crashme)...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @strcpy(i8* %11, i8* %14)
%16 = call i32 @strlen(i8* %11)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %11, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -15
%21 = call i32 @memcpy(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%22 = load i32, i32* %7
%23 = sub nsw i32 %22, 8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %11, i64 %24
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @execl(i8* %11, i8* %11, i8* null)
%28 = load i8*, i8** %5
ret i32 0
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [8 x i8] c"crashme\00"
@str = [40 x i8] c"foll-exec is about to execl(crashme)...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i8*, i8** %1
%7 = call i32 @strcpy(i8* %5, i8* %6)
%8 = call i32 @strlen(i8* %5)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
%11 = getelementptr inbounds i8, i8* %10, i64 -15
%12 = call i32 @memcpy(i8* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%13 = add nsw i32 %8, -8
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %5, i64 %14
store i8 0, i8* %15
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%17 = call i32 @execl(i8* %5, i8* %5, i8* null)
ret i32 0
}