#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 /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ readline(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(void) {
  char argstr[16][32];
  char *argv[16];
  char *token;
  char *copy;
  char string[32];
  char delimiter[] = " |\t\n";
  int idx = 0;
  pid_t PID;
  copy = malloc(sizeof(char) * 32);
  while (1) {
    printf("# ");
    readline(string, 32);
    strcpy(copy, string);
    token = strtok(copy, delimiter);
    while (token != NULL) {
      strcpy(&(argstr[idx][0]), token);
      argv[idx] = argstr[idx];
      idx++;
      token = strtok(NULL, delimiter);
      printf("%s\n", token);
    }
    free(copy);
    argv[idx] = NULL;
    if ((PID = fork()) == 0) {
      strcpy(string, "/bin/");
      strcat(string, argv[0]);
      execv(string, &argv[0]);
      printf("!!! panic !!!\n");
    } else if (PID < 0) {
      printf("fork failed\n");
      exit(-1);
    } else
      wait(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2e0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe38(%rip),%eax
mov    %eax,-0x2c5(%rbp)
mov    0xe30(%rip),%al
mov    %al,-0x2c1(%rbp)
movl   $0x0,-0x2cc(%rbp)
mov    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,-0x2a0(%rbp)
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x2c0(%rbp),%rdi
mov    $0x20,%esi
call   1060 <readline@plt>
mov    -0x2a0(%rbp),%rdi
lea    -0x2c0(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x2a0(%rbp),%rdi
lea    -0x2c5(%rbp),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x298(%rbp)
cmpq   $0x0,-0x298(%rbp)
je     12d4 <main+0x124>
movslq -0x2cc(%rbp),%rax
lea    -0x210(%rbp),%rdi
shl    $0x5,%rax
add    %rax,%rdi
mov    -0x298(%rbp),%rsi
call   1040 <strcpy@plt>
movslq -0x2cc(%rbp),%rax
lea    -0x210(%rbp),%rcx
shl    $0x5,%rax
add    %rax,%rcx
movslq -0x2cc(%rbp),%rax
mov    %rcx,-0x290(%rbp,%rax,8)
ff
mov    -0x2cc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2cc(%rbp)
lea    -0x2c5(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1080 <strtok@plt>
mov    %rax,-0x298(%rbp)
mov    -0x298(%rbp),%rsi
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1241 <main+0x91>
mov    -0x2a0(%rbp),%rdi
call   1030 <free@plt>
movslq -0x2cc(%rbp),%rax
movq   $0x0,-0x290(%rbp,%rax,8)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x2d8(%rbp)
cmp    $0x0,%rax
jne    1357 <main+0x1a7>
lea    -0x2c0(%rbp),%rdi
lea    0xcf3(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x2c0(%rbp),%rdi
mov    -0x290(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x2c0(%rbp),%rdi
lea    -0x290(%rbp),%rsi
call   10c0 <execv@plt>
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1389 <main+0x1d9>
cmpq   $0x0,-0x2d8(%rbp)
jge    137d <main+0x1cd>
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    1389 <main+0x1d9>
jmp    11f5 <main+0x45>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2b8,%rsp
movb   $0x0,0xc(%rsp)
movl   $0xa097c20,0x8(%rsp)
0a
mov    $0x20,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
lea    0x10(%rsp),%rbp
lea    0x8(%rsp),%rbx
xor    %r14d,%r14d
jmp    123a <main+0x7a>
nopw   0x0(%rax,%rax,1)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xdf9(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x30(%rsp),%rsi
mov    %rbp,%rdi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
lea    0x30(%rsp),%rsi
call   10d0 <execv@plt>
lea    0xde5(%rip),%rdi
call   1050 <puts@plt>
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    $0x20,%esi
call   1070 <readline@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     12c4 <main+0x104>
mov    %rax,%r13
movslq %r14d,%rax
lea    (%rsp,%rax,8),%r15
add    $0x30,%r15
shl    $0x5,%rax
lea    (%rsp,%rax,1),%rbp
add    $0xb0,%rbp
nopl   (%rax)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    %rbp,(%r15)
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <strtok@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <puts@plt>
add    $0x8,%r15
add    $0x20,%rbp
add    $0x1,%r14d
test   %r13,%r13
jne    1290 <main+0xd0>
mov    %r12,%rdi
call   1030 <free@plt>
movslq %r14d,%rax
movq   $0x0,0x30(%rsp,%rax,8)
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     1200 <main+0x40>
js     12fb <main+0x13b>
xor    %edi,%edi
call   10c0 <wait@plt>
lea    0x10(%rsp),%rbp
jmp    123a <main+0x7a>
lea    0xd0c(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.delimiter = [5 x i8] c" |\09\0A\00"
@.str = [3 x i8] c"# \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [6 x i8] c"/bin/\00"
@.str.3 = [15 x i8] c"!!! panic !!!\0A\00"
@.str.4 = [13 x i8] c"fork failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [16 x [32 x i8]]
%3 = alloca [16 x i8*]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [32 x i8]
%7 = alloca [5 x i8]
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %1
%10 = bitcast [5 x i8]* %7 to i8*
store i32 0, i32* %8
%11 = call i8* @malloc(i32 32)
store i8* %11, i8** %5
br label %12
12: ; preds = %0, %73
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%15 = call i32 @readline(i8* %14, i32 32)
%16 = load i8*, i8** %5
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = load i8*, i8** %5
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%21 = call i8* @strtok(i8* %19, i8* %20)
store i8* %21, i8** %4
br label %22
22: ; preds = %25, %12
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %45
25: ; preds = %22
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %2, i64 0, i64 %27
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %28, i64 0, i64 0
%30 = load i8*, i8** %4
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %2, i64 0, i64 %33
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %34, i64 0, i64 0
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %37
store i8* %35, i8** %38
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%42 = call i8* @strtok(i8* null, i8* %41)
store i8* %42, i8** %4
%43 = load i8*, i8** %4
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %43)
br label %22
45: ; preds = %22
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %49
store i8* null, i8** %50
%51 = call i64 (...) @fork()
store i64 %51, i64* %9
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %64
53: ; preds = %45
%54 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%55 = call i32 @strcpy(i8* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%57 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%58 = load i8*, i8** %57
%59 = call i32 @strcat(i8* %56, i8* %58)
%60 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%61 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%62 = call i32 @execv(i8* %60, i8** %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %73
64: ; preds = %45
%65 = load i64, i64* %9
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %64
%71 = call i32 @wait(i32 0)
br label %72
72: ; preds = %70
br label %73
73: ; preds = %72, %53
br label %12
}

LLVM IR (-O2):
@__const.main.delimiter = [5 x i8] c" |\09\0A\00"
@.str = [3 x i8] c"# \00"
@.str.2 = [6 x i8] c"/bin/\00"
@str = [12 x i8] c"fork failed\00"
@str.5 = [14 x i8] c"!!! panic !!!\00"
define i32 @main() {
%1 = alloca [16 x [32 x i8]]
%2 = alloca [16 x i8*]
%3 = alloca [32 x i8]
%4 = alloca [5 x i8]
%5 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %1, i64 0, i64 0, i64 0
%6 = bitcast [16 x i8*]* %2 to i8*
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%9 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%10 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 0
br label %11
11: ; preds = %52, %0
%12 = phi i32 [ 0, %0 ], [ %33, %52 ]
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @readline(i8* %7, i32 32)
%15 = call i32 @strcpy(i8* %9, i8* %7)
%16 = call i8* @strtok(i8* %9, i8* %8)
%17 = icmp eq i8* %16, null
br i1 %17, label %32, label %18
18: ; preds = %11
%19 = sext i32 %12 to i64
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ %19, %18 ], [ %26, %20 ]
%22 = phi i8* [ %16, %18 ], [ %27, %20 ]
%23 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %1, i64 0, i64 %21, i64 0
%24 = call i32 @strcpy(i8* %23, i8* %22)
%25 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %21
store i8* %23, i8** %25
%26 = add i64 %21, 1
%27 = call i8* @strtok(i8* null, i8* %8)
%28 = call i32 @puts(i8* dereferenceable(1) %27)
%29 = icmp eq i8* %27, null
br i1 %29, label %30, label %20
30: ; preds = %20
%31 = trunc i64 %26 to i32
br label %32
32: ; preds = %30, %11
%33 = phi i32 [ %12, %11 ], [ %31, %30 ]
%34 = call i32 @free(i8* %9)
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %35
store i8* null, i8** %36
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%41 = load i8*, i8** %10
%42 = call i32 @strcat(i8* %7, i8* %41)
%43 = call i32 @execv(i8* %7, i8** %10)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %52
45: ; preds = %32
%46 = icmp slt i64 %37, 0
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %45
%51 = call i32 @wait(i32 0)
br label %52
52: ; preds = %50, %39
br label %11
}