#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Pass command to run as an argument: \"./space parent\" /cmd/to/run "
           "[args]\n");
    exit(1);
  }
  pid_t pid = fork();
  if (pid == -1) {
    printf("fork() failed\n");
    exit(2);
  }
  if (pid == 0) {
    execv(argv[1], &argv[1]);
    printf("execv() failed\n");
    exit(3);
  } else {
    int status;
    waitpid(pid, &status, 0);
    if (status != 0) {
      printf("Child regurned with a non-zero status: %d\n", status);
      exit(4);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
jge    1181 <main+0x31>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ad <main+0x5d>
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11e4 <main+0x94>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1060 <execv@plt>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3,%edi
call   1050 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmpl   $0x0,-0x18(%rbp)
je     1217 <main+0xc7>
mov    -0x18(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <exit@plt>
jmp    121c <main+0xcc>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jle    119b <main+0x3b>
mov    %rsi,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b1 <main+0x51>
cmp    $0xffffffff,%eax
je     11d7 <main+0x77>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0xc(%rsp),%esi
test   %esi,%esi
jne    11ed <main+0x8d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1070 <execv@plt>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x4,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [73 x i8] c"Pass command to run as an argument: \22./space parent\22 /cmd/to/run [args]\0A\00"
@.str.1 = [15 x i8] c"fork() failed\0A\00"
@.str.2 = [16 x i8] c"execv() failed\0A\00"
@.str.3 = [43 x i8] c"Child regurned with a non-zero status: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = call i32 (...) @fork()
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 2)
unreachable
19: ; preds = %12
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = load i8**, i8*** %4
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = call i32 @execv(i8* %25, i8** %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 3)
unreachable
31: ; preds = %19
%32 = load i32, i32* %5
%33 = call i32 @waitpid(i32 %32, i32* %6, i32 0)
%34 = load i32, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = call i32 @exit(i32 4)
unreachable
40: ; preds = %31
br label %41
41: ; preds = %40
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [43 x i8] c"Child regurned with a non-zero status: %d\0A\00"
@str = [15 x i8] c"execv() failed\00"
@str.4 = [14 x i8] c"fork() failed\00"
@str.5 = [72 x i8] c"Pass command to run as an argument: \22./space parent\22 /cmd/to/run [args]\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str.5, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 (...) @fork()
switch i32 %9, label %19 [
i32 -1, label %10
i32 0, label %13
]
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @execv(i8* %15, i8** %14)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 3)
unreachable
19: ; preds = %8
%20 = bitcast i32* %3 to i8*
%21 = call i32 @waitpid(i32 %9, i32* %3, i32 0)
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%26 = call i32 @exit(i32 4)
unreachable
27: ; preds = %19
ret i32 0
}