#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*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  char executable[] = "prog";
  char *newargv[] = {executable, NULL, NULL, NULL};
  char *newenviron[] = {NULL};
  printf("I am %s, PID = %ld\n", argv[0], (long)getpid());
  printf("About to replace myself with the executable %s...\n", executable);
  sleep(2);
  execve(executable, newargv, newenviron);
  perror("execve");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe74(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xe6f(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x15(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x15(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    -0x15(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1060 <execve@plt>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movb   $0x0,0x4(%rsp)
movl   $0x676f7270,(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
mov    %rsp,%r14
mov    %r14,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    (%rsi),%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe63(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
call   1050 <execve@plt>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.main.executable = [5 x i8] c"prog\00"
@.str = [20 x i8] c"I am %s, PID = %ld\0A\00"
@.str.1 = [51 x i8] c"About to replace myself with the executable %s...\0A\00"
@.str.2 = [7 x i8] c"execve\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [5 x i8]
%7 = alloca [4 x i8*]
%8 = alloca [1 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [5 x i8]* %6 to i8*
%10 = bitcast [4 x i8*]* %7 to i8*
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 0
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
store i8* %12, i8** %11
%13 = getelementptr inbounds i8*, i8** %11, i64 1
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = bitcast [1 x i8*]* %8 to i8*
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i64 (...) @getpid()
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %19, i64 %20)
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 @sleep(i32 2)
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 0
%27 = getelementptr inbounds [1 x i8*], [1 x i8*]* %8, i64 0, i64 0
%28 = call i32 @execve(i8* %25, i8** %26, i8** %27)
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@__const.main.executable = [5 x i8] c"prog\00"
@.str = [20 x i8] c"I am %s, PID = %ld\0A\00"
@.str.1 = [51 x i8] c"About to replace myself with the executable %s...\0A\00"
@.str.2 = [7 x i8] c"execve\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5 x i8]
%4 = alloca [4 x i8*]
%5 = alloca [1 x i8*]
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = bitcast [4 x i8*]* %4 to i8*
%8 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
store i8* %6, i8** %8
%9 = bitcast [1 x i8*]* %5 to i8*
%10 = bitcast [1 x i8*]* %5 to i64*
store i64 0, i64* %10
%11 = load i8*, i8** %1
%12 = call i64 (...) @getpid()
%13 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %11, i64 %12)
%14 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%15 = call i32 @sleep(i32 2)
%16 = getelementptr inbounds [1 x i8*], [1 x i8*]* %5, i64 0, i64 0
%17 = call i32 @execve(i8* %6, i8** %8, i8** %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
}