#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 scalar_t__ pid_t;

/* Variables and functions */
int execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
void myFork(int cmdIndex) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    printf("-------fork error-------\n");
    exit(0);
  } else if (pid == 0) {
    int execl_status = -1;
    printf("-------C: child is running -------\n");
    switch (cmdIndex) {
    case 1:
      execl_status = execl("./cmd1", "cmd1", NULL);
      break;
    case 2:
      execl_status = execl("./cmd2", "cmd2", NULL);
      break;
    case 3:
      execl_status = execl("./cmd3", "cmd3", NULL);
      break;
    default:
      printf("-------Invalid Command -------\n");
      break;
    }
    if (execl_status < 0) {
      printf("-------C : fork error -------\n");
      exit(0);
    }
    printf("-------C : child is ok!-------\n");
    exit(0);
  } else {
    return;
  }
}


x86 assembly (clang, -O0):
<myFork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1173 <myFork+0x33>
lea    0xe99(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1260 <myFork+0x120>
movl   $0xffffffff,-0x14(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x1,%eax
je     11c7 <myFork+0x87>
jmp    11a5 <myFork+0x65>
mov    -0x18(%rbp),%eax
sub    $0x2,%eax
je     11e6 <myFork+0xa6>
jmp    11b6 <myFork+0x76>
mov    -0x18(%rbp),%eax
sub    $0x3,%eax
je     1205 <myFork+0xc5>
jmp    1224 <myFork+0xe4>
lea    0xe70(%rip),%rdi
lea    0xe6b(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execl@plt>
mov    %eax,-0x14(%rbp)
jmp    1230 <myFork+0xf0>
lea    0xe58(%rip),%rdi
lea    0xe53(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execl@plt>
mov    %eax,-0x14(%rbp)
jmp    1230 <myFork+0xf0>
lea    0xe40(%rip),%rdi
lea    0xe3b(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execl@plt>
mov    %eax,-0x14(%rbp)
jmp    1230 <myFork+0xf0>
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jge    124d <myFork+0x10d>
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myFork>:
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1153 <myFork+0x13>
je     115c <myFork+0x1c>
pop    %rbx
ret
lea    0xf3a(%rip),%rdi
jmp    11cc <myFork+0x8c>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x3,%ebx
je     1187 <myFork+0x47>
cmp    $0x2,%ebx
je     1197 <myFork+0x57>
cmp    $0x1,%ebx
jne    11b9 <myFork+0x79>
lea    0xe82(%rip),%rdi
lea    0xe7d(%rip),%rsi
jmp    11a5 <myFork+0x65>
lea    0xe80(%rip),%rdi
lea    0xe7b(%rip),%rsi
jmp    11a5 <myFork+0x65>
lea    0xe69(%rip),%rdi
lea    0xe64(%rip),%rsi
xor    %edx,%edx
call   1050 <execl@plt>
test   %eax,%eax
js     11c5 <myFork+0x85>
lea    0xe81(%rip),%rdi
jmp    11cc <myFork+0x8c>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"-------fork error-------\0A\00"
@.str.1 = [36 x i8] c"-------C: child is running -------\0A\00"
@.str.2 = [7 x i8] c"./cmd1\00"
@.str.3 = [5 x i8] c"cmd1\00"
@.str.4 = [7 x i8] c"./cmd2\00"
@.str.5 = [5 x i8] c"cmd2\00"
@.str.6 = [7 x i8] c"./cmd3\00"
@.str.7 = [5 x i8] c"cmd3\00"
@.str.8 = [32 x i8] c"-------Invalid Command -------\0A\00"
@.str.9 = [31 x i8] c"-------C : fork error -------\0A\00"
@.str.10 = [32 x i8] c"-------C : child is ok!-------\0A\00"
define void @myFork(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %1
%11 = load i64, i64* %3
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %10
store i32 -1, i32* %4
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %2
switch i32 %15, label %22 [
i32 1, label %16
i32 2, label %18
i32 3, label %20
]
16: ; preds = %13
%17 = call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* null)
store i32 %17, i32* %4
br label %24
18: ; preds = %13
%19 = call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
store i32 %19, i32* %4
br label %24
20: ; preds = %13
%21 = call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32* null)
store i32 %21, i32* %4
br label %24
22: ; preds = %13
%23 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %24
24: ; preds = %22, %20, %18, %16
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %24
%31 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0))
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str.2 = [7 x i8] c"./cmd1\00"
@.str.3 = [5 x i8] c"cmd1\00"
@.str.4 = [7 x i8] c"./cmd2\00"
@.str.5 = [5 x i8] c"cmd2\00"
@.str.6 = [7 x i8] c"./cmd3\00"
@.str.7 = [5 x i8] c"cmd3\00"
@str = [35 x i8] c"-------C: child is running -------\00"
@str.11 = [31 x i8] c"-------C : child is ok!-------\00"
@str.12 = [30 x i8] c"-------C : fork error -------\00"
@str.13 = [31 x i8] c"-------Invalid Command -------\00"
@str.14 = [25 x i8] c"-------fork error-------\00"
define void @myFork(i32 %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.14, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %1
%8 = icmp eq i64 %2, 0
br i1 %8, label %9, label %28
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
switch i32 %0, label %17 [
i32 1, label %11
i32 2, label %13
i32 3, label %15
]
11: ; preds = %9
%12 = tail call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %19
13: ; preds = %9
%14 = tail call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
br label %19
15: ; preds = %9
%16 = tail call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32* null)
br label %19
17: ; preds = %9
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.13, i64 0, i64 0))
br label %22
19: ; preds = %15, %13, %11
%20 = phi i32 [ %16, %15 ], [ %14, %13 ], [ %12, %11 ]
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17, %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.12, i64 0, i64 0))
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %19
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
%27 = tail call i32 @exit(i32 0)
unreachable
28: ; preds = %7
ret void
}