#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 /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid = fork();
  switch (pid) {
  case -1:
    printf("An error occured, could not fork\n");
    exit(1);
    break;
  case 0:
    if (execv(*++argv, argc > 2 ? ++argv : NULL) < 0)
      printf("An error occured in exec\n");
    break;
  default:
    waitpid(pid, NULL, 0);
    printf("waited in child\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     1194 <main+0x44>
jmp    1184 <main+0x34>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11aa <main+0x5a>
jmp    1212 <main+0xc2>
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11e0 <main+0x90>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    %rax,-0x28(%rbp)
jmp    11eb <main+0x9b>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11eb <main+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <execv@plt>
cmp    $0x0,%eax
jge    120d <main+0xbd>
lea    0xe1a(%rip),%rdi
call   1030 <printf@plt>
jmp    122c <main+0xdc>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
lea    0xe15(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     117c <main+0x2c>
cmp    $0xffffffff,%eax
je     11ab <main+0x5b>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
lea    0xec0(%rip),%rdi
jmp    119d <main+0x4d>
mov    0x8(%rbx),%rdi
add    $0x10,%rbx
xor    %esi,%esi
cmp    $0x3,%ebp
cmovge %rbx,%rsi
call   1060 <execv@plt>
test   %eax,%eax
jns    11a2 <main+0x52>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"An error occured, could not fork\0A\00"
@.str.1 = [26 x i8] c"An error occured in exec\0A\00"
@.str.2 = [17 x i8] c"waited in child\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 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
switch i32 %8, label %29 [
i32 -1, label %9
i32 0, label %12
]
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i32 1
store i8** %14, i8*** %5
%15 = load i8*, i8** %14
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i32 1
store i8** %20, i8*** %5
br label %22
21: ; preds = %12
br label %22
22: ; preds = %21, %18
%23 = phi i8** [ %20, %18 ], [ null, %21 ]
%24 = call i32 @execv(i8* %15, i8** %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
br label %33
29: ; preds = %2
%30 = load i32, i32* %6
%31 = call i32 @waitpid(i32 %30, i32* null, i32 0)
%32 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %29, %28
ret i32 0
}

LLVM IR (-O2):
@str = [25 x i8] c"An error occured in exec\00"
@str.3 = [33 x i8] c"An error occured, could not fork\00"
@str.4 = [16 x i8] c"waited in child\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %15 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.3, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = icmp sgt i32 %0, 2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = select i1 %10, i8** %11, i8** null
%13 = tail call i32 @execv(i8* %9, i8** %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %17, label %20
15: ; preds = %2
%16 = tail call i32 @waitpid(i32 %3, i32* null, i32 0)
br label %17
17: ; preds = %7, %15
%18 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0), %15 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0), %7 ]
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
br label %20
20: ; preds = %17, %7
ret i32 0
}