#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
void *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int parsecmd(char *line) {
  char *cmd, *tmp, **opts;
  int counter = 0;
  cmd = strtok(line, " \t");
  if (!cmd) {
    return -1;
  }
  if ((opts = malloc(11 * sizeof(char *))) == NULL) {
    printf("Cannot allocate memory\n");
  }
  opts[counter] = malloc(strlen(cmd) + 1);
  strcpy(opts[counter++], cmd);
  while ((tmp = strtok(NULL, " \t")) != NULL && counter < 10) {
    opts[counter] = malloc(strlen(tmp) + 1);
    strcpy(opts[counter], tmp);
    counter++;
  }
  opts[counter] = NULL;
  if (strcmp(cmd, "exit") == 0) {
    exit(EXIT_SUCCESS);
  } else {
    pid_t pid = fork();
    if (pid == 0) {
      char path[255];
      strcpy(path, "/bin/");
      strcat(path, cmd);
      execv(path, opts);
      printf("error");
    } else if (pid > 0) {
      int status;
      waitpid(pid, &status, WUNTRACED);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<parsecmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f1 <parsecmd+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <parsecmd+0x1fb>
mov    $0x58,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1215 <parsecmd+0x65>
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9f(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x145(%rbp)
je     128c <parsecmd+0xdc>
cmpl   $0xa,-0x2c(%rbp)
setl   %al
mov    %al,-0x145(%rbp)
mov    -0x145(%rbp),%al
test   $0x1,%al
jne    129f <parsecmd+0xef>
jmp    12e4 <parsecmd+0x134>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1256 <parsecmd+0xa6>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rdi
lea    0xd1c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    131c <parsecmd+0x16c>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1376 <parsecmd+0x1c6>
lea    -0x140(%rbp),%rdi
lea    0xce0(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x140(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x140(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10c0 <execv@plt>
lea    0xcba(%rip),%rdi
call   1050 <printf@plt>
jmp    139f <parsecmd+0x1ef>
cmpq   $0x0,-0x38(%rbp)
jle    139a <parsecmd+0x1ea>
mov    -0x38(%rbp),%rdi
mov    0x2c54(%rip),%rax
mov    (%rax),%edx
lea    -0x144(%rbp),%rsi
call   1080 <waitpid@plt>
jmp    139f <parsecmd+0x1ef>
jmp    13a4 <parsecmd+0x1f4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parsecmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xe28(%rip),%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1290 <parsecmd+0xd0>
mov    %rax,%r14
mov    $0x58,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1207 <parsecmd+0x47>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,(%r12)
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xdd3(%rip),%rsi
xor    %edi,%edi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1297 <parsecmd+0xd7>
mov    %rax,%rbp
mov    $0x1,%ebx
lea    0xdb8(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0x1(%rbx),%r13
xor    %edi,%edi
mov    %r15,%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
je     129d <parsecmd+0xdd>
mov    %rax,%rbp
cmp    $0x9,%rbx
mov    %r13,%rbx
jb     1250 <parsecmd+0x90>
jmp    129d <parsecmd+0xdd>
mov    $0xffffffff,%ebp
jmp    1313 <parsecmd+0x153>
mov    $0x1,%r13d
movq   $0x0,(%r12,%r13,8)
lea    0xd57(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1327 <parsecmd+0x167>
xor    %ebp,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     12df <parsecmd+0x11f>
jle    1313 <parsecmd+0x153>
mov    0x2d10(%rip),%rcx
mov    (%rcx),%edx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1090 <waitpid@plt>
jmp    1313 <parsecmd+0x153>
lea    0xd22(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <execv@plt>
lea    0xd00(%rip),%rdi
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c" \09\00"
@.str.1 = [24 x i8] c"Cannot allocate memory\0A\00"
@.str.2 = [5 x i8] c"exit\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [6 x i8] c"/bin/\00"
@.str.4 = [6 x i8] c"error\00"
@WUNTRACED = global i32 0
define i32 @parsecmd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i64
%9 = alloca [255 x i8]
%10 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %7
%11 = load i8*, i8** %3
%12 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i32 -1, i32* %2
br label %100
16: ; preds = %1
%17 = call i8* @malloc(i32 88)
%18 = bitcast i8* %17 to i8**
store i8** %18, i8*** %6
%19 = icmp eq i8** %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i8*, i8** %4
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = call i8* @malloc(i32 %25)
%27 = load i8**, i8*** %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
store i8* %26, i8** %30
%31 = load i8**, i8*** %6
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8*, i8** %31, i64 %34
%36 = load i8*, i8** %35
%37 = load i8*, i8** %4
%38 = call i32 @strcpy(i8* %36, i8* %37)
br label %39
39: ; preds = %47, %22
%40 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %40, i8** %5
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 10
br label %45
45: ; preds = %42, %39
%46 = phi i1 [ false, %39 ], [ %44, %42 ]
br i1 %46, label %47, label %65
47: ; preds = %45
%48 = load i8*, i8** %5
%49 = call i32 @strlen(i8* %48)
%50 = add nsw i32 %49, 1
%51 = call i8* @malloc(i32 %50)
%52 = load i8**, i8*** %6
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
store i8* %51, i8** %55
%56 = load i8**, i8*** %6
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = load i8*, i8** %5
%62 = call i32 @strcpy(i8* %60, i8* %61)
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
store i32 %64, i32* %7
br label %39
65: ; preds = %45
%66 = load i8**, i8*** %6
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %66, i64 %68
store i8* null, i8** %69
%70 = load i8*, i8** %4
%71 = call i64 @strcmp(i8* %70, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %65
%74 = load i32, i32* @EXIT_SUCCESS
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %65
%77 = call i64 (...) @fork()
store i64 %77, i64* %8
%78 = load i64, i64* %8
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %90
80: ; preds = %76
%81 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%82 = call i32 @strcpy(i8* %81, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%83 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%84 = load i8*, i8** %4
%85 = call i32 @strcat(i8* %83, i8* %84)
%86 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%87 = load i8**, i8*** %6
%88 = call i32 @execv(i8* %86, i8** %87)
%89 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %98
90: ; preds = %76
%91 = load i64, i64* %8
%92 = icmp sgt i64 %91, 0
br i1 %92, label %93, label %97
93: ; preds = %90
%94 = load i64, i64* %8
%95 = load i32, i32* @WUNTRACED
%96 = call i32 @waitpid(i64 %94, i32* %10, i32 %95)
br label %97
97: ; preds = %93, %90
br label %98
98: ; preds = %97, %80
br label %99
99: ; preds = %98
store i32 0, i32* %2
br label %100
100: ; preds = %99, %15
%101 = load i32, i32* %2
ret i32 %101
}

LLVM IR (-O2):
@.str = [3 x i8] c" \09\00"
@.str.2 = [5 x i8] c"exit\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [6 x i8] c"/bin/\00"
@.str.4 = [6 x i8] c"error\00"
@WUNTRACED = global i32 0
@str = [23 x i8] c"Cannot allocate memory\00"
define i32 @parsecmd(i8* %0) {
%2 = alloca [255 x i8]
%3 = alloca i32
%4 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %55, label %6
6: ; preds = %1
%7 = tail call dereferenceable_or_null(88) i8* @malloc(i32 88)
%8 = bitcast i8* %7 to i8**
%9 = icmp eq i8* %7, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = tail call i32 @strlen(i8* %4)
%14 = add nsw i32 %13, 1
%15 = tail call i8* @malloc(i32 %14)
store i8* %15, i8** %8
%16 = tail call i32 @strcpy(i8* %15, i8* %4)
%17 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %32, label %19
19: ; preds = %12, %19
%20 = phi i64 [ %27, %19 ], [ 1, %12 ]
%21 = phi i8* [ %28, %19 ], [ %17, %12 ]
%22 = tail call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 1
%24 = tail call i8* @malloc(i32 %23)
%25 = getelementptr inbounds i8*, i8** %8, i64 %20
store i8* %24, i8** %25
%26 = tail call i32 @strcpy(i8* %24, i8* %21)
%27 = add nuw nsw i64 %20, 1
%28 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%29 = icmp ne i8* %28, null
%30 = icmp ult i64 %20, 9
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %19, label %32
32: ; preds = %19, %12
%33 = phi i64 [ 1, %12 ], [ %27, %19 ]
%34 = getelementptr inbounds i8*, i8** %8, i64 %33
store i8* null, i8** %34
%35 = tail call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @EXIT_SUCCESS
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = tail call i64 (...) @fork()
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%45 = call i32 @strcpy(i8* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @strcat(i8* %44, i8* %4)
%47 = call i32 @execv(i8* %44, i8** %8)
%48 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %55
49: ; preds = %40
%50 = icmp sgt i64 %41, 0
br i1 %50, label %51, label %55
51: ; preds = %49
%52 = bitcast i32* %3 to i8*
%53 = load i32, i32* @WUNTRACED
%54 = call i32 @waitpid(i64 %41, i32* %3, i32 %53)
br label %55
55: ; preds = %43, %51, %49, %1
%56 = phi i32 [ -1, %1 ], [ 0, %49 ], [ 0, %51 ], [ 0, %43 ]
ret i32 %56
}