#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 */
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ scanf(char *, int *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t parent, child;
  int status;
  int i;
  char *cmd[] = {"ps", "ls", "date", "goof"};
  parent = getpid();
  if ((child = fork()) == -1) {
    do {
      perror("fork");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if (child) {
    printf("Parent with pid = %d\nJust sapwned Child with pid = %d\n", parent,
           child);
    waitpid(child, &status, 0);
    printf("child exited with status = 0x%08x\n", status);
    if (WIFEXITED(status)) {
      printf("Child exited with status = %d\n", WEXITSTATUS(status));
    }
    if (WIFSIGNALED(status)) {
      printf("Child exited due to signal = %d\n", WTERMSIG(status));
    }
  } else {
    child = getpid();
    printf("Running child with pid = %d\n", child);
    printf("1. ps\t2. ls\t3. date\t4. goof\n");
    if (scanf("%d", &i) == EOF) {
      do {
        perror("scanf");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (i > 0 && i < 5) {
      printf("Found command %s\n", cmd[i - 1]);
      execlp(cmd[i - 1], cmd[i - 1], NULL);
    } else {
      printf("Unknown command option %d\n", i);
      exit(EXIT_FAILURE);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c03(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c00(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2bfd(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2bfa(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    123e <main+0x7e>
jmp    1224 <main+0x64>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     12d4 <main+0x114>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x1c(%rbp),%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12a5 <main+0xe5>
mov    -0x1c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12cf <main+0x10f>
mov    -0x1c(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b7 <main+0x1f7>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde5(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <scanf@plt>
mov    0x2ccc(%rip),%rcx
cmp    (%rcx),%rax
jne    133c <main+0x17c>
jmp    1322 <main+0x162>
lea    0xdc3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1393 <main+0x1d3>
cmpl   $0x5,-0x20(%rbp)
jge    1393 <main+0x1d3>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x40(%rbp,%rax,8),%rsi
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x40(%rbp,%rax,8),%rdi
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x40(%rbp,%rax,8),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10d0 <execlp@plt>
jmp    13b2 <main+0x1f2>
mov    -0x20(%rbp),%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    13b7 <main+0x1f7>
mov    0x2c0a(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1275 <main+0xa5>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1300 <main+0x130>
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    (%rsp),%esi
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     124a <main+0x7a>
mov    (%rsp),%edi
call   1090 <WEXITSTATUS@plt>
lea    0xe29(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     12f0 <main+0x120>
mov    (%rsp),%edi
call   1070 <WTERMSIG@plt>
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12f0 <main+0x120>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe27(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe28(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1080 <scanf@plt>
mov    0x2d30(%rip),%rcx
cmp    (%rcx),%rax
je     1309 <main+0x139>
mov    0x4(%rsp),%esi
lea    -0x1(%rsi),%eax
cmp    $0x3,%eax
ja     1317 <main+0x147>
mov    %eax,%eax
lea    0x2b16(%rip),%rbx
mov    (%rbx,%rax,8),%rsi
lea    0xdfb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movslq 0x4(%rsp),%rax
mov    -0x8(%rbx,%rax,8),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10e0 <execlp@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd04(%rip),%rdi
jmp    1310 <main+0x140>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
jmp    1325 <main+0x155>
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"ps\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"date\00"
@.str.3 = [5 x i8] c"goof\00"
@__const.main.cmd = [4 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0)]
@.str.4 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [55 x i8] c"Parent with pid = %d\0AJust sapwned Child with pid = %d\0A\00"
@.str.6 = [35 x i8] c"child exited with status = 0x%08x\0A\00"
@.str.7 = [31 x i8] c"Child exited with status = %d\0A\00"
@.str.8 = [33 x i8] c"Child exited due to signal = %d\0A\00"
@.str.9 = [29 x i8] c"Running child with pid = %d\0A\00"
@.str.10 = [29 x i8] c"1. ps\092. ls\093. date\094. goof\0A\00"
@.str.11 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.12 = [6 x i8] c"scanf\00"
@.str.13 = [18 x i8] c"Found command %s\0A\00"
@.str.14 = [27 x i8] c"Unknown command option %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [4 x i8*]* %10 to i8*
%12 = call i32 (...) @getpid()
store i32 %12, i32* %6
%13 = call i32 (...) @fork()
store i32 %13, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
br label %16
16: ; preds = %15
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; No predecessors!
br label %21
21: ; preds = %20, %2
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %48
24: ; preds = %21
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %25, i32 %26)
%28 = load i32, i32* %7
%29 = call i32 @waitpid(i32 %28, i32* %8, i32 0)
%30 = load i32, i32* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %8
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %24
%36 = load i32, i32* %8
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %24
%40 = load i32, i32* %8
%41 = call i64 @WIFSIGNALED(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* %8
%45 = call i32 @WTERMSIG(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43, %39
br label %92
48: ; preds = %21
%49 = call i32 (...) @getpid()
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
%53 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32* %9)
%54 = load i64, i64* @EOF
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %48
br label %57
57: ; preds = %56
%58 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; No predecessors!
br label %62
62: ; preds = %61, %48
%63 = load i32, i32* %9
%64 = icmp sgt i32 %63, 0
br i1 %64, label %65, label %86
65: ; preds = %62
%66 = load i32, i32* %9
%67 = icmp slt i32 %66, 5
br i1 %67, label %68, label %86
68: ; preds = %65
%69 = load i32, i32* %9
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 %71
%73 = load i8*, i8** %72
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0), i8* %73)
%75 = load i32, i32* %9
%76 = sub nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 %77
%79 = load i8*, i8** %78
%80 = load i32, i32* %9
%81 = sub nsw i32 %80, 1
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 %82
%84 = load i8*, i8** %83
%85 = call i32 @execlp(i8* %79, i8* %84, i32* null)
br label %91
86: ; preds = %65, %62
%87 = load i32, i32* %9
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.14, i64 0, i64 0), i32 %87)
%89 = load i32, i32* @EXIT_FAILURE
%90 = call i32 @exit(i32 %89)
unreachable
91: ; preds = %68
br label %92
92: ; preds = %91, %47
%93 = load i32, i32* @EXIT_SUCCESS
ret i32 %93
}

LLVM IR (-O2):
@.str = [3 x i8] c"ps\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"date\00"
@.str.3 = [5 x i8] c"goof\00"
@__const.main.cmd = [4 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0)]
@.str.4 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [55 x i8] c"Parent with pid = %d\0AJust sapwned Child with pid = %d\0A\00"
@.str.6 = [35 x i8] c"child exited with status = 0x%08x\0A\00"
@.str.7 = [31 x i8] c"Child exited with status = %d\0A\00"
@.str.8 = [33 x i8] c"Child exited due to signal = %d\0A\00"
@.str.9 = [29 x i8] c"Running child with pid = %d\0A\00"
@.str.11 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.12 = [6 x i8] c"scanf\00"
@.str.13 = [18 x i8] c"Found command %s\0A\00"
@.str.14 = [27 x i8] c"Unknown command option %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [28 x i8] c"1. ps\092. ls\093. date\094. goof\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @fork()
switch i32 %8, label %13 [
i32 -1, label %9
i32 0, label %33
]
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %7, i32 %8)
%15 = call i32 @waitpid(i32 %8, i32* %3, i32 0)
%16 = load i32, i32* %3
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %3
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %13
%22 = load i32, i32* %3
%23 = call i32 @WEXITSTATUS(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %21, %13
%26 = load i32, i32* %3
%27 = call i64 @WIFSIGNALED(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %63, label %29
29: ; preds = %25
%30 = load i32, i32* %3
%31 = call i32 @WTERMSIG(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %31)
br label %63
33: ; preds = %2
%34 = tail call i32 (...) @getpid()
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i32 %34)
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%37 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32* %4)
%38 = load i64, i64* @EOF
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %33
%45 = load i32, i32* %4
%46 = add i32 %45, -1
%47 = icmp ult i32 %46, 4
br i1 %47, label %48, label %59
48: ; preds = %44
%49 = zext i32 %46 to i64
%50 = getelementptr inbounds [4 x i8*], [4 x i8*]* @__const.main.cmd, i64 0, i64 %49
%51 = load i8*, i8** %50
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0), i8* %51)
%53 = load i32, i32* %4
%54 = add nsw i32 %53, -1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [4 x i8*], [4 x i8*]* @__const.main.cmd, i64 0, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @execlp(i8* %57, i8* %57, i32* null)
br label %63
59: ; preds = %44
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.14, i64 0, i64 0), i32 %45)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %25, %29, %48
%64 = load i32, i32* @EXIT_SUCCESS
ret i32 %64
}