#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*/ execlp(char *, char *, int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
main() {
  int parentID = getpid();
  char prgname[1024];
  gets(prgname);
  int cid = fork();
  if (cid == 0) {
    execlp(prgname, prgname, 0);
    printf("I didn't find program %s\n", prgname);
  } else {
    sleep(1);
    waitpid(cid, 0, 0);
    printf("Program %s finished\n", prgname);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <gets@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jne    11df <main+0x6f>
lea    -0x410(%rbp),%rdi
lea    -0x410(%rbp),%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
lea    -0x410(%rbp),%rsi
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
jmp    120b <main+0x9b>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x414(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <waitpid@plt>
lea    -0x410(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rsp,%rdi
call   1050 <gets@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11b2 <main+0x42>
mov    %eax,%ebx
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xe6a(%rip),%rdi
jmp    11c6 <main+0x56>
mov    %rsp,%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
lea    0xe3a(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"I didn't find program %s\0A\00"
@.str.1 = [21 x i8] c"Program %s finished\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @getpid()
store i32 %5, i32* %2
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @gets(i8* %6)
%8 = call i32 (...) @fork()
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = call i32 @execlp(i8* %12, i8* %13, i32 0)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %23
17: ; preds = %0
%18 = call i32 @sleep(i32 1)
%19 = load i32, i32* %4
%20 = call i32 @waitpid(i32 %19, i32 0, i32 0)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %17, %11
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [26 x i8] c"I didn't find program %s\0A\00"
@.str.1 = [21 x i8] c"Program %s finished\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = tail call i32 (...) @getpid()
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = call i32 @gets(i8* %3)
%5 = call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @execlp(i8* %3, i8* %3, i32 0)
br label %12
9: ; preds = %0
%10 = call i32 @sleep(i32 1)
%11 = call i32 @waitpid(i32 %5, i32 0, i32 0)
br label %12
12: ; preds = %9, %7
%13 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), %7 ]
%14 = call i32 @printf(i8* %13, i8* %3)
ret i32 0
}